C# number guessing game - c#

I'm trying to create a simple number guessing game in C#. The program generates a random number when you click the "Generate random number" button. Once you enter your guess in the textbox and click "Guess", the program lets you know if you've guessed right or wrong.
The problem is that I can't pass the randomly generated number to myFunction() so it can validate the user's guess. Here's the code and it's a bit of a mess; and thank you all in advance for your help.
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 Number_guessing_game
{
public partial class Form1 : Form
{
int montH;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
myFunction(int montH);
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Random rnd = new Random();
int montH = rnd.Next(1, 10);
}
void myFunction(int montH)
{
int guess = int.Parse(textBox1.Text);
if (guess == montH)
{
MessageBox.Show("You win!");
}
if (guess != montH)
{
MessageBox.Show("You lose!");
}
}
}
}

The problem is in your button2_Click function. Inside the function after you generate a random number, you are creating a new int and assigning the random number to that, instead of the already declared class-level variable. Change your button2_Click to the following:
private void button2_Click(object sender, EventArgs e)
{
montH = new Random().Next(1,10);
}
Another few things I will mention, because montH is a class-level variable, it is accessible by every function in your class, so you don't need to pass it as a parameter to myFunction(), in fact, button1_Click could validate the result for you:
private void button1_Click(object sender, EventArgs e)
{
if (int.Parse(TextBox1.Text) == montH)
{
MessageBox.Show("You Win!!");
}
else
{
MessageBox.Show("You Lose...");
}
}

Simply define your montH as a class level variable(as you did), then use following code:
int montH;
private void button2_Click(object sender, EventArgs e)
{
Random rnd = new Random();
montH = rnd.Next(1, 10); //<--- there is no need to redefine montH by int monthH
}
private void GussButton_Click(object sender, EventArgs e)
{
myFunction(month);
}
If in your button2_click event handler want to use it there isn't need to redefine it by int montH;,
because int montH; cause define a new montH variable in method level.

Change your code to:
private void button2_Click(object sender, EventArgs e)
{
Random rnd = new Random();
montH = rnd.Next(1, 10);
}

Try this below:
namespace Number_guessing_game
{
public partial class Form1 : Form
{
public int montH;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
myFunction(montH);
}
private void Form1_Load(object sender, EventArgs e)
{
montH=0;
}
private void button2_Click(object sender, EventArgs e)
{
Random rnd = new Random();
montH = rnd.Next(1, 10);
}
void myFunction(montH)
{
int guess = int.Parse(textBox1.Text);
if (guess == montH)
{
MessageBox.Show("You win!");
}
if (guess != montH)
{
MessageBox.Show("You lose!");
}
}
}
}

Don't create a new montH in button2_Click()... otherwise the new montH only alive in this function
private void button2_Click(object sender, EventArgs e)
{
Random rnd = new Random();
montH = rnd.Next(1, 10);
}

I hope the below works for you ! What you dont need to do is actually put your montH in your Function itself since it is a seen variable to the class. When you are comparing the guess ==montH it is already taking the montH int assigned and checking from your montH variable.
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 Number_guessing_game
{
public partial class Form1 : Form
{
int montH;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
myFunction();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button2_Click(object sender, EventArgs e)
{
Random rnd = new Random();
int montH = rnd.Next(1, 10);
}
void myFunction()
{
int guess = int.Parse(textBox1.Text);
if (guess == montH)
{
MessageBox.Show("You win!");
}
if (guess != montH)
{
MessageBox.Show("You lose!");
}
}
}
}

Related

C# Button Click and Private Variables

