int Agent<Reversi>::minMaxAlphaBeta(Reversi& currentGame, unsigned int depth, int alpha, int beta) { if(depth >= maxDepth || currentGame.actionBegin() == currentGame.actionEnd()) { return heuristic(currentGame); } Reversi tempGame; int score, actionCounter = 0, bestAction = 0; if(depth%2 == 0) { for(Action iter = currentGame.actionBegin(); iter != currentGame.actionEnd(); iter++) { tempGame = currentGame; tempGame.play(iter); score = minMaxAlphaBeta(tempGame,depth+1,alpha,beta); if(score > alpha) { bestAction = actionCounter; alpha = score; } if(alpha>=beta) { bestAction = actionCounter; return alpha; } actionCounter++; } if(depth == 0) { return bestAction; } else { return alpha; } } else { for(Action iter = currentGame.actionBegin(); iter != currentGame.actionEnd(); iter++) { tempGame = currentGame; tempGame.play(iter); score = minMaxAlphaBeta(tempGame,depth+1,alpha,beta); if(score < beta) { bestAction = actionCounter; beta = score; } if(alpha>=beta) { bestAction = actionCounter; return beta; } actionCounter++; } if(depth == 0) { return bestAction; } else { return beta; } } }
void Agent<Reversi>::playerMove(Action& a, Reversi& currentGame) { vector<typename Reversi::Action> actions(currentGame.actionBegin(), currentGame.actionEnd()); a = actions[minMaxAlphaBeta(currentGame,0,-999999,999999)]; }