Code package#

Code.MCTS module#

Code.Board module#

class Code.Board_script.Board[source]#

Bases: object

A class representing a Tic-Tac-Toe board.

Attributes#

gridlist of list of int

The game board, a 3x3 grid where 1 represents player X, -1 represents player O, and 0 represents an empty space.

moveslist of tuple

A list of moves made in the game, where each move is represented by a tuple (row, column, player).

gameStateint

The current state of the game: 0 for ongoing, 1 for player X win, -1 for player O win, and 9 for a draw.

winningMovetuple

The winning move and its direction, if applicable.

available_movesset of tuple

A set of available moves, each represented by a tuple (row, column).

copy()[source]#

Creates a deep copy of the current board state.

Returns#

Board

A new Board instance with the copied state.

display(addSpace=False)[source]#

Displays the entire board state in the console.

Parameters#

addSpacebool, optional

If True, adds an extra newline after displaying the board. Default is False.

display_cell(i, j)[source]#

Prints the content of a specified cell on the board.

Parameters#

iint

The row index of the cell.

jint

The column index of the cell.

display_player_name(player)[source]#

Returns the display name of the player.

Parameters#

playerint

The player identifier (1 for ‘X’, -1 for ‘O’).

Returns#

str

The display name of the player.

get_representation()[source]#

Generates a string representation of the entire board state.

Returns#

str

A string representing the board’s current state.

get_square_representation(i, j)[source]#

Returns a string representation of a cell’s content.

Parameters#

iint

The row index of the cell.

jint

The column index of the cell.

Returns#

str

The content of the cell represented as ‘X’, ‘O’, or ‘*’ for an empty cell.

isGameOver()[source]#

Checks if the game is over.

Returns#

bool

True if the game is over, False otherwise.

play(i, j, player)[source]#

Plays a move on the board.

Parameters#

iint

The row index for the move.

jint

The column index for the move.

playerint

The identifier of the player making the move.

Raises#

Exception

If the game is over or the cell is not empty.

playerWins_column(i, j, player)[source]#

Checks if a player wins by completing a column.

Parameters#

iint

The row index of the last move.

jint

The column index of the last move.

playerint

The identifier of the player who made the last move.

Returns#

bool

True if the player wins by completing a column, False otherwise.

playerWins_diag(player)[source]#

Checks if a player wins by completing a diagonal.

Parameters#

playerint

The identifier of the player.

Returns#

tuple

A tuple containing a boolean indicating if a player wins and an integer indicating which diagonal.

playerWins_row(i, j, player)[source]#

Checks if a player wins by completing a row.

Parameters#

iint

The row index of the last move.

jint

The column index of the last move.

playerint

The identifier of the player who made the last move.

Returns#

bool

True if the player wins by completing a row, False otherwise.

undo_last_move(return_move=False)[source]#

Undoes the last move made on the board.

Parameters#

return_movebool, optional

If True, returns the last move made. Default is False.

Returns#

tuple, optional

The last move made (i, j, player) if return_move is True. Otherwise, nothing is returned.

updateGameState(i, j, player)[source]#

Updates the game state after a move.

Parameters#

iint

The row index of the last move.

jint

The column index of the last move.

playerint

The identifier of the player who made the last move.

class Code.Board_script.direction(value, names=None, *values, module=None, qualname=None, type=None, start=1, boundary=None)[source]#

Bases: Enum

COL = 2#
DIAG_BOTTOM_TOP = 4#
DIAG_TOP_BOTTOM = 3#
ROW = 1#

Module contents#