Lolinondoda

NumberGameSimple

Jan 2nd, 2020
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.38 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace GuessTheNumberGameSimplified {
  8.     class Program {
  9.         static void Main(string[] args) {
  10.  
  11.             //The correct answer
  12.             int answer = 8;
  13.             //Console.WriteLine(answer); TOGGLE FOR CHECKING PURPOSES
  14.  
  15.             //Initial message.
  16.             Console.WriteLine("Number Guessing Game \nI am thinking of a number between 1 and 10 - can you guess which it is?");
  17.  
  18.             //The users string input is converted to an integer and stored in the variable userGuess
  19.             int userGuess = 0;
  20.             //To show that it is stored correctly
  21.             //Console.WriteLine(userGuess); TOGGLE FOR CHECKING PURPOSES
  22.             //Variable to tell the program if it should perform the do-loop or jump to end.
  23.             bool GameOver = false;
  24.  
  25.             //Program will always execute this as long as GameOVer has value false.
  26.             do
  27.             {
  28.                 //Program should execute this while GameOver has value false.
  29.                 while (GameOver == false) {
  30.                     //The user should be able to guess
  31.                     Console.WriteLine("Enter your guess:");
  32.                     userGuess = Convert.ToInt32(Console.ReadLine());
  33.  
  34.                     //if the user guesses a number that is too high they should get this message:
  35.                     if (userGuess > answer) {
  36.                         Console.WriteLine($"{userGuess} is not correct, try a lower number!");
  37.                         GameOver = false;
  38.                     }
  39.                     //if the user guesses a number that is too low they should get this message:
  40.                     else if (userGuess < answer) {
  41.                         Console.WriteLine($"{userGuess} is not correct, try a higher number!");
  42.                         GameOver = false;
  43.                     }
  44.                     //if the user guesses the correct number they should get this:  
  45.                     else {
  46.                         //Win message
  47.                         Console.WriteLine("Correct, you win!");
  48.  
  49.                         do {
  50.                             //Option to play again
  51.                             Console.WriteLine("Press 1 to play again, or 2 to quit");
  52.  
  53.                             //new user input - stored as int in a new variable.
  54.                             int playAgain = Convert.ToInt32(Console.ReadLine());
  55.                             Console.WriteLine(playAgain);
  56.  
  57.                             //execute if player wants to play again
  58.                             if (playAgain == 1) {
  59.                                 break;
  60.                             }
  61.                             //execute if player chooses to not play again.
  62.                             else {
  63.                                 GameOver = true;
  64.                             }
  65.                         }
  66.                         while (GameOver == false);
  67.                     }
  68.                 }
  69.             }
  70.             while (GameOver == false);
  71.  
  72.             //End of program message.
  73.             Console.WriteLine("Thank you for playing!");
  74.  
  75.             //Makes the console stay open after game is completed.
  76.             Console.ReadLine();
  77.  
  78. //DONT TOUCH THESE!! END OF PROGRAM
  79.         }
  80.     }
  81. }
  82. //DONT TOUCH THESE!! END OF PROGRAM
Add Comment
Please, Sign In to add comment