Example #1
0
        private void UndoMove(Position source, Position target, Piece capturedPiece)
        {
            ChessPiece p = (ChessPiece)_board.RemovePiece(target);

            p.DecreaseMoveCount();
            _board.PlacePiece(p, source);

            if (capturedPiece != null)
            {
                _board.PlacePiece(capturedPiece, target);
                _piecesOnTheBoard.Add(capturedPiece);
                _capturedPieces.Remove(capturedPiece);
            }

            // #specialmove castling kingside rook
            if (p is King && target.Column == source.Column + 2)
            {
                Position   origemT  = new Position(source.Row, source.Column + 3);
                Position   destinoT = new Position(source.Row, source.Column + 1);
                ChessPiece rook     = (ChessPiece)_board.RemovePiece(destinoT);
                rook.DecreaseMoveCount();
                _board.PlacePiece(rook, origemT);
            }

            // #specialmove castling queenside rook
            if (p is King && target.Column == source.Column - 2)
            {
                Position   origemT  = new Position(source.Row, source.Column - 4);
                Position   destinoT = new Position(source.Row, source.Column - 1);
                ChessPiece rook     = (ChessPiece)_board.RemovePiece(destinoT);
                rook.DecreaseMoveCount();
                _board.PlacePiece(rook, origemT);
            }

            // #specialmove en passant
            if (p is Pawn)
            {
                if (source.Column != target.Column && capturedPiece == EnPassantVulnerable)
                {
                    ChessPiece pawn = (ChessPiece)_board.RemovePiece(target);
                    Position   pawnPosition;
                    if (p.Color == Color.White)
                    {
                        pawnPosition = new Position(3, target.Column);
                    }
                    else
                    {
                        pawnPosition = new Position(4, target.Column);
                    }
                    _board.PlacePiece(pawn, pawnPosition);
                }
            }
        }