Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 

Repository files navigation

UDP Number Guessing Game

A simple client-server number guessing game built with Java UDP sockets (DatagramSocket / DatagramPacket).

The server picks a secret number. The client sends up to 5 guesses, and the server replies after each guess to say whether the secret number is higher, lower, or if the client guessed correctly.

How it works

  • Server (GuessServer) listens on UDP port 7000.
  • Client (Player) listens on UDP port 8000 and sends guesses to the server.
  • The server compares each guess to the secret number and replies with one of:
    • Plus grand — the secret number is greater than the guess
    • Plus petit — the secret number is smaller than the guess
    • Gagne — the client guessed correctly (server then stops)
  • The client stops early once it receives Gagne, otherwise it gets up to 5 attempts.

Project structure

udp-number-guessing-game/
├── client/
│   └── src/com/game/client/Player.java
└── server/
    └── src/com/game/server/GuessServer.java

Running it

Both programs assume localhost (127.0.0.1), so run them on the same machine in two terminals.

1. Compile:

javac -d server/bin server/src/com/game/server/GuessServer.java
javac -d client/bin client/src/com/game/client/Player.java

2. Start the server first (it needs the secret number before the client sends guesses):

java -cp server/bin com.game.server.GuessServer

Enter the secret number when prompted.

3. Start the client:

java -cp client/bin com.game.client.Player

Enter a guess when prompted, for up to 5 attempts.

Notes

This project was originally written with French variable/message names (joueur, serveurG, Plus grand/Plus petit) as part of a networking course exercise, then refactored for clarity and to fix a couple of small bugs (a double-increment in the client's loop, and reading stray null bytes from the receive buffer).

Possible improvements

  • Add input validation (non-numeric input currently crashes the program)
  • Make the host/port configurable via command-line arguments
  • Add a Timeout on the client socket so it doesn't hang forever waiting for a reply
  • Package as a Maven/Gradle project instead of raw javac compilation

About

A simple client-server number guessing game built with Java UDP sockets (DatagramSocket/DatagramPacket).

Topics

Resources

Stars

Watchers

Forks

Releases

Packages

Contributors

Languages