Reference

class hexea.Board(*args, **kwargs)

Protocol defining a game board. Yboard and Hexboard both implement this protocol.

get_dict() Dict[str, Marker]
Returns:

a dictionary indicating which marker occupies each hex

Returns 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.

get_free_hexes() List[Tuple[int, int]]
Returns:

A list of tuples indicating the column and row of all 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_list_of_dicts(num_playouts: int, quick: bool = True) List[Dict[str, Marker]]
Parameters:
  • num_playouts – number of playouts to run

  • quick – boolean indicating whether quick playouts are desired

Returns:

a list of dictionaries indicating how many wins each player had, and who won

This is essentially the equivalent of running num_playouts random playouts and calling Board.get_dict() after each one, then returning the results in a list. The main difference is that each dictionary in the list also contains a winner item indicating who won. This can be useful for creating Pandas dataframes from Hex and Y boards via the .from_records() method, for example to train ML models for evaluating positions.

get_next_player() Marker
Returns:

the player who is allowed to move next

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.

get_winner() Marker
Returns:

the winner of the current board

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

move(col: int, row: int) Self
Parameters:
  • col – column of the hex to move to

  • row – row of the hex to move to

Returns:

self (for method chaining)

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.

random_playout(quick: bool = True) Self
Parameters:

quick – boolean indicating whether quick playouts are desired

Returns:

self, after the playout has been run

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.

If quick is set to True (the default), a quick playout is generated. This does not check for wins between turns, which means that the resulting board will be completely full. If quick is set to False, the function will return a result as soon as a winning position is reached.

random_playouts_won(num_playouts: int, quick: bool = True) Dict[Marker, int]
Parameters:
  • num_playouts – number of playouts to run

  • quick – boolean indicating whether quick playouts are desired

Returns:

a dictionary indicating how many wins each player had

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.

class hexea.Hexboard(size)
Parameters:

size – desired board size

Implementation of the Board protocol representing a Hex board. The constructor will create a size x size board.

class hexea.Marker

Enumeration for the possible contents of a hex

Can be one of three values: red, corresponding to the integer 1; blue, corresponding to the integer -1; or none, corresponding to the integer 0.

Members:

red

blue

none

property name
class hexea.Yboard
Parameters:

size – desired board size

Implementation of the Board protocol representing a Y board. The constructor will create a board of size hexes on each side.