In this game, the computer will randomly choose a number and the player must guess the number. To guess the number, for each guess the computer will show whether the guess is too high or too low. If the user is able to guess the number then he will be the winner otherwise he loses the game.
In this game, there are the following three levels.
- 1 and 10
- 1 and 100
- 1 and 1000
- Between 1 and 10: The computer will randomly take a number between 1 and 10 and the user has five chances to guess the number.
- Between 1 and 100: The computer will randomly take a number between 1 and 100 and the user has seven chances to guess the number.
- Between 1 and 1000: The computer will randomly take a number between 1 and 1000 and the user has ten chances to guess the number.
Main Page
On this page, there are 7 labels and 3 buttons. The label only contains the description and the button is used for redirecting to another form.
1 and 10 redirect to form2, 1 and 100 redirect to form3 and 1 and 1000 redirect to form4.
Coding
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 game
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
// Button 1 Click Event: Opens Form2 and hides Form1
private void button1_Click(object sender, EventArgs e)
{
Form2 f = new Form2();
f.Show();
Application.OpenForms["Form1"].Hide();
}
// Button 2 Click Event: Opens Form3 and hides Form1
private void button2_Click(object sender, EventArgs e)
{
Form3 f = new Form3();
f.Show();
Application.OpenForms["Form1"].Hide();
}
// Button 3 Click Event: Opens Form4 and hides Form1
private void button3_Click(object sender, EventArgs e)
{
Form4 f = new Form4();
f.Show();
Application.OpenForms["Form1"].Hide();
}
}
}
Form2
It contains a label to show the data a TextBox for the user to enter the value and a button. It also has rich text boxes to show whether the value is low or high. There are 5 chances to guess the correct number.
Coding
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 game
{
public partial class Form2 : Form
{
// Initializing component
static Random r = new Random();
int value = r.Next(10);
int guessnum;
int win = 5;
int guess = 1;
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Form1 f = new Form1();
f.Show();
Application.OpenForms["Form2"].Hide();
}
private void button1_Click(object sender, EventArgs e)
{
// Game logic
guessnum = Convert.ToInt32(textBox1.Text);
while (win >= 0)
{
if (guessnum == value)
{
if (guess == 1)
{
label4.Text = "Wow! In 1st chance you got the number";
}
else
{
label4.Text = "You got the number and number of chances you took are " + guess;
}
break;
}
else if (guessnum < value)
{
richTextBox1.Text += guessnum + "\n";
label4.Text = "Wrong guess and number of guesses left are " + (5 - guess);
}
else if (guessnum > value)
{
richTextBox2.Text += guessnum + "\n";
label4.Text = "Wrong guess and number of guesses left are " + (5 - guess);
}
guess++;
win--;
break;
}
if (guess == 6)
{
label4.Text = "You lose. Correct guess is " + value;
}
}
}
}
Form3
It contains a label to show the data a TextBox for the user to enter the value and a button. It also has rich text boxes to show whether the value is low or high. There are 7 chances to guess the correct number.
Coding
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 game
{
public partial class Form3 : Form
{
// Initializing component
static Random r = new Random();
int value = r.Next(100);
int guessnum;
int win = 7;
int guess = 1;
public Form3()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Form1 f = new Form1();
f.Show();
Application.OpenForms["Form3"].Hide();
}
private void button1_Click(object sender, EventArgs e)
{
// Game logic
guessnum = Convert.ToInt32(textBox1.Text);
while (win >= 0)
{
if (guessnum == value)
{
if (guess == 1)
{
label4.Text = "Wow! In 1st chance you got the number";
}
else
{
label4.Text = "You got the number and number of chances you took are " + guess;
}
break;
}
else if (guessnum < value)
{
richTextBox1.Text += guessnum + "\n";
label4.Text = "Wrong guess and number of guesses left are " + (7 - guess);
}
else if (guessnum > value)
{
richTextBox2.Text += guessnum + "\n";
label4.Text = "Wrong guess and number of guesses left are " + (7 - guess);
}
guess++;
win--;
break;
}
if (guess == 8)
{
label4.Text = "You lose. Correct guess is " + value;
}
}
}
}
Form4
It contains a label to show the data a TextBox for the user to enter the value and a button. It also has rich text boxes to show whether the value is low or high. There are 10 chances to guess the correct number.
Coding
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 game
{
public partial class Form4 : Form
{
// Initializing component
static Random r = new Random();
int value = r.Next(1000);
int guessnum;
int win = 10;
int guess = 1;
public Form4()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Form1 f = new Form1();
f.Show();
Application.OpenForms["Form4"].Hide();
}
private void button1_Click(object sender, EventArgs e)
{
// Game logic
guessnum = Convert.ToInt32(textBox1.Text);
while (win >= 0)
{
if (guessnum == value)
{
if (guess == 1)
{
label4.Text = "Wow! In 1st chance you got the number";
}
else
{
label4.Text = "You got the number and number of chances you took are " + guess;
}
break;
}
else if (guessnum < value)
{
richTextBox1.Text += guessnum + "\n";
label4.Text = "Wrong guess and number of guesses left are " + (10 - guess);
}
else if (guessnum > value)
{
richTextBox2.Text += guessnum + "\n";
label4.Text = "Wrong guess and number of guesses left are " + (10 - guess);
}
guess++;
win--;
break;
}
if (guess == 11)
{
label4.Text = "You lose. Correct guess is " + value;
}
}
}
}
Output
Output of 1 and 10
Output of 1 and 100
Output of 1 and 1000