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 guesses 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
 
main design.PNG
 
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
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. namespace game  
  10. {  
  11.     public partial class Form1 : Form  
  12.     {  
  13.         public Form1()  
  14.         {  
  15.             InitializeComponent();  
  16.         }  
  17.         //coding of button 1 and 10.It open form2 and hide form1  
  18.         private void button1_Click(object sender, EventArgs e)  
  19.         {  
  20.             Form2 f = new Form2();  
  21.             f.Show();  
  22.             Application.OpenForms["form1"].Hide();  
  23.         }  
  24.         //coding of button 1 and 100.It open form3 and hide form1  
  25.         private void button2_Click(object sender, EventArgs e)  
  26.         {  
  27.             Form3 f = new Form3();  
  28.             f.Show();  
  29.             Application.OpenForms["form1"].Hide();  
  30.         }  
  31.         //coding of button 1 and 1000.It open form4 and hide form1  
  32.         private void button3_Click(object sender, EventArgs e)  
  33.         {  
  34.             Form4 f = new Form4();  
  35.             f.Show();  
  36.             Application.OpenForms["form1"].Hide();  
  37.         }  
  38.     }  
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:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. namespace game  
  10. {  
  11.     public partialclass Form2 : Form  
  12.     {  
  13.         // Intializing component  
  14.         static Random r = newRandom();  
  15.         int value=r.Next(10);  
  16.         int guessnum;  
  17.         int win = 5;  
  18.         int guess = 1;  
  19.         public Form2()  
  20.         {  
  21.             InitializeComponent();  
  22.         }  
  23.         private void button2_Click(object sender,EventArgs e)  
  24.         {  
  25.             Form1 f =new Form1();  
  26.             f.Show();  
  27.             Application.OpenForms["form2"].Hide();  
  28.         }  
  29.         private void button1_Click(object sender,EventArgs e)  
  30.         {  
  31.             // coding game  
  32.             guessnum = Convert.ToInt32(textBox1.Text);  
  33.             while (win >= 0)  
  34.             {  
  35.                 if (guessnum == value)  
  36.                 {  
  37.                     if (guess == 1)  
  38.                     {  
  39.                         label4.Text = "wow In 1st chance you got the number";  
  40.                     }  
  41.                     else  
  42.                         label4.Text = "you got the number and no of chance you took are " + guess;  
  43.                     break;  
  44.                 }  
  45.                 elseif (guessnum < value)  
  46.                 {  
  47.                     richTextBox1.Text += guessnum +"\n";  
  48.                     label4.Text = "wrong Guess and number of guesses left are " + (5 - guess);  
  49.                 }  
  50.                 elseif (guessnum > value)  
  51.                 {  
  52.                     richTextBox2.Text += guessnum +"\n";  
  53.                     label4.Text = "wrong Guess and number of guesses left are " + (5 - guess);  
  54.                 }  
  55.                 guess++;  
  56.                 win--;  
  57.                 break;  
  58.             }  
  59.             if (guess == 6)  
  60.             {  
  61.                 label4.Text = "You loose,Correct Guess is " + value;  
  62.             }  
  63.         }  
  64.     }  
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
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. namespace game  
  10. {  
  11.     public partialclass Form3 : Form  
  12.     {  
  13.         // Intialising component  
  14.         static Random r = newRandom();  
  15.         int value= r.Next(100);  
  16.         int guessnum;  
  17.         int win = 7;  
  18.         int guess = 1;  
  19.         public Form3()  
  20.         {  
  21.             InitializeComponent();  
  22.         }  
  23.         private void button2_Click(object sender,EventArgs e)  
  24.         {  
  25.             Form1 f =new Form1();  
  26.             f.Show();  
  27.             Application.OpenForms["form3"].Hide();  
  28.         }  
  29.         private void button1_Click(object sender,EventArgs e)  
  30.         {  
  31.             // coding game  
  32.             guessnum = Convert.ToInt32(textBox1.Text);  
  33.             while (win >= 0)  
  34.             {  
  35.                 if (guessnum == value)  
  36.                 {  
  37.                     if (guess == 1)  
  38.                     {  
  39.                         label4.Text = "wow In 1st chance you got the number";  
  40.                     }  
  41.                     else  
  42.                         label4.Text = "you got the number and no of chance you took are " + guess;  
  43.                     break;  
  44.                 }  
  45.                 elseif (guessnum < value)  
  46.                 {  
  47.                     richTextBox1.Text += guessnum +"\n";  
  48.                     label4.Text = "wrong Guess and number of guesses left are " + (7 - guess);  
  49.                 }  
  50.                 elseif (guessnum > value)  
  51.                 {  
  52.                     richTextBox2.Text += guessnum +"\n";  
  53.                     label4.Text = "wrong Guess and number of guesses left are " + (7 - guess);  
  54.                 }  
  55.                 guess++;  
  56.                 win--;  
  57.                 break;  
  58.             }  
  59.             if (guess == 8)  
  60.             {  
  61.                 label4.Text = "You loose,Correct Guess is " + value;  
  62.             }  
  63.         }  
  64.     }  
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
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. namespace game  
  10. {  
  11.     public partialclass Form4 : Form  
  12.     {  
  13.         // Intialising component  
  14.         static Random r = newRandom();  
  15.         int value=r.Next(1000);  
  16.         int guessnum;  
  17.         int win = 10;  
  18.         int guess = 1;  
  19.         public Form4()  
  20.         {  
  21.             InitializeComponent();  
  22.         }  
  23.         private void button2_Click(object sender,EventArgs e)  
  24.         {  
  25.             Form1 f =new Form1();  
  26.             f.Show();  
  27.             Application.OpenForms["form4"].Hide();  
  28.         }  
  29.         private void button1_Click(object sender,EventArgs e)  
  30.         {  
  31.             // Coding of game  
  32.             guessnum = Convert.ToInt32(textBox1.Text);  
  33.             while (win >= 0)  
  34.             {  
  35.                 if (guessnum == value)  
  36.                 {  
  37.                     if (guess == 1)  
  38.                     {  
  39.                         label4.Text = "wow In 1st chance you got the number";  
  40.                     }  
  41.                     else  
  42.                         label4.Text = "you got the number and no of chance you took are " + guess;  
  43.                     break;  
  44.                 }  
  45.                 elseif (guessnum < value)  
  46.                 {  
  47.                     richTextBox1.Text += guessnum +"\n";  
  48.                     label4.Text = "wrong Guess and number of guesses left are " + (10 - guess);  
  49.                 }  
  50.                 elseif (guessnum > value)  
  51.                 {  
  52.                     richTextBox2.Text += guessnum +"\n";  
  53.                     label4.Text = "wrong Guess and number of guesses left are " + (10 - guess);  
  54.                 }  
  55.                 guess++;  
  56.                 win--;  
  57.                 break;  
  58.             }  
  59.             if (guess == 11)  
  60.             {  
  61.                 label4.Text = "You loose,Correct Guess is " + value;  
  62.             }  
  63.         }  
  64.     }  
Output:
 
Output of 1 and 10
 
outcome.PNG
 
Output of 1 and 100
 
output 1and100.PNG
 
Output of 1 and 1000
 
output 1and1000.PNG