BitBully 0.0.56-a6
Loading...
Searching...
No Matches
bitbully.board.Board Class Reference

Public Member Functions

None __init__ (self, Sequence[Sequence[int]]|Sequence[int]|str|None board=None)
bool __eq__ (self, object value)
bool __ne__ (self, object value)
str __repr__ (self)
list[Boardall_positions (self, int up_to_n_ply, bool exactly_n)
bool can_win_next (self, int|None move=None)
Board copy (self)
int count_tokens (self)
list[int] get_legal_moves (self)
bool has_win (self)
int __hash__ (self)
bool is_legal_move (self, int move)
Board mirror (self)
int moves_left (self)
bool play_move (self, int move)
Board|None play_move_on_copy (self, int move)
bool set_board (self, list[list[int]]|list[int] board)
list[list[int]] to_array (self)
str to_string (self)
int uid (self)
None reset (self)

Static Public Member Functions

None random_board (int n_ply, bool forbid_direct_win)

Protected Attributes

 _board = bitbully_core.BoardCore()

Detailed Description

Represents the state of a Connect Four board. Mostly a thin wrapper around BoardCore.

Definition at line 10 of file board.py.

Constructor & Destructor Documentation

◆ __init__()

None bitbully.board.Board.__init__ ( self,
Sequence[Sequence[int]] | Sequence[int] | str | None board = None )
Initializes a Board instance.

Args:
    board (Sequence[Sequence[int]] | Sequence[int] | str | None):
        Optional initial board state. Accepts:
        - 2D array (list, tuple, numpy-array) with shape 7x6 or 6x7
        - 1D sequence of ints: a move sequence of columns (e.g., [0, 0, 2, 2, 3, 3])
        - String: A move sequence of columns as string (e.g., "002233...")
        - None for an empty board

Example:
    Here we have an example:
    ``` py
    board = Board()  # Empty board

    board = Board([[0] * 6 for _ in range(7)])  # empty board as 2D list
    ```

    It is possible to write some text inbetween the examples.

Example:
    And here we have another example:
    ``` py
    board = Board("002233...")  # String
    ```

Definition at line 13 of file board.py.

Member Function Documentation

◆ __eq__()

bool bitbully.board.Board.__eq__ ( self,
object value )
Checks equality between two Board instances.

Args:
    value (object): The other Board instance to compare against.

Returns:
    bool: True if both boards are equal, False otherwise.

Raises:
    NotImplementedError: If the other value is not a Board instance.

Definition at line 43 of file board.py.

◆ __hash__()

int bitbully.board.Board.__hash__ ( self)
Returns a hash of the Board instance for use in hash-based collections.

Returns:
    int: The hash value of the Board instance.

Definition at line 138 of file board.py.

◆ __ne__()

bool bitbully.board.Board.__ne__ ( self,
object value )
Checks inequality between two Board instances.

Args:
    value (object): The other Board instance to compare against.

Returns:
    bool: True if both boards are not equal, False otherwise.

Definition at line 59 of file board.py.

◆ __repr__()

str bitbully.board.Board.__repr__ ( self)
Returns a string representation of the Board instance.

Definition at line 70 of file board.py.

◆ all_positions()

list[Board] bitbully.board.Board.all_positions ( self,
int up_to_n_ply,
bool exactly_n )
Finds all positions on the board up to a certain ply.

Args:
    up_to_n_ply (int): The maximum ply depth to search.
    exactly_n (bool): If True, only returns positions at exactly N ply.

Returns:
    list[Board]: A list of Board instances representing all positions.

Definition at line 74 of file board.py.

◆ can_win_next()

bool bitbully.board.Board.can_win_next ( self,
int | None move = None )
Checks if the current player can win in the next move.

Args:
    move (int | None): Optional column to check for an immediate win. If None, checks all columns.

Returns:
    bool: True if the current player can win next, False otherwise.

Definition at line 88 of file board.py.

◆ copy()

Board bitbully.board.Board.copy ( self)
Creates a copy of the current Board instance.

Returns:
    Board: A new Board instance that is a copy of the current one.

Definition at line 101 of file board.py.

◆ count_tokens()

int bitbully.board.Board.count_tokens ( self)
Counts the total number of tokens on the board.

Returns:
    int: The total number of tokens.

Definition at line 111 of file board.py.

◆ get_legal_moves()

list[int] bitbully.board.Board.get_legal_moves ( self)
Returns a list of legal moves (columns) that can be played.

Returns:
    list[int]: A list of column indices (0-6) where a move can be played.

Raises:
    NotImplementedError: If the method is not implemented yet.

Definition at line 119 of file board.py.

◆ has_win()

bool bitbully.board.Board.has_win ( self)
Checks if the current player has a winning position.

Returns:
    bool: True if the current player has a winning position (4-in-a-row), False otherwise.

Definition at line 130 of file board.py.

◆ is_legal_move()

bool bitbully.board.Board.is_legal_move ( self,
int move )
Checks if a move (column) is legal.

Args:
    move (int): The column index (0-6) to check.

Returns:
    bool: True if the move is legal, False otherwise.

Definition at line 146 of file board.py.

◆ mirror()

Board bitbully.board.Board.mirror ( self)
Returns a new Board instance that is the mirror image of the current board.

Returns:
    Board: A new Board instance that is the mirror image.

Definition at line 157 of file board.py.

◆ moves_left()

int bitbully.board.Board.moves_left ( self)
Returns the number of moves left until the board is full.

Returns:
    int: The number of moves left (0-42).

Definition at line 167 of file board.py.

◆ play_move()

bool bitbully.board.Board.play_move ( self,
int move )
Plays a move (column) for the current player.

Args:
    move (int): The column index (0-6) where the token should be placed.

Returns:
    bool: True if the move was played successfully, False if the move was illegal.

Definition at line 175 of file board.py.

◆ play_move_on_copy()

Board | None bitbully.board.Board.play_move_on_copy ( self,
int move )
Plays a move on a copy of the current board and returns the new board.

Args:
    move (int): The column index (0-6) where the token should be placed.

Returns:
    Board | None: A new Board instance with the move played, or None if the move was illegal.

Definition at line 186 of file board.py.

◆ random_board()

None bitbully.board.Board.random_board ( int n_ply,
bool forbid_direct_win )
static
Generates a random board state by playing a specified number of random moves.

Args:
    n_ply (int): The number of random moves to play on the board.
    forbid_direct_win (bool): If True, the board will have a state that would result in an immediate win.

Definition at line 243 of file board.py.

◆ reset()

None bitbully.board.Board.reset ( self)
Resets the board to an empty state.

Definition at line 252 of file board.py.

◆ set_board()

bool bitbully.board.Board.set_board ( self,
list[list[int]] | list[int] board )
Sets (overrides) the board to a specific state.

Args:
    board (list[list[int]] | list[int]):
        The new board state. Accepts:
        - 2D array (list, tuple, numpy-array) with shape 7x6 or 6x7
        - 1D sequence of ints: a move sequence of columns (e.g., [0, 0, 2, 2, 3, 3])
        - String: A move sequence of columns as string (e.g., "002233...")

Returns:
    bool: True if the board was set successfully, False otherwise.

Definition at line 200 of file board.py.

◆ to_array()

list[list[int]] bitbully.board.Board.to_array ( self)
Returns the board state as a 2D array (list of lists).

Returns:
    list[list[int]]: A 2D list representing the board state.

Definition at line 218 of file board.py.

◆ to_string()

str bitbully.board.Board.to_string ( self)
Returns a string representation of the board to print on the command line.

Returns:
    str: A string representing the board (e.g., "002233...").

Definition at line 226 of file board.py.

◆ uid()

int bitbully.board.Board.uid ( self)
Returns a unique identifier for the current board state.

Returns:
    int: A unique integer identifier for the board state.

Definition at line 234 of file board.py.

Member Data Documentation

◆ _board

bitbully.board.Board._board = bitbully_core.BoardCore()
protected

Definition at line 41 of file board.py.


The documentation for this class was generated from the following file: