Exemplo n.º 1
0
  public static int MiniMax(Game game, char player, int depth, int maxDepth) {
    if (depth == maxDepth || game.isGameOver()) {
      return evaluate(game, player);
    }

    Move bestMove = null;
    int bestScore;
    if (game.getTurn() == player) {
      bestScore = Integer.MIN_VALUE;
    } else {
      bestScore = Integer.MAX_VALUE;
    }

    List<Move> allMoves = getMoves(game, game.getTurn());
    for (Move move : allMoves) {
      int[][] matrixCopy = game.getBoard().getMatrixCopy();

      game.processMove(move.getCopy());
      game.changeTurn();
      int moveScore = MiniMax(game, player, depth + 1, maxDepth);

      game.changeTurn();
      game.getBoard().setMatrix(matrixCopy);

      if (game.getTurn() == player) {
        if (moveScore > bestScore) {
          bestScore = moveScore;
          bestMove = move.getCopy();
        }
      } else {
        if (moveScore < bestScore) {
          bestScore = moveScore;
          bestMove = move.getCopy();
        }
      }
    }

    if (depth == 0) {
      computedBestMove = bestMove;
    }

    return bestScore;
  }