Number Guessing Game

Build a simple number guessing game to test your luck.

Start building, submit solution and get feedback from the community.
2Submit Solution·
5 upvotes10 upvotes

You are required to build a simple number guessing game where the computer randomly selects a number and the user has to guess it. The user will be given a limited number of chances to guess the number. If the user guesses the number correctly, the game will end, and the user will win. Otherwise, the game will continue until the user runs out of chances.

Requirements

It is a CLI-based game, so you need to use the command line to interact with the game. The game should work as follows:

  • When the game starts, it should display a welcome message along with the rules of the game.
  • The computer should randomly select a number between 1 and 100.
  • User should select the difficulty level (easy, medium, hard) which will determine the number of chances they get to guess the number.
  • The user should be able to enter their guess.
  • If the user’s guess is correct, the game should display a congratulatory message along with the number of attempts it took to guess the number.
  • If the user’s guess is incorrect, the game should display a message indicating whether the number is greater or less than the user’s guess.
  • The game should end when the user guesses the correct number or runs out of chances.

Here is a sample output of the game:

Welcome to the Number Guessing Game!
I'm thinking of a number between 1 and 100.
You have 5 chances to guess the correct number.

Please select the difficulty level:
1. Easy (10 chances)
2. Medium (5 chances)
3. Hard (3 chances)

Enter your choice: 2

Great! You have selected the Medium difficulty level.
Let's start the game!

Enter your guess: 50
Incorrect! The number is less than 50.

Enter your guess: 25
Incorrect! The number is greater than 25.

Enter your guess: 35
Incorrect! The number is less than 35.

Enter your guess: 30
Congratulations! You guessed the correct number in 4 attempts.

To make the game more interesting, you can add the following features:

  • Allow the user to play multiple rounds of the game (i.e., keep playing until the user decides to quit). You can do this by asking the user if they want to play again after each round.
  • Add a timer to see how long it takes the user to guess the number.
  • Implement a hint system that provides clues to the user if they are stuck.
  • Keep track of the user’s high score (i.e., the fewest number of attempts it took to guess the number under a specific difficulty level).
Found a mistake? Help us improve.

Join the Community

roadmap.sh is the 7th most starred project on GitHub and is visited by hundreds of thousands of developers every month.

Rank 7th  out of 28M!

292K

GitHub Stars

Star us on GitHub
Help us reach #1

+90k every month

+1M

Registered Users

Register yourself
Commit to your growth

+1.5k every month

26K

Discord Members

Join on Discord
Join the community

Roadmaps Best Practices Guides Videos FAQs YouTube

roadmap.sh by @kamrify

Community created roadmaps, best practices, projects, articles, resources and journeys to help you choose your path and grow in your career.

© roadmap.sh · Terms · Privacy · Advertise ·

ThewNewStack

The top DevOps resource for Kubernetes, cloud-native computing, and large-scale development and deployment.