Skip to main content

Python library for the games of Hex and Y

Project description

Hexea

This is a simple library for working with the closely related connection games of Y and Hex. This is not meant to be a standalone, playable game, but rather a set of tools one could use to implement such a game, train a machine learning model to play a game, and so forth.

Basic Usage

All of the functionality currently available in the hexea module is in the Yboard and Hexboard classes, which both implement the Board protocol. Yboard is implemented as a C++ extension using pybind11. Hexboard is a Python wrapper that uses a Yboard under the hood, since Hex is a special case of Y, and a Y board can be converted into a Hex board by selectively filling in hexes.

The hexea module also provides a Marker class, which is an enumeration class for representing the type of game marker inhabiting a hex: Marker.red, Marker.blue, or Marker.none. The __str__ method of Marker has been overridden to return a character representing the marker: X, O, or ., respectively.

The following methods are available for both Yboard and Hexboard:

get_next_player()

Returns a Marker indicating which player is up next: Marker.red for an empty board, and then alternating between Marker.blue and Marker.red as moves are played. Note that this does not change even after a player has won; it is possible to keep placing moves as long as there are empty hexes, and even when the board is filled up, get_next_player() will return either Marker.red or Marker.blue. If this behavior turns out to be undesirable in practice, code may be added in a future version to return Marker.none whenever the board represents a winning position for either player.

move(col: int, row:int)

Places a Marker representing the next player at the specified column and row. Throws IndexError if the specified column and row do not exist, and ValueError if the specified location is already occupied.

move() returns self in order to allow daisy chaining like so:

b = (
    Hexboard(3)
    .move(0, 1)
    .move(1, 0)
    .move(1, 1)
`)

get_free_hexes()

Returns a list of 2-tuples, each of which contains the column and row of a hex that is not currently occupied.

get_winner()

Returns a Marker representing the current winning player, or Marker.none if the board is not yet in a winning state.

random_playout()

Performs a single random playout. Starting with the current board state, markers for each player are alternately placed on a random hex until no empty hexes are left. Does not stop when a winning position has been reached. This is in the interest of speed: Random playouts are typically done hundreds or thousands at a time, and checking for a winner after each turn would be slower than just filling up the board.

random_playouts_won(num_playouts: int)

Starting with the current board position, runs num_playouts random playouts. Returns a dictionary whose keys are Marker.red and Marker.blue, and whose values are the number of wins each player has.

Since neither Hex nor Y can end in a draw, there is never a key for Marker.none.

get_dict()

Gets a dictionary whose keys are all strings representing a cell on the board (e.g. cell0,0) and whose values are Marker objects indicating the occupant of the cell.

This is admittedly an esoteric method. I once used something similar when building a Y-playing AI using genetic programming: Each gene represented a function that performed mathematical operations on the values of cells, and was evaluated using string subtitution (replacing cell0,0 with the integer value of that cell). There are undoubtedly better ways to do this, so expect this method to change or be deprecated soon.

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

hexea-0.2.1.tar.gz (7.5 kB view details)

Uploaded Source

Built Distributions

hexea-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (141.4 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

hexea-0.2.1-cp311-cp311-macosx_13_0_x86_64.whl (111.4 kB view details)

Uploaded CPython 3.11 macOS 13.0+ x86-64

File details

Details for the file hexea-0.2.1.tar.gz.

File metadata

  • Download URL: hexea-0.2.1.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for hexea-0.2.1.tar.gz
Algorithm Hash digest
SHA256 5f18ecfd5947034aa7dfd739343de85463c1cc3272cd416009e5ee7bd14b8b2c
MD5 a7d2240adfd70f067a2275b28a4b24a2
BLAKE2b-256 c31f6d2740aa39a35a9dc2db67cfb0ad19126950e9b277365bdd77f67fa33739

See more details on using hashes here.

File details

Details for the file hexea-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for hexea-0.2.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 0ed1da8860754b04d0d1c484dc85775016bb51b0b9a0453d558dd8e0dffbc0a2
MD5 057222b90e94663c03ed1af800a4c93b
BLAKE2b-256 7e2d43a093a194fbefe2c2a9607869da7c3da089b93f252ce06b027d6ded8a8e

See more details on using hashes here.

File details

Details for the file hexea-0.2.1-cp311-cp311-macosx_13_0_x86_64.whl.

File metadata

File hashes

Hashes for hexea-0.2.1-cp311-cp311-macosx_13_0_x86_64.whl
Algorithm Hash digest
SHA256 d2af2fc0f7864f96e9eb22a01b48c4dbace12bb4ac9a9baee9e8b7c2fcb26500
MD5 4649fce951730c2e4658bd41e7efa07c
BLAKE2b-256 a5c92d58353ac270944d713a1566ff69aca2b1da1391ed48572603a10a4e0cda

See more details on using hashes here.

Supported by

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