Tim Kister
Tim Kister

Reputation: 39

Trouble with random number guessing game in c#

So I am working on a project for school that in a guessing game for a randomly generated number between 1-100. The only trouble I am having at the moment is that every time the user enters a new number the generated number also changes. I tried putting the code to generate the number in the form loader but then I can't access it later in the program. Here's what I have so far.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{

    public partial class Form1 : Form
    {
        public Form1()
        {

            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void guessButton_Click(object sender, EventArgs e)
        {

            int userGuess;
            userGuess = int.Parse(guessText.Text);
            Random rand = new Random();
            int number = rand.Next(1, 100);

                label2.Text = "" + number;


                    if (userGuess > number)
                    {
                        resultLabel.Text = "Your guess is too high";
                    }

                    else if (userGuess < number)
                    {
                        resultLabel.Text = "Your guess is too low.";
                    }

            else if (userGuess == number)
            {
                resultLabel.Text = "That is correct!";
            }

                guessText.Clear();



        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

Upvotes: 2

Views: 1921

Answers (2)

AStopher
AStopher

Reputation: 4530

As it currently stands, the Random number is overwritten every time the guessButton_Click function is run.

You are declaring Random inside the guessButton_Click function, that is called every time the guess button is clicked (that's also a memory leak!). To fix, declare it as a global variable, in the namespace:

Edit: The below code compiles properly, and works perfectly.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{

    public partial class Form1 : Form
    {
        int number = new Random().Next(1, 100);
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void guessButton_Click(object sender, EventArgs e)
        {

            int userGuess;
            userGuess = int.Parse(guessText.Text);
            label2.Text = "" + number;


            if (userGuess > number)
            {
                resultLabel.Text = "Your guess is too high";
            }

            else if (userGuess < number)
            {
                resultLabel.Text = "Your guess is too low.";
            }

            else if (userGuess == number)
            {
                resultLabel.Text = "That is correct!";
            }

            guessText.Clear();



        }

        private void exitButton_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

Upvotes: 2

Sybren
Sybren

Reputation: 1079

You are getting a new random number every button click try the following:

    public partial class Form1 : Form
{

    Random rand;
    int number;

    public Form1()
    {
        InitializeComponent();

    }

    private void Form1_Load(object sender, EventArgs e)
    {
        rand = new Random();
        number = rand.Next(1, 100);
    }

    private void guessButton_Click(object sender, EventArgs e)
    {
        int userGuess;
        userGuess = int.Parse(guessText.Text);

        label2.Text = "" + number;


        if (userGuess > number)
        {
            resultLabel.Text = "Your guess is too high";
        }

        else if (userGuess < number)
        {
            resultLabel.Text = "Your guess is too low.";
        }

        else if (userGuess == number)
        {
            resultLabel.Text = "That is correct!";
        }

        guessText.Clear();
    }


}

Upvotes: 0

Related Questions