Given an integer N. A number guessing game is a simple guessing game where a user is supposed to guess a number between 0 and N in a maximum of 10 attempts. The game will end after 10 attempts and if the player failed to guess the number, and then he loses the game.
N = 100
Number chosen: 20
Machine: Guess a number between 1 and N
Machine: Lower number please!
Machine: Higher number please!
Machine: You guessed the number in 3 attempts
Now, terminate the game.
Approach: The following steps can be followed to design the game:
- Generate a random number between 0 and N.
- Then iterate from 1 to 10 and check if the input number is equal to the assumed number or not.
- If yes, then the player wins the game.
- Otherwise, terminate the game after 10 attempts.
Below is the implementation of the above approach: