First, you'll need Microsoft Visual C# 2005 or 2008 Express Edition, or Microsoft Visual Studio 2005 or 2008.
Now, create a new project. To do so, go to file, new project, Windows Application Form. Name is Guessing Game.
Change Form1's text property to 'Guessing Game'.
Add a label to your form, and make its text property 'Guess a Number 1-10!'
You can make it from whatever number you want to whatever, it doesn't matter. I'm going to use 10, but it can easily be changed.
Now, add a TextBox to go under it. Nothing to change in its properties.
Then add a button that says "Guess!", or whatever you want.
Then add 2 labels; one that says "Tries Left:" then put the other right next to it and make it say "3".
Alright, now it's time to add some code to this project.
First, we need to make our global variables. So, double-click the form, and it should lead you to Form1's load event. Above where it says
Form1_Load, type in:
Random number = new Random(); // This is declaring number; our randomizer int randomNumber; // Our randomized number int guessedNumber; // Our guessed number
Now, in the Form1 Load event, type this in:
randomNumber = number.Next(1,10);
That code says that randomNumber is a randomized number with the minimum being 1 and the maximum being 10.
Then, go to the TextBox's TextChanged event by double-clicking it on the GUI form.
When you're there, type this in:
if (textBox1.Text != "")
{
try
{
guessedNumber = int.Parse(textBox1.Text);
}
catch
{
MessageBox.Show("Please enter an integer from 1-10", "Error!");
textBox1.Clear();
}
}
if (textBox1.Text != "")
This says 'if textBox1's text is not equal to being empty, do this:'
try
{
guessedNumber = int.Parse(textBox1.Text);
}
catch
{
MessageBox.Show("Please enter an integer from 1-10!", "Error!");
textBox1.Clear();
}
try/catch is an error catching control structure. In the try block you type in what you want to error-check, in this case, textBox1 parsed to an int. Parsing is transferring it from a string into an int in this case. catch is what is displayed if the user enters an invalid character. textBox1.Clear();, obviously, clears the textbox of any text within it. And if you don't know what MessageBox is, why are you reading this? J/k, MessageBox.Show(); is a method that displays a MessageBox. The text before the comma is what's in it, and after the comma is the title.
Now, it's time to move on to the big code; the button1's Click event. Double-click the button to access its Click event. I'm going to give you 1 majorly long code, but don't freak out. I'll explain it afterward.
guessedNumber = int.Parse(textBox1.Text);
bool win = false;
bool wrong = false;
if (textBox1.Text == "")
{
MessageBox.Show("Please enter an integer from 1-10!", "Error!");
textBox1.Clear();
}
else
{
if (guessedNumber >= 1 && guessedNumber <= 10)
{
if (guessedNumber == randomNumber)
{
win = true;
if (win == true)
{
if (MessageBox.Show("You have won! Would you like to play again ?", "You win!", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
randomNumber = number.Next(1, 10);
label3.Text = "3";
textBox1.Clear();
}
else
{
this.Close();
}
}
else
{
// Do nothing
}
}
else
{
wrong = true;
}
}
else
{
MessageBox.Show("Please enter an integer from 1-10!", "Error!");
textBox1.Clear();
}
}
if (wrong == true)
{
label3.Text = (int.Parse(label3.Text) - 1).ToString();
if (label3.Text == "0")
{
if (MessageBox.Show("You have lost! The randomized number was " + randomNumber + ". Would you like to play again ?", "You lost!", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
randomNumber = number.Next(1, 10);
label3.Text = "3";
textBox1.Clear();
}
else
{
this.Close();
}
}
else
{
if (guessedNumber > randomNumber)
{
MessageBox.Show("Lower!", "Wrong!");
textBox1.Clear();
}
else
{
MessageBox.Show("Higher!", "Wrong!");
textBox1.Clear();
}
}
}
else
{
// Do nothing
}
We are parsing the textbox at the beginning.
Then, we are declaring 2 bool (boolean) variables, which answer the true/false question; win and wrong, both by default set to false.
So first, we check if the textbox is empty when they hit the enter button. If it is, an error message appears; if not, the rest of the code is executed.
If guessedNumber is greater than or equal to 1 or less than or equal to 10, it allows the rest of the code to be executed.
If our guessed number(guessedNumber) is equal to our randomized number (randomNumber) then our bool variable win is set to true.
If win is true, then it makes a MessageBox appear asking if you want to play again, using an if to execute it. If DialogResult is yes, then it re-randomizes the number, sets the label back to 3 (we haven't gotten to the part where the label changes yet) and clears the TextBox. If DialogResult is no, then it closes the program.
And if win is not equal to true, then do nothing. The comment is not required, it just helps me remember.
If guessedNumber is not equal to randomNumber, then wrong is equal to true.
If guessedNumber is not greater than or equal to 1 or less than or equal to 10, it gives you the error that we've used throughout the program.
Then, finally, we're out of those ifs.
Now time for more ifs, yay! <3
If wrong is true, then label3.Text is parsed into an int then subtracted by one then converted back to a string so that the form will accept it as text.
If label3's text is 0, then it tells you what the randomized number was and gives you an option to play again. If yes, it resets the randomized number, then makes the label equal 3 again; if no, then the application closes. So if the label's text is not equal to 0, then it detects whether guessedNumber was greater than or less than randomNumber.
If greater than, then a MessageBox appears and tells you to go lower, then clears the TextBox.
If less than, a MessageBox appears and tells you to go higher, then clears the TextBox.
And finally, if wrong is not true, then it does nothing.
Thanks for reading my tutorial
Tell me how I did; it was my first tutorial.





MultiQuote







|