So I'm trying to make a window forms guess the number game, simple but when I click guess no matter what the label goes up by one. I think it may be due to my variables as despite having them global userGuess still comes up as a local variable...`
Commenting out userScore removes the problem, but I still do not understand why the logic is failing
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;
using System.Drawing;
namespace Guess_The_Number_Form
{
public partial class Form1 : Form
{
private int userScore;
private int randNum;
private int userGuess;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
txtBoxGuess.Hide()
;
}
private void toolTip1_Popup(object sender, PopupEventArgs e)
{
}
private void btnRandom_Click(object sender, EventArgs e)
{
Random rand = new Random();
int randNum = rand.Next(0, 10);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
userGuess = Convert.ToInt32(txtBoxGuess.Text);
}
private void txtBoxGuess_Enter(object sender, EventArgs e)
{
}
private void btnGuess_Click(object sender, EventArgs e)
{
if (userGuess == randNum)
{
// userScore++;
lbluserScore.Text = userScore.ToString();
lbluserScore.Text = $"{userScore}";
}
else if (userGuess != randNum)
{
userScore--;
lbluserScore.Text = userScore.ToString();
lbluserScore.Text = $"{userScore}";
}
else if (userScore < 0)
{
lbluserScore.Text = Color.Red.ToString();
}
}
}
}
You need to change this:
private void btnRandom_Click(object sender, EventArgs e)
{
Random rand = new Random();
int randNum = rand.Next(0, 10);
}
to
private void btnRandom_Click(object sender, EventArgs e)
{
Random rand = new Random();
randNum = rand.Next(0, 10);
}
That way, you'll set the member variable randNum rather than a local variable randNum. You also want to check that you really are assigning the user-inputted value into userGuess properly. My guess is that it's not, which means you never change the value of either of those variables, and the program thinks the user always guessed the right value.

Using Visual Basic to Make Average Test Score Generator in C#

I'm trying to create a calculator for an assignment that takes three integer inputs and takes the average and returns that to user. I'm using Visual Basic (it is required) to make the GUI. I'm having trouble with two things, first, I cannot get the aVer to divide by 3 because it is not a integer and second, I do not know how to get an output with the average in the last textbox.
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
private string userInfo1;
private string userInfo2;
private string userInfo3;
private string aVer;
private string num1;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
int aVer = 0;
aVer = Int32.Parse(userInfo1 + userInfo2 + userInfo3);
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
int userInfo1 = 0;
userInfo1 = Int32.Parse(textBox1.Text);
}
private void button2_Click(object sender, EventArgs e)
{
Close();
}
private void textBox2_TextChanged(object sender, EventArgs e)
{
int userInfo2 = 0;
userInfo2 = Int32.Parse(textBox2.Text);
}
private void textBox3_TextChanged(object sender, EventArgs e)
{
int userInfo3 = 0;
userInfo3 = Int32.Parse(textBox3.Text);
}
private void textBox4_TextChanged(object sender, EventArgs e)
{
int num = Int32.Parse(aVer);
MessageBox.Show(num.ToString());
}
private void label3_Click(object sender, EventArgs e)
{
}
}
}
You are declaring your variables multiple times. The variables inside the function calls will hide the variables you have defined outside in the class declaration, and the class variables will never be set to a value.
It doesn't appear that you are ever using any of the variables in their string form, so all the private string declarations should be changed to private int declarations. This also makes the int userInfo1 = 0; declarations unnecessary. Having them actually breaks the ability to see the values in the button1_Click function.

Looping back to start

I am building this program for my CIT class and want to have it loopback and create a new number when the number is guessed correctly. I read over the section in my textbook that covers this but I am a little confused as to how to add this and where exactly.
Here is the code I have now:
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 Guess_My_Number
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int number;
private void Form1_Load(object sender, EventArgs e)
{
// Generate the number.
Random generator = new Random();
number = generator.Next(0, 100);
MessageBox.Show("Can you guess the number I am thinking of
between 1 and 100?");
}
private void guessButton_Click(object sender, EventArgs e)
{
// Get the guess from the textbox.
int guess = Convert.ToInt32(guessTextbox.Text);
// Check if the number is right.
if (guess > number)
{
MessageBox.Show("Too high, try again.");
}
if (guess < number)
{
MessageBox.Show("Too low, try again.");
}
if (guess == number)
{
MessageBox.Show("Congratulations, you guessed my number!");
}
}
private void exitButton_Click(object sender, EventArgs e)
{
// Clost the program.
this.Close();
}
}
}
namespace Guess_My_Number
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
int number;
Random generator = new Random();
private void Form1_Load(object sender, EventArgs e)
{
GenerateNewNumber();
}
private void GenerateNewNumber()
{
number = generator.Next(0, 100);
MessageBox.Show("Can you guess the number I am thinking of
between 1 and 100?");
}
private void guessButton_Click(object sender, EventArgs e)
{
// Get the guess from the textbox.
int guess = Convert.ToInt32(guessTextbox.Text);
// Check if the number is right.
if (guess > number)
{
MessageBox.Show("Too high, try again.");
}
if (guess < number)
{
MessageBox.Show("Too low, try again.");
}
if (guess == number)
{
MessageBox.Show("Congratulations, you guessed my number!");
GenerateNewNumber();
}
}
private void exitButton_Click(object sender, EventArgs e)
{
this.Close();
}
}
}

