Below is a C# Hi-Lo Guessing Game program. In this game, the computer generates a random number, and the user tries to guess it. After each guess, the program tells the user whether the guess is too high, too low, or correct.
Full Code
using System; class HiLoGuessingGame { static void Main(string[] args) { Console.WriteLine("Welcome to the Hi-Lo Guessing Game!"); Console.WriteLine("Try to guess the number between 1 and 100."); // Generate a random number between 1 and 100 Random random = new Random(); int targetNumber = random.Next(1, 101); int guess = 0; // User's guess int attempts = 0; // Number of attempts while (true) { Console.Write("Enter your guess: "); // Validate input if (!int.TryParse(Console.ReadLine(), out guess) || guess < 1 || guess > 100) { Console.WriteLine("Invalid input. Please enter a number between 1 and 100."); continue; } // Increment the number of attempts attempts++; // Check the guess if (guess < targetNumber) { Console.WriteLine("Too low! Try again."); } else if (guess > targetNumber) { Console.WriteLine("Too high! Try again."); } else { Console.WriteLine($"Congratulations! You guessed the number {targetNumber} in {attempts} attempts."); break; } } Console.WriteLine("Thanks for playing!"); } }
Explanation
- Random Number Generation:
- The program generates a random number between 1 and 100 using the Random class.
- The Next(1, 101) method generates a number inclusive of 1 and exclusive of 101 (i.e., 1 to 100).
Random random = new Random(); int targetNumber = random.Next(1, 101);
- User Input:
- The program prompts the user to enter a guess.
- Input validation ensures the user enters an integer between 1 and 100. If the input is invalid, the program displays an error message and prompts the user again.
if (!int.TryParse(Console.ReadLine(), out guess) || guess < 1 || guess > 100) { Console.WriteLine("Invalid input. Please enter a number between 1 and 100."); continue; }
- Game Logic:
- The program uses a while loop to repeatedly prompt the user until they guess the correct number.
- After each guess:
- If the guess is less than the target, the program outputs “Too low!”
- If the guess is greater than the target, the program outputs “Too high!”
- If the guess matches the target, the program congratulates the user and ends the loop.
if (guess < targetNumber) { Console.WriteLine("Too low! Try again."); } else if (guess > targetNumber) { Console.WriteLine("Too high! Try again."); } else { Console.WriteLine($"Congratulations! You guessed the number {targetNumber} in {attempts} attempts."); break; }
- Tracking Attempts:
- A counter (attempts) keeps track of how many guesses the user has made.
- The counter increments after each valid guess.
attempts++;
- Exiting the Game:
- The loop ends when the user guesses the correct number.
- A farewell message is displayed at the end of the game.
Console.WriteLine("Thanks for playing!");
Sample run
Run the program:
Welcome to the Hi-Lo Guessing Game! Try to guess the number between 1 and 100. Enter your guess: 50 Too high! Try again. Enter your guess: 25 Too low! Try again. Enter your guess: 37 Congratulations! You guessed the number 37 in 3 attempts. Thanks for playing!
Features
- Randomized Target Number:
- The Random class ensures the game is different each time.
- Input Validation:
- Ensures the user enters a valid integer between 1 and 100.
- Feedback:
- Provides real-time feedback after each guess, guiding the player to the correct number.
- Attempts Counter:
- Tracks and displays the number of attempts made by the user.
Ideas for Enhancement
- Custom Range:
- Allow the user to specify a custom range for the game.
Console.Write("Enter the minimum number: "); int min = int.Parse(Console.ReadLine()); Console.Write("Enter the maximum number: "); int max = int.Parse(Console.ReadLine()); int targetNumber = random.Next(min, max + 1);
- Replay Option:
- Add a feature to let the user play multiple rounds without restarting the program.
Console.Write("Would you like to play again? (y/n): "); string playAgain = Console.ReadLine().ToLower(); if (playAgain != "y") break;
- Hints:
- Provide hints after a certain number of incorrect guesses.
if (attempts == 5) { Console.WriteLine($"Hint: The number is {(targetNumber % 2 == 0 ? "even" : "odd")}."); }
- Leaderboard:
- Save the user’s name and number of attempts in a leaderboard for competitive play.
This C# Hi-Lo Guessing Game is a fun and interactive way to practice working with loops, conditionals, randomization, and user input validation. It’s simple yet easily extendable for additional features!