fork download
  1. using System;
  2. using System.Text;
  3.  
  4.  
  5. namespace guess_the_number
  6. {
  7. class MainClass
  8. {
  9. private Random random = new Random();
  10.  
  11. int getRandomNumber()
  12. {
  13. return random.Next(1, 100);
  14. }
  15.  
  16. void showStartText()
  17. {
  18. Console.Write("~~~Guess-The-Number~~~\n\n\n");
  19. //
  20. //
  21. Console.Write("Instructions:\n\n\n");
  22. //
  23. //
  24. Console.Write("You will attempt to guess a number, 1-100.\n");
  25. Console.Write("To guess the number, type it, then press enter.\n");
  26. Console.Write("After each guess, you will be told if the number is higher or lower than your \nguess.\n\n");
  27. }
  28.  
  29. int getValidGuess()
  30. {
  31. while (true)
  32. {
  33. Console.ForegroundColor = ConsoleColor.Red;
  34.  
  35. Console.Write("Please enter your guess:\n");
  36.  
  37. int result;
  38.  
  39. Console.ForegroundColor = ConsoleColor.Green;
  40.  
  41. if (int.TryParse(Console.ReadLine(), out result))
  42. ;
  43. else
  44. {
  45. Console.ForegroundColor = ConsoleColor.Gray;
  46.  
  47. Console.Write("\nInvalid input.\n");
  48.  
  49. continue;
  50. }
  51.  
  52. //parse is good, check if number is in desired range
  53.  
  54. if (!(result >= 1 && result <= 100))
  55. {
  56. Console.ForegroundColor = ConsoleColor.Gray;
  57.  
  58. Console.Write("\nInvalid input.\n");
  59.  
  60. continue;
  61. }
  62.  
  63. return result;
  64. }
  65.  
  66.  
  67. }
  68.  
  69. bool quit()
  70. {
  71. while (true)
  72. {
  73. Console.ForegroundColor = ConsoleColor.Red;
  74.  
  75. Console.Write("Would you like to play again? y/n\n");
  76.  
  77. Console.ForegroundColor = ConsoleColor.Green;
  78.  
  79. string input = Console.ReadLine();
  80.  
  81. Console.ForegroundColor = ConsoleColor.Gray;
  82.  
  83. if (input.ToLowerInvariant() == "y")
  84. return false;
  85. else if (input.ToLowerInvariant() == "n")
  86. return true;
  87. else
  88. {
  89. Console.Write("\n\nInvalid input\n\n");
  90.  
  91. continue;
  92. }
  93. }
  94. }
  95.  
  96.  
  97.  
  98. static void Main(string[] args)
  99. {
  100. MainClass main = new MainClass();
  101.  
  102. //current number that is being guessed
  103. int randomNumber;
  104.  
  105. //users guess
  106. int guess;
  107.  
  108. //starting text
  109. main.showStartText();
  110.  
  111. //main game loop
  112. while (true)
  113. {
  114. //get new random number
  115. randomNumber = main.getRandomNumber();
  116.  
  117. //per-guess loop
  118. while (true)
  119. {
  120. //get valid guess - parses to make return an int, always valid input
  121. guess = main.getValidGuess();
  122.  
  123. if (guess == randomNumber)
  124. {
  125. Console.ForegroundColor = ConsoleColor.Gray;
  126.  
  127. Console.Write("Congratulations, you have guessed the number!\n\n");
  128.  
  129. if (main.quit())
  130. return;
  131. else
  132. break;
  133. }
  134. else if (guess < randomNumber)
  135. {
  136. Console.ForegroundColor = ConsoleColor.Gray;
  137.  
  138. Console.WriteLine("Your guess was lower than the number\n\n");
  139.  
  140. continue;
  141. }
  142. else
  143. {
  144. Console.ForegroundColor = ConsoleColor.Gray;
  145.  
  146. Console.WriteLine("Your guess was higher than the number\n\n");
  147.  
  148. continue;
  149. }
  150. }
  151. }
  152.  
  153. Console.Read();
  154.  
  155. }
  156. }
  157. }
  158.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty