Skip to content

Instantly share code, notes, and snippets.

@spetroll
Last active December 10, 2015 18:58
Show Gist options
  • Save spetroll/4477678 to your computer and use it in GitHub Desktop.
Save spetroll/4477678 to your computer and use it in GitHub Desktop.
using System;
namespace GuessTheNumber
{
internal class Program
{
private static void Main()
{
Random rng = new Random();
int min = 0, guess = 0, tries = 0, max = 100, number = rng.Next(1, 100);
bool ai = false;
Console.WriteLine("Do you want to [g]uess or [c]hoose?");
string choice = Console.ReadLine();
if (choice.StartsWith("c"))
{
Console.WriteLine("Insert your number between 1 and 100:");
int.TryParse(Console.ReadLine(), out number);
ai = true;
}
string message = ai
? "You generated a number between 1 and 100. Let me try"
: "I generated a number between 1 and 100. Can you guess it?";
Console.WriteLine(message);
while (guess != number)
{
int.TryParse(ai
? (guess < number ? rng.Next(min, max = guess) : rng.Next(min = guess, max)).ToString()
: Console.ReadLine(),
out guess);
if (guess != number)
Console.WriteLine("No, it's {0} than {1}. Try again.", guess < number ? "higher" : "lower", guess);
tries++;
}
message = ai
? "Great, the answer was {0}. It took me {1} tries."
: "Great, the answer was {0}. It took you {1} tries.";
Console.WriteLine(message, number, tries);
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment