Speeed
Speeed

Reputation: 79

I'm using a 2D array to place pieces on a board but need the pieces to stay once they're placed

I'm using a 2D array as a game board and need the user to input their row and column for the piece, I've got that working but can't crack how to make the pieces stay for the duration of the program. Currently when the user is then asked to place another piece the new piece overwrites the old piece but need both pieces to be visible.

Here is the class menu:

import java.util.Scanner;

public class menu {
    board a = new board();

    public static void main(String[] args) {
        menu myMenu = new menu();
        myMenu.run();
    }

    public void run() {
        a.display();
        makeMove();
    }

    public void makeMove() {
        Scanner choice = new Scanner(System.in);

        System.out.println("Enter the column");
        int tempCol = choice.nextInt();
        System.out.println("Now the row");
        int tempRow = choice.nextInt();

        a.isValid(tempRow, tempCol);
    }
}

Board class:

import java.util.Arrays;
import java.util.Scanner;

public class board {
    String[][] board = {
            {"-", "-", "-", "-", "-", "-", "-", "-"},
            {"-", "-", "-", "-", "-", "-", "-", "-"},
            {"-", "-", "-", "-", "-", "-", "-", "-"},
            {"-", "-", "-", "-", "-", "-", "-", "-"},
            {"-", "-", "-", "-", "-", "-", "-", "-"},
            {"-", "-", "-", "-", "-", "-", "-", "-"},
            {"-", "-", "-", "-", "-", "-", "-", "-"},
            {"-", "-", "-", "-", "-", "-", "-", "-"}};

    public board() {
        board[3][3] = "W";
        board[4][4] = "W";
        board[3][4] = "B";
        board[4][3] = "B";
    }

    public void display() {
        System.out.print("   A  B  C  D  E  F  G  H");
        for (int i = 0; i < 8; i++) {
            System.out.println();
            System.out.print(i + 1 + "  ");
            for (int j = 0; j < 8; j++) {
                System.out.print(board[i][j] + "  ");
            }
        }
        System.out.println();
    }

    public void setPiece(int row, int col, String piece) {
        board[row][col] = piece;
    }

    public String getPiece(int row, int col) {
        return board[row][col];
    }

    public void isValid(int tempRow, int tempCol) {
        menu b = new menu();
        if (board[tempRow][tempCol] == "W") {
            System.out.println("Move invalid, try again");
            b.makeMove();
        } else if (board[tempRow][tempCol] != "W") {
            if (board[tempRow + 1][tempCol + 1] == "W") {
                System.out.println("Valid move");
                setPiece(tempRow, tempCol, "W");
            } else if (board[tempRow - 1][tempCol + 1] == "W") {
                System.out.println("Valid move");
                setPiece(tempRow, tempCol, "W");
            } else if (board[tempRow + 1][tempCol - 1] == "W") {
                System.out.println("Valid move");
                setPiece(tempRow, tempCol, "W");
            } else if (board[tempRow - 1][tempCol - 1] == "W") {
                System.out.println("Valid move");
                setPiece(tempRow, tempCol, "W");
            } else {
                System.out.println("Invalid move, try again2");
            }
            display();
            System.out.println("------------------------------");
            b.makeMove();
        }
    }
}

Upvotes: 0

Views: 277

Answers (1)

DarkMatter
DarkMatter

Reputation: 1017

Your code is essentially creating new "game" instances recursively.

Initially, a new Menu is created and inside it a new, fresh board b.

After the user has entered their input, you place the piece on that b and display the updated board. Then, however, you create a new Menu object. So after the first Board instance has been displayed, you leave that game and enter the first round of a new game in in the new Menu (which contains a new fresh Board b' ).

Your run() method should probably contain a loop and you should not create new Menu objects inside isValid();

Note: I change the class names to start with Capital letters as is Java convention.

New Menu with loop and exit condition:

public class Menu {
    Board a = new Board();

    public static void main(String[] args) {
        Menu myMenu = new Menu();
        myMenu.run();
    }

    public void run() {
        boolean isRunning = true;
        while (isRunning) {
            a.display();
            isRunning = makeMove();
        }
    }

    public boolean makeMove() {
        Scanner choice = new Scanner(System.in);

        System.out.println("Enter the column (or 99 to exit)");
        int tempCol = choice.nextInt();
        if (tempCol == 99) {
            return false;
        }
        System.out.println("Now the row");
        int tempRow = choice.nextInt();

        a.isValid(tempRow, tempCol);
        return true;
    }

}

New Board without creating new Menu:

public class Board {
    String[][] board = {{"-", "-", "-", "-", "-", "-", "-", "-"},
            {"-", "-", "-", "-", "-", "-", "-", "-"},{"-", "-", "-", "-", "-", "-", "-", "-"},
            {"-", "-", "-", "-", "-", "-", "-", "-"},{"-", "-", "-", "-", "-", "-", "-", "-"},
            {"-", "-", "-", "-", "-", "-", "-", "-"},{"-", "-", "-", "-", "-", "-", "-", "-"},
            {"-", "-", "-", "-", "-", "-", "-", "-"}};



    public Board(){
        board[3][3] = "W";
        board[4][4] = "W";
        board[3][4] = "B";
        board[4][3] = "B";
    }

    public void display(){
        System.out.print("   A  B  C  D  E  F  G  H");
        for(int i = 0; i < 8; i++){
            System.out.println();
            System.out.print(i + 1 + "  ");
            for (int j = 0; j < 8; j++){
                System.out.print(board[i][j] + "  ");
            }
        }
        System.out.println();
    }

    public void setPiece(int row, int col, String piece){
        board[row][col] = piece;
    }

    public String getPiece(int row, int col){
        return board[row][col];
    }

    public void isValid(int tempRow, int tempCol){
        if(board[tempRow][tempCol] == "W"){
            System.out.println("Move invalid, try again");
        } else if (board[tempRow][tempCol] != "W"){
            if (board[tempRow + 1][tempCol + 1] == "W"){
                System.out.println("Valid move");
                setPiece(tempRow, tempCol, "W");
            }
            else if (board[tempRow - 1][tempCol + 1] == "W"){
                System.out.println("Valid move");
                setPiece(tempRow, tempCol, "W");
            }
            else if (board[tempRow + 1][tempCol - 1] == "W"){
                System.out.println("Valid move");
                setPiece(tempRow, tempCol, "W");
            }
            else if (board[tempRow - 1][tempCol - 1] == "W"){
                System.out.println("Valid move");
                setPiece(tempRow, tempCol, "W");
            } else {
                System.out.println("Invalid move, try again2");
            }
            display();
            System.out.println("------------------------------");
        }
    }
}

Upvotes: 3

Related Questions