简体   繁体   中英

How to use the board from Chess in Test?

So I'm creating a game and when I use board in the class Chess everything is okay but once I use it in another class it's telling me that board is null

Here are the simplified classes :

Chess

    public class Chess {
    public int[][] board;

    public int[][] getBoard() {
        return board;
    }
    public void setBoard(int[][] board) {
        this.board = board;
    }

    public Chess() {
        initialiseBoard();
    }
    private void initialiseBoard(){ // 1 = king, 2 = queen, 3 = knight, 4 = bishop , 5 = rook, 6 = pawn
        int[][] board = new int[8][8];
        for (int i = 0; i < board.length; i++) {
            for (int j = 0; j < board[i].length; j++) {
                board[i][j] = 0;
            }
        }
        for (int j = 1; j < 9; j++) {
            board[1][j - 1] = -6;
        }
        for (int j = 1; j < 9; j++) {
            board[6][j - 1] = 6;
        }
        int[] Lblanc = {5,3,4,2,1,4,3,5};
        int[] Lnoir = {-5,-3,-4,-2,-1,-4,-3,-5};
        for (int j = 0; j < 8; j++) {
            board[0][j] = Lnoir[j];
            board[7][j] = Lblanc[j];
        }
        setBoard(board);
    }

    public void printBoard(){ //mettre private
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                System.out.print(board[i][j] + "\t");
            }
            System.out.println();
        }
    }



    public void play(){
        initialiseBoard();
        board[4][4] = 7;
        Test test = new Test();
        test.lol(); //print the board without the change
    }


}

Test

public class Test {
    Chess chess = new Chess();

    public void lol(){
        chess.printBoard();
    }
}

Main

public class Main {
    public static void main(String[] args) {

        Chess chess = new Chess();
        chess.play();
    }
}

I suppose thats cause I created a new Chess(), but I don't know how to use the methods and attributes otherwise.

edit : the true play so just initialiseBoard in constructor doesn't work does it?

public void play() {
        while (true) {
            createPlayers();
            initialiseBoard();
            while (!isCheckMate()) {
                printBoard();
                Position[] move;
                do {
                    move = askMove();
                }
                while (!isValidMove(move,board));
                updateBoard(move);
                //switchPlayer();
            }
        }
    }

You need to call your initialiseBoard method in Chess constructor:

public Chess() {
    initialiseBoard();
}

Or, you can call it right after the construction of Chess object:

Chess chess = new Chess();
chess.initialiseBoard();

You get a null because you never call this method.

you are instantiating Test (which in turn instantiates Chess) inside Chess.play() which means you are making another instance of Chess inside Chess. now the second instance which is made inside Test has a separate board parameter that hasn't been initialized since the Test class does not call chess.play() in it so the initialiseBoard() is never called for Test class which means board is never initialized! Now I'm not sure that's such a good idea...

You need to initialize your board variable within the constructor for your Chess class. So for example:

public class Chess {
        public int[][] board;
        
        public Chess() {
             this.board = new int[8][8];
        }

        public int[][] getBoard() {
            return board;
        }

        public void setBoard(int[][] board) {
           this.board = board;
        }    
}

Note that when you run your code with this, it will print out the memory location of board. If you want to actually print the array with values you'll have to write out some loops.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM