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

            Piece capturedPiece = Board.RemovePiece(destination);

            Board.PlacePiece(piece, destination);

            piece.MovesAmountIncrease();

            if (capturedPiece != null)
            {
                captured.Add(capturedPiece);
            }
            //#Short Castling
            if (piece is King && destination.Column == origin.Column + 2)
            {
                Position rOrigin      = new Position(origin.Line, origin.Column + 3);
                Position rDestination = new Position(origin.Line, origin.Column + 1);
                Piece    R            = Board.RemovePiece(rOrigin);
                R.MovesAmountIncrease();
                Board.PlacePiece(R, rDestination);
            }

            //#Long Castling
            if (piece is King && destination.Column == origin.Column - 2)
            {
                Position rOrigin      = new Position(origin.Line, origin.Column - 4);
                Position rDestination = new Position(origin.Line, origin.Column - 1);
                Piece    R            = Board.RemovePiece(rOrigin);
                R.MovesAmountIncrease();
                Board.PlacePiece(R, rDestination);
            }

            //enPassant
            if (piece is Pawn)
            {
                if (origin.Column != destination.Column && capturedPiece == null)
                {
                    Position pawnP;
                    if (piece.Color == Color.White)
                    {
                        pawnP = new Position(destination.Line + 1, destination.Column);
                    }
                    else
                    {
                        pawnP = new Position(destination.Line - 1, destination.Column);
                    }
                    capturedPiece = Board.RemovePiece(pawnP);
                    captured.Add(capturedPiece);
                }
            }

            return(capturedPiece);
        }
Example #2
0
        public void UndoMovement(Position origin, Position destination, Piece capturedPiece)
        {
            Piece piece = Board.RemovePiece(destination);

            if (capturedPiece != null)
            {
                Board.PlacePiece(capturedPiece, destination);
                captured.Remove(capturedPiece);
            }
            Board.PlacePiece(piece, origin);
            piece.MovesAmountDecrease();

            //#Short Castling
            if (piece is King && destination.Column == origin.Column + 2)
            {
                Position rOrigin      = new Position(origin.Line, origin.Column + 3);
                Position rDestination = new Position(origin.Line, origin.Column + 1);
                Piece    R            = Board.RemovePiece(rDestination);
                R.MovesAmountIncrease();
                Board.PlacePiece(R, rOrigin);
            }

            //#Long Castling
            if (piece is King && destination.Column == origin.Column - 2)
            {
                Position rOrigin      = new Position(origin.Line, origin.Column - 4);
                Position rDestination = new Position(origin.Line, origin.Column - 1);
                Piece    R            = Board.RemovePiece(rDestination);
                R.MovesAmountIncrease();
                Board.PlacePiece(R, rOrigin);
            }
            if (piece is Pawn)
            {
                if (origin.Column != destination.Column && capturedPiece == EnPassantVul)
                {
                    Piece    pawn = Board.RemovePiece(destination);
                    Position pawnP;
                    if (piece.Color == Color.White)
                    {
                        pawnP = new Position(3, destination.Column);
                    }
                    else
                    {
                        pawnP = new Position(4, destination.Column);
                    }
                    Board.PlacePiece(pawn, pawnP);
                }
            }
        }