ChessGameState
| Attribute | Description |
|---|---|
ChessGameState.board |
A ChessBoard object representing the board of a chess game. |
ChessGameState.turn |
A string with value 'w' if it is white's turn to move and 'b' when it is black's turn to move. |
ChessGameState.turn_num |
The number of executed moves. |
ChessGameState.last_move |
The last move made in this game, in UCI. None at the start of the game. |
ChessGameState.moves |
A list of every move made in this game, oldest first, in UCI. Empty at the start of the game. Unlike the FEN, this holds information about repetition. |
ChessGameState.clocks |
A numpy array of two floats where where the first is the time left for your algo at the start of the turn (in ms) and the second is how much time your opponent finished their turn with. |
ChessGameState.time_left |
Seconds left on your clock, as a float. On a board you are asked to move on, your clock is running, so it counts down as your algo thinks: it is measured each time you read it, so read it again rather than keeping the number, and it goes negative once your clock has run out. On any other board your clock is stopped, so it is the time your clock stopped at. None on a board from make_pseudo_move. |
ChessGameState.clock_ends |
On a board you are asked to move on, the moment your clock runs out, as a UTC datetime; time_left counts down to it. None on every other board. |
ChessGameState.all_valid_moves |
A list of strings for every valid move for the current game state, each written in UCI. |
ChessGameState.fen |
The Forsyth-Edwards Notation (FEN) string representation of the game state. |
ChessGameState.gameover |
True if this position ends the game, otherwise False. Only a board from make_pseudo_move can: play stops before a finished board would reach you, and your algo is simply told the game is over. |
ChessGameState.in_check |
True if the active color is in check, otherwise False. |
ChessGameState.winner |
A string with value 'w' if white has won, 'b' if black has won, or 'draw' if the game ended in a draw, on a board from make_pseudo_move that ends the game. Otherwise None. |
ChessGameState.endgame_type |
How the position ends the game, on a board from make_pseudo_move that does: 'checkmate', 'stalemate', 'insufficient_material', 'fifty_move_rule' or 'repetition'. Otherwise None. |
'e4', or matrix indices. If a cell does not have a piece on it, `None` will be returned.
| Attribute | Description |
|---|---|
ChessBoard.array |
The board as a numpy array. |
ChessBoard.pieces |
A ChessPieces object containing the pieces on the board. |
ChessPieces
An iterable of ChessPiece objects with useful helper methods. Can be indexed by a lowercase 'w' or 'b' to get all pieces of the indexed color, or by uppercase letters corresponding to piece type.
| Operation | Description |
|---|---|
ChessPieces + ChessPieces |
Concatenates both sets of chess pieces into a new ChessPieces object. Analogous to adding list objects. |
| Method | Description |
|---|---|
ChessPieces.get_color(color) |
Returns a new ChessPieces object containing all pieces of the specified color. (Equivalent to indexing with 'w' or 'b') |
ChessPieces.get_piece_type(piece_type) |
Returns a new ChessPieces object containing all pieces of the specified type. Valid piece types are 'R','N','B','P','Q', and 'K'. (Equivalent to indexing with uppercase letter) |
ChessPieces.get_valid_moves() |
A list of all the possible moves using the pieces contained in the object, in UCI. |
ChessPieces.get_numpy_bitboard() |
A 64 element numpy array of 1's and 0's describing the locations of the pieces on a flattened 8x8 chess board. |
ChessPiece
A simple object representing a chess piece.
| Attribute | Description |
|---|---|
ChessPiece.type |
An uppercase letter for the piece type: 'R', 'N', 'B', 'P', 'Q' or 'K'. |
ChessPiece.color |
'w' if the piece is white, 'b' if the piece is black. |
ChessPiece.valid_moves |
A list of the valid moves for this piece, in UCI. |
ChessPiece.pos |
The name of the square the piece is on, like 'e4'. |
Examples
def on_update(state):
# Assume the match just started
# Use a square's name or matrix indexing to get the black rook
ChessInterface.log(state.board[0,0]) # or state.board['a8']
# > ChessPiece('r', 'a8')
# None if no piece at indexed location
ChessInterface.log(state.board['b6'])
# > None
# Slice with square names or matrix notation
ChessInterface.log(state.board['a8':'c6']) # or state.board[0:3, 0:3]
# > ---------
# 8 │ r n b |
# 7 │ p p p │
# 6 │ · · · │
# ---------
# a b c
# Get the pieces from a board
ChessInterface.log(state.board['a1':'a8'].pieces['P'])
# > [ChessPiece('p', 'a7'), ChessPiece('P', 'a2')]