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.3.tar.gz (7.8 kB view details)

Uploaded Source

Built Distributions

hexea-0.2.3-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl (122.0 kB view details)

Uploaded CPython 3.11 manylinux: glibc 2.24+ x86-64 manylinux: glibc 2.28+ x86-64

hexea-0.2.3-cp311-cp311-macosx_10_9_universal2.whl (217.8 kB view details)

Uploaded CPython 3.11 macOS 10.9+ universal2 (ARM64, x86-64)

File details

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

File metadata

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

File hashes

Hashes for hexea-0.2.3.tar.gz
Algorithm Hash digest
SHA256 9e10f149922afe145491609746bbb1b68e2b154ae7735587851e7433f6356e0a
MD5 e4756743ae3bfc8d55249e19f2a3e714
BLAKE2b-256 891ed9ca246c5e9e3ce0d134e1b7bd44164bdac1cf5a1baabe5b51ca0029a919

See more details on using hashes here.

File details

Details for the file hexea-0.2.3-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for hexea-0.2.3-cp311-cp311-manylinux_2_24_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 71ea1f5343d4c8840859a936de99c7d01138ed00fb9f125937f47c88757abec3
MD5 ace8e847e7aaccf5ba3398ae1561ce78
BLAKE2b-256 22b1c8cfb2cd0a4044bcc0d608fddf0cdba348a1b638ebd3a18d1f6066bf16aa

See more details on using hashes here.

File details

Details for the file hexea-0.2.3-cp311-cp311-macosx_10_9_universal2.whl.

File metadata

File hashes

Hashes for hexea-0.2.3-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 7fc17ab35aadc13c08ca902d2ac44bb039acf5533b84566821ea548e87299856
MD5 4d268713c9eaa7bd519f3a332987e655
BLAKE2b-256 9eafa72f0cf0135dcdbb471f7faf7eb8f0285078bffd7226e362067738f1662b

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