C#增量增加超过预期

时间:2016-09-08 02:48:58

标签: c#

我正在编写一个摇滚剪刀游戏,并且计数器增加了一些奇怪的原因。任何人都可以向我解释为什么当我赢了它时增加1,当我失去增量2时,当我打平时,它增加3?我只想把它添加到1.我很欣赏所有答案。

class RPSLS
{        
    public enum GameMode
    {
        Unknown,
        Single_Player,
        Two_Player,            
    }
    public GameMode GameType {get; set;}
    public enum Hand { Rock = 1, Paper, Scissors, Lizard, Spock, Report, Exit };
    public enum Outcome { Win, Lose, Tie };

    public Hand ComputerHand { get; set; }
    public Hand PlayerHand { get; set; }
    public char UserSelection { get; set; }
   int win = 0;
    int lose = 0;
    int tie = 0;
    public Hand getUserHand()
    {
        while (!validateSelection())
        {
            Console.Clear();
            Console.WriteLine("Invalid Input");
            UserSelection = Convert.ToChar(Console.ReadLine());
        }

        switch (Char.ToUpper(UserSelection))
        {
            case 'R':
                PlayerHand = Hand.Rock;
                break;
            case 'P':
                PlayerHand = Hand.Paper;
                break;
            case 'S':
                PlayerHand = Hand.Scissors;
                break;
            case 'L':
                PlayerHand = Hand.Lizard;
                break;
            case 'K':
                PlayerHand = Hand.Spock;
                break;
            case 'Q':
                PlayerHand = Hand.Exit;
                break;
            case 'T':
                PlayerHand = Hand.Report;
                break;
            default:
                throw new Exception("Unexpected Error");
        }
        return PlayerHand;
    }

    public Outcome DetermineWinner()
    {
        if (PlayerHand == Hand.Scissors && ComputerHand == Hand.Paper)
        {
            win++;
            return Outcome.Win;
        }
        else if (PlayerHand == Hand.Scissors && ComputerHand == Hand.Lizard)
        {
            win++;
            return Outcome.Win;
        }
        else if (PlayerHand == Hand.Rock && ComputerHand == Hand.Scissors)
        {
            win++;
            return Outcome.Win;
        }
        else if (PlayerHand == Hand.Rock && ComputerHand == Hand.Scissors)
        {
            win++;
            return Outcome.Win;
        }
        else if (PlayerHand == Hand.Paper && ComputerHand == Hand.Rock)
        {
            win++;
            return Outcome.Win;
        }
        else if (PlayerHand == Hand.Paper && ComputerHand == Hand.Spock)
        {
            win++;
            return Outcome.Win;
        }
        else if (PlayerHand == Hand.Lizard && ComputerHand == Hand.Paper)
        {
            win++;
            return Outcome.Win;
        }
        else if (PlayerHand == Hand.Lizard && ComputerHand == Hand.Spock)
        {
            win++;
            return Outcome.Win;
        }
        else if (PlayerHand == Hand.Spock && ComputerHand == Hand.Scissors)
        {
            win++;
            return Outcome.Win;
        }
        else if (PlayerHand == Hand.Spock && ComputerHand == Hand.Rock)
        {
            win++;
            return Outcome.Win;
        }

        else if (PlayerHand == Hand.Scissors && ComputerHand == Hand.Rock)
        {
            lose++;
            return Outcome.Lose;
        }
        else if (PlayerHand == Hand.Scissors && ComputerHand == Hand.Spock)
        {
            lose++;
            return Outcome.Lose;
        }
        else if (PlayerHand == Hand.Rock && ComputerHand == Hand.Paper)
        {
            lose++;
            return Outcome.Lose;
        }
        else if (PlayerHand == Hand.Rock && ComputerHand == Hand.Spock)
        {
            lose++;
            return Outcome.Lose;
        }
        else if (PlayerHand == Hand.Paper && ComputerHand == Hand.Scissors)
        {
            lose++;
            return Outcome.Lose;
        }
        else if (PlayerHand == Hand.Paper && ComputerHand == Hand.Lizard)
        {
            lose++;
            return Outcome.Lose;
        }
        else if (PlayerHand == Hand.Lizard && ComputerHand == Hand.Rock)
        {
            lose++;
            return Outcome.Lose;
        }
        else if (PlayerHand == Hand.Lizard && ComputerHand == Hand.Scissors)
        {
            lose++;
            return Outcome.Lose;
        }
        else if (PlayerHand == Hand.Spock && ComputerHand == Hand.Lizard)
        {
            lose++;
            return Outcome.Lose;
        }
        else if (PlayerHand == Hand.Spock && ComputerHand == Hand.Paper)
        {
            lose++;
            return Outcome.Lose;
        }
        else if (PlayerHand == Hand.Exit)
        {
            Environment.Exit(0);
        }
        else if (PlayerHand == Hand.Report)
        {
            Report();
        }
        tie++;
        return Outcome.Tie;
    }

