Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2012/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过while块锁定布尔值_C#_While Loop_Boolean_Console Application - Fatal编程技术网

C# 通过while块锁定布尔值

C# 通过while块锁定布尔值,c#,while-loop,boolean,console-application,C#,While Loop,Boolean,Console Application,我正在创建一个纸牌游戏,我希望创建一个上帝模式来吓唬我的朋友,但我需要在我使用的所有方法中保持一个布尔值 所有代码: namespace Card_Game_1 { class Program { static void Main(string[] args) { while (true) { P1Hand(); } }

我正在创建一个纸牌游戏,我希望创建一个上帝模式来吓唬我的朋友,但我需要在我使用的所有方法中保持一个布尔值

所有代码:

namespace Card_Game_1
{
    class Program
    {
        static void Main(string[] args)
        {
            while (true)
            {
                P1Hand();
            }

        }


        #region PosAns
        static string PosAns()
        {
            string posans = "Y";
            return posans;
        }
        #endregion


        #region NegAns
        static string NegAns()
        {
            string posans = "N";
            return posans;
        }
        #endregion


        #region P1Hand
        static int P1Hand()
        {
            int P1Hand = 0;
            if (PlayerInput().Equals(PosAns()))
            {
                int NewRnd = Rnd();
                string StringRnd = NewRnd.ToString();
                Console.WriteLine(StringRnd);
                P1Hand = P1Hand + NewRnd;
                Console.WriteLine("You're Hand Is " + P1Hand);
            }
            else
            {
                if (GMActP1() == true)
                {
                    P1Hand = GodModeP1();
                    Console.WriteLine("You're Hand Is " + P1Hand);
                }
                if (PlayerInput().Equals(NegAns()))
                {
                    Console.WriteLine("You're Hand Is " +P1Hand);
                }

            }

            return P1Hand;
        }
        #endregion


        #region P2Hand
        static int P2Hand()
        {
            // Not completing till P1 is done
            return P2Hand;
        }
        #endregion


        #region NormalMode
        static int Rnd()
        {
            Random Rd = new Random();            
            int[] Cards = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
            int RandomIdex = Rd.Next(1, Cards.Length);
            return RandomIdex;
        }
        #endregion


        #region GodMString
        static string GodMString()
        {
            string GmAct = " ChTy";
            return GmAct;
        }
        #endregion


        #region GodModeP1
        static int GodModeP1()
        {
            int NewP1Hand = P1Hand();
            int WinHand = 21 - NewP1Hand;
            return WinHand;
        }
        #endregion


        #region GMActP1
        static bool GMActP1()
        {
            bool Godmode = false;
            if (PlayerInput().Equals(PosAns() + GodMString()))
            {
                Godmode = true;

            }
            bool NewGodM = Godmode;
            return NewGodM;            
        }
        #endregion


        #region GodModeP2
        static int GodModeP2()
        {
            int NewP2Hand = P2Hand();
            int WinHand = 21 - NewP2Hand;
            return WinHand;
        }
        #endregion


        #region PlayerInput
        static string PlayerInput()
        {
            string S = Console.ReadLine();

            return S;
        }
        #endregion
    }
}
现在我的问题是:

1:当逻辑转向God模式方法或GM方法时,它会画一个空白,然后请求另一个PlayerInput方法

2:我相信这是因为GMActP1方法中的布尔值在while语句中没有保持为true,或者根本没有返回值,所以它不符合P1Hand方法中的逻辑

这些方法只在我完成之前存在,因为它可以帮助我跟踪事情,所以编辑起来更容易,当我完成后,我会编辑它,让它不那么疯狂。GMString用作密码,而GMActP1是上帝模式的激活器,用于P1Hand中的逻辑

问题就在这里:

   static bool GMActP1()
    {
        bool Godmode = false;
        Console.WriteLine(Godmode);
        if (PlayerInput().Equals(PosAns() + GodMString()))
        {
            Godmode = true;
            Console.WriteLine(Godmode);

        }
        bool NewGodM = Godmode;
        Console.WriteLine(NewGodM);
        return NewGodM;            
    }
我检查了最后一段代码,它在所有阶段都返回false。
我的问题是,在满足所需逻辑后,是否可以将bool var锁定为true?我不希望每次运行时都将bool var声明为false,因为这意味着您必须在每次运行时都写上“Y ChTy”,如果您不想让您的朋友知道,当你输入这个“作弊代码”时,要让他们换个角度看是不容易的。当我让它工作时,我会让它在前几轮正常运行,直到所需的数字21小于12,因为12是你能收到的最大卡数。这样看来,它就像是一个对您有利的普通游戏。

为什么不使用一个全局声明的变量呢?我可以,但我相信我的问题在于它试图在if语句之后返回值。我还想只在字符串被输入后激活godmode,比如:Y-ChTy。所以我也不能说它是真的,因为它总是开着的,我也只想让一个玩家玩。因此,如果玩家1在游戏中输入,他们有上帝模式,但不是玩家2。现在我想起来了,我可以把它放到逻辑中,也许可以一起绕过布尔值。使用断点,然后找出,如果你的布尔变量有你期望的值,它在所有阶段都只返回false。PlayerInput().Equals(PosAns()+GodMString());仅当输入为“Y ChTy”时,此方法才为真,然后该方法将返回真。我检查过了,它还在工作。你面临的问题是什么?