我正在探索如何在带有 alpha-beta 修剪的四人游戏中使用 Minimax 算法。
因此,我查看了有关 Connect4 播放器策略的源代码,发现了这个评估函数:
/**
* Get the score of a board
*/
public int score(){
int score = 0;
for (int r= 0; r < ROWS; r++) {
if (r <= ROWS-4) {
for (int c = 0; c < COLS; c++) {
score += score(r, c);
}
} else {
for (int c = 0; c <= COLS-4; c++) {
score += score(r, c);
}
}
}
return score;
}
/**
* Helper method to get the score of a board
*/
public int score(int row, int col){
int score = 0;
boolean unblocked = true;
int tally = 0;
//int r, c;
if (row < ROWS-3) {
//check up
unblocked = true;
tally = 0;
for (int r=row; r<row+4; r++) {
if (board[r][col] == CHECKERS[1-playerToMoveNum]) {
unblocked = false;
}
if (board[r][col] == CHECKERS[playerToMoveNum]) {
tally ++;
}
}
if (unblocked == true) {
score = score + (tally*tally*tally*tally);
}
if (col < COLS-3) {
//check up and to the right
unblocked = true;
tally = 0;
for (int r=row, c=col; r<row+4; r++, c++) {
if (board[r][c] == CHECKERS[1-playerToMoveNum]) {
unblocked = false;
}
if (board[r][c] == CHECKERS[playerToMoveNum]) {
tally ++;
}
}
if (unblocked == true) {
score = score + (tally*tally*tally*tally);
}
}
}
if (col < COLS-3) {
//check right
unblocked = true;
tally = 0;
for (int c=col; c<col+4; c++) {
if (board[row][c] == CHECKERS[1-playerToMoveNum]) {
unblocked = false;
}
if (board[row][c] == CHECKERS[playerToMoveNum]) {
tally ++;
}
}
if (unblocked == true) {
score = score + (tally*tally*tally*tally);
}
if (row > 2) {
//check down and to the right
unblocked = true;
tally = 0;
for (int r=row, c=col; c<col+4; r--, c++) {
if (board[r][c] == CHECKERS[1-playerToMoveNum]) {
unblocked = false;
}
if (board[r][c] == CHECKERS[playerToMoveNum]) {
tally ++;
}
}
if (unblocked == true) {
score = score + (tally*tally*tally*tally);
}
}
}
return score;
}
我在这个 PDF 中找到了所有这些代码:http ://ryanmaguiremusic.com/media_files/pdf/ConnectFourSource.pdf
我只是想了解此评估功能的工作原理并决定要采取的最佳行动……有人可以帮我吗?这将不胜感激。