Niomi
Niomi

Reputation: 9

rock paper scissors auto submitting

Im making a rock, paper, scissors game and i've run into trouble when i actually run the program. I dont get an errors in the code but when i run the program the message box (how i show who wins, loses, tie) already pops up before the user can choose their option and hit the submit button. Ive tried moving my methods around but that has not helped either. Im not sure what else to do.

private int choice(int min,int max)
    {

        Random ran = new Random();
        return ran.Next(min,max);
    }


    private void button1_Click(object sender, EventArgs e)
    {
        int number = choice(1, 3);

        if (number == 1)
        {
        textBox1.Text = number.ToString("Rock");
        }
        else if (number == 2)
        {
        textBox1.Text = number.ToString("Paper");
        }
        else if (number == 3)
        {
        textBox1.Text = number.ToString("Scissors");
        }
        else
        {
        MessageBox.Show("The T-Rex broke.Sorry");
        }

      }
    private void rockRadioButton_CheckedChanged(object sender, EventArgs e)
    {
        int number = choice(1, 3);

        if (number == 3)
        {
            MessageBox.Show("Rock beats Scissors. You win!");
        }
        else if (number == 2)
        {
            MessageBox.Show("Paper beats Rock.You lose!");
        }
        else if (number == 1)
        {
            MessageBox.Show("Rock vs Rock. Tie!");
        }
        else{
            MessageBox.Show("T-Rex is off hunting.Come back later.");
        }
    }

    private void paperRadioButton_CheckedChanged(object sender, EventArgs e)
    {
        int number = choice(1, 3);

       if (number == 3)
       {
           MessageBox.Show("Scissors beats Paper.You lose!");
       }
       else if (number == 2)
       {
           MessageBox.Show("Paper vs Paper.Tie!");
       }
       else if (number == 1)
       {
           MessageBox.Show("Paper beats Rock.You win!");
       }
       else
       {
           MessageBox.Show("T-Rex is off hunting. Come back later.");
       }
    }

    private void scissorsRadioButton_CheckedChanged(object sender, EventArgs e)
    {
        int number = choice(1, 3);

        if (number == 3)
        {
            MessageBox.Show("Scissors vs Scissors.Tie!");
        }
        else if (number == 2)
        {
            MessageBox.Show("Scissors beats Paper.You win!");
        }
        else if (number == 1)
        {
            MessageBox.Show("Rock beats Scissors.You lose!");
        }
        else
        {
            MessageBox.Show("T-Rex is off hunting.Come back later.");
        }
    }
}

}

Upvotes: 0

Views: 282

Answers (2)

Phil Gref
Phil Gref

Reputation: 987

I have absolutely no idea how you actually designed the game to be played, but, from what I can see, everytime you select a new radio button, you program actually makes a decision about the outcome of the game, which is not what you want.

So you probably want to hold off MessageBox.Show(); within the various CheckedChanged methods. Let's keep it very simple and just use an enum to keep the state of you selection.

    public enum Move
    {
        Rock, 
        Paper, 
        Scissors
    }

    private Move CurrentlySelectedMove = Move.Paper;

If you don't know about enums, I suggest you check them out, as they are very useful. With that helping us, you can save your move when choosing a new one through the radio buttons.

On that topic, if I am not mistaken, you are currently listening to checkedChange event on the radio buttons, which is not exactly good, since both the button that is being selected AND the one that is being unselected will trigger the event, which kinda breaks the logic of choosing a move. Therefore, you should actually check if the button whose state has just changed is checked or not. You can do it this way:

    private void rockRadioButton_CheckedChanged(object sender, EventArgs e)
    {
        if (rockRadioButton.Checked)
        {
            CurrentlySelectedMove = Move.Rock;
        }
    }

Here, we check if this radio button is checked. If this is true, we set the selected move to what the user chose.

Now, when the player hits the button, all we need to do is generate a random move from the opponent and check it against our own currently selected move, like so:

    private void button1_Click(object sender, EventArgs e)
    {
        int number = int number = choice(0, 2);

        Move opponentMove = (Move) number;

        if (CurrentlySelectedMove == opponentMove)
        {
            MessageBox.Show("Draw !");  
        }
        else if ((CurrentlySelectedMove == Move.Rock && opponentMove == Move.Paper) ||
                (CurrentlySelectedMove == Move.Paper && opponentMove == Move.Scissors) ||
                (CurrentlySelectedMove == Move.Scissors && opponentMove == Move.Rock))
        {
            MessageBox.Show("You loose !");  
        }
        else
        {
            MessageBox.Show("You win !");     
        }
    }

The fun thing about enums is that they are really just integers in disguise, which means you can go from a number to an enum easily, as you can see with the line Move opponentMove = (Move) number;

There's about a billion things to make this better (I think we'll keep the lesson on Binding for another time), but I suggest you try and understand this and try it out for yourself.

Upvotes: 1

blarrg
blarrg

Reputation: 16

With your current setup the computer roll is running everytime the radiobutton selection changes. Move the code you have in the fooRadioButton_CheckedChanged methods to the button1_Click message and update textbox1.Text when the radio button selection changes.

    private int choice(int min,int max)
    {

        Random ran = new Random();
        return ran.Next(min,max);
    }


    private void button1_Click(object sender, EventArgs e)
    {

        int number = choice(1, 3);

        if (number == 1)
        {
        //Computer selects rock
            if (rockRadioButton.IsChecked==true)
            {
                MessageBox.Show("Rock vs Rock. Tie!");
            }
            else if(paperRadioButton.IsChecked==true)
            {
                MessageBox.Show("Paper vs Rock. You win!");
            }
            else if(scissorsRadioButton.IsChecked==true)
            {
                MessageBox.Show("Scissors vs Rock. You lose!");
            }
        }
        else if (number == 2)
        {
            //Computer selects Paper
            if (rockRadioButton.IsChecked == true)
            {
                MessageBox.Show("Rock vs Paper. You lose!");
            }
            else if (paperRadioButton.IsChecked == true)
            {
                MessageBox.Show("Paper vs Paper. Tie!");
            }
            else if (scissorsRadioButton.IsChecked == true)
            {
                MessageBox.Show("Scissors vs Paper. You win!");
            }
        }
        else if (number == 3)
        {
           //Computer selects Scissors
           if (rockRadioButton.IsChecked == true)
           {
               MessageBox.Show("Rock vs Scissors. You win!");
           }
           else if (paperRadioButton.IsChecked == true)
           {
               MessageBox.Show("Paper vs Scissors. You lose!");
           }
           else if (scissorsRadioButton.IsChecked == true)
           {
               MessageBox.Show("Scissors vs Scissors. Tie!");
           }
        }
        else
        {
        MessageBox.Show("The T-Rex broke.Sorry");
        }

      }
    private void rockRadioButton_CheckedChanged(object sender, EventArgs e)
    {
        textBox1.Text = "Rock";
    }

    private void paperRadioButton_CheckedChanged(object sender, EventArgs e)
    {
        textBox1.Text = "Paper";
    }

    private void scissorsRadioButton_CheckedChanged(object sender, EventArgs e)
    {
        textBox1.Text = "Scissors";
    }

Upvotes: 0

Related Questions