Skip to content

Latest commit

 

History

1 Commit

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🎮 Multiplayer Tic Tac Toe — OS Lab Project

A production-quality, GUI-based multiplayer Tic Tac Toe game built in Java, demonstrating core Operating Systems concepts including multithreading, synchronization, TCP socket programming, and concurrent resource management.


📋 Project Overview

This project implements a full client-server multiplayer game using Java Swing for the GUI and raw TCP sockets for networking. Multiple clients can connect simultaneously, enter a matchmaking queue, and play concurrent Tic Tac Toe matches — all while the server coordinates everything with thread-safe shared state.


✨ Features

Feature Description
🌐 Client-Server Full TCP socket architecture
🔁 Matchmaking Queue Thread-safe FIFO queue with live position updates
🎭 Concurrent Sessions Multiple matches run simultaneously
💬 In-Game Chat Real-time chat panel during gameplay
📊 Session Stats Win/Loss/Draw stats shown in the header
🎨 Modern Dark UI Polished Swing GUI with animations

🏗️ Architecture

project/
├── server/
│   ├── GameServer.java       ← Main entry point; accept loop
│   ├── ClientHandler.java    ← Per-client thread (state machine)
│   ├── GameSession.java      ← Manages one match (board, turns, chat)
│   ├── MatchmakingQueue.java ← Thread-safe queue (ReentrantLock)
│   └── ServerUtils.java      ← Logging, LAN IP
│
├── client/
│   ├── GameClient.java       ← Client entry point
│   ├── NetworkManager.java   ← TCP socket I/O on background thread
│   ├── GameGUI.java          ← Game board window (Swing)
│   └── QueueScreen.java      ← Login + queue waiting screen (Swing)
│
├── common/
│   ├── Message.java          ← Protocol POJO
│   ├── MessageType.java      ← All protocol message type enums
│   └── Protocol.java         ← JSON builders/parsers (Gson)
│
├── lib/
│   └── gson-2.10.1.jar       ← JSON serialization library
│
├── build.bat                 ← Windows build script
├── run_server.bat            ← Launch server
└── run_client.bat            ← Launch one client instance

🧠 OS Concepts Demonstrated

1. Multithreading

  • One thread per client: GameServer.acceptLoop() spawns a new Thread(ClientHandler) for every connection. With 4 clients connected, 4 threads run concurrently.
  • One thread per game session: MatchmakingQueue.tryCreateMatch() starts a Thread(GameSession) for each match. Two simultaneous matches = two session threads.
  • Background I/O thread: NetworkManager.startListening() runs a dedicated reader thread on the client, preventing the Swing EDT from ever blocking.

2. Synchronization

  • ReentrantLock in MatchmakingQueue: Guards all queue reads and writes. Multiple ClientHandler threads call addPlayer() concurrently; the fair lock ensures no thread starves.
  • synchronized in GameSession.processMove(): Prevents two threads from modifying the board simultaneously (e.g., both players sending a move packet at the same instant).
  • synchronized sendMessage() in ClientHandler: Ensures only one thread writes to a socket's PrintWriter at a time (game session + heartbeat may call it concurrently).

3. TCP Socket Programming

  • Server: ServerSocket.accept() (blocking OS call) returns a Socket per client.
  • Client: Socket(host, port) opens a TCP connection to the server.
  • Data flows as newline-delimited JSON strings over PrintWriter / BufferedReader.

4. Shared Resource Management

  • ConcurrentHashMap-backed Set<ClientHandler> tracks active connections safely.
  • Timer tasks in GameSession are cancelled on game end to prevent resource leaks.

5. Queue Management

  • MatchmakingQueue uses a LinkedList (FIFO) with position broadcast after every add/remove.
  • Queue positions are re-numbered and pushed to clients in real time.

6. Concurrent Game Sessions

  • When 4 clients join, two GameSession threads start simultaneously — each with its own independent board, turn state, and timer.

🔌 Communication Protocol

All messages are JSON objects sent as single newline-delimited lines:

{ "type": "LOGIN",        "payload": { "name": "Alice" } }
{ "type": "JOIN_QUEUE",   "payload": {} }
{ "type": "QUEUE_UPDATE", "payload": { "position": 2, "total": 3 } }
{ "type": "MATCH_FOUND",  "payload": { "opponent": "Bob", "symbol": "X" } }
{ "type": "MOVE",         "payload": { "position": 4 } }
{ "type": "BOARD_UPDATE", "payload": { "board": "X_O_X____", "turn": "O" } }
{ "type": "GAME_OVER",    "payload": { "result": "WIN", "winner": "Alice" } }
{ "type": "CHAT",         "payload": { "sender": "Alice", "text": "gg!" } }
{ "type": "STATS_UPDATE", "payload": { "wins": 3, "losses": 1, "draws": 0 } }

🚀 How to Compile and Run

Prerequisites

  • Java 11 or higher (java -version)
  • lib/gson-2.10.1.jar (included / downloaded by setup)

Step 1 — Build

build.bat

Compiles all .java files into out/.

Step 2 — Start the Server

run_server.bat

The server prints its LAN IP and port. Example output:

[23:18:32] ╔══════════════════════════════════════════╗
[23:18:32] ║   Tic Tac Toe Game Server  [OS Lab]      ║
[23:18:32] ║  LAN IP  : 192.168.1.105                 ║
[23:18:32] ║  Port    : 5555                          ║
[23:18:32] ╚══════════════════════════════════════════╝

Step 3 — Start Clients

Open multiple terminals and run:

run_client.bat
  • Enter the server's IP shown above.
  • Enter a username and click Join Queue.
  • After 2 players join, a match starts automatically.

Testing with 4+ clients

Run run_client.bat 4 times simultaneously → 2 matches start concurrently.


🧩 Threading Model

Main Thread (Server)
  └─ acceptLoop()  →  [Thread: Client-192.168.1.x]  ← ClientHandler #1
                   →  [Thread: Client-192.168.1.y]  ← ClientHandler #2
                   →  [Thread: Client-192.168.1.z]  ← ClientHandler #3
                   →  [Thread: Client-192.168.1.w]  ← ClientHandler #4
                             │
                   MatchmakingQueue (ReentrantLock)
                             │ tryCreateMatch()
                             ▼
                   [Thread: Session-Alice-vs-Bob]    ← GameSession #1
                   [Thread: Session-Carol-vs-Dave]   ← GameSession #2

Client Side:
  EDT (Swing)           ← all GUI operations
  [Thread: NetworkReader] ← reads from socket, dispatches to EDT

📈 Future Improvements

  • Spectator mode (observe live matches)
  • Leaderboard screen with top-10 players
  • Server-side replay recording
  • TLS encryption for socket communication
  • Web-based GUI alternative (React/WebSocket bridge)
  • Room-based matchmaking (create/join named rooms)

🛠️ Technologies Used

Technology Purpose
Java 11+ Core language
Java Swing GUI framework
TCP Sockets (ServerSocket / Socket) Network transport
ReentrantLock Queue synchronization
synchronized Board / send mutual exclusion
ConcurrentHashMap Thread-safe client registry
Gson 2.10.1 JSON serialization
Timer / TimerTask Inactivity timeout

📸 Screenshots

(Run the application and add screenshots to this folder)


Built for the Operating Systems Lab — demonstrating real-world OS concepts through a working multiplayer game.

About

OS Lab Project based on client server communication

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages