using System;
namespace GuessTheNumberGame
{
public class NumberGuesser
{
private int low;
private int high;
private int guess;
private int attempts;
public NumberGuesser(int min, int max)
{
low = min;
high = max;
attempts = 0;
}
public void StartGame()
{
Console.WriteLine($"Think of a number between {low} and {high}.");
Console.WriteLine("I will try to guess it!");
Console.WriteLine("Answer with: 'too high', 'too low', or 'correct'");
bool guessedCorrectly = false;
while (!guessedCorrectly)
{
guess = (low + high) / 2;
attempts++;
Console.WriteLine($"Is your number {guess}?");
string response = Console.ReadLine().ToLower();
if (response == "correct")
{
Console.WriteLine($"Yay! I guessed your number {guess} in {attempts} attempts!");
guessedCorrectly = true;
}
else if (response == "too high")
{
high = guess - 1;
}
else if (response == "too low")
{
low = guess + 1;
}
else
{
Console.WriteLine("Please answer with: 'too high', 'too low', or 'correct'");
}
if (low > high && !guessedCorrectly)
{
Console.WriteLine("Hmm... Something is wrong. Did you change your number?");
guessedCorrectly = true;
}
}
}
}
}
class Program
{
static void Main()
{
GuessTheNumberGame.NumberGuesser guesser = new GuessTheNumberGame.NumberGuesser(1, 100);
guesser.StartGame();
}
}