My assignment is as follows: Create a higher/lower guessing game using a graphical user interface. allow users to keep guessing until they guess the number. Choose two colors for the game: one should be used to indicate that the value the users guessed is higher than the target; the other is used to indicate the the value the users guesed is lower than the target. With each new guess, change the form color based on whether the guess is higher than the target or lower. Keep a count of the number of guesses it took. when they hit the target display a MessageBox indicating the number of guesses it took. Several approaches can be used to seed the target: one is to generate a random number by the constructing an object of the Randomclass. For example the following stores a random whole number between 0 and 100 in target

Random r = new Random();
int target = r.Next(0,100);


I seem to be having an issue with the "if" statements.

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

namespace GuessingGame
{
    public partial class Form1 : Form
    {
        //create the form
        public Form1()
        {
            InitializeComponent();
        }

        //create the button for the random generator
        private void btnBegin_Click(object sender, EventArgs e)
        {
            //initialize the random number generator
            Random num = new Random();

            //set random number parameters
            int range = num.Next(0, 100);

            //initialize variables
            int guess;
            guess = int.Parse(txtBox1.Text);

            //parameters to show higher or lower
            if (guess > range)
            {
                Form1.DefaultBackColor(Color.Red);
            }
            else
                if (guess < range)
                {
                    Form.DefaultBackColor(Color.Green);
                }
                else
                {
                    MessageBox.Show("YOU WIN!");
                }
        }

        private static void DefaultBackColor(Color color)
        {
            throw new NotImplementedException();
        }
    }
}

The errors I am receiving are as follows:

Error	
1	Non-invocable member 'System.Windows.Forms.Control.DefaultBackColor' cannot be used like a method.	C:\Users\Dave Morand\Desktop\Rio Salado\CIS162AD - C# Programming\Lesson 9 Project 2\Guessing Game\Guessing Game\Form1.cs	41	26	Guessing Game
Warning	

2	'GuessingGame.Form1.DefaultBackColor(System.Drawing.Color)' hides inherited member 'System.Windows.Forms.Control.DefaultBackColor'. Use the new keyword if hiding was intended.	C:\Users\Dave Morand\Desktop\Rio Salado\CIS162AD - C# Programming\Lesson 9 Project 2\Guessing Game\Guessing Game\Form1.cs	49	29	Guessing Game
Error	

3	'GuessingGame.Form1' does not contain a definition for 'Form1_Load' and no extension method 'Form1_Load' accepting a first argument of type 'GuessingGame.Form1' could be found (are you missing a using directive or an assembly reference?)	C:\Users\Dave Morand\Desktop\Rio Salado\CIS162AD - C# Programming\Lesson 9 Project 2\Guessing Game\Guessing Game\Form1.Designer.cs	107	55	Guessing Game

There are a few errors here. First instead of using:

Form1.DefaultBackColor(Color.Red);

Try

this.BackColor = Color.Red;

for each time you were using that method.

Also every time you click the button a new random number is generated. You should put :

Random num = new Random();

//set random number parameters
int range = num.Next(0, 100);

Into this method

public Form1()
{
    InitializeComponent();
}

full code

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

namespace DaniwebHelp1
{
    public partial class Form1 : Form
    {
        int range;

        public Form1()
        {
            InitializeComponent();

            //initialize the random number generator
            Random num = new Random();

            //set random number parameters
            range = num.Next(0, 100);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //initialize variables
            int guess;
            guess = int.Parse(textBox1.Text);

            //parameters to show higher or lower
            if (guess > range)
            {
                this.BackColor = Color.Red;
            }
            else if (guess < range)
            {
                this.BackColor = Color.Green;
            }
            else
            {
                MessageBox.Show("YOU WIN!");
            }
        }
    }
}

Also this code still breaks when you enter nothing in the textbox. You should find a way to handle that.

Edited 4 Years Ago by ShadyTyrant: n/a

I am still getting an error in the code, it appears that

public partial class Form1 : Form
    {
        int range;

        //create the form
        public Form1()
        {
            InitializeComponent();

            //initialize the random number generator
            Random num = new Random();

            //set random number parameters
            range = num.Next(0, 100);
        }

is where the issue is at, the variable "range" is retaining its value of "0" and it is not getting re-assigned by the random number.

Apparently I didn't give it enough tries. The first number it randomly picked twice in a row was 0. Then I tested again and it picked 8, then 58. So it appears to be working properly, thanks so much for your help!

This question has already been answered. Start a new discussion instead.