A C terminal game that uses binary search to efficiently guess a number chosen by the player. The game includes two difficulty levels, replayability, input validation, and a statistics summary.
- Easy Mode: Number between 1–50
- Hard Mode: Number between 1–1000
- Algorithm: Computer uses binary search to narrow down the answer
- Simple Controls: Player provides feedback using
H,L, orC - Replayability: Built-in replay functionality
- Statistics Tracking:
- Games played
- Average guesses
- Best game (fewest guesses)
- Worst game (most guesses)
- Input Validation: Handles invalid responses
The player secretly chooses a number within the selected range. The computer guesses the middle of the current range and asks whether the player's number is:
H— Higher: The number is higher than the computer's guess.L— Lower: The number is lower than the computer's guess.C— Correct: The computer guessed the number.
Based on the response, the computer adjusts its search boundaries and makes the next guess until it finds the correct number.
You need a C compiler installed on your system, such as GCC or Clang.
Download the repository as a ZIP from GitHub and extract it. Open Terminal and navigate into the downloaded project folder.
From inside the project folder, run these commands to compile and launch the game automatically:
gcc src/number_guesser.c -o number_guesserFollowed by
./number_guesserThe program will start in your terminal.
-
Choose a difficulty and think of a number within the displayed range.
-
When the computer makes a guess:
- Enter
Hif your number is higher. - Enter
Lif your number is lower. - Enter
Cif the computer is correct.
- Enter
-
The computer will continue narrowing the range until it finds your number.
After each game, you can choose whether to play again. If you choose N, the program displays your statistics, including:
- Games played
- Average guesses
- Best game
- Worst game
(My number was 23)
Number.Gusser.Demo.mov
This project helped me practice:
- Variables and data types
- if / else statements
- while and do-while loops
- Character handling with toupper
- Binary search
- Tracking and calculating statistics
- Debugging and testing a terminal-based program
- Had trouble setting up and compiling the C program in VS Code.
- Initially allowed invalid inputs, which I later fixed with input validation.
I developed the project incrementally, starting with the core number-guessing logic and then adding difficulty levels, replay functionality, statistics, and input validation. I tested the program using different numbers, difficulty levels, and invalid inputs to make sure the game behaved as expected.
Possible improvements include:
- Adding a maximum guesses indicator
- Adding additional game modes
- Creating a graphical interface
- Tracking more detailed statistics
- Utilize pointers and more advanced features