Advertisement
Guest User

tutorial

a guest
May 3rd, 2017
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.08 KB | None | 0 0
  1. //Using System; includes all the libraries that we will use
  2.  
  3. using System;
  4.  
  5. // Namespace organizes your code
  6. namespace ProgrammingTutorial
  7. {
  8.     // In c# all code has to go inside a class
  9.     class Program
  10.     {
  11.         //Random Class to generate random numbers
  12.         static Random r = new Random();
  13.  
  14.         //Function that returns a random number that we can use
  15.         public static int GetRandomNumber(int min, int max)
  16.         {
  17.             return r.Next(min, max + 1);
  18.         }
  19.  
  20.         // Function to display text to the console
  21.         public static void Say(string message)
  22.         {
  23.             Console.WriteLine(message);
  24.         }
  25.  
  26.         // Input validation to get a number from the user
  27.         public static int AskForNum(string optionalMessage = "")
  28.         {
  29.             while(true)
  30.             {
  31.                 string s = Console.ReadLine();
  32.                 if(int.TryParse(s, out int x))
  33.                 {
  34.                     return x;
  35.                 }
  36.                 if(!string.IsNullOrEmpty(optionalMessage))
  37.                     Say(optionalMessage);
  38.             }
  39.         }
  40.  
  41.         //This is main starting point for your program
  42.         public static void Main(string[] args)
  43.         {
  44.             while(true)
  45.             {
  46.                 Say("Hello human! Would you like to play a game? Enter 1 to play!");
  47.                 int x = AskForNum("That wasn't a vaild response!");
  48.                 //if x is equal to 1 we play game, else we quit
  49.                 if(x == 1)
  50.                 {
  51.                     //store a random number for the user to guess
  52.                     int number = GetRandomNumber(1, 100);
  53.                     while(true)
  54.                     {
  55.                         Say("Ok! I am guessing of a number between 1 and 100! Can you guess it!?");
  56.                         //make the user input a guess
  57.                         int guess = AskForNum("Guess a proper number!");
  58.                         //then we compare the guess to the computer's number
  59.                         if(guess == number)
  60.                         {
  61.                             //if they guess right, they win!
  62.                             Say("You guessed the number correctly!");
  63.                             //quit the current game loop
  64.                             break;
  65.                         }
  66.                         if(guess > number)
  67.                         {
  68.                             //too high!
  69.                             Say("Too high! Guess again!");
  70.                         }
  71.                         else if(guess < number)
  72.                         {
  73.                             //too low!
  74.                             Say("Too low! Guess again!");
  75.                         }
  76.                     }
  77.                 }
  78.                 else
  79.                 {
  80.                     Say("Goodbye!");
  81.                     //break out of the while loop
  82.                     return;
  83.                 }
  84.             }
  85.         }
  86.     }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement