playUnchecked method

  1. @override
Position playUnchecked(
  1. Move move
)
override

Plays a move without checking if the move is legal.

In addition to standard rules, all captures cause an explosion by which the captured piece, the piece used to capture, and all surrounding pieces except pawns that are within a one square radius are removed from the board.

Implementation

@override
Position playUnchecked(Move move) {
  final castlingSide = _getCastlingSide(move);
  final capturedPiece = castlingSide == null ? board.pieceAt(move.to) : null;
  final isCapture = capturedPiece != null || move.to == epSquare;
  final newPos = super.playUnchecked(move);

  if (isCapture) {
    Castles newCastles = newPos.castles;
    Board newBoard = newPos.board.removePieceAt(move.to);
    for (final explode in kingAttacks(move.to)
        .intersect(newBoard.occupied)
        .diff(newBoard.pawns)
        .squares) {
      final piece = newBoard.pieceAt(explode);
      newBoard = newBoard.removePieceAt(explode);
      if (piece != null) {
        if (piece.role == Role.rook) {
          newCastles = newCastles.discardRookAt(explode);
        }
        if (piece.role == Role.king) {
          newCastles = newCastles.discardSide(piece.color);
        }
      }
    }
    return newPos.copyWith(board: newBoard, castles: newCastles);
  } else {
    return newPos;
  }
}