Exemple #1
0
  /** Returns a move that minimizes the maximum utility by this agent. */
  private Move min(Board board, int depth) {
    nodes++;
    // If the node is a terminal, return the heuristic, which is the score advantage for this agent.
    if (depth <= 0) return new Move(board.getScoreAdvantage(ID));
    // Get an array of all legal moves, and test all of them to find the one that maximizes the
    // minimum
    // score advantage of this agent.
    Move[] moves = board.getLegalMoves();
    // If there are no legal moves, return a value reflective of whether the agent has won, tied, or
    // lost.
    if (moves.length == 0)
      return new Move(
          (board.getScoreAdvantage(ID) > 0)
              ? Integer.MAX_VALUE
              : ((board.getScoreAdvantage(ID) == 0) ? 0 : Integer.MIN_VALUE));

    Move min = null;
    for (Move m : moves) {
      // Test the given move and replace min with the move if it has a lower value.
      Move test = max(m.move(board), depth - 1);
      board.unmove(1);
      if (min == null || test.getValue() < min.getValue()) {
        min = m;
        min.setValue(test.getValue());
      }
    }
    // Return the minimum move
    return min;
  }