Rock,Paper,Scissor游戏 - 如何赢得三次游戏结束游戏?

时间:2014-03-13 21:52:38

标签: c#

我正在写一个与电脑对战的Rock,Paper,Scissor游戏。它有效但除了我赢得三次之外我想要打破游戏。但它一直在循环...... 如果代码很乱,我真的很喜欢编程这样的借口... :(

对于这篇双重帖子感到非常抱歉,但是在我把它改成英语后,我的第一篇文章被推回到了很远的位置以获得一些帮助...

这是主要的:

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

1 个答案:

答案 0 :(得分:1)

Points 方法内部:

int computerpoints = 0;
int playerpoints = 0;

如果我正确读取这个,你继续调用这个Points函数并重置那些本地声明的int。

每次调用函数时,这些变量都会初始化为0.

你应该尝试使用它:

    static int computerpoints = 0;
    static int playerpoints = 0;

这样你的int将不再被0初始化。确保将这些变量声明为全局变量,因此它们不会超出范围(不要在方法中声明它们)。

希望这有帮助。