Esempio n. 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);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// builder to start the match
        /// </summary>
        public ChessMatch()
        {
            StringBuilder text = new StringBuilder(string.Concat("START MATCH ", DateTime.Now.ToShortDateString()));

            text.AppendLine(string.Concat(" Time: ", DateTime.Now.ToShortTimeString()));
            WriteLog.ChecksLogs(text);

            Board           = new Board(8, 8);
            TotalMoves      = 1;
            CurrentPlayer   = Color.White;
            Pieces          = new HashSet <Piece>();
            CapturedPieces  = new HashSet <Piece>();
            IsCheck         = false;
            EnableEnPassant = null;
            BuildPieces();
        }
Esempio n. 3
0
        /// <summary>
        /// Put piece
        /// </summary>
        /// <param name="piece">Piece</param>
        /// <param name="column">Column</param>
        /// <param name="line">Row</param>
        private void PutPiece(Piece piece, char column, int line)
        {
            Board.PutPiece(piece, new ChessPosition(column, line).ToPosition());
            Pieces.Add(piece);


            StringBuilder text = new StringBuilder();

            text.AppendLine("Put piece");
            text.AppendLine(string.Concat("Piece: ", piece.GetType()));
            text.AppendLine(string.Concat("Color: ", piece.Color.ToString()));
            text.AppendLine(string.Concat("ChessPosition: ", column, line, " CanvasPosition(Column, Row): ", piece.Position.Column, piece.Position.Row));
            text.AppendLine(string.Concat("Date: ", DateTime.Now.ToShortDateString(), " Time: ", DateTime.Now.ToShortTimeString()));

            WriteLog.ChecksLogs(text);
        }
Esempio n. 4
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);
        }
Esempio n. 5
0
        /// <summary>
        /// Sets initial position of pices on the board.
        /// </summary>
        private void BuildPieces()
        {
            // Blacks
            PutPiece(new Rook(Board, Color.Black), 'a', 8);
            PutPiece(new Knight(Board, Color.Black), 'b', 8);
            PutPiece(new Bishop(Board, Color.Black), 'c', 8);
            PutPiece(new Queen(Board, Color.Black), 'd', 8);
            PutPiece(new King(Board, Color.Black, this), 'e', 8);
            PutPiece(new Bishop(Board, Color.Black), 'f', 8);
            PutPiece(new Knight(Board, Color.Black), 'g', 8);
            PutPiece(new Rook(Board, Color.Black), 'h', 8);
            PutPiece(new Pawn(Board, Color.Black, this), 'a', 7);
            PutPiece(new Pawn(Board, Color.Black, this), 'b', 7);
            PutPiece(new Pawn(Board, Color.Black, this), 'c', 7);
            PutPiece(new Pawn(Board, Color.Black, this), 'd', 7);
            PutPiece(new Pawn(Board, Color.Black, this), 'e', 7);
            PutPiece(new Pawn(Board, Color.Black, this), 'f', 7);
            PutPiece(new Pawn(Board, Color.Black, this), 'g', 7);
            PutPiece(new Pawn(Board, Color.Black, this), 'h', 7);

            // Whites
            PutPiece(new Rook(Board, Color.White), 'a', 1);
            PutPiece(new Knight(Board, Color.White), 'b', 1);
            PutPiece(new Bishop(Board, Color.White), 'c', 1);
            PutPiece(new Queen(Board, Color.White), 'd', 1);
            PutPiece(new King(Board, Color.White, this), 'e', 1);
            PutPiece(new Bishop(Board, Color.White), 'f', 1);
            PutPiece(new Knight(Board, Color.White), 'g', 1);
            PutPiece(new Rook(Board, Color.White), 'h', 1);
            PutPiece(new Pawn(Board, Color.White, this), 'a', 2);
            PutPiece(new Pawn(Board, Color.White, this), 'b', 2);
            PutPiece(new Pawn(Board, Color.White, this), 'c', 2);
            PutPiece(new Pawn(Board, Color.White, this), 'd', 2);
            PutPiece(new Pawn(Board, Color.White, this), 'e', 2);
            PutPiece(new Pawn(Board, Color.White, this), 'f', 2);
            PutPiece(new Pawn(Board, Color.White, this), 'g', 2);
            PutPiece(new Pawn(Board, Color.White, this), 'h', 2);

            StringBuilder text = new StringBuilder();

            text.AppendLine(string.Concat("Finished put: ", DateTime.Now.ToShortDateString(), " Time: ", DateTime.Now.ToShortTimeString()));

            WriteLog.ChecksLogs(text);
        }
Esempio n. 6
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);
        }
Esempio n. 7
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");
                    }
                }
            }
        }