-1

I'm coding a rock paper scissors game and some reason the counter is incrementing weird. Could anyone explain to me why when I win it increments by 1, when I lose it increments by 2, and when I tie, it increments by 3? I just want it to add by 1. I appreciate all answers.

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();
    }
}
}
ArK
  • 20,698
  • 67
  • 109
  • 136
  • 3
    Holy code dump, Batman! Please, reduce your question so that it includes just a good [mcve] that reliably reproduces your problem. – Peter Duniho Sep 08 '16 at 02:52
  • Thanks for the reply! I'm not sure where the problem is exactly that's why I posted the whole code. =/ – Annie Lowry Sep 08 '16 at 02:54
  • 1
    The [mcve] page provides some good advice about how and why to provide a good MCVE. There are links at the bottom of that page that expand on the topic. You can also look at [ask], again both the page and the links at the bottom. Not being sure where the problem is, does not excuse the person asking the question from doing their due diligence and narrowing the problem to a proper [mcve] (which includes **complete**, which the above also is not). – Peter Duniho Sep 08 '16 at 02:56
  • On a side note, your code can be massively simplified. For inspiration check out this javascript version: http://jsfiddle.net/h3TcP/4/. Inspired by my answer to this question: https://stackoverflow.com/questions/22623331/rock-paper-scissors-lizard-spock-in-javascript – Jon P Sep 08 '16 at 06:13

2 Answers2

0

The side effects from this block of code will increment your class level variables.

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

Each time DetermineWinner() is called the fields are touched.

THBBFT
  • 1,161
  • 1
  • 13
  • 29
0

You're incrementing when you call DetermineWinner() which you do 3 times for a tie, twice for a loss, and once for a win. eg assuming the result is a tie, you're calling DetermineWinner() to check if it's a win which meets the tie condition in the function and increments the value. The evaluation fails, so it runs the else and calls DetermineWinner() again, whcih increments again.

Fix would be to call DetermineWinner() once and assign the result to a variable then you can evaluate that without reincrementing.

Kirlac
  • 684
  • 6
  • 18