mardi 26 février 2019

Java gomoku algorithm with If's and loops: Constant null error

I am trying to create a java gomoku player that scans the board (8*8 2D array) and return a move that is somewhat competent (not using any complex data structures yet).

There is a GUI that lets me test a program, but every time I run this I get a 'null' and my player looses.

I've tried to put comments to explain - I dont know which part is causing the nulls so I've had to put my whole class here.

class Player160489815 extends GomokuPlayer{

public Move chooseMove(Color[][] board, Color myColour) {
    //first argument is current state of the board (each cell contains Color.black, Color.white or null)
    //second argument identifies which colour I am playing (Color.black or Color.white)

    //Initialise two variables to hold best values for row and column
    int x = 0; //int for row
    int y = 0; //int for col

    //2 loops to scan the entire board and evalute each empty square
    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            //check is square is empty - if not, then ignore
            if (board[i][j] == null) {
                //give x,y values of empty square (possible valid move)
                x=i;
                y=j;
                //Check if the next two cells happen to be the same colour
                if (board[i + 1][j] == myColour && isNextSpaceSameHorizontal(board, myColour, i + 1, j) == true) {
                    //if true, moving to i,j would make a 3 in a row
                    x=i;
                    y=j;
                    //now check if text 3 cells = same colour
                    if (board[i + 2][j] == myColour && isNextSpaceSameHorizontal(board, myColour, i + 2, j) == true) {
                        //if true, moving to i,j would make a 4 in a row
                        x=i;
                        y=j;
                        //do same for next 4 cells
                        if (board[i + 3][j] == myColour && isNextSpaceSameHorizontal(board, myColour, i + 3, j) == true) {
                            //if true, moving to i,j would make a 5 in a row; since this is a winning move: return
                            return new Move(i, j);
                        }
                    }
                }
                //repeat above but horizontally
                if (board[i][j + 1] == myColour && isNextSpaceSameVertical(board, myColour, i, j + 1) == true) {
                    //if true, moving to i,j would make 3 in a row
                    x=i;
                    y=j;
                    if (board[i][j + 2] == myColour && isNextSpaceSameHorizontal(board, myColour, i, j + 2) == true) {
                        //if true, moving to i,j would make a 4 in a row
                        x=i;
                        y=j;
                        if (board[i][j + 3] == myColour && isNextSpaceSameHorizontal(board, myColour, i, j + 3) == true) {
                            //if true, moving to i,j would make a 5 in a row; since this is a winning move: return
                            return new Move(i, j);
                        }
                    }
                }
            }
        }//first for-loop
    }//second for-loop
    return new Move(x, y);
}//end chooseMove methods

//this method will check is the next horizontal cell is the same colour
public boolean isNextSpaceSameHorizontal(Color[][] board, Color me, int a, int b){
    boolean isTrue = false;
    if (board[a][b].equals(me)) {
        isTrue = true;
    }
    return isTrue;
}

//this method will check is the next vertical cell is the same colour
public boolean isNextSpaceSameVertical(Color[][] board, Color me, int a, int b){
    boolean isTrue = false;
    if (board[a][b].equals(me)) {
        isTrue = true;
    }
    return isTrue;
}

}//end class

The Move object has two fields, row and col, corresponding to a move at board[row][col], so both fields should be between 0 and 7

Help much appreciated.

Aucun commentaire:

Enregistrer un commentaire