Skip to content

kigster/gomoku-cpp-client-server

Repository files navigation

Gomoku C++ Client-Server Architecture

A modern C++ implementation of the Gomoku game with a client-server architecture, featuring WebSocket communication, AI optimization, and a clean separation of concerns.

Architecture Overview

This project has been refactored from a monolithic C application into a modern C++ client-server architecture with the following components:

Shared Library (libgomoku_shared.so)

  • Board: Game board management and validation
  • GameState: Complete game state management
  • AIEngine: Advanced AI with optimizations (transposition tables, killer moves, etc.)
  • JSONSerializer: JSON serialization/deserialization for network communication

Server (gomoku-server)

  • HTTPServer: HTTP request handling and session management
  • WebSocketHandler: Real-time WebSocket communication
  • GameSession: Individual game session management
  • RedisPublisher: Optional Redis integration for pub/sub

Client (gomoku-client)

  • TerminalUI: Console-based user interface
  • WebSocketClient: WebSocket client for real-time updates
  • HTTPClient: HTTP client for game state synchronization
  • GameController: Client-side game orchestration

Features

  • Modern C++20: Leverages latest C++ features and best practices
  • Single Responsibility Principle: Each class has a focused, well-defined purpose
  • Real-time Communication: WebSocket-based real-time updates
  • Advanced AI: Optimized AI engine with multiple search optimizations
  • Stateless Server: Server maintains minimal state, relying on client-provided game state
  • Cross-platform: Works on Linux, macOS, and Windows
  • Optional Redis: Can use Redis for additional pub/sub capabilities

Building

Prerequisites

Install the required dependencies:

Ubuntu/Debian:

make install-deps

macOS:

make install-deps-macos

CentOS/RHEL/Fedora:

make install-deps-rhel

Build Options

Using Make (Recommended):

# Build both server and client
make

# Build with debug flags
make debug

# Enable Redis support
USE_REDIS=1 make

Using CMake:

mkdir build && cd build
cmake ..
make

Running

Start the Server

# Using Make
make run-server

# Or directly
./bin/gomoku-server -p 3030 -b 0.0.0.0

Start the Client

# Using Make
make run-client

# Or directly
./bin/gomoku-client -h localhost -p 3030 -l hard

Game Flow

  1. Client starts: Connects to server via HTTP and WebSocket
  2. Game initialization: Client sends game configuration to server
  3. Human move: Client sends move via HTTP POST to /games/gomoku/play
  4. AI thinking: Server processes move and starts AI calculation
  5. Real-time updates: Server sends AI progress via WebSocket
  6. AI move: Server sends completed AI move via WebSocket
  7. Game continues: Process repeats until game ends

API Endpoints

HTTP Endpoints

  • POST /games/gomoku/start - Start a new game
  • POST /games/gomoku/play - Make a move

WebSocket Messages

  • MOVE_MADE - Move completed notification
  • AI_PROGRESS - AI thinking progress update
  • GAME_OVER - Game completion notification
  • ERROR - Error notification
  • PING - Keep-alive ping

Configuration

Server Configuration

  • -p, --port: Server port (default: 3030)
  • -b, --bind: Bind address (default: 0.0.0.0)
  • -v, --verbose: Enable verbose logging

Client Configuration

  • -h, --host: Server host (default: localhost)
  • -p, --port: Server port (default: 3030)
  • -l, --level: AI difficulty level (easy, medium, hard)
  • -t, --timeout: Move timeout in seconds
  • -s, --size: Board size (15 or 19)

JSON Format

Game State Data

{
  "move": {
    "game_id": "uuid",
    "session_id": "uuid",
    "game_type": "gomoku",
    "name": "Gomoku",
    "status": "in_progress",
    "players": {
      "x": {"name": "Human", "type": "human"},
      "o": {"name": "Computer", "type": "ai"}
    },
    "depth": 5,
    "moves": [
      {"x": [10, 10], "timing": 34.34},
      {"o": [11, 12], "timing": 0.00, "moves_evaluated": 1}
    ],
    "current_player": "o"
  }
}

AI Optimizations

The AI engine includes several advanced optimizations:

  • Transposition Tables: Cache evaluated positions
  • Killer Moves: Prioritize moves that caused beta cutoffs
  • Iterative Deepening: Start with shallow searches and deepen
  • Alpha-Beta Pruning: Efficient search space reduction
  • Move Ordering: Sort moves for better pruning
  • Threat-Space Search: Focus on threatening positions
  • Null-Move Pruning: Skip moves to detect zugzwang

Development

Project Structure

gomoku-cpp/
├── include/           # Header files
│   ├── shared/       # Shared library headers
│   ├── server/       # Server headers
│   └── client/       # Client headers
├── src/              # Source files
│   ├── shared/       # Shared library implementation
│   ├── server/       # Server implementation
│   └── client/       # Client implementation
├── tests/            # Test files
├── build/            # Build artifacts
├── bin/              # Executables
├── lib/              # Libraries
├── CMakeLists.txt    # CMake configuration
└── Makefile          # Make configuration

Testing

# Run tests (when implemented)
make test

Code Style

  • Follow modern C++ best practices
  • Use RAII and smart pointers
  • Prefer const correctness
  • Use meaningful names and documentation
  • Follow the Single Responsibility Principle

Performance

The AI engine is optimized for:

  • Search Depth: Up to 8 plies in reasonable time
  • Move Evaluation: 1000+ positions per second
  • Memory Usage: Efficient transposition table management
  • Network Latency: Minimal overhead for real-time play

Contributing

  1. Fork the repository
  2. Create a feature branch
  3. Make your changes following the coding standards
  4. Add tests for new functionality
  5. Submit a pull request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • libwebsockets: WebSocket library for C/C++
  • jsoncpp: JSON parsing and generation
  • hiredis: Redis client library (optional)
  • Google Test: Testing framework

About

Client/Server Gomoku implementation based on the ANSI C version

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors