Example #1
0
        public Piece MovePiece(Position origin, Position destination)
        {
            Piece piece = Board.TakePiece(origin);

            piece.AddMovements();
            Piece pieceCaptured = Board.TakePiece(destination);

            Board.PutPiece(piece, destination);
            if (pieceCaptured != null)
            {
                Captured.Add(pieceCaptured);
            }

            // #Small castling
            if (piece is King && destination.Column == origin.Column + 2)
            {
                Position RookOriginPosition      = new Position(origin.Row, origin.Column + 3);
                Position RookDestinationPosition = new Position(origin.Row, origin.Column + 1);
                Piece    rook = Board.TakePiece(RookOriginPosition);
                rook.AddMovements();
                Board.PutPiece(rook, RookDestinationPosition);
            }

            // #Big castling
            if (piece is King && destination.Column == origin.Column - 2)
            {
                Position RookOriginPosition      = new Position(origin.Row, origin.Column - 4);
                Position RookDestinationPosition = new Position(origin.Row, origin.Column - 1);
                Piece    rook = Board.TakePiece(RookOriginPosition);
                rook.AddMovements();
                Board.PutPiece(rook, RookDestinationPosition);
            }

            // #En passant
            if (piece is Pawn)
            {
                if (origin.Column != destination.Column && pieceCaptured == null)
                {
                    Position pawnPosition;
                    if (piece.Colour == Colour.white)
                    {
                        pawnPosition = new Position(destination.Row + 1, destination.Column);
                    }
                    else
                    {
                        pawnPosition = new Position(destination.Row - 1, destination.Column);
                    }
                    pieceCaptured = Board.TakePiece(pawnPosition);
                    Captured.Add(pieceCaptured);
                }
            }

            return(pieceCaptured);
        }