Esempio n. 1
0
  /**
   * Returns the move with the best score for the selected player, doing a recursive search to the
   * specified depth. Returns a ScoreMove object that holds the move and its score
   *
   * @param depth the max depth to search to
   * @param color the color whose score to maximize
   * @param a
   * @return the highest scoring move and score
   */
  private ScoreMove chooseMove(int depth, int color, int a, int b) {
    Move bestMove = new Move();
    int bestScore = 0;
    if (color == this.color) bestScore = a;
    else bestScore = b;

    LinkedList<ScoreMove> scores = new LinkedList<ScoreMove>();
    boolean first = true; // first is used to get the method started
    for (Move move : board.getValidMoves(color)) {
      int moveScore = getScore(move, depth, color, a, b);
      scores.add(new ScoreMove(moveScore, move));
      if (first
          || (this.color == color && moveScore > bestScore)
          || (oppositeColor(this.color) == color
              && moveScore < bestScore)) // This player's move (max score)
      { // Other player's move (min score)
        bestMove = move;
        bestScore = moveScore;
        if (this.color == color) {
          a = moveScore;
        } else {
          b = moveScore;
        }
        first = false;
      }
      if (a >= b) break;
    }
    return new ScoreMove(bestScore, bestMove);
  }