我已经实现了一个 Gomoku Player 类,它使用 alpha beta 修剪来为计算机选择可能的最佳移动,但是我无法弄清楚如何编写评估函数来正确地对棋盘上的每个位置进行评分。
我有一个 'evaluateBoard' 方法,可以评估棋盘位置并更新每行、每列和对角线中的黑色、白色或空白位置的数量。我的 'scoreChange' 方法对每个位置进行评分,这就是我觉得我的程序出错的地方,因为当我连续获得 3 或 4 时它不会阻止我。
如何正确评估和评分每个职位?
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;
}