Guess the Number
A simple client-server game where the server generates a random number between 0 and 500, and the client tries to guess it. After each guess, the server sends feedback. Each execution represents a full game session containing multiple rounds. The best score (fewest guesses needed to find the number) is shown at the end of the session.
Phase 1 — Game logic & communication protocol
-
The server and client communicate through a text-based protocol (line-oriented).
-
Client → Server messages:
START→ begins a new round (server generates a new random number in [0, 500])GUESS <number>→ the player’s attempt to guess the secret number
-
Server → Client responses:
TOO_LOW→ the guessed number is smaller than the secret numberTOO_HIGH→ the guessed number is larger than the secret numberCORRECT <attempts> <best>→ the number was guessed; includes current attempts and best score in this session
-
-
Game model: each round tracks the secret number, current number of guesses, and session-best (minimum attempts across rounds).
-
Input validation: guesses must be integers between 0 and 500; invalid inputs receive a short
ERROR Invalid inputresponse.
Functional result: The logic module can simulate a complete guessing game locally, producing correct feedback messages (TOO_LOW, TOO_HIGH, CORRECT) and tracking the best score across multiple rounds.
Phase 2 — Server implementation
-
server.pylistens on a TCP socket. -
On client connection: start a new session and wait for
STARTcommands. -
For each round:
- Generate a random secret number.
- Handle
GUESSmessages and respond with appropriate feedback. - When the player guesses correctly, update and return the best score so far.
-
Support multiple sequential rounds in the same session.
Functional result: A connected client can play multiple rounds in one session; the server correctly responds with feedback and maintains the best score.
Phase 3 — Client interaction (CLI)
-
client.pyconnects to the server, sendsSTART, and then repeatedly prompts the user for a number. -
Sends
GUESS <number>for each attempt and displays the server’s responses in a user-friendly way:- “The number is higher/lower”
- “Correct! You found it in X attempts. Best: Y attempts.”
-
After each correct guess, ask: “Play again? (y/n)” and send
STARTor close connection accordingly.
Functional result: The client provides an interactive command-line experience, letting the player guess numbers, get instant feedback, and choose to play multiple rounds.
Phase 4 — Session summary & Error handling
- When the client quits, the server sends a final message summarizing the session:
"Your best score this session: N attempts" - The server handles invalid commands or out-of-range guesses gracefully with short messages instead of disconnecting.
Functional result: The client displays the final session summary; invalid inputs trigger short warnings.