Example #1
0
        public void PlayNow(Position origin, Position destination)
        {
            Piece capturedPiece = PerformMovement(origin, destination);

            if (InCheck(NowPlaying))
            {
                UndoMovement(origin, destination, capturedPiece);
                throw new BoardException("You can't put yourself in Check condition");
            }
            Piece p = Board.Piece(destination);

            //promotion

            if (p is Pawn)
            {
                if ((p.Color == Color.White && destination.Line == 0) || (p.Color == Color.Black && destination.Line == 7))
                {
                    p = Board.RemovePiece(destination);
                    pieces.Remove(p);
                    Piece queen = new Queen(Board, p.Color);
                    Board.PlacePiece(queen, destination);
                    pieces.Add(queen);
                }
            }
            if (InCheck(Opponent(NowPlaying)))
            {
                Check = true;
            }
            else
            {
                Check = false;
            }

            if (InCheckMate(Opponent(NowPlaying)))
            {
                GameOver = true;
            }
            else
            {
                if (NowPlaying == Color.Black)
                {
                    Turn++;
                }
                ChangePlayer();
            }


            //enPassant
            if (p is Pawn && destination.Line == origin.Line - 2 || destination.Line == origin.Line + 2)
            {
                EnPassantVul = p;
            }
            else
            {
                EnPassantVul = null;
            }
        }
        public Piece Move(Position source, Position target)
        {
            Piece p = Board.RemovePiece(source);

            p.IncreaseMove();
            Piece capturedPiece = Board.RemovePiece(target);

            Board.PlacePiece(p, target);
            if (capturedPiece != null)
            {
                CapturedPieces.Add(capturedPiece);
            }

            // #specialMove kingside Castling
            if (p is King && target.Column == source.Column + 2)
            {
                Position sourceR = new Position(source.Row, source.Column + 3);
                Position targetR = new Position(source.Row, source.Column + 1);

                Piece R = Board.RemovePiece(sourceR);
                R.IncreaseMove();
                Board.PlacePiece(R, targetR);
            }

            // #specialMove queenside Castling
            if (p is King && target.Column == source.Column - 2)
            {
                Position sourceR = new Position(source.Row, source.Column - 4);
                Position targetR = new Position(source.Row, source.Column - 1);

                Piece R = Board.RemovePiece(sourceR);
                R.IncreaseMove();
                Board.PlacePiece(R, targetR);
            }

            // #specialMove EnPassant
            {
                if (p is Pawn && source.Column != target.Column && capturedPiece == null)
                {
                    Position posP;
                    if (p.Color == Color.White)
                    {
                        posP = new Position(target.Row + 1, target.Column);
                    }
                    else
                    {
                        posP = new Position(target.Row - 1, target.Column);
                    }

                    capturedPiece = Board.RemovePiece(posP);
                    CapturedPieces.Add(capturedPiece);
                }
            }

            return(capturedPiece);
        }
Example #3
0
        private Piece MakeMove(Position source, Position target)
        {
            ChessPiece sourcePiece = (ChessPiece)Board.RemovePiece(source);

            sourcePiece.IncreaseMoveCount();
            Piece capturedPiece = Board.RemovePiece(target);

            Board.PlacePiece(sourcePiece, target);

            if (capturedPiece != null)
            {
                PiecesOnTheBoard.Remove(capturedPiece);
                CapturedPieces.Add(capturedPiece);
            }

            //SpecialMove Castling KingSide Rook
            if (sourcePiece is King && target.Column == source.Column + 2)
            {
                Position   sourceRook = new Position(source.Row, source.Column + 3);
                Position   targetRook = new Position(source.Row, source.Column + 1);
                ChessPiece rook       = (ChessPiece)Board.RemovePiece(sourceRook);
                Board.PlacePiece(rook, targetRook);
                rook.IncreaseMoveCount();
            }
            //SpecialMove Castling QueenSide Rook
            if (sourcePiece is King && target.Column == source.Column - 2)
            {
                Position   sourceRook = new Position(source.Row, source.Column - 4);
                Position   targetRook = new Position(source.Row, source.Column - 1);
                ChessPiece rook       = (ChessPiece)Board.RemovePiece(sourceRook);
                Board.PlacePiece(rook, targetRook);
                rook.IncreaseMoveCount();
            }

            //SpecialMove EnPassant
            if (sourcePiece is Pawn)
            {
                if (source.Column != target.Column && capturedPiece == null)
                {
                    Position pawnPosition;
                    if (sourcePiece.Color == Color.White)
                    {
                        pawnPosition = new Position(target.Row + 1, target.Column);
                    }
                    else
                    {
                        pawnPosition = new Position(target.Row - 1, target.Column);
                    }
                    capturedPiece = Board.RemovePiece(pawnPosition);
                    CapturedPieces.Add(capturedPiece);
                    PiecesOnTheBoard.Remove(capturedPiece);
                }
            }

            return(capturedPiece);
        }