Is a 'method' which is not valid in the given context error

This is an example form the "Head First CSharp - page 113"
I'm getting the following error
Error 1 'Guys.Form1.joesCashLabel(object, System.EventArgs)' is a 'method', which is not valid in the given context c:\temp\Guys\Guys\Form1.cs 20 12 Guys
And the same with the other two labels
This is the 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 Guys
{
public partial class Form1 : Form
{
Guy Joe;
Guy Bob;
int Bank = 100;
public void UpdateForm()
{
joesCashLabel.Text = Joe.Name + "$" + Joe.Money;
bobsCashLabel.Text = Bob.Name + "$" + Bob.Money;
bankCashLabel.Text = "Bank has" + Bank;
}
public Form1()
{
InitializeComponent();
Guy Bob = new Guy();
Bob.Name = "Bob";
Bob.Money =100;
Guy Joe = new Guy();
Joe.Name = "Joe";
Joe.Money =50;
UpdateForm();
}
private void joesCashLabel(object sender, EventArgs e)
{
}
private void bobsCashLabel(object sender, EventArgs e)
{
}
private void bankCashLabel(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
if (Bank >= 10)
{
Bank -= Joe.ReceiveMoney(10);
UpdateForm();
}
else
{
MessageBox.Show("No money in the bank");
}
}
private void button2_Click(object sender, EventArgs e)
{
Bank = Bank + Bob.GiveMoney(5);
UpdateForm();
}
}
}
it's a method(an event at that),
private void joesCashLabel(object sender, EventArgs e)
{
}
but you're using it as a variable
joesCashLabel.Text = Joe.Name + "$" + Joe.Money;
My guess, is that there's some sort of label that that event is supposed to be associated with.
You can't define two types in same project with same name, here you have three controls and three events with same name. so remove the below methods to compile without errors.
private void joesCashLabel(object sender, EventArgs e){}
private void bobsCashLabel(object sender, EventArgs e){}
private void bankCashLabel(object sender, EventArgs e){}
If you want to add events make sure you are following naming standard like ControlName_EventName

adding a counter class to winform

I have a project that wants
A method that returns the current count.
A Constructor that sets the count to zero.
I have the first few down but need help with the return count to 0 and then the constructor. I need to do this by adding a counter class but I'm confused about the way to add it.
Can some one help me out?
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 Project10TC
{
public partial class Form1 : Form
{
int zero = 0;
int i = 1;
public Form1()
{
InitializeComponent();
}
private EventHandler myCounter;
// end of Form class
private class myCounter()
{
myCounter = new myCounter( );
}
private void exitToolStripMenuItem1_Click(object sender, EventArgs e)
{
this.Close();
}
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Teancum Clark\nCS 1400\n Project 10");
}
private void button1_Click(object sender, EventArgs e)
{
textBox1.Text = (++i).ToString();
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = (--i).ToString();
}
private void button3_Click(object sender, EventArgs e)
{
textBox1.Text = (zero).ToString();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
}
}
public class Counter
{
public int Value { get; private set; }
public void Increment()
{
Value = Value + 1;
}
public void Decrement()
{
if (Value > 0) Value = Value - 1;
}
public Counter()
{
Value = 0;
}
}

Categories

Resources