Repository navigation
Features
chess.Game(
fen: str = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1",
raise_errors: bool = True,
evaluate_openings: bool = False,
pieces: Type = Piece
)
The game class
Parameter fen (optional): The FEN to load in the game
Parameter raise_errors (optional): If False, silences all errors produced by the game. Can be changed later using the raise_errors variable
Parameter evaluate_openings (optional): If True, evaluates the opening of the game after a change. Can be changed later using the evaluate_openings variable
Parameter pieces (optional): The piece class to use
import chess
game = chess.Game()
print(game)Output:
Chess Game with FEN rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w - - 0 1
chess.Game.loadFEN(
fen: str,
evaluate_opening: bool = True,
evaluate_checks: bool = True
) -> bool
Loads the given FEN into the game
Parameter fen: The FEN to load
Parameter evaluate_opening (optional): If True, evaluates the opening after loading the FEN, if allowed in the game
Parameter evaluate_checks (optional): If True, updates in_check and checking_piece variables after loading the FEN
Returns whether the FEN loaded successfully
import chess
game = chess.Game()
game.loadFEN("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 1")chess.Game.loadPGN(
pgn: Optional[str] = None,
file: Optional[str] = None,
quotes: str = "\""
) -> Optional[bool]
Loads the given PGN into the game
Parameter pgn (optional): The PGN to load
Parameter file (optional): An alternative to the pgn parameter, the file containing the PGN. Either the pgn or file parameter must be provided
Parameter quotes (optional): The quotes used in the PGN (usually ' or ")
Returns whether the PGN loaded successfully, or returns None if neither the pgn nor the file parameter was provided
import chess
game = chess.Game()
game.loadPGN("[Some_header_text 'value']\n\n1. e4 e5 2. Nf3 Nc6 *", quotes="'")chess.Game.loadOpening(
opening_name: str
) -> bool
Searches in the openings list for the given opening. If found, loads the opening into the game.
Parameter opening_name: The name of the opening to search for
Returns whether the opening was found
import chess
game = chess.Game()
game.loadOpening("Sicilian Defense: Grand Prix Attack")chess.Game.reset() -> None
Resets the game with the default FEN
import chess
game = chess.Game()
game.loadFEN("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 1")
game.reset()chess.Game.FEN() -> str
Returns the FEN of the game
import chess
game = chess.Game()
game.loadFEN("rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 1")
print(game.FEN())Output:
"rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR w KQkq - 0 1"
chess.Game.PGN(
**kwargs: Any
) -> str
Returns the PGN of the game
Parameter **kwargs (optional): Key-value pairs to be inserted in the header of the PGN
import chess
game = chess.Game()
game.loadPGN("[Some_header_text 'value']\n\n1. e4 e5 2. Nf3 Nc6 *", quotes="'")
print(game.PGN())Output:
1. e4 e5 2. Nf3 Nc6*
chess.Game.error(
error: Type
) -> None
Raises the given error if allowed
chess.Game.placePiece(
coordinate: str,
color: str,
piece_type: str
) -> Type
Places a piece of type piece_type and color color at the given coordinate, replacing any existing piece already on the coordinate.
Parameter coordinate: The coordinate to place the piece at
Parameter color: The piece color
Parameter piece_type: The type of piece
Returns the constructed piece
import chess
game = chess.Game()
game.placePiece("e4", "white", "pawn")chess.Game.removePiece(
coordinate: str
) -> Optional[Type]
Removes the piece at the given coordinate, if one exists
Parameter coordinate: The coordinate to remove the piece
Returns the removed piece, or None if there was no piece at the coordinate
import chess
game = chess.Game()
game.removePiece("a2")chess.Game.getKing(
color: str
) -> Optional[Type]
Returns the king of the given color
Parameter color: The color of the king to return
Returns the piece if the color is valid, otherwise raises an error
import chess
game = chess.Game()
game.getKing("white")chess.Game.checkLine() -> Optional[Line]
Returns the line of check if either side is in check, otherwise returns None
import chess
game = chess.Game()
print(game.checkLine())Output:
None
chess.Game.move(
move: str | Move,
evaluate_checks: bool = True,
evaluate_opening: bool = True,
evaluate_move_checks: bool = True,
evaluate_move_checkmate: bool = True
) -> Optional[Move]
Makes the move, and returns a Move object
Parameter move: A string or Move object representing the move. Can also be a space-separated string containing multiple moves.
Parameter evaluate_checks (optional): If True, evaluates if either side is in check after making the move
Parameter evaluate_opening (optional): If True, evaluates the opening after making the move
Parameter evaluate_move_checks (optional): Passed to the legal_moves function as the evaluate_checks parameter
Parameter evaluate_move_checkmate (optional): Passed to the legal_moves function as the evaluate_checkmate parameter
If the move was made successfully, a Move object containing the move data is returned
import chess
game = chess.Game()
print(game.FEN())
game.move("e4")
print(game.FEN())Output:
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1
chess.Game.legal_moves(
show_data: bool = False,
color: str = Color.current,
evaluate_checks: bool = True,
evaluate_checkmate: bool = True,
piece_type: str | Iterable[str]
) -> List[str | Move]
Returns the legal moves of the game
Parameter show_data (optional): If False, returns a list of strings representing the move in SAN. If True, returns a list containing Move objects with verbose data about each move
Parameter color (optional): The color to find the legal moves, defaults to the current turn
Parameter evaluate_checks (optional): If True, evaluates if each move is a check
Parameter piece_type (optional): A piece type, or a list of piece types to search for legal moves
Returns the found moves
import chess
game = chess.Game()
print(game.legal_moves())
game.move("e4")
print(game.legal_moves())Output:
['a3', 'a4', 'b3', 'b4', 'c3', 'c4', 'd3', 'd4', 'e3', 'e4', 'f3', 'f4', 'g3', 'g4', 'h3', 'h4', 'Na3', 'Nc3', 'Nf3', 'Nh3']
['Nc6', 'Na6', 'Nh6', 'Nf6', 'a6', 'a5', 'b6', 'b5', 'c6', 'c5', 'd6', 'd5', 'e6', 'e5', 'f6', 'f5', 'g6', 'g5', 'h6', 'h5']
chess.Game.pieceType(
piece: str,
color: str = Color.any
) -> List[Type]
Returns all pieces with the given piece type, and the given color if specified
Parameter piece: The piece type to search for
Parameter color (optional): The color to search for
Returns a list containing the pieces found with the provided specifications
import chess
game = chess.Game()
print(game.pieceType("pawn"))
print(game.pieceType("pawn", "white"))Output:
[<chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>, <chess.Piece object at 0x10dd1ef40>]
[<chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>]
chess.Game.gamePhase() -> str
Returns the current phase of the game. Can be "opening", "middlegame", or "endgame"
import chess
game = chess.Game()
print(game.gamePhase())
game.move("e4 e5 Nf3 Nc6 Bc4 Bc5 b4 Bxb4 c3 Bc5 d4 exd4 cxd4 Bb6")
print(game.gamePhase())Output:
opening
middlegame
chess.Game.totalMaterial() -> int
Returns the sum of the piece values in the game, excluding the king
import chess
game = chess.Game()
print(game.totalMaterial())
game.move("e4 d5 exd5")
print(game.totalMaterial())Output:
78
77
chess.Game.materialDifference() -> int
Returns the material difference of the game. Positive values indicate white has more material, while negative values indicate black has more.
import chess
game = chess.Game()
print(game.materialDifference())
game.move("e4 d5 exd5")
print(game.materialDifference())
game.move("Qxd5 c4 Qxc4")
print(game.materialDifference())Output:
0
1
-1
chess.Game.evaluate() -> float
Returns a float representing the estimated evaluation of the game using material difference and piece-square tables. Positive values indicate white has more material, while negative values indicate black has more.
import chess
game = chess.Game()
game.move("e4 f5 exf5")
print(game.evaluate())
game.reset()
game.move("e4 d6 e5 dxe5")
print(game.evaluate())Output:
0.901
-1.115
chess.Game.pieceAt(
coordinate: str
) -> Optional[Type]
Returns the piece at the given coordinate if one exists, otherwise returns None
Parameter coordinate: The coordinate
import chess
game = chess.Game()
print(repr(game.pieceAt("e2")))
print(repr(game.pieceAt("e3")))Output:
<chess.Piece object at 0x123456789>
None
chess.Game.takeback(
evaluate_openings: bool = True,
evaluate_checks: bool = True
) -> None
Undoes a move
Parameter evaluate_openings (optional): If True, evaluates the opening after the undo
Parameter evaluate_checks (optional): If True, evaluates checks after the undo
import chess
game = chess.Game()
print(game.FEN())
game.move("e4")
print(game.FEN())
game.takeback()
print(game.FEN())Output:
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
chess.Game.updateOpening() -> False | str
Updates the opening if allowed
Returns the opening name if a new opening was discovered, or False otherwise.
import chess
game = chess.Game(evaluate_openings=True)
game.move("e4")
print(game.opening)Output:
B00 King's Pawn
chess.game.attackers(
coordinate: str,
color: str
) -> List[Type]
Gets the pieces of the given color that attack the coordinate
Parameter coordinate: The coordinate
Parameter color: The color
Returns a list of the attacking pieces found
import chess
game = chess.Game()
print(game.attackers("e3", "white"))Output:
[<chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>]
chess.Game.protectors(
piece: Type
) -> List[Type]
Gets the pieces that protect the given piece
Parameter piece: The piece
Returns a list of the protecting pieces found
import chess
game = chess.Game()
print(game.protectors(game.pieceAt("e2")))Output:
[<chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>, <chess.Piece object at 0x123456789>]
chess.Game.visualized(
print_result: bool = False,
use_unicode: bool = True,
empty_squares: str = " ",
separators: bool = True
) -> Optional[str]
Returns/prints a string representation of the game
Parameter print_result (optional): If True, prints the result rather than returning it
Parameter use_unicode (optional): If True, uses unicode chess characters. Otherwise, uses uppercase latin letters (e.g. WP, WR).
Parameter empty_squares (optional): The character to use on empty squares
Parameter separators (optional): If True, inserts file and rank separators
Returns the result if print_result is False
import chess
game = chess.Game()
print(game.visualized())
game.move("e4")
print(game.visualized())
print(game.visualized(use_unicode=False))
print(game.visualized(empty_squares="*"))Output:
---------------------------------
| ♜ | ♞ | ♝ | ♛ | ♚ | ♝ | ♞ | ♜ |
---------------------------------
| ♟ | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ |
---------------------------------
| | | | | | | | |
---------------------------------
| | | | | | | | |
---------------------------------
| | | | | | | | |
---------------------------------
| | | | | | | | |
---------------------------------
| ♙ | ♙ | ♙ | ♙ | ♙ | ♙ | ♙ | ♙ |
---------------------------------
| ♖ | ♘ | ♗ | ♕ | ♔ | ♗ | ♘ | ♖ |
---------------------------------
---------------------------------
| ♜ | ♞ | ♝ | ♛ | ♚ | ♝ | ♞ | ♜ |
---------------------------------
| ♟ | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ |
---------------------------------
| | | | | | | | |
---------------------------------
| | | | | | | | |
---------------------------------
| | | | | ♙ | | | |
---------------------------------
| | | | | | | | |
---------------------------------
| ♙ | ♙ | ♙ | ♙ | | ♙ | ♙ | ♙ |
---------------------------------
| ♖ | ♘ | ♗ | ♕ | ♔ | ♗ | ♘ | ♖ |
---------------------------------
-----------------------------------------
| BR | BN | BB | BQ | BK | BB | BN | BR |
-----------------------------------------
| BP | BP | BP | BP | BP | BP | BP | BP |
-----------------------------------------
| | | | | | | | |
-----------------------------------------
| | | | | | | | |
-----------------------------------------
| | | | | WP | | | |
-----------------------------------------
| | | | | | | | |
-----------------------------------------
| WP | WP | WP | WP | | WP | WP | WP |
-----------------------------------------
| WR | WN | WB | WQ | WK | WB | WN | WR |
-----------------------------------------
---------------------------------
| ♜ | ♞ | ♝ | ♛ | ♚ | ♝ | ♞ | ♜ |
---------------------------------
| ♟ | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ | ♟ |
---------------------------------
| * | * | * | * | * | * | * | * |
---------------------------------
| * | * | * | * | * | * | * | * |
---------------------------------
| * | * | * | * | ♙ | * | * | * |
---------------------------------
| * | * | * | * | * | * | * | * |
---------------------------------
| ♙ | ♙ | ♙ | ♙ | * | ♙ | ♙ | ♙ |
---------------------------------
| ♖ | ♘ | ♗ | ♕ | ♔ | ♗ | ♘ | ♖ |
---------------------------------
chess.Game.generatePawnMoves(
position: str,
color: str,
return_all: bool = False,
piece: Optional[Type] = None
) -> List[Move]
Generates pawn moves
Parameter position: The position of the pawn
Parameter color: The color of the pawn
Parameter return_all (optional): If True, returns all possible moves regardless of other pieces
Parameter piece (optional): Passed as the piece parameter in Move objects
Returns the generated moves
chess.Game.generatePawnCaptures(
position: str,
color: str,
return_all: bool = False,
piece: Optional[Type] = None
) -> List[Move]
Generates pawn captures
Parameter position: The position of the pawn
Parameter color: The color of the pawn
Parameter return_all (optional): If True, returns all possible moves regardless of other pieces
Parameter piece (optional): Passed as the piece parameter in Move objects
Returns the generated moves
chess.Game.generateKnightMoves(
position: str,
color: str,
return_all: bool = False,
piece: Optional[Type] = None
) -> List[Move]
Generates knight moves
Parameter position: The position of the knight
Parameter color: The color of the knight
Parameter return_all (optional): If True, returns all possible moves regardless of other pieces
Parameter piece (optional): Passed as the piece parameter in Move objects
Returns the generated moves
chess.Game.generateBishopMoves(
position: str,
color: str,
stop: str = Stop.capture_piece,
piece: Optional[Type] = None
) -> List[Move]
Generates bishop moves
Parameter position: The position of the bishop
Parameter color: The color of the bishop
Parameter stop (optional): The stopping point for the generator
Parameter piece (optional): Passed as the piece parameter in Move objects
Returns the generated moves
chess.Game.generateRookMoves(
position: str,
color: str,
stop: str = Stop.capture_piece,
piece: Optional[Type] = None
) -> List[Move]
Generates rook moves
Parameter position: The position of the rook
Parameter color: The color of the rook
Parameter stop (optional): The stopping point for the generator
Parameter piece (optional): Passed as the piece parameter in Move objects
Returns the generated moves
chess.Game.generateQueenMoves(
position: str,
color: str,
stop: str = Stop.capture_piece,
piece: Optional[Type] = None
) -> List[Move]
Generates queen moves
Parameter position: The position of the queen
Parameter color: The color of the queen
Parameter stop (optional): The stopping point for the generator
Parameter piece (optional): Passed as the piece parameter in Move objects
Returns the generated moves
chess.Game.generateKingMoves(
position: str,
color: str,
piece: Optional[Type] = None
) -> List[Move]
Generates king moves, excluding castles
Parameter position: The position of the king
Parameter color: The color of the king
Parameter piece (optional): Passed as the piece parameter in Move objects
Returns the generated moves
chess.Game.generateKingCastles(
position: str,
piece: Optional[Type] = None
) -> List[Move]
Generates castle moves
Parameter position: The position of the king
Parameter piece (optional): Passed as the piece parameter in Move objects
Returns the generated moves
chess.Game.minimax_evaluation(
depth: int,
alpha: float = float("-inf"),
beta: float = float("inf"),
maximizing: bool = True,
color: str = Color.current
) -> {"move": str, "evaluation": float}
Finds the best move and the evaluation using the minimax algorithm with alpha-beta pruning
Parameter depth: The depth to search
Parameter alpha (optional): The alpha value
Parameter beta (optional): The beta value
Parameter maximizing (optional): Maximizing or minimizing
Parameter color: The color
import chess
game = chess.Game()
game.move("e4 f5")
print(game.minimax_evaluation(1))Output:
{'move': 'exf5', 'evaluation': 0.901}
chess.Game.tags: dict
The tags of the game
import chess
game = chess.Game()
print(game.tags)Output:
{'Result': '*'}
chess.Game.properties: dict
Properties of the game
import chess
game = chess.Game()
print(game.properties)Output:
{'promotions': {'N': 'knight', 'B': 'bishop', 'R': 'rook', 'Q': 'queen'}}
chess.Game.pieces_class: Type
The class used for pieces (the pieces parameter when initializing chess.Game)
import chess
game = chess.Game()
print(game.pieces_class)Output:
<class 'chess.Piece'>
chess.Game.opening: str
The opening name for the game, updates if the evaluate_openings variable is True
import chess
game = chess.Game(evaluate_openings=True)
print(game.opening)
game.move("e4")
print(game.opening)Output:
B00 King's Pawn
chess.Game.evaluate_openings: bool
If True, evaluates the opening after a change (the evaluate_openings parameter when initializing chess.Game)
import chess
game = chess.Game()
print(game.evaluate_openings)
game.move("e4")
print(game.opening)
game.evaluate_openings = True
game.move("e5")
print(game.opening)Output:
False
C20 King's Pawn Game
chess.Game.squares: List[List[Square]]
A 2d-list representing the 64 squares of the board
chess.Game.pieces: List[Piece]
A list containing each piece of the game
chess.Game.in_check: False | str
False if neither side is in check, Color.white if white is in check, or Color.black if black is in check
import chess
game = chess.Game()
print(game.in_check)
game.move("e4 f5 Qh5+")
print(game.in_check)Output:
False
black
chess.Game.game_over: bool
Whether the game has ended or not
import chess
game = chess.Game()
print(game.game_over)
game.move("e4 f5 exf5 g5 Qh5+")
print(game.game_over)Output:
False
True
chess.Game.is_checkmate: bool
Whether the game ended in checkmate or not
import chess
game = chess.Game()
game.move("e4 f5 exf5 g5 Qh5+")
print(game.is_checkmate)Output:
True
chess.Game.is_stalemate: bool
Whether the game ended in stalemate or not
chess.Game.drawn: bool
Whether the game ended in a draw or not
chess.Game.checking_piece: Optional[Type]
None if neither king is in check, otherwise the piece checking the king
import chess
game = chess.Game()
print(game.checking_piece)
game.move("e4 f5 Qh5+")
print(game.checking_piece)Output:
None
White queen at h5
chess.Game.white_king: Type
A pointer to the white king
chess.Game.black_king: Type
A pointer to the black king
chess.Game.raise_errors: bool
If False, silences all errors produced by the game (the raise_errors parameter when initializing chess.Game)
chess.Game.move_list: str
The history of the game
import chess
game = chess.Game()
game.move("e4")
print(game.move_list)
game.move("e5")
print(game.move_list)
game.takeback()
print(game.move_list)Output:
1. e4
1. e4 e5
1. e4
chess.Game.raw_move_list: List[Move]
A list of every move made in the game as Move objects
import chess
game = chess.Game()
game.move("e4")
print(game.raw_move_list)
game.move("e5")
print(game.raw_move_list)
game.takeback()
print(game.raw_move_list)Output:
[e4]
[e4, e5]
[e4]
chess.Game.positions: List[str]
Each position achieved in the game in FEN format
import chess
game = chess.Game()
print(game.positions)
game.move("e4")
print(game.positions)
game.move("e5")
print(game.positions)
game.takeback()
print(game.positions)Output:
['rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1']
['rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1', 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1']
['rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1', 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1', 'rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e6 0 2']
['rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1', 'rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1']
chess.Game.starting_fen: str
The starting FEN of the game
chess.Game.captured_piece: bool
Whether a piece has been captured in the game
import chess
game = chess.Game()
game.move("e4 d5")
print(game.captured_piece)
game.move("exd5")
print(game.captured_piece)Output:
False
True
chess.Game.turn: str
The color to move
import chess
game = chess.Game()
print(game.turn)
game.move("e4")
print(game.turn)Output:
white
black
chess.Game.half_moves: int
The halfmove clock
chess.Game.full_moves: int
The number of full moves made in the game
chess.Game.castling_rights: str
The castling rights
chess.Game.en_passant_positions: Optional[str]
The possible en passant capture position, or None if there are no positions
chess.Game.squares_hashtable: dict
A dictionary with a key-value pair for each square in the game, the value is the piece on that square, or False if the square is empty
import chess
game = chess.Game()
print(game.squares_hashtable["a2"])
print(game.squares_hashtable["a3"])Output:
White pawn at a2
False
chess.ThreeCheck extends Game( ... )
The three-check chess variant
chess.ThreeCheck.white_checks: int
The amount of times white has checked black
chess.ThreeCheck.black_checks: int
The amount of times black has checked white
chess.KingOfTheHill extends Game( ... )
King of the hill chess variant
chess.RacingKings extends Game(
...,
pieces: Type = RacingKingsPiece
)
The racing kings chess variant
chess.RacingKings.RacingKingsPiece extends Piece( ... )
The piece class for RacingKings
chess.KingOfTheHill extends Game(
...,
pieces: Type = AtomicPiece
)
The atomic chess variant
chess.Atomic.AtomicPiece extends Piece( ... )
The piece class for Atomic
static Atomic.generateExplosionRadius(
coordinate: str
) -> List[str]
Generates the affected squares if a capture occurred at the given coordinate
Parameter coordinate: The coordinate
Returns a list of the affected squares
chess.Crazyhosue extends Game(
...,
pieces: Type = CrazyhousePiece
)
The crazyhouse chess variant
chess.Crazyhouse.CrazyhousePiece extends Piece( ... )
The piece class for crazyhouse
chess.Crazyhouse.dropPiece(
piece_type: str,
position: str
) -> Type | False
Drops a piece onto the board at the given position of the given piece type from the current player's pocket
chess.Crazyhouse.white_pocket: List[Type]
The pocket for white
chess.Crazyhouse.black_pocket: List[Type]
The pocket for black
chess.Piece(
position: str,
piece_type: str,
color: str,
board: Type
)
The piece class
chess.Piece.moveTo(
position: str,
override_pieces: bool = True,
evaluate_opening: bool = True,
evaluate_checks: bool = True
) -> None
Moves the piece to the given position
Parameter position: The position to move to
Parameter override_pieces (optional): If True, removes any piece already on the target position. Otherwise, moves any piece on the target position to the current position.
Parameter evaluate_opening (optional): If True, evaluates the opening after moving if the game allows
Parameter evaluate_checks (optional): If True, evaluates checks after moving
chess.Piece.pinLine() -> False | Line
Returns the line in which the piece is pinned. If the piece is not pinned, returns False
chess.Piece.moves(
show_data: bool = False,
evaluate_checks: bool = True
) -> list[Move | str]
Gets the legal moves of the piece
Parameter show_data: If True, returns Move objects. Otherwise, returns the move names as a string.
Parameter evaluate_checks: If True, appends + symbols to all moves that are checks
Returns a list of the found moves
chess.Piece.position: str
The position of the piece
chess.Piece.piece_type: str
The type of the piece
chess.Piece.color: str
The color of the piece
chess.Piece.board: Type
The game that the piece belongs to
chess.Piece.moved: bool
Whether the piece has moved
chess.Piece.promoted: bool
Whether the piece is a promoted pawn
chess.Piece.pinned: bool
Whether the piece is pinned
chess.Piece.pinning: Optional[Type]
None if the piece is not pinning another piece, otherwise the pinned piece
chess.Square(
position: str,
board: Type
)
The square class
chess.Square.position: str
The position of the square
chess.Square.board: Type
The board that the square belongs to
chess.Square.color: "white" | "black"
The color of the square. Can be "white" or "black"
Color type enumerations
Evaluates to the current turn in the game
Evaluates to both white and black
chess.Color.all() -> [Color.white, Color.black]
Returns a list of white and black
chess.Color.invert(
color: str
) -> Optional[str]
Returns the inverse of the given color
chess.Color.valid(
color: Any
) -> bool
Returns whether the given string is a valid color or not
chess.Color.isWhite(
color: str
) -> bool
Returns whether the color is white or not
chess.Color.isBlack(
color: str
) -> bool
Returns whether the color is black or not
Piece type enumerations
chess.PieceEnum.unicode_dictionary: dict
A dictionary mapping pieces to their unicode character
chess.PieceEnum.piece_values: dict
A dictionary mapping pieces to their piece values
chess.PieceEnum.piece_square_tables: dict
A dictionary containing piece-square tables for the opening/middlegame and endgame
chess.PieceEnum.all() -> [PieceEnum.pawn, PieceEnum.knight, PieceEnum.bishop, PieceEnum.rook, PieceEnum.queen, PieceEnum.king]
Returns all the enumerated pieces
chess.PieceEnum.unicode(
piece: str,
color: str = "white"
) -> str
Returns the unicode character for a piece with the given specifications
chess.PieceEnum.value(
piece: str
) -> int | float
Returns the value of the given piece
chess.PieceEnum.evaluate_piece_position(
piece: str,
position: str,
color: str,
game_phase: str
) -> int
Returns an evaluation of the piece's position
chess.PieceEnum.valid(
piece: Any
) -> bool
Returns whether the given value is a valid piece
Game phase enumerations
chess.Phase.all() -> [Phase.opening, Phase.middlegame, Phase.endgame]
Returns all the enumerated game phases
chess.Phase.valid(
phase: Any
) -> bool
Returns whether the given value is a valid game phase
Castling side enumerations
chess.Castle.all() -> [Castle.kingside, Castle.queenside]
Returns all the enumerated castling sides
chess.Castle.valid(
castle: Any
) -> bool
Returns whether the given value is a valid castling side
Move generator stopping point enumerations
chess.Stop.all() -> [chess.Stop.never, chess.Stop.capture_piece, chess.Stop.no_capture, chess.Stop.piece]
Returns all the enumerated stopping points
chess.Stop.valid(
stop: Any
) -> bool
Returns whether the given value is a valid stopping point
chess.Move(
name: str,
old_position: str,
new_position: str,
piece: Optional[Type],
is_capture: bool = False,
check: bool = False,
castle: Optional[str] = None,
castle_rook: Optional[Type] = None,
double_pawn_move: bool = False,
en_passant: bool = False,
en_passant_position: Optional[str] = None,
promotion: bool | str = False,
blocks: bool = False
)
Represents a move
Parameter name: The move in SAN
Parameter old_position: The current position of the piece
Parameter new_position: The position of the piece after moving
Parameter piece: The piece performing the move, can also be None
Parameter is_capture (optional): Whether the move is a capture
Parameter check (optional): Whether the move is a check
Parameter castle (optional): None if the move is not a castle, otherwise kingside or queenside
Parameter castle_rook (optional): None if the move is not a castle, otherwise the rook moving in the castle
Parameter double_pawn_move (optional): Whether the move is a double pawn push
Parameter en_passant (optional): Whether the move is an en passant capture
Parameter en_passant_position: None if the move is not a double pawn push, otherwise the square that the pawn skipped over
Parameter promotion: False if the move is not a promotion, otherwise the uppercase first letter that the pawn promoted to
Parameter blocks: Whether the move is blocking a check
chess.Move.visualized(
print_result: bool = False,
empty_squares: str = " ",
separators: bool = True,
old_position_symbol: bool = "□",
new_position_symbol: bool = "■",
capture_symbol: bool = "X"
) -> Optional[str]
Returns a string visualization of the move
Parameter print_result (optional): If True, returns None and prints the result instead
Parameter emoty_squares (optional): The character for empty squares
Parameter separators (optional): Whether to use separators in the string or not
Parameter old_position_symbol (optional): The symbol detonating the old position
Parameter new_position_symbol (optional): The symbol detonating the new position
Parameter capture_symbol (optional): Replaces the new_position_symbol if the move is a capture
The piece parameter
The name parameter
The old_position parameter
The new_position parameter
The is_capture parameter
The check parameter
The castle parameter
The castle_rook parameter
The double_pawn_move parameter
The en_passant parameter
The en_passant_position parameter
The promotion parameter
The blocks parameter
None if the move is not a capture, otherwise the captured piece
chess.MoveSet(
*moves: Iterable[Move] | *Move
)
A set of moves
chess.MoveSet.visualized(
print_result: bool = False,
empty_squares: str = " ",
separators: bool = True,
old_position_symbol: str = "□",
new_position_symbol: str = "■",
capture_symbol: str = "X"
) -> Optional[str]
Returns a string visualization of the moves
Parameter print_result (optional): If True, returns None and prints the result instead
Parameter emoty_squares (optional): The character for empty squares
Parameter separators (optional): Whether to use separators in the string or not
Parameter old_position_symbol (optional): The symbol detonating old positions
Parameter new_position_symbol (optional): The symbol detonating new positions
Parameter capture_symbol (optional): Replaces the new_position_symbol for captures
chess.MoveSet.old_positions() -> List[str]
Returns a list of every old position of each move
chess.MoveSet.new_positions() -> List[str]
Returns a list of every new position of each move
chess.MoveSet.moves: List[Move]
The moves parameter
chess.Line(
start: str,
end: str,
jump: bool = False
)
A line
chess.Line.visualized(
print_result: bool = False,
separators: bool = True,
empty_squares: str = " ",
line_symbol: str = "●",
start_position_symbol: str = "○",
end_position_symbol: str = "◎"
) -> Optional[str]
Returns a string visualization of the line
Parameter print_result (optional): If True, returns None and prints the result instead
Parameter emoty_squares (optional): The character for empty squares
Parameter separators (optional): Whether to use separators in the string or not
Parameter line_symbol (optional): The symbol detonating positions in the line
Parameter start_position_symbol (optional): The symbol detonating the start position
Parameter end_position_symbol (optional): The symbol detonating the end position
chess.Line.start_position: str
The start position of the line
chess.Line.end_position: str
The end position of the line
chess.Line.positions: List[str]
A list of every position in the line
Functions
chess.functions.getMovesFromString(
string: str
) -> List[str]
Parses a string containing moves
Parameter string: The string containing the moves
Returns a list of each move found in the string
import chess
print(chess.functions.getMovesFromString("1. e4 e5 2. Nf3 Nc6"))Output:
['e4', 'e5', 'Nf3', 'Nc6']
chess.functions.splitNumbers(
string: str
) -> str
Splits numbers in a string
Parameter string: The string
Returns the new string with numbers split
import chess
print(chess.functions.splitNumbers("5"))
print(chess.functions.splitNumbers("aaaaa8aa2"))Output:
11111
aaaaa11111111aa11
chess.functions.combineNumbers(
string: str
) -> str
Combines numbers in a string
Parameter string: The string
Returns the new string with numbers joined
import chess
print(chess.functions.splitNumbers("11111"))
print(chess.functions.splitNumbers("aaaaa11111111aa11"))Output:
5
aaaaa8aa2
chess.functions.indexToCoordinate(
index: [int, int]
) -> str
Converts a raw index to a coordinate
Parameter index: A list of length 2 with integer values within the inclusive range of 0...7
Returns the coordinate
import chess
print(chess.functions.indexToCoordinate([0, 0]))
print(chess.functions.indexToCoordinate([7, 7]))
print(chess.functions.indexToCoordinate([0, 7]))Output:
a8
h1
h8
chess.functions.coordinateToIndex(
coordinate: str
) -> [int, int]
Converts a coordinate to a raw index
Parameter coordinate: A coordinate
Returns the raw index
import chess
print(chess.functions.coordinateToIndex("a8"))
print(chess.functions.coordinateToIndex("h1"))
print(chess.functions.coordinateToIndex("h8"))Output:
[0, 0]
[7, 7]
[0, 7]
chess.functions.coordinateValid(
coordinate: str
) -> bool
Checks if the given string is a valid coordinate
Parameter coordinate: The string to check
Returns whether the string is a valid coordinate
import chess
print(chess.functions.coordinateValid("a1"))
print(chess.functions.coordinateValid("a9"))Output:
True
False
chess.functions.toLAN(
move: str,
game: Type
) -> str
Rewrites the move in long algebraic notation
Parameter move: The move to rewrite in LAN
Parameter game: The game
Returns the move in LAN
import chess
game = chess.Game()
print(chess.functions.toLAN("e4", game))
print(chess.functions.toLAN("Nf3", game))Output:
e2e4
g1f3
chess.functions.toSAN(
move: str,
game: Type
) -> str
Rewrites the move in standard algebraic notation
Parameter move: The move to rewrite in SAN
Parameter game: The game
Returns the move in SAN
import chess
game = chess.Game()
print(chess.functions.toSAN("e2e4", game))
print(chess.functions.toSAN("g1f3", game))Output:
e4
Nf3
chess.functions.FENvalid(
fen: str
) -> bool
Checks if the given FEN is valid
Parameter fen: The FEN to check
Returns whether the FEN is valid
chess.functions.isLine(
pos1: str,
pos2: str
) -> bool
Check if pos1 and pos2 form a valid (vertical, horizontal, or bishop-attack) line
Parameter pos1: The first position
Parameter pos2: The second position
Returns whether the positions form a valid line
Chess exceptions
chess.errors.Error extends Exception(
message: str
)
The parent class of every chess exception
chess.errors.MoveNotPossible extends chess.errors.Error(
move: str
)
Raised when a syntactically-valid but illegal move is being made
chess.errors.InvalidMove extends chess.errors.Error(
move: Any
)
Raised when an invalid move is being made
chess.errors.InvalidFEN extends chess.errors.Error(
fen: Any
)
Raised when an invalid FEN is being loaded
chess.errors.InvalidCoordinate extends chess.errors.Error(
coordinate: Any
)
Raised when an invalid coordinate is being used
chess.errors.InvalidColor extends chess.errors.Error(
color: Any
)
Raised when an invalid color is being used
chess.errors.InvalidPiece extends chess.errors.Error(
piece: Any
)
Raised when an invalid piece type is being used
chess.errors.UndefinedColor extends chess.errors.Error(
color: Any
)
Raised when an invalid color is being used
chess.errors.UndefinedPiece extends chess.errors.Error(
piece: Any
)
Raised when an invalid piece is being used
chess.errors.InvalidLineCoordinates extends chess.errors.Error(
coordinate: Any
)
Raised when invalid start/end line coordinates are being used
chess.errors.InvalidPGNLine extends chess.errors.Error(
piece: Any
)
Raised when there is an invalid PGN line
chess.errors.InvalidPGNMove extends chess.errors.Error(
piece: Any
)
Raised when an invalid move exists in a PGN move list
Openings
A list containing opening properties