Skip to content
C project #02Beginner

Number Guessing Game

Guess a number from 1 to 100 in seven attempts, with hints, multiple rounds and a reproducible random generator.

Estimated time
~2 hours to build
Files
5 files · 256 lines
Technologies
C11, <stdio.h>, <time.h>

Introduction

Games are a great way to practise loops and conditions. This one also teaches something many tutorials skip: how pseudo-random numbers work. Instead of relying only on rand(), it implements a tiny linear congruential generator, which means a chosen seed always produces the same game — perfect for testing.

Problem statement

Write a game in which the computer chooses a secret number between 1 and 100. The player has seven attempts per round, receives “higher/lower” hints, may play several rounds, and sees a summary of rounds won and the best score at the end. Out-of-range guesses must not count as attempts.

Objectives

  • Write nested loops for rounds and attempts
  • Implement a linear congruential pseudo-random generator
  • Track game statistics across rounds
  • Read single-character answers safely with " %c"

Features

  • Seeded (reproducible) or time-based random games
  • Seven attempts with higher/lower hints
  • Out-of-range guesses are rejected without using an attempt
  • Statistics: rounds played, rounds won and best score

Concepts demonstrated

  • Loops
  • Pseudo-random numbers
  • static global state
  • Input with scanf

System architecture

next_random advances a 32-bit LCG state (state = 1664525 × state + 1013904223 mod 2³²); random_between maps it to a range. play_round runs one round and returns the attempts used (0 for a loss, −1 if input ended). main reads the seed, loops over rounds and keeps the statistics.

Folder structure

TextProject tree
number-guessing-game/
├── README.md
├── Makefile
├── src/main.c
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CC = gcc
CFLAGS = -std=c11 -Wall -Wextra -O2
SRC := $(wildcard src/*.c)
HDR := $(wildcard src/*.h)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CC) $(CFLAGS) -Isrc $(SRC) -o app -lm
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/main.c
The random generator, the round logic in play_round, and the round loop with statistics in main.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
gcc -std=c11 -Wall -Wextra -Isrc src/*.c -o app -lm
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
Enter a seed (0 for a random game):
--- Round 1 ---
I'm thinking of a number between 1 and 100.
Attempt 1/7 - your guess:
Too low - go higher.
Attempt 2/7 - your guess:
Too high - go lower.
Attempt 3/7 - your guess:
Please guess between 1 and 100 (attempt not counted).
Attempt 3/7 - your guess:
Correct! You found it in 3 attempt(s).
Play again? (y/n):
--- Round 2 ---
I'm thinking of a number between 1 and 100.
Attempt 1/7 - your guess:
Too low - go higher.
Attempt 2/7 - your guess:
Too low - go higher.
Attempt 3/7 - your guess:
Too low - go higher.
Attempt 4/7 - your guess:
Too high - go lower.
Attempt 5/7 - your guess:
Too low - go higher.
Attempt 6/7 - your guess:
Correct! You found it in 6 attempt(s).
Play again? (y/n):
Rounds played: 2, won: 2, best: 3 attempt(s)
Input
42
25
75
150
50
y
50
75
88
94
91
93
n

Testing

  • tests/sample-input.txt uses seed 42, whose first two secrets are 50 and 93; compare the output with tests/expected-output.txt.
  • Enter 0 as the seed to get a different game each run.
  • Guess 150 or 0 and check that the attempt counter does not advance.

Common errors

The same number every game

You entered a fixed seed. Use 0 to seed from the clock.

"Play again" is skipped

Read the answer with scanf(" %c", &again) — the space skips the newline left by the previous number.

Possible improvements

  • Difficulty levels that change the range and number of attempts
  • Save the best score to a file
  • Let the computer guess the player's number using binary search

Additional challenges

  1. 1Prove that binary search always finds the number in at most 7 guesses for 1–100.
  2. 2Replace the LCG with xorshift32 and compare the sequences.
  3. 3Add a two-player mode where one player sets the number.

Built and tested it yourself? Mark it to update your dashboard.