hi. i'm trying to write a reversi game with a smart computer player.
i want to use an alpha beta pruning minimax algorithm.
i wrote the code but the computer is not smart.
can somebody help me with my code ?
public int[] Play(String s,GameBoard gb,Color c)
{
int alpha = Integer.MAX_VALUE;
int beta = -alpha;
// returns the computers best move
return BestMove (gb,true,maxDepth, alpha, beta, c);
}
private int[] BestMove(GameBoard gb ,boolean isMax, int depth ,int alpha , int beta, Color c)
{
int score = -maxScore;
// the opponent takes the best score
if (!isMax)
score = -score;
// this is the array i will return as best move
int[] move = new int[3];
// all possible moves
List<int[]> AllMoves = gb.CalcAllMoves(c);
// check all the moves
for (int[] tmpMove : AllMoves)
{
int moveScore = 0;
// clone the game board and play the temp move
GameBoard tmpBoard = gb.CloneBoard();
tmpBoard.PlayMove(tmpMove, tmpBoard.CheckMove(tmpMove, c), c == Color.black ? true : false);
// if the game is over, evaluate the score
if (tmpBoard.isEnd())
moveScore = tmpBoard.scoreEval(c) - tmpBoard.scoreEval(c == Color.black ? Color.white : Color.black );
// perform recursive call
else if (depth > 0)
{
moveScore = BestMove (tmpBoard, !isMax, depth -1,alpha, beta, c)[2];
if (isMax && tmpMove[2] > beta)
beta = tmpMove[2];
if (!isMax && tmpMove[2] < alpha)
alpha = tmpMove[2];
}
// if we over the depth evaluate the board
else
moveScore = tmpBoard.bestEval(c);
if (isMax && tmpMove[2] > alpha)
{
tmpMove[2] = alpha;
return tmpMove;
}
if (!isMax && tmpMove[2] < beta)
{
tmpMove[2] = beta;
return tmpMove;
}
// take the move which got the highest score
if (isMax ? moveScore > score : moveScore < score)
{
score = moveScore;
move = tmpMove;
move[2] = moveScore;
}
}
return move;
}