c# - 剪刀石头布游戏——一个人连赢三场怎么结束?

标签 c#

我正在编写一个摇滚 (Sten)、布 (Påse)、剪刀 (Sax) 游戏,与计算机对战。它有效,但我想在两胜三负的情况下打破比赛。但是一直在循环... 我真的是编程新手,所以如果代码很乱请原谅... :( 我是瑞典人,所以代码是瑞典语的……希望你能理解,如果不问我……

这是主要的:

static void Main(string[] args)
        {

            Game ssp = new Game();

            Interaction.MsgBox("Welcome!");

            string Choice = Interaction.InputBox("Chose Rock, Scissor eller Paper:");
            ssp.Start();
            ssp.Seewicharethevinner(Choice);
         }

这是具有处理游戏方法的类:

string CompusterChoice;

        //Starts the game
        public void Start()
        {
            //Computers hand
            Random rnd = new Random();
            int x = rnd.Next(0, 3);
            if (x == 0)
            { DatornsVal = "Rock"; }
            else if (x == 1)
            { DatornsVal = "Paper"; }
            else if (x == 2)
            { DatornsVal = "Scissor"; }

        }

        //Look who will win
        public void Seewicharethewinner(string _Choice)
        {
            string PlayerChoice = _Choice;
            string _PlayerChoice = _Choice.ToUpper();
            string _ComputerChoice = ComputerChoice.ToUpper(); 

            if (_PlayerChoice == _ComputerChoice)
            {
                Interaction.MsgBox("Tie!\nYour choice was: " + _Choice + "\n" + "Computer choice was: " + _ComputerChoice);
            string Choice = Interaction.InputBox("Chose Rock, Scissor eller Paper:");
            ssp.Start();
            ssp.Seewicharethevinner(Choice);
            }

            else if (_ComputerChoice == "ROCK" && _PlayerChoice == "SCISSOR" || _ComputerChoice == "SICSSOR" && _PlayerChoice == "PAPER" || _ComputerChoice == "PAPER"
                && _PlayerChoice == "ROCK")

            {
                Interaction.MsgBox("You Lose!\nYour choice was: " + _Choice + "\n" + "Computer choice was: " + _ComputerChoice);

                int player = 0;
                int computer = 1;
                Points(computer, player);

            string Choice = Interaction.InputBox("Chose Rock, Scissor eller Paper:");
            ssp.Start();
            ssp.Seewicharethevinner(Choice);
            }

            else if (_ComputerChoice == "ROCK" && _PlayerChoice == "PAPER" || _ComputerChoice == "SICSSOR" && _PlayerChoice == "ROCK" || _ComputerChoice == "PAPER"
                && _PlayerChoice == "SICSSOR")
            {

                Interaction.MsgBox("You won!\nYour choice was: " + _Choice + "\n" + "Computer choice was: " + _ComputerChoice);
                int player = 1;
                int computer = 0;
                Points(computer, player);

            string Choice = Interaction.InputBox("Chose Rock, Scissor eller Paper:");
            ssp.Start();
            ssp.Seewicharethevinner(Choice);
            }


        }
        public void Points(int _computer, int _player)
        {
            int computerpoints = 0;
            int playerpoints = 0;
            if (_computer > _player)
            {
                computerpoints++;
            }
            else
            {
                playerpoints++;
            }
            if (computerpoints == 3)
            {
              Interaction.MsgBox("Computer won three times!");
            }
            if (playerpoints == 3)
            {
                Interaction.MsgBox("You won three times!");
            }
        }

最佳答案

所以看起来问题在于,在您的 Poang 方法中,您检查是否有人赢了 3 次,如果赢了,则显示一条消息,但在您检查之后您没有终止程序。它只是继续进行,就好像什么都没发生过一样。此外,您的获胜计数变量是局部范围的,因此每次函数结束时它们都会丢失其值。

可以做很多事情来让这个程序变得更好,但是我在这里只提供最简单的修复:

    public void UtseVinnare(string _Val)
    {
        string SpelareVal = _Val;
        string _SpelarVal = _Val.ToUpper();
        string _DatornsVal = DatornsVal.ToUpper(); 

        if (_DatornsVal == _SpelarVal)
        {
            Interaction.MsgBox("Oavgjort!\nDitt val var: " + SpelareVal + "\n" + "Datorns val var: " + DatornsVal);
            string Val = Interaction.InputBox("Välj Sten, Sax eller Påse:");
            Starta();
            UtseVinnare(Val);
        }

        else if (_DatornsVal == "STEN" && _SpelarVal == "SAX" || _DatornsVal == "SAX" && _SpelarVal == "PÅSE" || _DatornsVal == "PÅSE"
            && _SpelarVal == "STEN")

        {
            Interaction.MsgBox("Du förlorade!\nDitt val var: " + SpelareVal + "\n" + "Datorns val var: " + DatornsVal);
            int spelare = 0;
            int dator = 1;
            if (Poang(dator, spelare))
            {
                return;
            }
            string Val = Interaction.InputBox("Välj Sten, Sax eller Påse:");
            Starta();
            UtseVinnare(Val);
        }

        else if (_DatornsVal == "STEN" && _SpelarVal == "PÅSE" || _DatornsVal == "SAX" && _SpelarVal == "STEN" || _DatornsVal == "PÅSE"
            && _SpelarVal == "SAX")
        {

            Interaction.MsgBox("Du vann!\nDitt val var: " + SpelareVal + "\n" + "Datorns val var: " + DatornsVal);
            int spelare = 1;
            int dator = 0;
            if (Poang(dator, spelare))
            {
                return;
            }
            string Val = Interaction.InputBox("Välj Sten, Sax eller Påse:");
            Starta();
            UtseVinnare(Val);
        }


    }

    int datorpoangraknare = 0;
    int spelarpoangraknare = 0;

    public bool Poang(int _dator, int _spelare)
    {
        if (_dator > _spelare)
        {
            datorpoangraknare++;
        }
        else
        {
            spelarpoangraknare++;
        }
        if (datorpoangraknare == 3)
        {
          Interaction.MsgBox("Datorn vann tre gånger!");
          return true;
        }
        if (spelarpoangraknare == 3)
        {
            Interaction.MsgBox("Du vann tre gåger!");
            return true;
        }
        return false;
    }

关于c# - 剪刀石头布游戏——一个人连赢三场怎么结束?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22390796/

相关文章:

c# - 字符串数组存储时间

c# - 如何将 XML 读入数据集

c# - 自托管 ASP.NET Core 2.0 : TagHelper doesnt work

c# - ASP.NET Web API 解码中的参数绑定(bind) + (%2B) As Space

c# - 使用 ItemsSource 填充 WPF ListBox - 好主意?

c# - 对象引用未设置为对象 ViewData ["Title"] = "Index"的实例;

c# - 在 ASP.NET 中使用依赖注入(inject)和工厂模式传递服务

c# - GroupBy 执行缓慢

c# - 通过多线程下载文件

c# - Linq group by 与父对象