我在探索如何在連接四個遊戲中使用Minimax算法進行alpha-beta修剪。這個評估函數如何在Connect 4遊戲中使用? (Java)
所以我一直在尋找通過關於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
我只是想了解如何這個評估功能的作品,並決定最好的舉動... 任何人都可以給我一些幫助?這將不勝感激。