javaandroidalgorithmminimaxreversi

Alpha-beta pruning


I've implemented the following MiniMax algorithm for my Android Reversi game:

@Override
public Field findBestMove(GameBoard gb, int depth, boolean player) 
{   
/** maximum depth of search reached, we stop */
if(depth >= max_depth) return null;

//player = (depth+1)%2 + 1;

/** getting a list of moves to chose from */
ArrayList <Field> moves = findAllPossibleMoves(gb, player); 

Field best_move = null;

/** iterating over all possible moves, to find the best one */      
for (int i=0; i<moves.size(); i++)
{
    /** board to simulate moves */
    GameBoard temp_board = new GameBoard(gb);
    /** getting the current move */
    Field move = moves.get(i);      
    /** simulating the move for the current node */
    game.move(move, temp_board, player);
    Log.i("board", "Depth:"+depth+" Player:"+player+" Move:"+i+" Rating:"+evaluate(temp_board));
    Log.i("board", ""+moves.get(i).getX()+","+moves.get(i).getY());         
    temp_board.printBoard();
    /** getting to the next inferior node */            
    Field best_deep_move = findBestMove (temp_board, depth + 1, !player);           

    /** if the maximum depth is reached, we have a null, so we evaluate */
    if (best_deep_move == null) 
    {
        move.setRating(evaluate (temp_board));
    }
    /** if we are not the deepest possible, we get the rating from the lower node */
    else 
    {
        move.setRating(best_deep_move.getRating());         
        Log.i("eval", ""+best_deep_move.getRating());
    }           
    if(best_move == null) 
    {
        best_move = move;           
    }

    else
    {   
        Log.i("update", "Current move rating:"+move.getRating());
        Log.i("update", "New move rating:"+best_move.getRating());
        if (depth%2==0)
        {
            Log.i("update", "MAX player");
            /** for us, we look for the maximum */
            if (best_move.getRating() < move.getRating()) 
                {


                best_move = move;

                }

        }
        else
        {
            Log.i("update", "MIN player");
            /** for the opponent, we look for the minimum */
            if (best_move.getRating() > move.getRating())
            { 


                best_move = move;

            }
        }
        Log.i("update", "Updated move rating"+best_move.getRating());
    }
}

return best_move;

}

I've made myself familiar with the Alpha-Beta pruning in theory, though I'm having some trouble proceeding with applying that knowledge in this algorithm. Thanks in advance


Solution

  • There following changes that need to done to your code to implement alpha-beta pruning:-

    1. pass a parameter public Field findBestMove(GameBoard gb, int depth, boolean player,int aplha_beta)

    2. Stop recursion if current best_move will never affect alpha_beta of previous depth.

      if(player == max && best_move!=null && aplha_beta <= best_move.getRating()) {
      
                 return(best_move);
       }
      
       if(player == min && best_move!=null && alpha_beta >= best_move.getRating()) {
      
              return(best_move);
       }
      
       Field best_deep_move = findBestMove(temp_board,depth+1,!player,best_move.getRating());