chessengine.bitboard#
Note
Read Internal Board Representation to know how chessengine represents the chess board internally and for a list of attributes available. It also details how you should specify different sides and pieces of the board when you pass them as arguments to any function.
A complete bitboard representation of a chessboard, with all the methods needed to play a game of chess.
- class chessengine.bitboard.Board(side: str)#
A class implementing a bitboard representation of a chess board. A particular bitboard can be accessed via the get_bitboard method or as an attribute with the name <side>_<piece>s. For example,
white_pawns
,black_bishops
,white_queens
,black_kings
.- Parameters:
side (str) – The side that the _board_ will play. Should be one of “white” or “black”
- Variables:
score – The score/evaluation of the current board positions. A higher/more positive score favors white, a lower/more negative score favors black
- __eq__(other)#
Return self==value.
- __hash__()#
Return hash(self).
- __init__(side: str)#
- Parameters:
side (str) –
- __repr__()#
Return repr(self).
- __str__()#
Return str(self).
- __weakref__#
list of weak references to the object (if defined)
- alpha_beta_search(depth: int = 4, alpha: int = -100000, beta: int = 100000, maximizing_player: bool = True) int #
Execute an alpha-beta pruned search. You probably won’t need to call this function yourself, use Board.search_forward instead.
- Parameters:
depth (int) – The number of plies to search forward (default=4)
alpha (int) – The minimum score that the maximizing player is guaranteed (default=-1000). You probably won’t need to specify this argument.
beta (int) – The maximum score that the minimizing player is guaranteed (default=1000). You probably won’t need to specify this argument.
maximizing_player (bool) – True if white is searching for a move, False if black is searching for a move.
- Returns:
The score of the best board position found.
- Return type:
int
- property board#
A dictionary mapping a side and piece to its corresponding bitboard. Useful when we want to iterate over all the bitboards of the board
- property board_pieces#
A list of all bitboards of the pieces that belong to the Board.
- copy()#
Create and return a copy of the board.
- evaluate_score() int #
Evaluate the current score/evaluation of the board state. Use this method to reset the board score to the correct value if the game starts from an intermediate stage.
- Returns:
The score/evaluation of the current board state.
- Return type:
int
- get_bitboard(side: str, piece: str) int #
Returns the bitboard of the passed side for the passed pieces. For example, calling with side=”black” and piece=”king” will return the black_kings bitboard, and so on.
Raises AttributeError if a bitboard with an invalid name is requested. See above for the bitboard naming convention.
- Parameters:
side (str) – “white” or “black”
piece (str) – Can be one of - “kings”, “queens”, “bishops”, “knights”, “rooks”, “pawns”
- Returns:
Bitboard
- Return type:
int
- get_moves(side: str, piece: Optional[str] = None, position: Optional[int] = None) list[tuple[int, int, int]] #
Get all end positions a piece of side can reach starting from position.
side
is always required, piece and position are optional.If piece is not specified, gets all moves for all pieces of the passed side, i.e. get all valid moves for white or black.
If side and piece are specified and position is not, gets all valid moves for the specified side and piece on the board, i.e. if side is “white” and piece is “rooks”, gets all valid moves for all white rooks on the board.
If side, piece, and position are specified, gets all moves for the piece present on position.
- Parameters:
side (str) – “white” or “black”
piece (Optional[str]) – Can be one of - “kings”, “queens”, “bishops”, “knights”, “rooks”, “pawns”
position (Optional[int]) – A power of 2 corresponding to a position on the board. See Representing Positions On The Board
- Returns:
A list of moves as tuples
(start, end)
, wherestart
andend
are positions on the board. See Representing Positions On The Board- Return type:
list[tuple[int, int, int]]
- get_self_piece_bitboard(piece: str) int #
Returns the bitboard corresponding to the passed piece, considering the board’s own side. i.e. - If the board is white, calling with piece=”king” will return white king, etc.
- Parameters:
piece (str) – Can be one of - “kings”, “queens”, “bishops”, “knights”, “rooks”, “pawns”
- Returns:
Bitboard
- Return type:
int
- get_side_bitboard(side: str) int #
Returns the bitboard containing all pieces for the given side.
- Parameters:
side (str) – “white” or “black”
- Returns:
If
side == "white"
, returnsself.all_white
, elseself.all_black
- Return type:
int
- handle_player_move(side_to_move: str, last_move: str) Tuple[str, int, bool] #
Ask for user input until accepted. When a valid input is received, execute the move.
- Parameters:
side_to_move (str) – “white” or “black”
last_move (str) – The last move made by the board to print out to the user
- Returns:
A 3-tuple of the form - the valid (move, lines_printed, move_undone) where move is the valid move received as input, lines_printed is the number of lines printed, and move_undone is a boolean that is
True
when the user chose to undo their previous move- Return type:
Tuple[str, int, bool]
- identify_piece_at(position: int) tuple #
Identifies if there is any piece on the position passed.
- Parameters:
position (int) – A power of 2 corresponding to a position on the board. See Representing Positions On The Board
- Returns:
Returns a 3-tuple of the format (side, piece, bitboard) where side is the side of the piece identified at position (e.g, “black”), piece is the type of piece identified at position (e.g, “bishops”), and bitboard is the bitboard of the piece (e.g, Board.black_bishops).
- Return type:
tuple
- make_moves(*moves: Iterable[tuple[int, int]]) None #
Given a number of moves as tuples (start, end), call Board.move on all. Tracks all moves by default in
self.moves
- Parameters:
moves (Iterable[tuple[int, int]]) –
- Return type:
None
- move(start: int, end: int, score: Optional[int] = None, track: bool = True) None #
Moves the piece at start to end. Doesn’t check anything, just makes the move (unless the start or end positions are invalid). Also checks if move is a castle, moves the rook automatically on castle, and updates each side’s ability to castle on each move.
Important
This is the underlying function that is called by both
Board.move_san
andBoard.move_raw
. This function keeps track of castling status, but performs no validation.Board.move_san
andBoard.move_raw
perform validation, but don’t keep track of castling status. In general, you should useBoard.move_san
orBoard.move_raw
inside the game loop to ensure both move validation and castling status are correctly performed/updated. Only useBoard.move
in special cases when you want to make arbitrary moves outside the rules and/or the game loop.- Parameters:
start (int) – The start position of the move. See Representing Positions On The Board
end (int) – The end position of the move. See Representing Positions On The Board
score (Optional[int]) – The new score/evaluation of the board after the move is made
track (bool) – If
True
, the move made will be stored in self.moves
- Raises:
PositionError – If an invalid position was passed.
- Return type:
None
- move_raw(start: int, end: int, track: bool = True) None #
Moves the piece at start to end. Checks if the move is a valid move to make given the current state of the board.
- Parameters:
start (int) – The start position of the move. See Representing Positions On The Board
end (int) – The end position of the move. See Representing Positions On The Board
track (bool) – If
True
, the move made will be stored in self.moves
- Raises:
PositionError – If there is not piece at
start
to moveMoveError – If an invalid move is passed
- Return type:
None
- move_san(move: str, side: str) None #
Make a move given in standard algebraic notation.
- Parameters:
move (str) – A move given in SAN
side (str) – “white” or “black”
- Raises:
MoveError – If an ambiguous or invalid move was passed
PGNParsingError – If the move was invalid SAN
- Return type:
None
- property opponent_pieces#
A list of all bitboards of the pieces that belong to the opponent.
- play(search_depth: int = 4) None #
Play a game of chess against the computer.
- Parameters:
search_depth (int) – The number of plies the computer should search forward. Be careful passing values about 4 as the search depth. It increases the running time of the move search exponentially.
- Return type:
None
- play_pvp() None #
Play a game of chess on the terminal with another player
- Return type:
None
- search_forward(depth: int = 4) tuple[int, tuple[int, int, int]] #
Execute an alpha-beta pruned depth-first search to find the optimal move from the current board state.
- Parameters:
depth (int) – int - The number of plies to search (1 move is 2 plies). Default = 4 plies.
- Returns:
A 2-tuple where the first element is the best board score found, and the second element is the best found move as a 2 tuple containing the start position and the end position
- Return type:
tuple[int, tuple[int, int, int]]
- set_bitboard(side: str, piece: str, board: int) None #
Sets the bitboard for the passed arguments to the passed bitboard
- Parameters:
side (str) – “white” or “black”
piece (str) – Can be one of - “kings”, “queens”, “bishops”, “knights”, “rooks”, “pawns”
board (int) – The bitboard to be set
- Return type:
None
- undo_move() None #
Undo the last tracked move.
- Return type:
None