Java chess game programming and gameOver boolean not working

I am trying to make a chess game in Java that can be played on the console. For each move, the game asks the player for a piece (eg 12 for a piece in the second row, third column) and the final location (eg 32 for a fourth row, third column). Everything works fine, including tests to check if the move is illegal and if the given player is in control, but my gameOver boolean seems to change the value of my array before the first step is requested.

Here is the relevant code. From my main method:

public static void main(String[] args) {
    int[][] board = {   {12, 13, 14, 15, 16, 14, 13, 12},
                        {11, 11, 11, 11, 11, 11, 11, 11},
                        {0,  0,  0,  0,  0,  0,  0,  0},
                        {0,  0,  0,  0,  0,  0,  0,  0},
                        {0,  0,  0,  0,  0,  0,  0,  0},
                        {0,  0,  0,  0,  0,  0,  0,  0},
                        {21, 21, 21, 21, 21, 21, 21, 21},
                        {22, 23, 24, 25, 26, 24, 23, 22}
                    };
    Scanner scan = new Scanner(System.in);
    boolean oneTurn = true;
    while(!(gameOver(oneTurn, board))) {
        printBoard(board);
        System.out.println(((oneTurn) ? "1's" : "2's") +  " turn");
        System.out.print("What piece? ");
        int origin = scan.nextInt();

      

GameOver boolean that checks if there are any actions the current player can do that are legal and will not check the player:

public static boolean gameOver(boolean oneTurn, int[][] board) {
    for (int a = 0; a<8; a++) {
        for(int b = 0; b<8; b++) {
            for (int c = 0; c<8; c++) {
                for(int d = 0; d<8; d++) {
                    if(board[a][b] / 10 == (oneTurn ? 1 : 2) && !(illegal(oneTurn, 10*a+b, 10*c+d, board)) && !(illegalCheck(oneTurn, 10*a+b, 10*c+d, board))) return false;
                }
            }
        }
    }
    return true;
}

      

A check method that tries to find a legal move that another player can make will eliminate the king:

public static boolean check(boolean oneTurn, int[][] board) {
    int king = kingNum(oneTurn, board);
    for (int a = 0; a<8; a++) {
        for(int b = 0; b<8; b++) {
            if(board[a][b] / 10 == (oneTurn ? 2 : 1) && !(illegal(!oneTurn, 10*a+b, king, board))) return true;
        }
    }
    return false;
}

      

Invalid checker method that checks if the transition will move the player. I think this is the problem. I tried to create a separate new block so the original board doesn't change, but it still poses a problem.

public static boolean illegalCheck(boolean oneTurn, int origin, int dest, int[][] board) {
    int[][] newBoard = board;
    newBoard[dest / 10][dest % 10] = board[origin / 10][origin % 10];
    newBoard[origin / 10][origin % 10] = 0;
    if(check(oneTurn, newBoard)) return true;
    else return false;
}

      

When I run the program for the first time, the first output I get is the following:

    |  0 |  1 |  2 |  3 |  4 |  5 |  6 |  7 |
_____________________________________________

0   | 1R |    | 1B | 1Q | 1K | 1B | 1N | 1R |
_____________________________________________

1   | 1P | 1P | 1P | 1P | 1P | 1P | 1P | 1P |
_____________________________________________

2   |    |    |    |    |    |    |    |    |
_____________________________________________

3   |    |    |    |    |    |    |    |    |
_____________________________________________

4   |    |    |    |    |    |    |    |    |
_____________________________________________

5   |    |    |    |    |    |    |    |    |
_____________________________________________

6   | 2P | 2P | 2P | 2P | 2P | 2P | 2P | 2P |
_____________________________________________

7   | 2R | 2N | 2B | 2Q | 2K | 2B | 2N | 2R |
_____________________________________________

1 turn
What piece? 

      

The first legal move found in game is a knight and I wonder why the knight doesn't appear on the first board. When I set the printBoard method before the boolean test, the board prints fine.

I recently started learning programming, so I would appreciate any advice on why this is happening. It might be something blatantly obvious that I'm missing. Let me know if there are other details I can provide. Thank!

+3


source to share


3 answers


When you write

int[][] newBoard = board;
newBoard[dest / 10][dest % 10] = board[origin / 10][origin % 10];
newBoard[origin / 10][origin % 10] = 0;

      

you create newBoard

as a new reference to the same array object board

as and then proceed to modify the array. You can copy it like this:



int[][] newBoard = new int[8][];
for (int i = 0; i < newBoard.length; ++i)
    newBoard[i] = board[i].clone();

      

Java multidimensional arrays are arrays of references to other arrays (since arrays are objects), so we first create a new top-level array and then assign copies of the auxiliary arrays to it. (There are several rules about Object.clone()

and Cloneable

, but all you need to know is that cloning an array makes a new array with the same values. For multidimensional arrays, this is a shallow copy (the copy refers to the same sub arrays), so we can't just call board.clone()

and do with it.)

+1


source


If you think it has to do with a copy of the array, try System.arraycopy

in illegal check.



+1


source


I would recommend using a debugger and setting some breakpoints so you can see exactly what is going on so you won't be guessing.

More info on debugging sessions from java: http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html

0


source







All Articles