Guessing Game

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 looses 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

 main design.PNG
 
 In 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();
         }
         //coding of button 1 and 10.It open form2 and hide form1
         private void button1_Click(object sender, EventArgs e)
         {
             Form2 f = new Form2();
             f.Show();
             Application.OpenForms["form1"].Hide();
         }
         //coding of button 1 and 100.It open form3 and hide form1
         private void button2_Click(object sender, EventArgs e)
         {
             Form3 f = new Form3();
             f.Show();
             Application.OpenForms["form1"].Hide();
         }
         //coding of button 1 and 1000.It open form4 and hide form1
         private void button3_Click(object sender, EventArgs e)
         {
             Form4 f = new Form4();
             f.Show();
             Application.OpenForms["form1"].Hide();
         }
     }
 }
 
 Form2

form 2.PNG

It contains the label to show the data and 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 partialclass Form2 : Form
 
    {
         // Intializing component
         static Random r = newRandom();
         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)
         {
             // coding game
             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 no of chance you took are " + guess;
                     break;
                 }
                 elseif (guessnum < value)
                 {
                     richTextBox1.Text += guessnum +"\n";
                     label4.Text = "wrong Guess and number of guesses left are " + (5 - guess);
                 }
                 elseif (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 loose,Correct Guess is " + value;
             }
         }
     }
 }
 
 Form3

form3.PNG

It contains the label to show the data and 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 partialclass Form3 : Form
 
    {
         // Intialising component
         static Random r = newRandom();
         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)
         {
             // coding game
             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 no of chance you took are " + guess;
                     break;
                 }
                 elseif (guessnum < value)
                 {
                     richTextBox1.Text += guessnum +"\n";
                     label4.Text = "wrong Guess and number of guesses left are " + (7 - guess);
                 }
                 elseif (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 loose,Correct Guess is " + value;
             }
         }
     }
 }
 
 Form4

form4.PNG

It contains the label to show the data and 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 partialclass Form4 : Form
 
    {
         // Intialising component
         static Random r = newRandom();
         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)
         {
             // Coding of game
             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 no of chance you took are " + guess;
                     break;
                 }
                 elseif (guessnum < value)
                 {
                     richTextBox1.Text += guessnum +"\n";
                     label4.Text = "wrong Guess and number of guesses left are " + (10 - guess);
                 }
                 elseif (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 loose,Correct Guess is " + value;
             }
         }
     }
 }
 
 Output:

Output of 1 and 10

outcome.PNG

Output of 1 and 100

output 1and100.PNG

Output of 1 and 1000

output 1and1000.PNG

Up Next
    Ebook Download
    View all
    Learn
    View all