Exemple #1
0
  /**
   * Converts the string representation of a move into a move and then applies it to the current
   * board.
   *
   * @param m the move string.
   */
  public void applyMove(String m) {
    if (board.plyCount() != plyCount++) {
      throw new IllegalStateException("Did you forget to call undoMove() somewhere?");
    }

    board.applyMove(board.createMoveFromString(m));
  }
 /*
  * evalHigh
  *
  * This function evaluates the board according to the above spec. It
  * loops through all the pieces on the board and assigns two sums:
  * whiteSum and blackSum, for the value of white's and black's pieces,
  * respectively. Then it adds them and sets the sign according to whose
  * turn it is.
  */
 private int evalHigh(ArrayBoard board, int color) {
   if (color == ArrayBoard.WHITE) {
     int whiteSum = 0;
     if (board.hasCastled[ArrayBoard.WHITE]) whiteSum += CASTLE_BONUS;
     for (ArrayPiece p : board.allPiecesOfColor(ArrayBoard.WHITE)) {
       switch (p.type()) {
         case ArrayPiece.KING:
           whiteSum += kingval;
           break;
         case ArrayPiece.QUEEN:
           whiteSum += queenval;
           break;
         case ArrayPiece.ROOK:
           whiteSum += rookval;
           break;
         case ArrayPiece.BISHOP:
           whiteSum += bishoppos[p.row()][p.col()] + bishopval;
           break;
         case ArrayPiece.KNIGHT:
           whiteSum += knightpos[p.row()][p.col()] + knightval;
           break;
         case ArrayPiece.PAWN:
           whiteSum += pawnpos[p.row()][p.col()] + pawnval;
           break;
       }
     }
     return whiteSum;
   } else {
     int blackSum = 0;
     for (ArrayPiece p : board.allPiecesOfColor(ArrayBoard.BLACK)) {
       switch (p.type()) {
         case ArrayPiece.KING:
           blackSum += kingval;
           break;
         case ArrayPiece.QUEEN:
           blackSum += queenval;
           break;
         case ArrayPiece.ROOK:
           blackSum += rookval;
           break;
         case ArrayPiece.BISHOP:
           blackSum += bishoppos[7 - p.row()][p.col()] + bishopval;
           break;
         case ArrayPiece.KNIGHT:
           blackSum += knightpos[7 - p.row()][p.col()] + knightval;
           break;
         case ArrayPiece.PAWN:
           blackSum += pawnpos[7 - p.row()][p.col()] + pawnval;
           break;
       }
     }
     return blackSum;
   }
 }
  /*
   * eval
   *
   * This function figures out if it's endgame. if so,
   * evaluate with that knowledge. Else, use the regular evaluator,
   * which just counts pieces.
   */
  public int eval(ArrayBoard board) {
    int whiteSum = 0, blackSum = 0;

    int numWhitePieces = board.countOfColor(ArrayBoard.WHITE);
    int numBlackPieces = board.countOfColor(ArrayBoard.BLACK);

    if (numWhitePieces <= 5 || numBlackPieces <= 5) {
      whiteSum = evalLow(board, ArrayBoard.WHITE);
      blackSum = evalLow(board, ArrayBoard.BLACK);
    } else {
      whiteSum = evalHigh(board, ArrayBoard.WHITE);
      blackSum = evalHigh(board, ArrayBoard.BLACK);
    }

    return (board.toPlay() == ArrayBoard.WHITE) ? (whiteSum - blackSum) : (blackSum - whiteSum);
  }
  /*
   * evalLow
   *
   * This function evaluates the board according to the above spec,
   * but makes pieces more valuable. It also uses the kingpos array to
   * encourage the king to move towards the middle of the board.
   */
  private int evalLow(ArrayBoard board, int color) {

    if (color == ArrayBoard.WHITE) {
      int whiteSum = 0;
      if (board.hasCastled[ArrayBoard.WHITE]) whiteSum += CASTLE_BONUS;

      for (ArrayPiece p : board.allPiecesOfColor(ArrayBoard.WHITE)) {
        switch (p.type()) {
          case ArrayPiece.KING:
            whiteSum +=
                4
                    * (calculateWhiteKingWeight(
                        p,
                        board.countOfColor(ArrayBoard.WHITE),
                        board.countOfColor(ArrayBoard.BLACK)));
            break;
          case ArrayPiece.QUEEN:
            whiteSum += 2 * queenval;
            break;
          case ArrayPiece.ROOK:
            whiteSum += 2 * rookval;
            break;
          case ArrayPiece.BISHOP:
            whiteSum += 2 * (bishoppos[p.row()][p.col()] + bishopval);
            break;
          case ArrayPiece.KNIGHT:
            whiteSum += 2 * (knightpos[p.row()][p.col()] + knightval);
            break;
          case ArrayPiece.PAWN:
            whiteSum += 2 * (pawnpos[p.row()][p.col()] + pawnval);
            break;
        }
      }
      return whiteSum;
    } else {
      int blackSum = 0;
      for (ArrayPiece p : board.allPiecesOfColor(ArrayBoard.BLACK)) {
        switch (p.type()) {
          case ArrayPiece.KING:
            blackSum +=
                4
                    * (calculateBlackKingWeight(
                        p,
                        board.countOfColor(ArrayBoard.WHITE),
                        board.countOfColor(ArrayBoard.BLACK)));
            break;
          case ArrayPiece.QUEEN:
            blackSum += 2 * queenval;
            break;
          case ArrayPiece.ROOK:
            blackSum += 2 * rookval;
            break;
          case ArrayPiece.BISHOP:
            blackSum += 2 * (bishoppos[7 - p.row()][p.col()] + bishopval);
            break;
          case ArrayPiece.KNIGHT:
            blackSum += 2 * (knightpos[7 - p.row()][p.col()] + knightval);
            break;
          case ArrayPiece.PAWN:
            blackSum += 2 * (pawnpos[7 - p.row()][p.col()] + pawnval);
            break;
        }
      }
      return blackSum;
    }
  }