Esempio 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;
  }
Esempio n. 2
0
  public Move move(Square startSquare, Square endSquare) {
    Move move = InvalidMove.execute();

    if (startSquareContainsPlayersPiece(startSquare)) {
      move = executeMove(startSquare, endSquare);
      if (!move.moveType().equals(MoveType.INVALID)) {
        moves.add(move);
        changePlayersTurn();
      }
    }

    return move;
  }
Esempio n. 3
0
  private void removeCapturedPiece(Move move) {
    Piece capturedPiece = null;
    if (MoveType.CAPTURE.equals(move.moveType())) {
      capturedPiece = ((CaptureMove) move).opposingPiece();
    } else if (MoveType.EN_PASSANT.equals(move.moveType())) {
      capturedPiece = ((EnPassantMove) move).opposingPiece();
    } else if (MoveType.PROMOTION.equals(move.moveType())) {
      capturedPiece = ((PromotionMove) move).opposingPiece();
    }

    if (capturedPiece != null) {
      capturedPieces.get(capturedPiece.color()).remove(capturedPiece);
    }
  }
Esempio n. 4
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;
  }