playUnchecked method

Position playUnchecked(
  1. Move move
)

Plays a move without checking if the move is legal and returns the updated Position.

Implementation

Position playUnchecked(Move move) {
  switch (move) {
    case NormalMove(from: final from, to: final to, promotion: final prom):
      final piece = board.pieceAt(from);
      if (piece == null) {
        return copyWith();
      }
      final castlingSide = _getCastlingSide(move);
      Square? epCaptureTarget;
      Square? newEpSquare;
      Board newBoard = board.removePieceAt(from);
      Castles newCastles = castles;
      if (piece.role == Role.pawn) {
        if (to == epSquare) {
          epCaptureTarget = Square(to + (turn == Side.white ? -8 : 8));
          newBoard = newBoard.removePieceAt(epCaptureTarget);
        }
        final delta = from - to;
        if (delta.abs() == 16 && from >= Square.a2 && from <= Square.h7) {
          newEpSquare = Square((from + to) >>> 1);
        }
      } else if (piece.role == Role.rook) {
        newCastles = newCastles.discardRookAt(from);
      } else if (piece.role == Role.king) {
        if (castlingSide != null) {
          final rookFrom = castles.rookOf(turn, castlingSide);
          if (rookFrom != null) {
            final rook = board.pieceAt(rookFrom);
            newBoard = newBoard
                .removePieceAt(rookFrom)
                .setPieceAt(kingCastlesTo(turn, castlingSide), piece);
            if (rook != null) {
              newBoard = newBoard.setPieceAt(
                  rookCastlesTo(turn, castlingSide), rook);
            }
          }
        }
        newCastles = newCastles.discardSide(turn);
      }

      if (castlingSide == null) {
        final newPiece = prom != null
            ? piece.copyWith(role: prom, promoted: pockets != null)
            : piece;
        newBoard = newBoard.setPieceAt(to, newPiece);
      }

      final capturedPiece = castlingSide == null
          ? board.pieceAt(to)
          : to == epSquare && epCaptureTarget != null
              ? board.pieceAt(epCaptureTarget)
              : null;
      final isCapture = capturedPiece != null;

      if (capturedPiece != null && capturedPiece.role == Role.rook) {
        newCastles = newCastles.discardRookAt(to);
      }

      return copyWith(
        halfmoves: isCapture || piece.role == Role.pawn ? 0 : halfmoves + 1,
        fullmoves: turn == Side.black ? fullmoves + 1 : fullmoves,
        pockets: capturedPiece != null
            ? pockets?.increment(capturedPiece.color.opposite,
                capturedPiece.promoted ? Role.pawn : capturedPiece.role)
            : pockets,
        board: newBoard,
        turn: turn.opposite,
        castles: newCastles,
        epSquare: newEpSquare,
      );
    case DropMove(to: final to, role: final role):
      return copyWith(
        halfmoves: role == Role.pawn ? 0 : halfmoves + 1,
        fullmoves: turn == Side.black ? fullmoves + 1 : fullmoves,
        turn: turn.opposite,
        board: board.setPieceAt(to, Piece(color: turn, role: role)),
        pockets: pockets?.decrement(turn, role),
      );
  }
}