I have implemented a Gomoku Player class that uses alpha beta pruning to select the best possible move for the computer, but I am having trouble working out how to write the evaluation function to correctly score each position on the board.
I have an 'evaluateBoard' method that evaluates the board position and updates the number of blacks, whites or blank positions in each row, column and diagonal. My 'scoreChange' method scores each position, and this is where I feel my program is making errors as it does not block me when I have a 3 or 4 in a row.
How can I evaluate and score each position correctly?
public int evaluateBoard(Color[][] board, Color computer) {
int score = 0;
//Check all the rows
for (int i = 0; i < 8; ++i) {
int blank = 0;
int black = 0;
int white = 0;
for (int j = 0; j < 8; ++j) {
if (board[i][j] == null) {
blank++;
} else if (board[i][j] == Color.black) {
black++;
} else {
white++;
}
}
score+=scoreChange(black, white, computer);
}
//Check all the columns
for (int j = 0; j < 8; ++j) {
int blank = 0;
int black = 0;
int white = 0;
for (int i = 0; i < 8; ++i) {
if (board[i][j] == null) {
blank++;
} else if (board[i][j] == Color.black) {
black++;
} else {
white++;
}
}
score+=scoreChange(black, white, computer);
}
int blank = 0;
int black = 0;
int white = 0;
//Check diagonal (first)
for (int i = 0, j = 0; i < 8; ++i, ++j) {
if (board[i][j] == Color.black) {
black++;
} else if (board[i][j] == Color.white) {
white++;
} else {
blank++;
}
}
score+=scoreChange(black, white, computer);
blank = 0;
black = 0;
white = 0;
//Check Diagonal (Second)
for (int i = 7, j = 0; i > -1; --i, ++j) {
if (board[i][j] == Color.black) {
black++;
} else if (board[i][j] == Color.white) {
white++;
} else {
blank++;
}
}
score+=scoreChange(black, white, computer);
return score;
}
private int scoreChange(int black, int white){
int change;
if (black == 5) {
change = -10000;
} else if (black == 4 && white == 0) {
change = -1000;
} else if (black == 3 && white == 0) {
change = -100;
} else if (black == 2 && white == 0) {
change = -10;
} else if (black == 1 && white == 0) {
change = -1;
} else if (white == 5) {
change = 10000;
} else if (white == 4 && black == 0) {
change = 1000;
} else if (white == 3 && black == 0) {
change = 100;
} else if (white == 2 && black == 0) {
change = 10;
} else if (white == 1 && black == 0) {
change = 1;
} else {
change = 0;
}
return change;
}