A real-time multiplayer Tic-Tac-Toe game built with Java WebSocket server and HTML5/JavaScript client, using Protocol Buffers for efficient communication.
- Real-time multiplayer gameplay - Play against other players in real-time
- Lobby system - Create and join game lobbies
- Player identification - Unique player IDs and usernames
- Game state synchronization - All players see the same game state
- Responsive UI - Clean, modern interface with player names display
- WebSocket communication - Fast, bidirectional communication
- Protocol Buffers - Efficient binary serialization
xox-server/
├── client/ # Frontend (HTML5/JavaScript)
│ ├── index.html # Main HTML file
│ ├── script.js # Client-side JavaScript
│ ├── style.css # CSS styling
│ ├── game.proto # Protocol Buffers definition (client copy)
│ └── protobuf.js # Protocol Buffers JavaScript library
├── server/ # Backend (Java)
│ ├── src/main/
│ │ ├── java/com/game/
│ │ │ ├── connection/ # WebSocket connection handling
│ │ │ ├── model/ # Game models (Player, Lobby, Game)
│ │ │ ├── server/ # Server and connection handlers
│ │ │ └── service/ # Business logic services
│ │ └── proto/
│ │ └── game.proto # Protocol Buffers definition
│ ├── pom.xml # Maven configuration
│ └── README.MD # Server-specific documentation
└── README.md # This file
- Java 11 - Programming language
- Maven - Build and dependency management
- WebSocket - Real-time communication
- Protocol Buffers 3.21.7 - Message serialization
- HTML5 - Structure
- CSS3 - Styling
- JavaScript (ES6+) - Client-side logic
- Protocol Buffers JS - Message deserialization
- WebSocket API - Server communication
- Login: Players enter their username and connect to the server
- Lobby: Players can create new games or join existing ones
- Game: Two players play Tic-Tac-Toe with real-time updates
- Results: Game shows winner or draw, players can return to lobby
The game uses the following main message types:
InitialConnection- Player login with usernameCreateLobbyRequest- Create a new game lobbyJoinLobbyRequest- Join an existing lobbyMakeMoveRequest- Make a move in the game
PlayerIdResponse- Assign unique player IDLobbyListUpdate- Update available lobbiesGameStateUpdate- Update game stateErrorNotification- Error messages
- Java 11 or higher
- Maven 3.6+
- Modern web browser (Chrome, Firefox, Safari, Edge)
- Navigate to the server directory:
cd server- Compile and run the server:
mvn clean compile exec:javaThe server will start on localhost:8080.
- Navigate to the client directory:
cd client- Serve the files using a local web server. You can use:
Python 3:
python -m http.server 8000Python 2:
python -m SimpleHTTPServer 8000Node.js (if you have http-server installed):
npx http-server -p 8000PHP:
php -S localhost:8000- Open your browser and navigate to
http://localhost:8000
- Connect: Enter your username and click "Bağlan" (Connect)
- Create or Join:
- Click "Yeni Oyun Kur" (Create New Game) to create a lobby
- Or click "Katıl" (Join) next to an existing lobby
- Play:
- The first player (host) gets X, the second player gets O
- Click on empty cells to make your move
- Player names are displayed at the top of the game screen
- Leave: Click "Oyundan Ayrıl" (Leave Game) to return to the lobby
- Standard Tic-Tac-Toe rules apply
- 3x3 grid
- First player to get 3 in a row (horizontal, vertical, or diagonal) wins
- If all 9 spaces are filled without a winner, it's a draw
- Players take turns, starting with X (host)
- GameServer: Main server class with WebSocket endpoint
- ConnectionHandler: Manages WebSocket connections and message routing
- LobbyService: Handles lobby creation, joining, and management
- GameService: Manages game logic and moves
- NotificationService: Sends updates to clients
- WebSocket Connection: Handles server communication
- UI Management: Dynamic view switching (login, lobby, game)
- Game State: Synchronizes with server state
- Protocol Buffers: Message encoding/decoding
The Maven build automatically compiles Protocol Buffers. For manual compilation:
# In server directory
mvn clean compile- Server logs are printed to console
- Client logs are available in browser developer tools
- WebSocket traffic can be monitored in browser network tab
- Connection Refused: Make sure the server is running on port 8080
- CORS Issues: Serve the client files through a web server, not file://
- Protocol Buffers Errors: Ensure both client and server use the same .proto file