The number guessing game is a programming project used to demonstrate concepts such as random number generation, loops, conditional statements, and user interaction. The program randomly selects a number from a user-defined range and guides the player toward the correct answer by indicating whether each guess is higher or lower than the target number.
How the Game Works
To understand how the number guessing game functions, letβs walk through two practical scenarios. These examples demonstrate how narrowing down the range intelligently, similar to a binary search can help guess the number efficiently.
Example 1: Guessing in a Range from 1 to 100
Suppose the user defines the range from 1 to 100, and the system randomly selects 42 as the target number. The guessing process might look like this:
Guess 1: 50 β Too high
Guess 2: 25 β Too low
Guess 3: 37 β Too low
Guess 4: 43 β Too high
Guess 5: 40 β Too low
Guess 6: 41 β Too low
Guess 7: 42 β Correct!
Total Guesses: 7
Example 2: Guessing in a Range from 1 to 50
Now consider a smaller range, from 1 to 50, with the same target number 42. Here's how the guesses might proceed:
Guess 1: 25 β Too low
Guess 2: 37 β Too low
Guess 3: 43 β Too high
Guess 4: 40 β Too low
Guess 5: 41 β Too low
Guess 6: 42 β Correct!
Total Guesses: 6
Note: In both examples, the maximum number of chances is calculated using the binary search principle. However, the user is free to choose any guessing strategy during gameplay.
Algorithm
Read the lower and upper bounds from the user.
Generate a random number between the specified bounds.
Repeatedly accept guesses from the user until the maximum number of attempts is reached.
Indicate whether each guess is too high or too low.
Stop the game when the correct number is guessed and display the result.
If the user fails to guess the number, display the correct answer and a game-over message.
C Implementation
Below is the Implementation of the Algorithm in C:
Read User Input: The program accepts the lower and upper bounds and validates that the upper bound is greater than the lower bound.
Generate Random Number: The rand() function is used to generate a random number within the specified range after seeding the random number generator with srand(time(0)).
Calculate Allowed Attempts: The maximum number of guesses is determined using the binary-search-based formula ceil(log(range_size) / log(2)).
Process User Guesses: A while loop repeatedly accepts guesses from the user until the correct number is guessed or all attempts are exhausted.
Python Implementation
Below is the Implementation of the Algorithm in Python:
Generate a Random Number: The program uses random.randint() to generate a random number within the user-defined range.
Accept User Guesses: The user is given 7 chances to guess the generated number.
Provide Feedback: After each guess, the program indicates whether the guessed number is too high or too low.
Check for Success: If the user guesses the correct number, a success message is displayed along with the number of attempts used.
Handle Game Over: If all attempts are exhausted without a correct guess, the program reveals the correct number and displays a game-over message.
Time Complexity Analysis
Time Complexity: O(n) in the worst case, where n is the maximum number of allowed guesses. The program may need to compare each user guess with the generated number until all attempts are exhausted.
Space Complexity: O(1) as the program uses only a constant amount of extra memory regardless of the input range.