0% found this document useful (0 votes)
64 views2 pages

Guessing Game

This C# code defines a form with controls to create a number guessing game. When the form loads, it generates a random number between 1 and 100. The user can then enter a guess and click a button to check if they are correct. If incorrect, the attempts remaining is decremented by 1 and a new random number is generated. If correct, the guess button is disabled to end the game. It tracks the attempts and provides feedback on correct or incorrect guesses.

Uploaded by

Mavhungu Marvin
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views2 pages

Guessing Game

This C# code defines a form with controls to create a number guessing game. When the form loads, it generates a random number between 1 and 100. The user can then enter a guess and click a button to check if they are correct. If incorrect, the attempts remaining is decremented by 1 and a new random number is generated. If correct, the guess button is disabled to end the game. It tracks the attempts and provides feedback on correct or incorrect guesses.

Uploaded by

Mavhungu Marvin
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

public partial class Form1 : Form

{
public Form1()
{
InitializeComponent();
}
//Variables..
int randomNumber, guessedNumber, count = 3;
Random R;

//On formLoad.
private void Form1_Load(object sender, EventArgs e)
{
//Random generator. This code wil generate a number every time the form loads.

R = new Random();
randomNumber = R.Next(1, 101);
lblAttempts.Text = count.ToString();
txtNumber.Focus();
}

//When a user clicks a guess button.. the following code wil execute
private void btnGuess_Click(object sender, EventArgs e)
{
//Validate if the user has enter number between 1 and 100 on the textbox txtNumber
if (txtNumber.Text == " ")
{
//displays a messagebox with appropriate message.
MessageBox.Show("Please Guess any number between 1 and 100
inclusive..", "INFORMATION", MessageBoxButtons.OK);
txtNumber.Focus();
}
else
{
guessedNumber = int.Parse(txtNumber.Text);
txtRandom.Text = randomNumber.ToString();
//Checkes if the user has guessed the correct number.
if (guessedNumber == randomNumber)
{
txtResults.Text = "You have guessed right";
// if da user tries to gues 3 tyms the guess button is disabled
so dat da user musnt play again.
count = 0;
btnGuess.Enabled = false;
}
else
{
//If the user guessed the wrong number, a message is displayed
and new number is generated for da user to guess again.
//Count will decrement as da user attempt another guess.
count--;
MessageBox.Show("You have guessed wrong. Please Try agian",
"RETRY MESSAGE", MessageBoxButtons.OK);
lblAttempts.Text = count.ToString();
txtNumber.Clear();
txtRandom.Clear();
R = new Random();
randomNumber = R.Next(1, 101);
}

}
}

}
}

You might also like