Random random = new Random();
int maxNumberTries = 5;
int randomNumber = random.Next(1, 100);
Console.WriteLine($"Random number: {randomNumber}");
Console.WriteLine(" ");
Console.WriteLine("Welcome to this number guessing game from 1 to 100!");
Console.WriteLine("You have 5 guesses to guess the number!!");
Console.WriteLine("To exit the game press 0 (zero)");
Console.WriteLine(" ");
for (int i = 1; i <= maxNumberTries; i++)
{
Console.Write($"{i} => Guess the number from 1 to 100: ");
int numberTyped;
if (int.TryParse(Console.ReadLine(), out numberTyped))
{
Console.WriteLine($"Number Typed: {numberTyped}");
if (numberTyped == randomNumber)
{
Console.WriteLine("You won!!\nYou are the best!!");
break;
}
else if (i == maxNumberTries)
{
Console.WriteLine("Wrong answer!\nNo more guesses left!\nYou're a loser!!!!");
}
else if (numberTyped == 0)
{
Console.WriteLine("Bye! See you next time!!");
break;
}
else
{
if (numberTyped > randomNumber)
{
Console.WriteLine($"The random number is less than {numberTyped}");
}
else
{
Console.WriteLine($"The random number is higher than {numberTyped}");
}
Console.WriteLine(" ");
}
}
else
{
Console.WriteLine("Invalid input. Please enter an integer!");
Console.WriteLine(" ");
i--;
}
}