Ejemplo n.º 1
0
  private Move executeMove(Square startSquare, Square endSquare) {
    Move move = InvalidMove.execute();

    if (isPromotionMove(startSquare, endSquare)) {
      // promotion should only be performed with game.promote()!
      move = InvalidMove.execute();
    } else if (EnPassantMove.canBeExecuted(board, startSquare, endSquare, lastMove())) {
      move = EnPassantMove.execute(board, startSquare, endSquare);
      EnPassantMove enPassantMove = (EnPassantMove) move;
      capturedPieces.get(enPassantMove.opposingPiece().color()).add(enPassantMove.opposingPiece());
    } else if (CastlingMove.canBeExecuted(board, startSquare, endSquare)) {
      move = CastlingMove.execute(board, startSquare, endSquare);
    } else if (NormalMove.canBeExecuted(board, startSquare, endSquare)) {
      move = NormalMove.execute(board, startSquare, endSquare);
    } else if (CaptureMove.canBeExecuted(board, startSquare, endSquare)) {
      move = CaptureMove.execute(board, startSquare, endSquare);
      CaptureMove captureMove = (CaptureMove) move;
      capturedPieces.get(captureMove.opposingPiece().color()).add(captureMove.opposingPiece());
    }

    if (board.inCheck(playersTurn)) {
      move.undo(board);
      removeCapturedPiece(move);
      move = InvalidMove.execute();
    }

    return move;
  }
Ejemplo n.º 2
0
  public Move promote(Square startSquare, Square endSquare, PieceType pieceType) {
    Move move = InvalidMove.execute();

    if (PromotionMove.canBeExecuted(board, startSquare, endSquare, pieceType)) {
      move = PromotionMove.execute(board, startSquare, endSquare, pieceType);
    }
    if (MoveType.PROMOTION.equals(move.moveType())) {
      if (board.inCheck(playersTurn)) {
        move.undo(board);
        move = InvalidMove.execute();
      } else {
        PromotionMove promotionMove = (PromotionMove) move;
        if (promotionMove.opposingPiece() != null) {
          capturedPieces
              .get(promotionMove.opposingPiece().color())
              .add(promotionMove.opposingPiece());
        }
        moves.add(move);
        changePlayersTurn();
      }
    }

    return move;
  }