Skip to content

Latest commit

History

41 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Distributed-Tic-Tac-Toe

This project implements a distributed client-server Tic Tac Toe game using Java RMI (Remote Method Invocation). Players can connect to a central server, join game sessions, and play against opponents in real-time with automatic matchmaking.

Note

A command-line interface is available in /cli for testing purposes. The GUI version is recommended for end-users.

Features

  • Distributed Architecture: Server handles game logic, clients manage UI
  • Dynamic Session Management: Auto-scales game sessions as players connect
  • Rematch System: Players can restart games after completion
  • Heartbeat Monitoring: Detects disconnected players automatically
  • Modern GUI: Clean interface with game board, status updates, and controls
  • Connection Security: Custom security manager for RMI communication

Project Structure

ClassPurposeKey Features
ServerMainServer entry pointStarts RMI registry, registers primary service
TicTacToeServerSession managerCreates/destroys game sessions, manages player allocation
TicTacToeServiceImplGame logic coreMove validation, win detection, player management
ClientMainClient entry pointLaunches game GUI
TicTacToeGUIPlayer interfaceGame board, status display, input handling
TicTacToeClientClient logicServer communication, game state management
PlayerCallbackImplServer→client commsReal-time updates, heartbeat monitoring
MySecurityManagerSecurity configEnables RMI connections without strict policy files
GameConstantsShared configBoard size, player symbols, game rules

Prerequisites

  • Java JDK 17+
  • Network connectivity between machines
  • RMI ports open (1099 + 1100-1106 by default)

Setup & Execution

1. Compile Code

javac -d bin src/server/*.java src/client/*.java src/common/*.java src/security/*.java

2. Start Server

java -cp bin -Djava.rmi.server.codebase=file:bin/ security.MySecurityManager server.ServerMain 192.168.1.10

Note

Replace 192.168.1.10 with server's actual IP

3. Start Clients

java -cp bin -Djava.rmi.server.codebase=file:bin/ security.MySecurityManager client.ClientMain

Note

Run on player machines, enter server IP when prompted

Game Flow

1. Client Connection

Players connect via the GUI by entering server details:

  • Server IP (e.g., localhost for local testing)
  • Client IP (auto-detected or manually entered)

Screenshot 2025-05-30 111544

Error Handling:

  • Unreachable servers show connection timeout messages

    serverdown


  • Invalid IP formats trigger validation warnings
  • Duplicate player names are rejected

2. Session Matching

  • Server automatically pairs available players
  • New game sessions are created on-demand
  • Players see "Waiting for opponent" status until matched

3. Gameplay Loop

Players take turns making moves on the board: Screenshot 2025-04-26 151818

Features:

  • Turn indication ("YOUR TURN" vs "Opponent's turn")
  • Visual board updates after each move
  • Real-time move validation (prevents invalid placements)

4. Win/Draw Detection

Game automatically detects end conditions:

  • Win: 3-in-a-row patterns
  • Draw: Full board with no winner

drawdraw2


Error Recovery:

  • Network drops trigger automatic reconnection attempts
  • Missing heartbeats mark players as "disconnected"
  • Session timeouts after 30 seconds of inactivity

5. Rematch System

After game completion:

  • Both players get rematch prompts
  • New game starts if both choose "Play Again"
  • Session dissolves if either player quits

6. Graceful Exit

Players can quit anytime via:

  • "Quit Game" button during gameplay
  • Window close (X) button (NOT ON WINDOWS)
  • Console interrupt (Ctrl+C) for CLI clients

Termination Sequence:

  1. Client sends disconnect notification
  2. Server frees game resources
  3. Opponent receives "Player left" notification
  4. Session automatically closes after 60 seconds

Configuration Options

ParameterLocationDescription
MAX_SESSIONSTicTacToeServer.javaMax concurrent game sessions
portBaseTicTacToeServer.javaStarting port for game sessions
sun.rmi.transport.*TicTacToeClient.javaNetwork timeout settings
Color constantsTicTacToeGUI.javaUI color scheme

Key Design Patterns

  • Observer Pattern: Callbacks for game state updates
  • Singleton Pattern: Central server instance
  • Factory Pattern: Dynamic session creation
  • Asynchronous Processing: Non-blocking game operations

Troubleshooting

  • Connection Issues: Verify IPs match server's network interface
  • Port Conflicts: Change portBase in TicTacToeServer.java
  • Session Limits: Increase MAX_SESSIONS if needed
  • Timeout Errors: Adjust sun.rmi.transport.* properties

About

This project implements a distributed client-server Tic Tac Toe game using Java RMI (Remote Method Invocation). Players can connect to a central server, join game sessions, and play against opponents in real-time with automatic matchmaking.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages