Example #1
0
        /// <summary>
        /// Checks if last play was a castling and move the respective tower
        /// </summary>
        /// <param name="king">King</param>
        private void ExecuteCastling(Piece king)
        {
            Piece    tower  = null;
            Position origin = null;
            Position target = null;

            if (king.Position.Column == 6)
            {
                tower = Board.Piece(king.Position.Row, king.Position.Column + 1);
                if (tower.NumberOfMovements == 0)
                {
                    origin = tower.Position;
                    target = new Position(king.Position.Row, king.Position.Column - 1);
                    Board.RemovePiece(origin);
                    Board.PutPiece(tower, target);

                    StringBuilder text = new StringBuilder();
                    text.AppendLine("Execute castling piece");
                    text.AppendLine(string.Concat("Piece: ", tower.GetType()));
                    text.AppendLine(string.Concat("Color: ", tower.Color.ToString()));
                    text.AppendLine(string.Concat("Origin ChessPosition: ", origin.ToChessPosition().ToString(), " Origin CanvasPosition(Column, Row): ", origin.Column, ", ", origin.Row));
                    text.AppendLine(string.Concat("Target ChessPosition: ", target.ToChessPosition().ToString(), " Target CanvasPosition(Column, Row): ", target.Column, ", ", target.Row));
                    text.AppendLine(string.Concat("Turn: ", TotalMoves));
                    text.AppendLine(string.Concat("Date: ", DateTime.Now.ToShortDateString(), " Time: ", DateTime.Now.ToShortTimeString()));

                    WriteLog.ChecksLogs(text);

                    tower.IncrementMovements();
                }
            }
            else if (king.Position.Column == 2)
            {
                tower = Board.Piece(king.Position.Row, king.Position.Column - 2);
                if (tower.NumberOfMovements == 0)
                {
                    origin = tower.Position;
                    target = new Position(king.Position.Row, king.Position.Column + 1);
                    Board.RemovePiece(origin);
                    Board.PutPiece(tower, target);

                    StringBuilder text = new StringBuilder();
                    text.AppendLine("Execute castling piece");
                    text.AppendLine(string.Concat("Piece: ", tower.GetType()));
                    text.AppendLine(string.Concat("Color: ", tower.Color.ToString()));
                    text.AppendLine(string.Concat("Origin ChessPosition: ", origin.ToChessPosition().ToString(), " Origin CanvasPosition(Column, Row): ", origin.Column, ", ", origin.Row));
                    text.AppendLine(string.Concat("Target ChessPosition: ", target.ToChessPosition().ToString(), " Target CanvasPosition(Column, Row): ", target.Column, ", ", target.Row));
                    text.AppendLine(string.Concat("Turn: ", TotalMoves));
                    text.AppendLine(string.Concat("Date: ", DateTime.Now.ToShortDateString(), " Time: ", DateTime.Now.ToShortTimeString()));

                    WriteLog.ChecksLogs(text);

                    tower.IncrementMovements();
                }
            }

            if (KingIsCheck(king.Color) && origin != null)
            {
                UndoMovement(origin, target, null);
            }
        }
Example #2
0
        public void PerformMovement(Position origin, Position destination)
        {
            Piece piece = Board.RemovePiece(origin);

            piece.IncreaseNumberOfMovies();
            Piece capturedPiece = Board.RemovePiece(destination);

            Board.PutPiece(piece, destination);
        }
Example #3
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;
            }
        }
