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

Uploaded Source

Built Distribution

hexea-0.2.2-cp311-cp311-macosx_10_9_universal2.whl (217.7 kB view details)

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

File details

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

File metadata

  • Download URL: hexea-0.2.2.tar.gz
  • Upload date:
  • Size: 7.7 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.2.tar.gz
Algorithm Hash digest
SHA256 ea61f42cb5185ad8f1170e7ee10e2b3c275419459bf3551d8583f1318a850b98
MD5 7dc847e073be3b57242a44b92c73c7da
BLAKE2b-256 445953b381723ba34c260e8beccb51ff5c8aba67f25c723a34edd975c7ab5796

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for hexea-0.2.2-cp311-cp311-macosx_10_9_universal2.whl
Algorithm Hash digest
SHA256 e2be8562e524099b4eb0c5dfa6900f444c9b2b506a8f719034c7b159f7df8d34
MD5 359b76d6754a8bc316adc2ae7b982ebc
BLAKE2b-256 a936b81f1ece7991ca3babf870d28b8326ae080958e732b7830803701fa7984e

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