Example #4
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);
        }
        public void Play(Position source, Position target)
        {
            Piece capturedPiece = Move(source, target);

            if (IsCheck(CurrentPlayer))
            {
                UndoMove(source, target, capturedPiece);
                throw new BoardException("You cannot move yourself into check");
            }

            Piece p = Board.Piece(target);

            // #specialMove Promotion
            if (p is Pawn)
            {
                if ((p.Color == Color.White && target.Row == 0) || (p.Color == Color.Black && target.Row == 7))
                {
                    p = Board.RemovePiece(target);
                    Pieces.Remove(p);
                    Piece queen = new Queen(Board, p.Color);
                    Board.PlacePiece(queen, target);
                    Pieces.Add(queen);
                }
            }


            if (IsCheck(Opponent(CurrentPlayer)))
            {
                Check = true;
            }
            else
            {
                Check = false;
            };

            if (IsCheckmate(Opponent(CurrentPlayer)))
            {
                GameOver = true;
            }
            else
            {
                Turn++;
                ChangePlayer();
            };

            // #specialMove EnPassant
            if (p is Pawn && (target.Row == source.Row - 2 || target.Row == source.Row + 2))
            {
                EnPassantVulnerable = p;
            }
            else
            {
                EnPassantVulnerable = null;
            }
        }
        public void UndoMove(Position source, Position target, Piece capturedPiece)
        {
            Piece p = Board.RemovePiece(target);

            p.DecreaseMove();
            if (capturedPiece != null)
            {
                Board.PlacePiece(capturedPiece, target);
                CapturedPieces.Remove(capturedPiece);
            }
            Board.PlacePiece(p, source);

            // #specialMove kingside Castling
            if (p is King && target.Column == source.Column + 2)
            {
                Position sourceR = new Position(source.Row, source.Column + 3);
                Position targetR = new Position(source.Row, source.Column + 1);

                Piece R = Board.RemovePiece(targetR);
                R.DecreaseMove();
                Board.PlacePiece(R, sourceR);
            }

            // #specialMove queenside Castling
            if (p is King && target.Column == source.Column - 2)
            {
                Position sourceR = new Position(source.Row, source.Column - 4);
                Position targetR = new Position(source.Row, source.Column - 1);

                Piece R = Board.RemovePiece(targetR);
                R.DecreaseMove();
                Board.PlacePiece(R, sourceR);
            }

            // #specialMove EnPassant
            {
                if (p is Pawn && source.Column != target.Column && capturedPiece == EnPassantVulnerable)
                {
                    Piece    pawn = Board.RemovePiece(target);
                    Position posP;
                    if (p.Color == Color.White)
                    {
                        posP = new Position(3, target.Column);
                    }
                    else
                    {
                        posP = new Position(4, target.Column);
                    }

                    Board.PlacePiece(pawn, posP);
                    //CapturedPieces.Remove(capturedPiece);
                }
            }
        }
Example #7
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);
                CapturedPieces.Remove(capturedPiece);
                PiecesOnTheBoard.Add(capturedPiece);
            }

            //SpecialMove Castling KingSide Rook
            if (p is King && target.Column == source.Column + 2)
            {
                Position   sourceRook = new Position(source.Row, source.Column + 3);
                Position   targetRook = new Position(source.Row, source.Column + 1);
                ChessPiece rook       = (ChessPiece)Board.RemovePiece(targetRook);
                Board.PlacePiece(rook, sourceRook);
                rook.DecreaseMoveCount();
            }
            //SpecialMove Castling QueenSide Rook
            if (p is King && target.Column == source.Column - 2)
            {
                Position   sourceRook = new Position(source.Row, source.Column - 4);
                Position   targetRook = new Position(source.Row, source.Column - 1);
                ChessPiece rook       = (ChessPiece)Board.RemovePiece(targetRook);
                Board.PlacePiece(rook, sourceRook);
                rook.DecreaseMoveCount();
            }

            //SpecialMove EnPassant
            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(3, target.Column);
                    }
                    Board.PlacePiece(pawn, pawnPosition);
                }
            }
        }
Example #8
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);
                }
            }
        }
Example #9
0
        public ChessPiece ReplacePromotedPiece(String type)
        {
            if (Promoted == null)
            {
                throw new ApplicationException("There is no piece to be promoted.\n");
            }
            if (!type.Equals("B") && !type.Equals("N") && !type.Equals("R") && !type.Equals("Q"))
            {
                return(Promoted);
            }

            Position promotedPosition = Promoted.GetChessPosition().ToPosition();
            Piece    promoted         = Board.RemovePiece(promotedPosition);

            PiecesOnTheBoard.Remove(promoted);

            ChessPiece newPiece = NewPiece(type, Promoted.Color);

            Board.PlacePiece(newPiece, promotedPosition);
            PiecesOnTheBoard.Add(newPiece);

            return(newPiece);
        }
Example #10
0
 public void PlaceNewPiece(char column, int line, Piece piece)
 {
     Board.PlacePiece(piece, new ChessPosition(column, line).ToPosition());
     pieces.Add(piece);
 }
 public void PlaceNewPiece(char column, int row, Piece piece)
 {
     Board.PlacePiece(piece, new ChessPosition(column, row).ToPosition());
     Pieces.Add(piece);
 }
Example #12
0
 private void PlaceNewPiece(char column, int row, ChessPiece piece)
 {
     Board.PlacePiece(piece, new ChessPosition(column, row).ToPosition());
     PiecesOnTheBoard.Add(piece);
 }