0

I'm needing to make scissors win over paper, and rock win over scissors, and etc. I currently have the statement userChoice != computerChoice but this would not work as rock would win over paper. I don't know how to make this work, I've tried thinking and can't think of anything currently, I'm only a beginning programmer.

    const int ROCK = 1;
    const int PAPER = 2;
    const int SCISSORS = 3;

    private void rockButton_Click(object sender, EventArgs e)
    {
        int userChoice = ROCK;
        userPictureBox.Image = Properties.Resources.Rock;

        Random randomNumberGenerator = new Random();
        int computerChoice = randomNumberGenerator.Next(1, 4);
        switch (computerChoice)
        {
            case ROCK:
                computerPictureBox.Image = Properties.Resources.Rock;
                break;

            case PAPER:
                computerPictureBox.Image = Properties.Resources.Paper;
                break;

            case SCISSORS:
                computerPictureBox.Image = Properties.Resources.Scissors;
                break;
        }

        if (userChoice == computerChoice)
            MessageBox.Show("It's a tie.");

        else if (userChoice != computerChoice)
        {
            MessageBox.Show("You win!");
        }

    }

    private void paperButton_Click(object sender, EventArgs e)
    {
        int userChoice = PAPER;
        userPictureBox.Image = Properties.Resources.Paper;

        Random randomNumberGenerator = new Random();
        int computerChoice = randomNumberGenerator.Next(1, 4);
        switch (computerChoice)
        {
            case ROCK:
                computerPictureBox.Image = Properties.Resources.Rock;
                break;

            case PAPER:
                computerPictureBox.Image = Properties.Resources.Paper;
                break;

            case SCISSORS:
                computerPictureBox.Image = Properties.Resources.Scissors;
                break;

        }
        if (userChoice == computerChoice)
            MessageBox.Show("It's a tie.");

        else if (userChoice != computerChoice)
        {
            MessageBox.Show("You win!");
        }
    }

    private void scissorsButton_Click(object sender, EventArgs e)
    {
        int userChoice = SCISSORS;
        userPictureBox.Image = Properties.Resources.Scissors;

        Random randomNumberGenerator = new Random();
        int computerChoice = randomNumberGenerator.Next(1, 4);
        switch (computerChoice)
        {
            case ROCK:
                computerPictureBox.Image = Properties.Resources.Rock;
                break;

            case PAPER:
                computerPictureBox.Image = Properties.Resources.Paper;
                break;

            case SCISSORS:
                computerPictureBox.Image = Properties.Resources.Scissors;
                break;

        }
        if (userChoice == computerChoice)
            MessageBox.Show("It's a tie.");

        else if (userChoice != computerChoice)
        {
            MessageBox.Show("You win!");
  • A few things. You create a new `Random` object on every click on a button. That's not a problem for now, but you probably want to learn early, that you should only ever need to have *one* `Random` object (per thread) in the whole application, so you won't run into [problems](http://stackoverflow.com/questions/4855756/random-number-generation-same-number-returned). -- As you see, after the first two lines in a button click handler, you do exactly the same. If you do stuff exactly the same in different places, that's an indicator, that you should put it into a separate method and call it. – Corak Oct 29 '15 at 07:30
  • You use constants instead of [magic numbers](http://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad). That's awesome! But you can consider going a step further with [enums](https://msdn.microsoft.com/library/cc138362.aspx), just to better express that `ROCK`, `PAPER` and `SCISSORS` belong together. – Corak Oct 29 '15 at 07:30
  • Thank you very much for your help, I'll defiantly start learning more things that better my code, I'm only a beginner right now, and it is a little difficult for me, but I can do it! – Kyle Miller Oct 29 '15 at 07:38
  • Another thing, how would I make the wins count up on each side? The one that is selected is the one I'm talking about same with the other side right next to the play again, would that be necessary? And what about the play again button? I don't see the point for that button, but my teacher gave it to me like this http://imgur.com/SbxVB5T – Kyle Miller Oct 29 '15 at 07:42
  • Seems like "play again" would just reset the win counters on both sides to zero but feel free to ask your teacher about it. The counters themselve can simply be two variables `int userWinCount = 0; int computerWinCount = 0;` (you don't actually need the `= 0`, because that is the default value they'll have at the first start). And then something like: `if ([userWon]) { userWinCount = userWinCount + 1; } if ([computerWon]) { computerWinCount = computerWinCount + 1; }` – Corak Oct 29 '15 at 08:02
  • Or, if you're comfortable with it, write it like `userWinCount++;`. Just like with `userWinCount = userWinCount + 1;`, after `userWinCount++;` the content of `userWinCount` will be incremented by one. – Corak Oct 29 '15 at 08:02
  • Where would I put the 'if ([userWon])' exactly at? I tried putting it in and it wasn't working correctly – Kyle Miller Oct 29 '15 at 19:25

2 Answers2

0

Create matrix dict where rows - human choice, columns - computer choice, and dict[i,j] - result, who wins: human or computer.

var dict = new Dictionary<int, Dictionary<int, string>>();
dict[ROCK] = new Dictionary<int, string> { { ROCK , "draw"}, {PAPER, "computer"}, {SCISSORS , "human"} };
dict[PAPER] = new Dictionary<int, string> { { ROCK , "human" }, { PAPER, "draw" }, { SCISSORS, "computer" } };
dict[SCISSORS] = new Dictionary<int, string> { { ROCK , "computer" }, { PAPER , "human" }, { SCISSORS , "draw" } };
var winner = dict[userChoice, computerChoice];
Backs
  • 24,430
  • 5
  • 58
  • 85
  • Alright, where would I exactly put this in, in my code? And what would I take out? Like I said I'm new to this stuff so I do apologize, I have been reading my book but haven't seen this yet – Kyle Miller Oct 29 '15 at 05:46
  • @KyleMiller you can make `dict` as field and set it in constructor – Backs Oct 29 '15 at 06:39
0
if(userChoice == 1 && computerChoice == 2)
{
MessageBox.Show("You win!")
}
else if(userChoice == 2 && computerChoice == 3)
{
MessageBox.Show("You lose!")
}
else if(userChoice == 3 && computerChoice == 1)
{
MessageBox.Show("You lose!")
}

Since you have values for your rock, paper and scissors.

My seatmate told me this, seems so basic but has the logic. NOTE: just add the those another cases.

pruuylan
  • 86
  • 1
  • 1
  • 10
  • Thank you so much, I was trying to figure out how to use those values for my cases, you're a life saver! Haha, haven't attended my class and had to bust this out today. – Kyle Miller Oct 29 '15 at 06:08
  • There are such nice constants for `ROCK`, `PAPER` and `SCISSORS` it would be a shame not to use them and instead use ["magic" values](http://stackoverflow.com/questions/47882/what-is-a-magic-number-and-why-is-it-bad) `1`, `2` and `3`. – Corak Oct 29 '15 at 07:06