摇滚,纸,剪刀游戏 - 如何结束当一个人赢三次? [英] Rock, Paper, Scissor game - how to end when one wins three times?

查看:80
本文介绍了摇滚,纸,剪刀游戏 - 如何结束当一个人赢三次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个摇滚(斯登),纸(PASE),剪刀(萨克斯)游戏,可以播放对着电脑。它的工作原理和所有,但我想打破比赛的时候一关两连胜三次。但它一直循环......
Im真正新的编程所以借口如果代码是凌乱... :(
和IM瑞典这样的代码在瑞典语...希望你明白,如果不要问我。



这是主:

 静无效的主要(字串[] args)
{

游戏SSP =新游戏();

Interaction.MsgBox(欢迎光临!);

串选择= Interaction.InputBox(之所以选择摇滚,剪刀埃勒论文:);
ssp.Start();
ssp.Seewicharethevinner(选择);
$} b $ b

这是与HANDELS游戏方法的类:

 字符串CompusterChoice; 

//玩家在游戏开始
公共无效启动()
{
//计算机手
随机RND =新随机();
INT X = rnd.Next(0,3);
如果(X == 0)
{DatornsVal =岩 }
,否则如果(X == 1)
{DatornsVal =纸; }
,否则如果(X == 2)
{DatornsVal =剪刀脚; }

}

//看看谁就能赢得
公共无效Seewicharethewinner(字符串_Choice)
{
串PlayerChoice = _Choice;
串_PlayerChoice = _Choice.ToUpper();
串_ComputerChoice = ComputerChoice.ToUpper();

如果(_PlayerChoice == _ComputerChoice)
{
Interaction.MsgBox(铁\\\
Your选择是:+ _Choice +\\\
+电脑选择是:+ _ComputerChoice);
串选择= Interaction.InputBox(之所以选择摇滚,剪刀埃勒论文:);
ssp.Start();
ssp.Seewicharethevinner(选择);
}

,否则如果(_ComputerChoice ==ROCK&放大器;&安培; _PlayerChoice ==剪刀|| _ComputerChoice ==SICSSOR&放大器;&安培; _PlayerChoice ==纸 !|| _ComputerChoice ==纸
和;&安培; _PlayerChoice ==ROCK)

{
Interaction.MsgBox(你输\\\
Your选择是: + _Choice +\\\
+电脑的选择是:+ _ComputerChoice);

INT球员= 0;
INT电脑= 1;
点(电脑,播放器);

串选择= Interaction.InputBox(之所以选择摇滚,剪刀埃勒论文:);
ssp.Start();
ssp.Seewicharethevinner(选择);
}

,否则如果(_ComputerChoice ==ROCK&放大器;&安培; _PlayerChoice ==纸|| _ComputerChoice ==SICSSOR&放大器;&安培; _PlayerChoice ==ROCK || _ComputerChoice ==纸
和;&安培; _PlayerChoice ==SICSSOR)
{

Interaction.MsgBox(你赢了\\\
Your选择是: + _Choice +\\\
+电脑的选择是:+ _ComputerChoice);
INT球员= 1;
INT电脑= 0;
点(电脑,播放器);

串选择= Interaction.InputBox(之所以选择摇滚,剪刀埃勒论文:);
ssp.Start();
ssp.Seewicharethevinner(选择);
}


}
公共无效点(INT _computer,诠释_player)
{
INT computerpoints = 0;
INT playerpoints = 0;
如果(_computer> _player)
{
computerpoints ++;
}
,否则
{
playerpoints ++;
}
如果(computerpoints == 3)
{
Interaction.MsgBox(计算机三次夺冠!);
}
如果(playerpoints == 3)
{
Interaction.MsgBox(你赢了三次!);
}
}


解决方案

所以,它看起来像问题是,在你的方法POÄNG波昂你检查,看看是否有人已经赢得3次,如果是这样显示一条消息,但后检查,你不终止程序。它只是让事情仿佛什么都没有发生。另外,你的胜利数变量是局部范围,所以他们失去了所有的函数结束时自己的价值。



有很多事情可以做,使这个节目更好的,但我只是要提供简单的解决办法在这里:

 公共无效UtseVinnare(字符串_Val)
{
串SpelareVal = _Val;
串_SpelarVal = _Val.ToUpper();
串_DatornsVal = DatornsVal.ToUpper();

如果(_DatornsVal == _SpelarVal)
{
Interaction.MsgBox(Oavgjort \\\
Ditt VAL VAR:+ SpelareVal +\\\
+Datorns VAL变种:+ DatornsVal);
串瓦尔= Interaction.InputBox(Välj斯登,萨克斯埃勒PASE:);
启动市场();
UtseVinnare(VAL);
}

,否则如果(_DatornsVal ==斯登与&&安培; _SpelarVal ==SAX|| _DatornsVal ==SAX&放大器;&安培; _SpelarVal ==磷酸酶 || _DatornsVal ==磷酸酶
和;&安培; _SpelarVal ==斯登)

{
Interaction.MsgBox(杜förlorade\\\
Ditt VAL VAR! + SpelareVal +\\\
+Datorns VAL变种:+ DatornsVal);
INT spelare = 0;
INT dator = 1;
如果(POÄNG波昂(dator,spelare))
{
的回报;
}
串瓦尔= Interaction.InputBox(Välj斯登,萨克斯埃勒PASE:);
启动市场();
UtseVinnare(VAL);
}

如果其他人(_DatornsVal ==斯登与&&安培; _SpelarVal ==磷酸酶|| _DatornsVal ==SAX&放大器;&安培; _SpelarVal ==斯登 || _DatornsVal ==磷酸酶
和;&安培; _SpelarVal ==SAX)
{

Interaction.MsgBox(杜范恩\\\
Ditt VAL VAR! + SpelareVal +\\\
+Datorns VAL变种:+ DatornsVal);
INT spelare = 1;
INT dator = 0;
如果(POÄNG波昂(dator,spelare))
{
的回报;
}
串瓦尔= Interaction.InputBox(Välj斯登,萨克斯埃勒PASE:);
启动市场();
UtseVinnare(VAL);
}


}

INT datorpoangraknare = 0;
INT spelarpoangraknare = 0;

公共BOOLPOÄNG波昂(INT _dator,诠释_spelare)
{
如果(_dator> _spelare)
{
datorpoangraknare ++;
}
,否则
{
spelarpoangraknare ++;
}
如果(datorpoangraknare == 3)
{
Interaction.MsgBox(Datorn范恩TRE格兰杰!);
返回真;
}
如果(spelarpoangraknare == 3)
{
Interaction.MsgBox(杜范恩特雷高格!);
返回真;
}
返回FALSE;
}


I am writing a Rock(Sten), Paper(Påse), Scissor(Sax) game, that plays against the computer. It works and all but I want to break the game when one off the two wins three times. But it keeps looping... Im really new to programming so excuse if the code is messy... :( And im Swedish so the code is in Swedish to... Hope you understand, if not ask me..

This is the Main:

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);
         }

This is the class with the methods that handels the game:

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!");
            }
        }

解决方案

So it looks like the problem is that in your Poang method you check to see if someone has won 3 times and if so display a message, but after you check that you don't terminate the program. It just keeps going on as if nothing happened. Also, your win count variables are locally scoped, so they lose their value every time the function ends.

There are a lot of things that could be done to make this program better, however I am just going to provide the simplest fix here:

    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;
    }

这篇关于摇滚,纸,剪刀游戏 - 如何结束当一个人赢三次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