fork download
  1. using System;
  2.  
  3. class Program
  4. {
  5. public static void Main()
  6. {
  7. Random rng = new Random();
  8. int min = 0, guess = 0, tries = 0, max = 100, number = rng.Next(1, 100);
  9. bool ai = false;
  10.  
  11. Console.WriteLine("Do you want to [g]uess or [c]hoose?");
  12. string choice = Console.ReadLine();
  13. if (choice.StartsWith("c"))
  14. {
  15. Console.WriteLine("Insert your number between 1 and 100:");
  16. int.TryParse(Console.ReadLine(), out number);
  17. ai = true;
  18. }
  19.  
  20. string message = ai ? "You generated a number between 1 and 100. Let me try"
  21. : "I generated a number between 1 and 100. Can you guess it?";
  22. Console.WriteLine(message);
  23.  
  24. while (guess != number)
  25. {
  26. int.TryParse(ai ? (guess > number ? rng.Next(min, max = guess) : rng.Next(min = guess, max)).ToString()
  27. : Console.ReadLine(),
  28. out guess);
  29. if (guess != number)
  30. Console.WriteLine("No, it's {0} than {1}. Try again.", guess < number ? "higher" : "lower", guess);
  31. tries++;
  32. }
  33. message = ai ? "Great, the answer was {0}. It took me {1} tries."
  34. : "Great, the answer was {0}. It took you {1} tries.";
  35. Console.WriteLine(message, number, tries);
  36. Console.ReadLine();
  37. }
  38. }
  39.  
  40.  
Success #stdin #stdout 0.06s 33952KB
stdin
c
50
stdout
Do you want to [g]uess or [c]hoose?
Insert your number between 1 and 100:
You generated a number between 1 and 100. Let me try
No, it's lower than 97. Try again.
No, it's lower than 89. Try again.
No, it's lower than 71. Try again.
No, it's higher than 5. Try again.
No, it's higher than 45. Try again.
No, it's lower than 55. Try again.
No, it's lower than 51. Try again.
No, it's higher than 48. Try again.
Great, the answer was 50. It took me 9 tries.