-2

My program is set up to have the user guess an integer between 1 and 10. If the user guess too low, or high they are notified and can try again.

The problem I am having is that when the user guesses incorrectly a new random number is generated. So essentially the user is never trying to guess the same number after getting it wrong.

I need to make it so that when the user guesses wrong they are still trying to guess the same value.

Here is my code:

namespace IntegerGame
{
    public partial class guessGame : Form
    {
        int num1;
        int num2;

        public guessGame()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
        }

        private void guessButton_Click(object sender, EventArgs e)
        {
            Random rnd1 = new Random();
            num1 = rnd1.Next(1, 10);

            if (int.TryParse(textBox1.Text, out num2))
            {
                if (num2 < 0 || num2 > 10)
                {
                    textBox1.Clear();
                    MessageBox.Show("Please enter a number between 1 and 10");
                }
                else
                {
                    if (num2 > num1)
                    {
                        textBox1.Clear();
                        MessageBox.Show("You guessed to high, please try again");
                    }

                    else if (num2 < num1)
                    {
                        textBox1.Clear();
                        MessageBox.Show("You guessed to low, please try again");
                    }

                    else if (num2 == num1)
                    {
                        textBox1.Clear();
                        MessageBox.Show("You guessed " + num2 + ", which was the right number!!");
                    }
                }

            }
            else
            {
                textBox1.Clear();
                MessageBox.Show("This is not a valid integer, please enter a valid integer");
            }
       }
    }
}
freedomn-m
  • 27,664
  • 8
  • 35
  • 57
Brian Roper
  • 528
  • 7
  • 13
  • 5
    hint: move your generation to a different method. – Daniel A. White Sep 17 '15 at 15:24
  • And another hint to go with the first: move `Random rnd1` to the same place as `var num1` – freedomn-m Sep 17 '15 at 15:26
  • How do you want the program to work? Like when should a new number be generated? Or is it only once per run? – Michael Sep 17 '15 at 15:26
  • 1
    @Console the "duplicate" is not a duplicate - this question is how to keep the number, generating it is not an issue. (title could be fixed!) – freedomn-m Sep 17 '15 at 15:27
  • @Michael, I am trying to get a new number to generate once per run and then another after the user enters correctly – Brian Roper Sep 17 '15 at 15:28
  • 1
    Not directly related to the question, but did you know you're generating a number between 1 and 9? (guess it depends on your definition of "between") – freedomn-m Sep 17 '15 at 15:29
  • If you're still an undergrad, I strongly recommend approaching your teachers with this one; an answer's not as important as how to reach that answer in this case. – Flynn1179 Sep 17 '15 at 15:33
  • @Flynn1179, Yeah I'm still an undergrad. I understand what you are saying Ill reach out to my professor as well. Thanks. – Brian Roper Sep 17 '15 at 15:39
  • @BrianRoper This question has sat unresolved for a long time. If any answer helped you solve your issue, please mark it as the accepted answer to help others with similar issues – David Watts Jun 26 '17 at 14:49

6 Answers6

7

Generate the random number as a member of guessGame (or in the constructor, after InitializeComponent) instead of whenever the user presses the button

public partial class guessGame : Form
{
    Random rnd1 = new Random();
    int num1 = rnd1.Next(1, 10);
    int num2;
    ...
Ólafur Aron
  • 352
  • 2
  • 12
3

Every time the button is clicked, this code is ran:

Random rnd1 = new Random();
num1 = rnd1.Next(1, 10);

That means that everytime the user guesses, this will generate a new random number.

I would suggest making the random and the random number fields (Edit: Noticed that your number is already a field) and, for the initial one, creating it in the constructor, like so:

private Random _rnd1;
private int num1; 

GuessGame()
{
    _rnd1 = new Random();
    _num1 = _rnd1.Next(1,10);
    InitializeComponent();
}

Then when the user guesses correctly, you can simply generate a new number by setting the _num1 field to the next number from the random.

David Watts
  • 2,249
  • 22
  • 33
1

Check if there need to generate number while generating random number

Random rnd1 = new Random();
int num1 = -1;

private void guessButton_Click(object sender, EventArgs e)
{
    if (num1 == -1)
    {            
        num1 = rnd1.Next(1, 10);
    }
    //...

    //assign -1 to num1 after successful guess.
    num1 = -1;
}
NASSER
  • 5,900
  • 7
  • 38
  • 57
0

You should set a random value when the form is initialized. It'll only change if the form is closed and reopened.

Random rnd1 = new Random();

public guessGame()
{
    InitializeComponent();
    num1 = rnd1.Next(1, 10);
}
chiapa
  • 4,362
  • 11
  • 66
  • 106
0

Just generate your random number in the constructor instead of on the click handler:

namespace IntegerGame
{
public partial class guessGame : Form
{
    int num1;
    int num2;

public guessGame()
{
    Random rnd1 = new Random();
    num1 = rnd1.Next(1, 10);
    InitializeComponent();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}

private void guessButton_Click(object sender, EventArgs e)
{


    if (int.TryParse(textBox1.Text, out num2))
    {


            if (num2 < 0 || num2 > 10)
            {
                textBox1.Clear();
                MessageBox.Show("Please enter a number between 1 and 10");
            }

            else
            {
                if (num2 > num1)
                {
                    textBox1.Clear();
                    MessageBox.Show("You guessed to high, please try again");
                }

                else if (num2 < num1)
                {
                    textBox1.Clear();
                    MessageBox.Show("You guessed to low, please try again");
                }

                else if (num2 == num1)
                {
                    textBox1.Clear();
                    MessageBox.Show("You guessed " + num2 + ", which was the right number!!");
                }
            }

    }

    else
    {
        textBox1.Clear();
        MessageBox.Show("This is not a valid integer, please enter a valid integer");
    }




}

} }

JoaoFSA
  • 267
  • 1
  • 7
0
namespace IntegerGame
{
public partial class guessGame : Form
{
    int num1;
    int num2;
    Random rnd1 = new Random();

    public guessGame()
    {
        InitializeComponent();
        num1 = rnd1.Next(1, 10);
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void guessButton_Click(object sender, EventArgs e)
    {

        if (int.TryParse(textBox1.Text, out num2))
        {


                if (num2 < 0 || num2 > 10)
                {
                    textBox1.Clear();
                    MessageBox.Show("Please enter a number between 1 and 10");
                }

                else
                {
                    if (num2 > num1)
                    {
                        textBox1.Clear();
                        MessageBox.Show("You guessed to high, please try again");
                    }

                    else if (num2 < num1)
                    {
                        textBox1.Clear();
                        MessageBox.Show("You guessed to low, please try again");
                    }
                    // Note that this could be an else: it is the only case left
                    else if (num2 == num1)
                    {
                        num1 = rnd1.Next(1, 10);
                        textBox1.Clear();
                        MessageBox.Show("You guessed " + num2 + ", which was the right number!!");
                    }
                }

        }

        else
        {
            textBox1.Clear();
            MessageBox.Show("This is not a valid integer, please enter a valid integer");

}

You want to generate the number only when they are equal, instead of whenever the button is pressed.

Michael
  • 198
  • 10