Skip to main content

A clean, extensible Python library for communicating with Gomoku engines via the Gomocup protocol.

Project description

PyGomo

PyGomo is a clean, modern, and extensible Python library for communicating with Gomoku engines via the Gomocup protocol. It is designed for developers, researchers, and AI enthusiasts who need a robust interface to interact with Gomoku AI agents.

PyPI version License: MIT Python 3.8+


🚀 Features

  • Robust Engine Management: Automatically handles engine subprocesses (start, stop, restart).
  • Gomocup Protocol Support: Full implementation of the standard Gomocup protocol (START, TURN, BOARD, INFO, etc.).
  • Real-time Search Info: Parse and consume engine search data (depth, winrate, PV) in real-time via callbacks.
  • Rich Data Models: Strongly typed models for Move, BoardPosition, and SearchInfo.
  • Extensible Architecture: Built-in hook system and command registry for customizing behavior.
  • Board Library: High-performance BitBoard implementation for standard and Renju rules.
  • Console Game: Includes a built-in console-based client for playing against engines.

📦 Installation

Install via pip:

pip install pygomo-lib

Note: The package name on PyPI is pygomo-lib, but the import name is pygomo.

⚡ Quick Start

Here is a minimal example of how to load an engine and play a game.

from pygomo import EngineClient
from pygomo.protocol.models import SearchInfo

# Path to your Gomoku engine executable (e.g., Rapfi, Pela, Yixin)
ENGINE_PATH = "./path/to/engine.exe"

def on_search_info(info: SearchInfo):
    """Callback to print search progress in real-time."""
    print(f"Depth: {info.depth} | Winrate: {info.winrate_percent:.1f}% | PV: {info.pv[:4]}")

# Initialize and connect
with EngineClient(ENGINE_PATH) as engine:
    # 1. Start a new game (Board size 15)
    engine.start(board_size=15)

    # 2. Set strict time control (1 second per turn)
    engine.configure(timeout_turn=1000)

    # 3. Request the engine to play first (Black)
    result = engine.begin(on_info=on_search_info)
    print(f"Engine played: {result.move}")  # e.g., "h8"

    # 4. Send our move (White)
    my_move = "h9"
    result = engine.turn(my_move, on_info=on_search_info)
    print(f"Engine responded: {result.move}")

🎮 Console Game

PyGomo comes with a built-in console CLI for testing engines or playing directly in your terminal.

# Play against an engine
pygomo-console --engine ./engines/rapfi --time 5000

# Play using Renju rules
pygomo-console --engine ./engines/pela --rule renju

Key Commands in Console:

  • move <coord>: Play a move (e.g., h8).
  • undo: Undo the last move.
  • swap: Swap sides (if playing swap rule).
  • info: Show last search info.
  • quit: Exit.

📚 Core Concepts

EngineClient

The EngineClient class is the main entry point. It wraps the raw protocol communications into high-level Python methods like .start(), .turn(), and .board().

Data Models

PyGomo uses dataclasses to represent game entities cleanly:

  • Move: Handles coordinates. Supports (row, col), "h8" (algebraic), and "7,7" (numeric) formats.
  • Evaluate: Parses engine scores, supporting raw values (e.g., "150"), winrates, and mate scores (e.g., "+M5").
  • SearchInfo: Aggregates real-time search data (depth, NPS, nodes, PV).

Board Library

Under pygomo.board, you'll find efficient board representations:

  • BitBoard: 64-bit optimized board for standard Gomoku.
  • RenjuBitBoard: Extends BitBoard with forbidden move logic (3x3, 4x4, overline) for Renju rules.

🔧 Advanced Usage

Hooks

You can inject custom logic into the command lifecycle using hooks.

from pygomo.command import HookType

def log_command(context):
    print(f"Sending command: {context.command}")

engine.hooks.on(HookType.PRE_EXECUTE)(log_command)

Custom Commands

If your engine supports non-standard commands, you can send them raw:

# Send "MY_CUSTOM_CMD arg1"
engine.send_raw("MY_CUSTOM_CMD arg1")
response = engine.receive_raw()

🛠️ Development

To set up a development environment:

  1. Clone the repository.
  2. Install development dependencies:
    pip install -e .[dev]
    
  3. Run tests:
    pytest tests/
    

📄 License

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


Built with ❤️ for the Gomoku community.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pygomo_lib-0.1.1.tar.gz (46.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pygomo_lib-0.1.1-py3-none-any.whl (51.1 kB view details)

Uploaded Python 3

File details

Details for the file pygomo_lib-0.1.1.tar.gz.

File metadata

  • Download URL: pygomo_lib-0.1.1.tar.gz
  • Upload date:
  • Size: 46.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for pygomo_lib-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5e85ec3082ca70807bc216ce651b7ecb8c173a81a4f240addeb5a69066c276d9
MD5 1d4f7a096097538d8c17568222c67bc6
BLAKE2b-256 1ea67679d8e3c55bb959120901ea4b16d46b5179324f72a78959cb2abb3965dc

See more details on using hashes here.

File details

Details for the file pygomo_lib-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: pygomo_lib-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 51.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.11

File hashes

Hashes for pygomo_lib-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6567c2a360c5699f9e07c564ec1446e487fdc63dd27a19c081431163f85b0856
MD5 af4f8bf1af4f355789216767eca7ccd5
BLAKE2b-256 b3568ba5b79c3524a10305477dd715a2a54053545fe01dddd805105252c70b94

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page