Skip to main content

A fast, reusable Python engine for Hexo on an infinite hex grid.

Project description

Ruff License

Hexo Engine

Hexo is a Python engine for the Hexo game played on an infinite hex grid.

The goal of this repository is to provide a clean, reusable engine API (similar in spirit to python-chess) so others can build bots, analysis tools, and frontends without re-implementing game logic.

Game rules are documented in RULES.md. Agent handoff context is documented in AGENT_CONTEXT.md.

Features

  • Deterministic game engine with strict rule validation
  • Infinite hex-grid coordinate model
  • Opening and turn rules enforced by the engine
  • Win detection (connect 6 on any hex axis)
  • Undo support for search algorithms

Installation

Requirements

  • UV package manager

Clone the repository

  git clone https://github.com/<your-org>/hexo.git
  cd hexo

Initialize your environment

  uv sync
  uv run pre-commit install

API

Public entry point is the Hexo class.

  • Hexo.new(config=None) -> Hexo: create a new game with P1 already placed at (0, 0).
  • Hexo.from_state(state, config=None) -> Hexo: restore a game from serialized state.
  • game.to_state() -> dict: serialize committed turns and any in-progress partial turn.
  • game.turn() -> Player: return the player currently placing stones.
  • game.status() -> GameStatus: return ONGOING, P1_WON, or P2_WON.
  • game.moves_left_in_turn() -> int: return remaining moves in current turn (2 or 1).
  • game.pending_moves() -> tuple[Coord, ...]: return moves already made in the current turn.
  • game.is_legal_move(coord) -> tuple[bool, str | None]: validate one submove.
  • game.legal_moves() -> tuple[Coord, ...]: return legal single-move candidates.
  • game.push(coord) -> TurnRecord | None: play one move; returns None if turn is still partial, or TurnRecord when the turn completes (or wins early).
  • game.is_legal(move) -> tuple[bool, str | None]: validate a full 2-stone move (only when no partial turn is active).
  • game.play(move) -> TurnRecord: convenience wrapper that places two stones in sequence.
  • game.undo() -> TurnRecord: undo the last placement (works for both partial and completed turns).
  • game.at(coord) -> Player | None: inspect occupancy at a coordinate.
  • game.board() -> dict[Coord, Player]: get a snapshot of all occupied coordinates.

Notes:

  • Hexo.new() starts with P1 already placed at (0, 0).
  • Every played turn is a 2-stone move.

Quickstart

from hexo import Hexo

game = Hexo.new()
print(game.turn())  # Player.P2

legal, reason = game.is_legal_move((1, 0))
if not legal:
    raise ValueError(reason)
game.push((1, 0))

# Hook point for engines: run a search after first placement.
# ... search code here ...

record = game.push((0, 1))  # turn completes here
print(record)  # TurnRecord
print(game.moves_left_in_turn())  # 2

state = game.to_state()
restored = Hexo.from_state(state)
print(restored.turn())

Tests, linting and formatting

uv run pytest
uvx ruff check . --fix
uvx ruff format .

Run all hooks manually:

uv run pre-commit run --all-files

Benchmarks

Benchmarking uses pytest-benchmark and is kept separate from normal tests.

Run benchmarks:

uv run pytest benchmarks --benchmark-only

Save a baseline:

uv run pytest benchmarks --benchmark-only --benchmark-save=baseline

Compare with a saved baseline:

uv run pytest benchmarks --benchmark-only --benchmark-compare=baseline

Contributing

See CONTRIBUTING.md for:

  • issue reporting guidelines
  • feature proposal flow
  • pull request requirements
  • code style and testing expectations

License

This project is licensed under the MIT LICENSE

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

hexo-0.1.0.tar.gz (49.8 kB view details)

Uploaded Source

Built Distribution

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

hexo-0.1.0-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file hexo-0.1.0.tar.gz.

File metadata

  • Download URL: hexo-0.1.0.tar.gz
  • Upload date:
  • Size: 49.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hexo-0.1.0.tar.gz
Algorithm Hash digest
SHA256 025473aad0d73f0c890fb7b8ec1ed671c5e4c7c3e8a65bafd00146f89f8d495d
MD5 2819b8d8a64b6bcc83acd34b26ccdb8b
BLAKE2b-256 4772e4da49b746283bddc6ea51fcd8148332af23bacea3ccc96900fade3f4a9d

See more details on using hashes here.

Provenance

The following attestation bundles were made for hexo-0.1.0.tar.gz:

Publisher: python-publish.yml on PierreLapolla/hexo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file hexo-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: hexo-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 10.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for hexo-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fe3cd3222d8645dd2e4a5ff544f82643416b1174543a3fce3560e2c3086f3d6c
MD5 49127ddb4deb1f47bb3caa77509a9526
BLAKE2b-256 d9e9c88d4dec4d8f04e5434595ed60f94f368cf23b32ca4c30409b87e914cfd8

See more details on using hashes here.

Provenance

The following attestation bundles were made for hexo-0.1.0-py3-none-any.whl:

Publisher: python-publish.yml on PierreLapolla/hexo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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