    private void Report()
    {
        Console.WriteLine("Your Score is (W:L:T:) : {0}:{1}:{2}", win, lose, tie);
    }

    private void PlayGame()
    {
        bool gameOver = false;
        var rand = new Random();
        char response;

        while (!gameOver)
        {
            Screen();
            UserSelection = Convert.ToChar(Console.ReadLine());
            getUserHand();
            ComputerHand = (Hand)rand.Next(1, 6);
            Console.Clear();
            Console.WriteLine("Computer's Hand: {0}", ComputerHand);
            Console.WriteLine("Player's Hand: {0}", PlayerHand);

            if (DetermineWinner() == Outcome.Win)
                Console.WriteLine("Win");
            else if (DetermineWinner() == Outcome.Lose)
                Console.WriteLine("Lose");
            else if (DetermineWinner() == Outcome.Tie)
                Console.WriteLine("Tie");

            Console.WriteLine("Would you Like to play another game? (y/n)");
            response = Convert.ToChar(Console.ReadLine());

            while(validateResponse(response) == false)
            {
                Console.WriteLine("Invalid Input, Please re-enter your selection: ");
                response = Convert.ToChar(Console.ReadLine());
            }

            if (response == 'N' || response == 'n')
                gameOver = true;

            Console.Clear();
        }
    }

    public bool validateResponse (char response)
    {
        if (Char.ToUpper(response) != 'Y' && Char.ToUpper(response) != 'N')
            return false;

        return true;
    }
    private bool validateSelection()
    {
        char value = Char.ToUpper(UserSelection);
        if (value != 'R' && value != 'P' && value != 'S' && value != 'L' && value != 'K' && value != 'T' && value != 'Q')
            return false;
        return true;
    }
    private void Screen()
    {
        Console.WriteLine("(R)ock | (P)aper | (S)cissors | (L)izard | Spoc(k) | (Q)uit | Repor(t)");

    }
   public GameMode getGameType()
    {
        bool flag = true;
        string buffer;
        GameMode GameType = GameMode.Unknown;

        GameType = GameMode.Unknown;
        Console.WriteLine("Available options are: (S)ingle Player | (T)wo Player");
        Console.Write("What would you like to play?: ");

        while (flag)
        {
            buffer = Console.ReadLine();
            try
            {
                switch (buffer.ToUpper())
                {
                    case "S":
                    case "SINGLE PLAYER":
                        GameType = GameMode.Single_Player;
                        break;
                    case "T":
                    case "TWO PLAYER":
                        GameType = GameMode.Two_Player;
                        break;

                    default:
                        Console.WriteLine("Action not understood.");
                        break;
                }
                if (GameType != GameMode.Unknown)
                {
                    PerformWork(GameType);
                }
            }
            catch (ArgumentException)
            {
                Console.WriteLine("'{0}' is not understood.", buffer);
            }

        }
        return GameType;
    }

    private void PerformWork(GameMode cmd)
    {
        if (cmd == GameMode.Single_Player)
        {
            Console.Clear();
            SinglePlayer(cmd);


        }
        else if (cmd == GameMode.Two_Player)
        {
            Console.WriteLine("Two Player Mode");
        }
        else
        {
            Console.WriteLine("Action not understood.");
        }
    }
    private void SinglePlayer(GameMode cmd)
    {
        Console.WriteLine("Hello. You are now playing Single Player Mode");
        Console.Write("Please Enter Your Name: ");
        String nameInput = Console.ReadLine();
        Console.Write("Please Enter A Name For Computer: ");
        string computerNameInput = Console.ReadLine();
        Console.Clear();
      Console.WriteLine(nameInput + " vs " + computerNameInput);

        PlayGame();
    }
}
}

2 个答案:

答案 0 :(得分:0)

此代码块的副作用会增加您的类级别变量。

 if (DetermineWinner() == Outcome.Win)
    Console.WriteLine("Win");
 else if (DetermineWinner() == Outcome.Lose)
    Console.WriteLine("Lose");
 else if (DetermineWinner() == Outcome.Tie)
    Console.WriteLine("Tie");

每次调用DetermineWinner()时,都会触及字段。

答案 1 :(得分:0)

当你打电话给DetermineWinner()时你会递增,你做3次平局,两次输球,一次赢。 例如,假设结果为平局,您可以调用DetermineWinner()来检查它是否符合函数中的平局条件并增加该值。评估失败,因此它运行else并再次调用DetermineWinner(),再次递增。

修复将调用DetermineWinner()一次并将结果分配给变量,然后您可以在不重新生成的情况下对其进行评估。