gomoku module

class gomoku.Gomoku(parent=None)[source]

Bases: State

Class for states of Gomoku game.

Attributes:
M (int):

number of rows in the board, defaults to 15.

N (int):

number of columns in the board, defaults to 15.

SYMBOLS (List):

list of strings representing stone symbols (black, white) or "." for empty cell.

M = 15
N = 15
SYMBOLS = ['○', '+', '●']
__init__(parent=None)[source]

Constructor of State instances. Should be called (in the first line) in subclasses’ constructors as: super().__init__(parent).

Args:
parent (State):

reference to parent state object.

static class_repr()[source]

Returns a string representation of class Gomoku (meant to instantiate states of Gomoku game), informing about the size of board.

Returns:

str: string representation of class Gomoku (meant to instantiate states of Gomoku game), informing about the size of board.

__str__()[source]

Returns a string representation of this Gomoku state - the contents of its game board.

Returns:

str: string representation of this Gomoku state - the contents of its game board.

take_action_job(action_index)[source]

Places a stone onto the crossing of the board indicated by the action_index (row: action_index // Gomoku.N, column: action_index % Gomoku.N) and returns True if the action is legal (crossing was not occupied). Otherwise, does no changes and returns False.

Args:
action_index (int):

index of crossing where to place a stone.

Returns:
action_legal (bool):

boolean flag indicating if the specified action was legal and performed.

compute_outcome_job()[source]

Computes and returns the game outcome for this state in compliance with rules of Gomoku game: {-1, 1} denoting a win for the minimizing or maximizing player, respectively, if he connected exactly 5 his stones (6 or more do not count for a win); 0 denoting a tie, when the board is filled and no line of 5 exists; None when the game is ongoing.

Returns:
outcome ({-1, 0, 1} or None)

game outcome for this state.

static compute_outcome_job_numba_jit(M, N, turn, last_i, last_j, board)[source]

Called by compute_outcome_job for faster outcomes.

take_random_action_playout()[source]

Picks a uniformly random action from actions available in this state and returns the result of calling take_action with the action index as argument.

Returns:
child (State):

result of take_action call for the random action.

get_board()[source]

Returns the board of this state (a two-dimensional array of bytes).

Returns:
board (ndarray[np.int8, ndim=2]):

board of this state (a two-dimensional array of bytes).

get_extra_info()[source]

[To be implemented in subclasses only when a search using MCTSNC is planned. Not required for MCTS searches.]

Should return additional information associated with this state (as a one-dimensional array of bytes) not implied by the contents of the board itself (e.g., possibilities of castling or en-passant captures in chess, the contract in double dummy bridge, etc.), or any technical information useful to generate legal actions faster. If no additional information is needed should return None.

Returns:
extra_info (ndarray[np.int8, ndim=1] or None):

one-dimensional array with any additional information associated with this state.

static action_name_to_index(action_name)[source]

Returns an action’s index (numbering from 0) based on its name. E.g., name "B4" for 15 x 15 Gomoku maps to index 18.

Args:
action_name (str):

name of an action.

Returns:
action_index (int):

index corresponding to the given name.

static action_index_to_name(action_index)[source]

Returns an action’s name based on its index (numbering from 0). E.g., index 18 for 15 x 15 Gomoku maps to name "B4".

Args:
action_index (int):

index of an action.

Returns:
action_name (str):

name corresponding to the given index.

static get_board_shape()[source]

Returns a tuple with shape of boards for Gomoku game.

Returns:
shape (tuple(int, int)):

shape of boards related to states of this class.

static get_extra_info_memory()[source]

Returns amount of memory (in bytes) needed to memorize additional information associated with Gomoku states - currently 0 (no such information).

Returns:
extra_info_memory (int):

number of bytes required to memorize additional information associated with Gomoku states.

__module__ = 'gomoku'
static get_max_actions()[source]

Returns the maximum number of actions (the largest branching factor) equal to the product: Gomoku.M * Gomoku.N.

Returns:
max_actions (int):

maximum number of actions (the largest branching factor) equal to the product: Gomoku.M * Gomoku.N.