Пример #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);
            }
        }
Пример #2
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);
        }
Пример #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);
        }
Пример #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);
        }