minimax - How this evaluation function work in a Connect 4 game? (Java) -
i exploring how minimax algorithm can used in connect 4 game alpha-beta pruning.
so looking through source code connect4 player strategy , found evaluation function:
/** * score of board */ public int score(){ int score = 0; (int r= 0; r < rows; r++) { if (r <= rows-4) { (int c = 0; c < cols; c++) { score += score(r, c); } } else { (int c = 0; c <= cols-4; c++) { score += score(r, c); } } } return score; } /** * helper method score of 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 unblocked = true; tally = 0; (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 , right unblocked = true; tally = 0; (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; (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 , right unblocked = true; tally = 0; (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 code in pdf: http://ryanmaguiremusic.com/media_files/pdf/connectfoursource.pdf
i want understand how evaluation function works , decides best move made... give me help? appreciated.
here general answer:
the evaluation should give better values better positions. in games position evaluated calculating score following way: increase score desirable configurations/events , decrease undesirable ones. deciding how evaluated feature should change value (= balancing weights) can difficult.
if apply connect four 1 feature number of threats alive. algorithm (which solves 7x6) have if winning move on odd or line. , there rules "if 2nd player has 2 threats has won game" (it comes down when filling board , moves forced, rule given little simplified: 2nd player have kill threat if can't fill other columns if 1st player has odd threat there).
simple example rule given (o = first player, x = 2nd player), x wins:
* x x x * o o x o o 1 2 3 4 5 6 7
there once detailed scientific paper it. closest find @ moment: http://web.mit.edu/sp.268/www/2010/connectfourslides.pdf should give ideas.
btw. opening books (generally predefined wisdom in form e.g. joseki, fuseki) , special end game evaluators can improve performance of minimax.
Comments
Post a Comment