Reference¶
- class hexea.Board(*args, **kwargs)¶
Protocol defining a game board.
Yboard
andHexboard
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 areMarker
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 callingBoard.get_dict()
after each one, then returning the results in a list. The main difference is that each dictionary in the list also contains awinner
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 betweenMarker.blue
andMarker.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 eitherMarker.red
orMarker.blue
.
- get_winner() Marker ¶
- Returns:
the winner of the current board
Returns a
Marker
representing the current winning player, orMarker.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. ThrowsIndexError
if the specified column and row do not exist, andValueError
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 toTrue
(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. Ifquick
is set toFalse
, 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 areMarker.red
andMarker.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 asize
xsize
board.