c4 module

class c4.C4(parent=None)[source]

Bases: State

Class for states of Connect 4 game.

Attributes:
M (int):

number of rows in the board, defaults to 6.

N (int):

number of columns in the board, defaults to 7.

SYMBOLS (List):

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

M = 6
N = 7
SYMBOLS = ['○', '.', '●']
__init__(parent=None)[source]

Constructor (ordinary or copying) of C4 instances - states of Connect 4 game.

Args:
parent (State):

reference to parent state object.

static class_repr()[source]

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

Returns:

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

__str__()[source]

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

Returns:

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

take_action_job(action_index)[source]

Drops a disc into column indicated by the action_index and returns True if the action is legal (column not full yet). Otherwise, does no changes and returns False.

Args:
action_index (int):

index of column where to drop a disc.

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 Connect 4 game: {-1, 1} denoting a win for the minimizing or maximizing player, respectively, if he connected at least 4 his discs; 0 denoting a tie, when the board is filled and no line of 4 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]

Returns additional information associated with this state, as one-dimensional array of bytes, informing about fills of columns (how many discs have been dropped in each column).

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

one-dimensional array with additional information associated with this state - fills of columns.

static action_name_to_index(action_name)[source]

Returns an action’s index (numbering from 0) based on its name. E.g., name "0", denoting a drop into the leftmost column, maps to index 0.

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 0 maps to name "0", denoting a drop into the leftmost column.

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 Connect 4 game.

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

shape of boards related to states of this class.

__module__ = 'c4'
static get_extra_info_memory()[source]

Returns amount of memory (in bytes) needed to memorize additional information associated with Connect 4 states, i.e., the memory for fills of columns. That number is equal to the number of columns.

Returns:
extra_info_memory (int):

number of bytes required to memorize fills of columns.

static get_max_actions()[source]

Returns the maximum number of actions (the largest branching factor) equal to the number of columns.

Returns:
max_actions (int):

maximum number of actions (the largest branching factor) equal to the number of columns.