I am exploring how a Minimax algorithm can be used in a connect four game with alpha-beta pruning.
So I was looking through a source code about a Connect4 player strategy and found this evaluation function:
/**
* 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;
}
I found all this code in this PDF: http://ryanmaguiremusic.com/media_files/pdf/ConnectFourSource.pdf
I just want to understand how this evaluation function works and decides the best move to be made... Could anyone give me some help? It would be greatly appreciated.