Exemplo n.º 1
0
        //Method to move valid piece to valid location
        //		returns (true, null) if move was successful and neither king was checked
        //		returns (true, " ... ") if enemy king was checked or checkmated
        private static (bool, string) MovePiece(Piece piece, Point target)
        {
            string message     = null;
            Point  oldPoint    = piece.GetLoc();
            Piece  overwritten = Chess.GetBoard().MovePiece(piece, target);

            bool blackChecked = blackKing.CheckCheck();
            bool whiteChecked = whiteKing.CheckCheck();

            Func <bool, char, char, bool> CheckSelf = (isChecked, mine, kings) => isChecked && (mine == kings);

            bool checkSelf = CheckSelf(blackChecked, piece.GetColor(), 'b') || CheckSelf(whiteChecked, piece.GetColor(), 'w');

            //Return false with error message 'That would check your own king!'
            //		return board to original state
            if (checkSelf)
            {
                //Restoring board to original position
                Chess.GetBoard().MovePiece(piece, oldPoint);
                Chess.GetBoard().MovePiece(overwritten, target);

                return(false, "That move would check your king!");
            }
            else
            {
                //Now that we know we are not checked, check if enemy is checkmated
                bool enemyChecked;
                King checkedKing;
                (enemyChecked, checkedKing) = (blackChecked) ? (true, blackKing) : (whiteChecked, whiteKing);

                //If the enemy is checked, check if they are also checkmated
                if (enemyChecked)
                {
                    message = "Enemy king checked!";
                    bool enemeyCheckMated = checkedKing.CheckMate();

                    if (enemeyCheckMated)
                    {
                        message = "Check mate!";
                        return(false, message);
                    }
                }
            }
            return(true, message);
        }