Exemple #1
0
  private int Minimax(OthelloState state, int depth, int alpha, int beta) {

    // Reached max search depth or game is over
    if (depth == max_depth) return state.score();
    else {
      // Generate all moves
      List<OthelloMove> moves = state.generateMoves();

      // No possible moves or end of game
      if (moves == null) return state.score();

      // Maximizing (player 1)
      else if (state.nextPlayerToMove == 0) {
        int best_score = (int) Double.NEGATIVE_INFINITY;

        for (int i = 0; i < moves.size(); i++) {
          OthelloState new_state = state.applyMoveCloning(moves.get(i));
          int score = Minimax(new_state, depth + 1, alpha, beta);
          if (score > best_score) best_score = score;

          // alpha-beta pruning
          alpha = Math.max(alpha, best_score);
          if (alpha >= beta) return alpha;
        }
        return best_score;
      }
      // Minimizing (player 2)
      else {
        int best_score = (int) Double.POSITIVE_INFINITY;

        for (int i = 0; i < moves.size(); i++) {
          OthelloState new_state = state.applyMoveCloning(moves.get(i));
          int score = Minimax(new_state, depth + 1, alpha, beta);
          if (score < best_score) best_score = score;

          // alpha-beta pruning
          beta = Math.min(beta, best_score);
          if (beta <= alpha) return beta;
        }
        return best_score;
      }
    }
  }