Example #4
0
        public void MakeAPlay(Position origin, Position destiny)
        {
            Piece capturedPiece = ExecuteAMovement(origin, destiny);


            if (ItIsInCheck(CurrentPlayer))
            {
                UndoTheMovement(origin, destiny, capturedPiece);
                throw new BoardException("You cannot put yourself in check!");
            }

            Piece piece = Board.Piece(destiny);

            // #SpecialPlay Promotion

            if ((piece.Color == Color.White && destiny.Line == 0) || (piece.Color == Color.Black && destiny.Line == 7))
            {
                piece = Board.RemovePiece(destiny);
                Pieces.Remove(piece);

                Piece queen = new Queen(Board, piece.Color);
                Board.InsertPiece(queen, destiny);
                Pieces.Add(queen);
            }

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

            if (TestCheckMate(Opponent(CurrentPlayer)))
            {
                Finished = true;
            }
            else
            {
                Shift++;
                ChangePlayer();
            }



            //#SpecialPlay EnPassant

            if (piece is Pawn && (destiny.Line == origin.Line - 2 || destiny.Line == origin.Line + 2))
            {
                VulnerableEnPassant = piece;
            }
            else
            {
                VulnerableEnPassant = 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 #6
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 #7
0
        public void PerformMove(Position origin, Position destiny)
        {
            Piece capturatedPiece = MovementExecute(origin, destiny);

            if (IsInCheck(CurrentPlayer))
            {
                UndoMovement(origin, destiny, capturatedPiece);
                throw new BoardException("You cannot put yourself in a check!");
            }

            Piece p = Board.Piece(destiny);

            //#EspecialPlay promotion
            if (p is Peasant)
            {
                if ((p.Color == Color.white && destiny.Line == 0) || (p.Color == Color.black && destiny.Line == 7))
                {
                    p = Board.RemovePiece(destiny);
                    Pieces.Remove(p);
                    Piece queen = new Queen(Board, p.Color);
                    Pieces.Add(queen);
                }
            }


            if (IsInCheck(Adversary(CurrentPlayer)))
            {
                Check = true;
            }
            else
            {
                Check = false;
            }

            if (CheckmateTest(Adversary(CurrentPlayer)))
            {
                Finished = true;
            }
            else
            {
                Shift++;
                ChangePlayer();
            }

            p = Board.Piece(destiny);

            //#EspecialPlay en passant
            if (p is Peasant && (destiny.Line == origin.Line - 2 || destiny.Line == origin.Line + 2))
            {
                VulnerableEnPassant = p;
            }
            else
            {
                VulnerableEnPassant = null;
            }
        }
Example #8
0
        public Piece ExecuteAMovement(Position origin, Position destiny)
        {
            Piece piece = Board.RemovePiece(origin);

            piece.IncrementeNumberOfMovements();
            Piece capturedPiece = Board.RemovePiece(destiny);

            Board.InsertPiece(piece, destiny);
            if (capturedPiece != null)
            {
                Captured.Add(capturedPiece);
            }
            //#SpecialPlay Castling short

            if (piece is King && destiny.Column == origin.Column + 2)
            {
                Position rookOrigin  = new Position(origin.Line, origin.Column + 3);
                Position rookDestiny = new Position(origin.Line, origin.Column + 1);
                Piece    rook        = Board.RemovePiece(rookOrigin);
                rook.IncrementeNumberOfMovements();
                Board.InsertPiece(rook, rookDestiny);
            }

            //#SpecialPlay Castling long

            if (piece is King && destiny.Column == origin.Column - 2)
            {
                Position rookOrigin  = new Position(origin.Line, origin.Column - 4);
                Position rookDestiny = new Position(origin.Line, origin.Column - 1);
                Piece    rook        = Board.RemovePiece(rookOrigin);
                rook.IncrementeNumberOfMovements();
                Board.InsertPiece(rook, rookDestiny);
            }

            //#SpecialPlay EnPassant
            if (piece is Pawn)
            {
                if (origin.Column != destiny.Column && capturedPiece == null)
                {
                    Position pawnPosition;
                    if (piece.Color == Color.White)
                    {
                        pawnPosition = new Position(destiny.Line + 1, destiny.Column);
                    }
                    else
                    {
                        pawnPosition = new Position(destiny.Line - 1, destiny.Column);
                    }

                    capturedPiece = Board.RemovePiece(pawnPosition);
                    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;
            }
        }
Example #10
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 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 #12
0
        public void UndoTheMovement(Position origin, Position destiny, Piece capturedPice)
        {
            Piece piece = Board.RemovePiece(destiny);

            piece.DecrementNumberOfMovements();
            if (capturedPice != null)
            {
                Board.InsertPiece(capturedPice, destiny);
                Captured.Remove(capturedPice);
            }
            Board.InsertPiece(piece, origin);

            //#SpecialPlay Castling short
            if (piece is King && destiny.Column == origin.Column + 2)
            {
                Position rookOrigin  = new Position(origin.Line, origin.Column + 3);
                Position rookDestiny = new Position(origin.Line, origin.Column + 1);
                Piece    rook        = Board.RemovePiece(rookDestiny);
                rook.DecrementNumberOfMovements();
                Board.InsertPiece(rook, rookOrigin);
            }

            //#SpecialPlay Castling long
            if (piece is King && destiny.Column == origin.Column - 2)
            {
                Position rookOrigin  = new Position(origin.Line, origin.Column - 4);
                Position rookDestiny = new Position(origin.Line, origin.Column - 1);
                Piece    rook        = Board.RemovePiece(rookDestiny);
                rook.DecrementNumberOfMovements();
                Board.InsertPiece(rook, rookOrigin);
            }

            //#SpecialPlay En Passant

            if (piece is Pawn)
            {
                if (origin.Column != destiny.Column && capturedPice == VulnerableEnPassant)
                {
                    Piece    pawn = Board.RemovePiece(destiny);
                    Position pawnPosition;
                    if (piece.Color == Color.White)
                    {
                        pawnPosition = new Position(3, destiny.Column);
                    }
                    else
                    {
                        pawnPosition = new Position(4, destiny.Column);
                    }

                    Board.InsertPiece(pawn, pawnPosition);
                }
            }
        }
Example #13
0
        public Piece MovementExecute(Position origin, Position destiny)
        {
            Piece p = Board.RemovePiece(origin);

            p.MovementIncrement();
            Piece CapturedPiece = Board.RemovePiece(destiny);

            Board.addPiece(p, destiny);
            if (CapturedPiece != null)
            {
                Capturated.Add(CapturedPiece);
            }

            //#EspecialPlay small-Rock
            if (p is King && destiny.Column == origin.Column + 2)
            {
                Position originT  = new Position(origin.Line, origin.Column + 3);
                Position destinyT = new Position(origin.Line, origin.Column + 1);
                Piece    T        = Board.RemovePiece(originT);
                T.MovementIncrement();
                Board.addPiece(T, destinyT);
            }

            //#EspecialPlay big-Rock
            if (p is King && destiny.Column == origin.Column - 2)
            {
                Position originT  = new Position(origin.Line, origin.Column - 4);
                Position destinyT = new Position(origin.Line, origin.Column - 1);
                Piece    T        = Board.RemovePiece(originT);
                T.MovementIncrement();
                Board.addPiece(T, destinyT);
            }

            //#EspecialPlay en passant
            if (p is Peasant)
            {
                if (origin.Column != destiny.Column && CapturedPiece == null)
                {
                    Position posP;
                    if (p.Color == Color.white)
                    {
                        posP = new Position(destiny.Line + 1, destiny.Column);
                    }
                    else
                    {
                        posP = new Position(destiny.Line - 1, destiny.Column);
                    }
                    CapturedPiece = Board.RemovePiece(posP);
                    Capturated.Add(CapturedPiece);
                }
            }
            return(CapturedPiece);
        }
Example #14
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 #15
0
        private void UndoMovement(Position origin, Position destiny, Piece capturatedPiece)
        {
            Piece p = Board.RemovePiece(destiny);

            p.MovementDecrement();
            if (capturatedPiece != null)
            {
                Board.addPiece(capturatedPiece, destiny);
                Capturated.Remove(capturatedPiece);
            }
            Board.addPiece(p, origin);

            //#EspecialPlay small-Rock
            if (p is King && destiny.Column == origin.Column + 2)
            {
                Position originT  = new Position(origin.Line, origin.Column + 3);
                Position destinyT = new Position(origin.Line, origin.Column + 1);
                Piece    T        = Board.RemovePiece(destinyT);
                T.MovementDecrement();
                Board.addPiece(T, originT);
            }

            //#EspecialPlay big-Rock
            if (p is King && destiny.Column == origin.Column - 2)
            {
                Position originT  = new Position(origin.Line, origin.Column - 4);
                Position destinyT = new Position(origin.Line, origin.Column - 1);
                Piece    T        = Board.RemovePiece(destinyT);
                T.MovementDecrement();
                Board.addPiece(T, originT);
            }

            //#EspecialPlay en passant
            if (p is Peasant)
            {
                if (origin.Column != destiny.Column && capturatedPiece == VulnerableEnPassant)
                {
                    Piece    peasant = Board.RemovePiece(destiny);
                    Position posP;
                    if (p.Color == Color.white)
                    {
                        posP = new Position(3, destiny.Column);
                    }
                    else
                    {
                        posP = new Position(4, destiny.Column);
                    }
                    Board.addPiece(peasant, posP);
                }
            }
        }
Example #16
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 #17
0
        /// <summary>
        /// Execute move of chess piece
        /// </summary>
        /// <param name="origin">Origin of piece</param>
        /// <param name="target">Target position</param>
        public Piece ExecuteMove(Position origin, Position target)
        {
            Piece piece         = Board.RemovePiece(origin);
            Piece capturedPiece = Board.RemovePiece(target);

            Board.PutPiece(piece, target);

            // Log
            StringBuilder text = new StringBuilder();

            text.AppendLine("Move piece");
            text.AppendLine(string.Concat("Piece: ", piece.GetType()));
            text.AppendLine(string.Concat("Color: ", piece.Color.ToString()));
            text.AppendLine(string.Concat("Origin ChessPosition: ", origin.ToChessPosition().ToString(), " Origin CanvasPosition(Column, Row): ", origin.Column, ", ", origin.Row));
            text.AppendLine(string.Concat("Target ChessPosition: ", target.ToChessPosition().ToString(), " Target CanvasPosition(Column, Row): ", target.Column, ", ", target.Row));
            text.AppendLine(string.Concat("Turn: ", TotalMoves));
            text.AppendLine(string.Concat("Date: ", DateTime.Now.ToShortDateString(), " Time: ", DateTime.Now.ToShortTimeString()));


            piece.IncrementMovements();

            if (capturedPiece != null)
            {
                CapturedPieces.Add(capturedPiece);
                text.AppendLine(string.Concat("Captured: ", capturedPiece.GetType()));
            }
            WriteLog.ChecksLogs(text);

            if (Board.Piece(target) is King && ((target.Column + 2 == origin.Column) || (target.Column - 2 == origin.Column)))
            {
                ExecuteCastling(Board.Piece(target));
            }

            if (Board.Piece(target) != null && Board.Piece(target) is Pawn && origin.Column != target.Column && capturedPiece == null)
            {
                capturedPiece = ExecuteEnPassant(Board.Piece(target), origin);
            }

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

            return(capturedPiece);
        }
Example #18
0
        /// <summary>
        /// Undo the movement itself that would put in check
        /// </summary>
        /// <param name="piece">Current piece</param>
        /// <param name="capturedPiece">Captured piece</param>
        /// <param name="origin">Origin of current piece</param>
        private void UndoMovement(Position origin, Position target, Piece captured)
        {
            Piece piece = Board.RemovePiece(target);

            piece.DecrementMovements();
            StringBuilder text      = new StringBuilder();
            Position      originAux = null;

            if (captured != null)
            {
                text.AppendLine("Undo captured");
                text.AppendLine(string.Concat("Captured Piece: ", captured.GetType()));
                text.AppendLine(string.Concat("Color: ", captured.Color.ToString()));
                if (captured != EnableEnPassant)
                {
                    Board.PutPiece(captured, target);
                    text.AppendLine(string.Concat("Origin ChessPosition: ", origin.ToChessPosition().ToString(), " Origin CanvasPosition(Column, Row): ", origin.Column, ", ", origin.Row));
                    CapturedPieces.Remove(captured);
                }
                else
                {
                    originAux = new Position(origin.Row, target.Column);
                    Board.PutPiece(captured, originAux);
                    CapturedPieces.Remove(captured);
                    text.AppendLine(string.Concat("Origin ChessPosition: ", originAux.ToChessPosition().ToString(), " Origin CanvasPosition(Column, Row): ", originAux.Column, ", ", originAux.Row));
                }

                text.AppendLine(string.Concat("Turn: ", TotalMoves));
                text.AppendLine(string.Concat("Date: ", DateTime.Now.ToShortDateString(), " Time: ", DateTime.Now.ToShortTimeString()));
                text.AppendLine();
            }

            Board.PutPiece(piece, origin);

            text.AppendLine(string.Concat("Undo Piece: ", captured.GetType()));
            text.AppendLine(string.Concat("Color: ", captured.Color.ToString()));
            text.AppendLine(string.Concat("Origin ChessPosition: ", origin.ToChessPosition().ToString(), " Origin CanvasPosition(Column, Row): ", origin.Column, ", ", origin.Row));
            text.AppendLine(string.Concat("Turn: ", TotalMoves));
            text.AppendLine(string.Concat("Date: ", DateTime.Now.ToShortDateString(), " Time: ", DateTime.Now.ToShortTimeString()));

            WriteLog.ChecksLogs(text);
        }
Example #19
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 #20
0
        /// <summary>
        /// Checks is pawn can promotion
        /// </summary>
        /// <param name="color">To checks if a black or white</param>
        public void MakePromotion(Color color)
        {
            if (color is Color.Black)
            {
                if (Position.Row == 7)
                {
                    Console.WriteLine("This pawn was promote, you can promotion a new piece.");
                    Console.WriteLine("Choose any T: Tower, B: Bishop, N: Knight and Q: Queen: ");
                    string choose = Console.ReadLine().ToUpper();

                    Promotions promotion = 0;
                    if (Enum.TryParse <Promotions>(choose, out promotion))
                    {
                        Position pos = Position;
                        Board.RemovePiece(Position);
                        switch (promotion)
                        {
                        case Promotions.T:
                            Board.PutPiece(new Rook(Board, color), pos);
                            break;

                        case Promotions.B:
                            Board.PutPiece(new Bishop(Board, color), pos);
                            break;

                        case Promotions.N:
                            Board.PutPiece(new Knight(Board, color), pos);
                            break;

                        case Promotions.Q:
                            Board.PutPiece(new Queen(Board, color), pos);
                            break;
                        }
                    }
                    else
                    {
                        throw new BoardException("Invalid promotion");
                    }
                }
            }
            else if (color is Color.White)
            {
                if (Position.Row == 0)
                {
                    Console.WriteLine("This pawn was promote, you can promotion a new piece.");
                    Console.WriteLine("Choose any T: Tower, B: Bishop, N: Knight and Q: Queen: ");
                    string choose = Console.ReadLine().ToUpper();

                    Promotions promotion;
                    if (Enum.TryParse(choose, out promotion))
                    {
                        Position pos = Position;
                        Board.RemovePiece(Position);
                        StringBuilder text = new StringBuilder();
                        switch (promotion)
                        {
                        case Promotions.T:
                            Rook rook = new Rook(Board, color);
                            Board.PutPiece(rook, pos);
                            text.AppendLine(string.Concat("Promote pawn to: ", rook.GetType()));
                            text.AppendLine(string.Concat("ChessPosition: ", rook.Position.ToChessPosition(), " CanvasPosition(Column, Row): ", rook.Position.Column, ", ", rook.Position.Row));

                            break;

                        case Promotions.B:
                            Bishop bishop = new Bishop(Board, color);
                            Board.PutPiece(bishop, pos);
                            text.AppendLine(string.Concat("Promote pawn to: ", bishop.GetType()));
                            text.AppendLine(string.Concat("ChessPosition: ", bishop.Position.ToChessPosition(), " CanvasPosition(Column, Row): ", bishop.Position.Column, ", ", bishop.Position.Row));
                            break;

                        case Promotions.N:
                            Knight knight = new Knight(Board, color);
                            Board.PutPiece(knight, pos);
                            text.AppendLine(string.Concat("Promote pawn to: ", knight.GetType()));
                            text.AppendLine(string.Concat("ChessPosition: ", knight.Position.ToChessPosition(), " CanvasPosition(Column, Row): ", knight.Position.Column, ", ", knight.Position.Row));
                            break;

                        case Promotions.Q:
                            Queen queen = new Queen(Board, color);
                            Board.PutPiece(queen, pos);
                            text.AppendLine(string.Concat("Promote pawn to: ", queen.GetType()));
                            text.AppendLine(string.Concat("ChessPosition: ", queen.Position.ToChessPosition(), " CanvasPosition(Column, Row): ", queen.Position.Column, ", ", queen.Position.Row));
                            break;
                        }
                        text.AppendLine(string.Concat("Color: ", color));
                        WriteLog.ChecksLogs(text);
                    }
                    else
                    {
                        throw new BoardException("Invalid promotion");
                    }
                }
            }
        }
Example #21
0
        /// <summary>
        /// Playmake enpassant
        /// </summary>
        /// <param name="piece">Would was a pawn</param>
        private Piece ExecuteEnPassant(Piece pawn, Position origin)
        {
            Piece enemyPawn = Board.Piece(origin.Row, pawn.Position.Column);

            return(Board.RemovePiece(enemyPawn.Position));
        }