Пример #1
0
    /** Helper method to determine if special font is needed. */
    public static final boolean isSymbol(int value) {
      if (value == MoveEvaluation.minimum()) {
        return true;
      } else if (value == MoveEvaluation.maximum()) {
        return true;
      }

      return false; // nothing special.
    }
Пример #2
0
    /**
     * Helper method for converting values into properly visible labels.
     *
     * <p>These special characters are displayed in Symbol font.
     */
    public static final String convert(int value) {
      if (value == MoveEvaluation.minimum()) {
        return "-inf"; // - INF using an em-dash
      } else if (value == MoveEvaluation.maximum()) {
        return "inf"; // that is char for INF in Symbol font.
      }

      return "" + value;
    }
  /**
   * Provides a default evaluation function for the given board state. The evaluation is taken from
   * the point of view of a given player AFTER he has made his move.
   *
   * <p>If the board state is:
   *
   * <ul>
   *   <li>a win for the player, then MoveEvaluation.maximum() is returned.
   *   <li>a win for the opponent, then MoveEvaluation.minimum()
   *   <li>otherwise, score returned is difference NC(player) - NC(opponent), where NC(x) is the
   *       number of complete rows, columns or diagonals still open for player x on the board.
   * </ul>
   *
   * @param state The state of the game
   * @param ip The player who has just made their move
   * @return integer representing board score.
   */
  public int score(IGameState state, IPlayer ip) {
    TicTacToeState tttState = (TicTacToeState) state;
    TicTacToeBoard board = tttState.board();
    Player p = (Player) ip;

    // we have lost!
    int n = numInRow(board, 3, p.getOpponentMark());
    if (n > 0) {
      return MoveEvaluation.minimum();
    }

    // we are the champion!
    n = numInRow(board, 3, p.getMark());
    if (n > 0) {
      return MoveEvaluation.maximum();
    }

    // count number of complete rows/columns/diagonals that are still
    // open for a given player.
    int n1 = availableRow(board, p.getMark());
    int n2 = availableRow(board, p.getOpponentMark());
    return n1 - n2;
  }