Main Documentation#
- class Color#
-
Represents either White or Black, used to identify the two players and their pieces.
- static from_str(name: str) Color#
-
Return the color corresponding to name (case‑insensitive).
- Parameters:
-
name (str) – The string
"white"or"black"in any case. - Returns:
-
The matching
Colorinstance. - Return type:
- Raises:
-
ValueError – If name is not recognized.
>>> Color.from_str("White") is WHITE True >>> Color.from_str("BLACK") is BLACK True
- property opposite: Color#
-
Gets the opposite
Colorto this one.>>> WHITE.opposite is BLACK True >>> BLACK.opposite is WHITE True
- __str__() str#
-
Serializes a
Colorto astrof its name in title case.- Returns:
-
A
strof theColor’s name
>>> str(WHITE) "White" >>> str(BLACK) "Black"
- __invert__() Color#
-
Alias for
Color.opposite, Allows~WHITEsyntax.
- __hash__() int#
- __repr__() str#
- __eq__(other: Any) bool#
- class PieceType#
-
Represents one of the 6 types of pieces in chess, either a pawn, knight, bishop, rook, queen, or king.
- static from_str(piece_type: str) PieceType#
-
Return the piece type corresponding to name (case‑insensitive).
- Parameters:
-
name (str) – One of
"pawn","knight","bishop","rook","queen"or"king"(any case). - Return type:
- Returns:
-
The matching
PieceType. - Raises:
-
ValueError – If name is not recognized.
>>> PieceType.from_str("pawn") is PAWN True >>> PieceType.from_str("kNiGhT") is KNIGHT True
- __str__() str#
-
Serializes a
PieceTypeto astrof its name in title case.>>> str(PAWN) "Pawn" >>> str(BISHOP) "Bishop" ```
- __repr__() str#
- __eq__(other: Any) bool#
- __hash__() int#
- PIECE_TYPES: list[PieceType]#
-
A list of all
PieceTypevalues. In the order of[PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING]
- class Piece(color: Color, type: PieceType)#
-
Represents a piece with both a Color and a PieceType, such as a White Pawn, or a Black Rook.
- static from_chr(char: str) Piece#
-
Return the piece encoded by char (ASCII piece letter).
Uppercase letters encode
WHITEpieces, lowercase letters encodeBLACK.- Parameters:
-
char (str) – One of
PRNBQKprnbqk. - Return type:
- Returns:
-
The corresponding
Piece. - Raises:
-
ValueError – If char is not a valid piece letter.
>>> Piece.from_chr("P") <Piece: (White, Pawn)> >>> Piece.from_chr("n") <Piece: (Black, Knight)>
- unicode() str#
-
Returns Unicode figurine character corresponding to this
Piece.>>> Piece(WHITE, PAWN).unicode() "♙" >>> Piece(BLACK, KNIGHT).unicode() "♞"
- __eq__(other: Any)#
-
Evaluates to
Truewhen compared with anotherPiecewith the samePieceTypeandColor
- __str__() str#
-
Serializes a
Pieceas a single ASCII characterstr. Uses uppercase for anyPiecethat isWHITE, and lowercase for anyPiecethat isBLACK.>>> str(Piece(WHITE, PAWN)) "P" >>> str(Piece(BLACK, KNIGHT)) "n"
- __hash__() int#
- class Square#
-
Represents one of the 64 squares on a chess board.
- static from_str(name: str) Square#
-
Return the
Squareencoded by name (case‑insensitive).- Parameters:
-
name (str) – The name of the square
- Return type:
- Returns:
-
The corresponding
Square - Raises:
-
ValueErrorif given a string which does not represent a Square
>>> Square.from_string("E1") == E1 True >>> Square.from_string("a2") == A2 Tru
- bb() Bitboard#
-
Creates a
Bitboardcontaining only thisSquare.>>> A1.bb() == Bitboard([A1]) True >>> H3.bb() == Bitboard([H3]) True
- index() int#
-
Returns the index of this
Squarein the providedSQUARESlist.>>> A1.index() 0 >>> B1.index() 1 >>> A2.index() 8 >>> H8.index() 63
- adjacent() Bitboard#
-
Creates a
Bitboardof all neighbors orthogonal or diagonal to thisSquare.>>> A1.adjacent() == Bitboard([A2, B1, B2]) True >>> E5.adjacent() == Bitboard([D4, D5, D6, E4, E6, F4, F5, F6]) True
- north(distance: int = 1) Square | None#
-
Return the square that is the given distance number of ranks above this square.
- Parameters:
-
distance – how many ranks to move north.
- Returns:
-
the target square, or
Noneif it would be off the board. - Return type:
-
Square | None
>>> B6.north() is B7 True >>> A1.north(distance=3) is A4 True >>> B8.north(distance=2) None
- south(distance: int = 1) Square | None#
-
Return the square that is the given distance number of ranks below this square.
- Parameters:
-
distance (int, default = 1) – how many ranks to move south.
- Returns:
-
the target square, or
Noneif it would be off the board. - Return type:
-
Square | None
>>> B6.south() is B5 True >>> B8.south(distance=2) is B6 True >>> A1.south(distance=3) None
- east(distance: int = 1) Square | None#
-
Return the square that is the given distance number of files to the east of this square.
- Parameters:
-
distance (int, default = 1) – how many files to move east (toward the H-file).
- Returns:
-
the target square, or
Noneif it would be off the board. - Return type:
-
Square | None
>>> B6.east() is C6 True >>> A1.east(distance=3) is D1 True >>> H8.east(distance=2) None
- west(distance: int = 1) Square | None#
-
Return the square that is the given distance number of files to the west of this square.
- Parameters:
-
distance (int, default = 1) – how many files to move west (toward the A-file).
- Returns:
-
the target square, or
Noneif it would be off the board. - Return type:
-
Square | None
>>> B6.west() == A6 True >>> H8.west(distance=2) == E8 True >>> A1.west(distance=3) None
- nw(distance: int = 1) Square | None#
-
Return the square that is the given distance number of files west and ranks north of this square.
- Parameters:
-
distance (int, default = 1) – number of steps to move north-west.
- Returns:
-
the target square, or
Noneif it would be off the board. - Return type:
-
Square | None
>>> C3.nw() == B4 True >>> A1.nw() None >>> D4.nw(distance=2) == B6 True
- ne(distance: int = 1) Square | None#
-
Return the square that is the given distance number of files east and ranks north of this square.
- Parameters:
-
distance (int, default = 1) – number of steps to move north-east.
- Returns:
-
the target square, or
Noneif it would be off the board. - Return type:
-
Square | None
>>> C3.ne() is D4 True >>> E4.ne(distance = 2) is G2 True >>> H8.ne() None
- sw(distance: int = 1) Square | None#
-
Return the square that is the given distance number of files west and ranks south of this square.
- Parameters:
-
distance (int, default = 1) – number of steps to move south-west.
- Returns:
-
the target square, or
Noneif it would be off the board. - Return type:
-
Square | None
>>> C3.sw() is B2 True >>> E4.sw(distance = 2) is C6 True >>> A1.sw() None
- se(distance: int = 1) Square | None#
-
Return the square that is the given distance number of files east and ranks south of this square.
- Parameters:
-
distance (int, default = 1) – number of steps to move south-east.
- Returns:
-
the target square, or
Noneif it would be off the board. - Return type:
-
Square | None
>>> C3.se() is D2 True >>> E4.se(distance = 2) is G6 True >>> H1.se() None
- __and__(other: Bitboard) Bitboard#
-
Return the intersection, in the form of a
Bitboard, of thisSquareand aBitboard, or anotherSquare.- Parameters:
- Returns:
-
squares common to both operands.
- Return type:
>>> A1 & Bitboard([A1, A2]) == Bitboard([A1]) True >>> C_FILE & C1 == Bitboard([C1]) True >>> B2 & B3 == Bitboard([]) True >>> G2 & G2 == Bitboard([G2]) True
- __or__(other: Bitboard) Bitboard#
-
Return the union, in the form of a
Bitboard, of thisSquareand aBitboard, or anotherSquare.- Parameters:
- Returns:
-
squares common to both operands.
- Return type:
>>> A1 | Bitboard([A1, A2]) == Bitboard([A1, A2]) True >>> Bitboard([B2]) | D1 == Bitboard([B2, D1]) True >>> B2 | B3 == Bitboard([B2, B3]) True >>> G2 | G2 == Bitboard([G2]) True
- __xor__(other: Bitboard) Bitboard#
-
Return the symmetric difference, in the form of a
Bitboard, of thisSquareand aBitboard, or anotherSquare.- Parameters:
- Returns:
-
squares common to both operands.
- Return type:
>>> A1 ^ Bitboard([A1, A2]) == Bitboard([A2]) True >>> Bitboard([B2]) ^ D1 == Bitboard([B2, D1]) True >>> B2 ^ B3 == Bitboard([B2, B3]) True >>> G2 ^ G2 == Bitboard([]) True
- __eq__(other: Any) bool#
- __hash__() int#
- __str__() str#
- SQUARES: list[Square]#
-
Every defined
Square, in the order of[A1, B1, C1, D1, E1, F1, G1, H1, A2, B2, ..., H8].
- SQUARES_FLIPPED: list[Square]#
-
Every defined
Square, but flipped to be in the order of[A8, B8, C8, D8, E8, F8, G8, H8, A7, B7, ..., H1].
- class Bitboard(squares: Collection[Square])#
-
A set of squares represented as a 64-bit integer, where each bit indicates whether a
Squareis included.- __init__(squares: Collection[Square])#
-
Initialize a
Bitboardthat contains squares.- Parameters:
-
squares (Collection[Square]) – squares to include in the new Bitboard.
- __str__() str#
-
Return a
strin which included squares are shown as1and excluded squares as0.- Returns:
-
an 8×8 grid row-major from A8 to H1.
- Return type:
-
str
>>> print(str(RANK_5)) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
- static from_int(value: int) Bitboard#
-
Construct a
Bitboardfrom its integer encoding (see__int__()).- Parameters:
-
value (int) – 64-bit integer to convert.
- Returns:
-
new Bitboard corresponding to value.
- Return type:
- Raises:
-
OverflowError – if
value < 0orvalue >= 2 ** 64
>>> Bitboard.from_int(0) == Bitboard([]) True >>> Bitboard.from_int(1) == Bitboard([A1]) True >>> Bitboard.from_int(269498368) == Bitboard([E2, E3, E4, D2, F2]) True >>> Bitboard.from_int(0xFFFF_FFFF_FFFF_FFFF) == FULL_BB True
- __int__() int#
-
Return the integer encoding of this
Bitboard.- Returns:
-
64-bit integer with one bit per square.
- Return type:
-
int
>>> int(EMPTY_BB) 0 >>> int(Bitboard([A1])) 1 >>> int(Bitboard([E2, E3, E4, D2, F2])) 269498368 >>> Bitboard.from_int(int(DARK_SQUARE_BB)) == DARK_SQUARE_BB True
- __len__() int#
-
Return the number of squares contained in this
Bitboard.- Returns:
-
population count.
- Return type:
-
int
>>> len(Bitboard([])) 0 >>> len(Bitboard([A1, A2])) 2 >>> len(RANK_1) 8 >>> len(FULL_BB) 64
- __contains__(square: Square) bool#
-
Test whether square is in this
Bitboard.- Parameters:
-
square (Square) – square to test.
- Returns:
-
membership flag.
- Return type:
-
bool
>>> A1 in Bitboard([A1, A2]) True >>> H3 in FULL_BB True >>> C6 in RANK_1 False
- __eq__(other: Any) bool#
-
Return
Trueif other is aBitboardwith the same squares.- Parameters:
-
other (Any) – Bitboard to compare.
- Returns:
-
equality flag.
- Return type:
-
bool
>>> Bitboard([A1, A2, A3, A4, A5, A6, A7, A8]) == A_FILE True >>> Bitboard([C4]) == Bitboard([C3]) False
- __invert__() Bitboard#
-
Return the complement of this
Bitboard.- Returns:
-
new Bitboard with opposite membership.
- Return type:
>>> FULL_BB == ~EMPTY_BB True >>> LIGHT_SQUARE_BB == ~DARK_SQUARE_BB True
- __and__(other: Bitboard | Square) Bitboard#
-
Return the intersection of two Bitboards, or a Bitboard with a
Square.- Parameters:
- Returns:
-
squares common to both operands.
- Return type:
>>> Bitboard([A1]) & Bitboard([A1, A2]) == Bitboard([A1]) True >>> A_FILE & A1 == Bitboard([A1]) True >>> LIGHT_SQUARE_BB & DARK_SQUARE_BB == EMPTY_BB True
- __or__(other: Bitboard | Square) Bitboard#
-
Return the union of two Bitboards, or a Bitboard with a
Square.- Parameters:
- Returns:
-
squares contained in either operand.
- Return type:
>>> Bitboard([A1, A2]) | A3 == Bitboard([A1, A2, A3]) True >>> LIGHT_SQUARE_BB | DARK_SQUARE_BB == FULL_BB True
- __xor__(other: Bitboard | Square) Bitboard#
-
Return the symmetric difference of two Bitboards, or of a Bitboard with a
Square.- Parameters:
- Returns:
-
squares in exactly one operand.
- Return type:
>>> Bitboard([A1, A2]) ^ A1 == Bitboard([A2]) True >>> LIGHT_SQUARE_BB ^ DARK_SQUARE_BB == FULL_BB True
- __bool__() bool#
-
Return
Trueif theBitboardis non-empty.- Returns:
-
truth value.
- Return type:
-
bool
>>> bool(Bitboard([])) False >>> bool(EMPTY_BB) False >>> bool(Bitboard([A1])) True
- __repr__() str#
- __hash__() int#
- class CastlingType#
-
One of four legal castling options: the king moves either kingside or queenside for
WHITEorBLACK.- static from_chr(castling_type: str) CastlingType#
-
Return the castling type corresponding to a single-character code.
- Parameters:
-
castling_type (str) – one of
"K","Q","k","q". - Returns:
-
the matching
CastlingType. - Return type:
- Raises:
-
ValueError – if castling_type is not a valid code.
>>> CastlingType.from_chr("K") is WHITE_KINGSIDE True >>> CastlingType.from_chr("Q") is WHITE_QUEENSIDE True >>> CastlingType.from_chr("k") is BLACK_KINGSIDE True >>> CastlingType.from_chr("q") is BLACK_QUEENSIDE True
- __str__() str#
-
Return the one-character code for this castling type.
- Returns:
-
"K","Q","k", or"q". - Return type:
-
str
>>> str(WHITE_KINGSIDE) 'K' >>> str(WHITE_QUEENSIDE) 'Q' >>> str(BLACK_KINGSIDE) 'k' >>> str(BLACK_QUEENSIDE) 'q'
- __eq__(other: Any) bool#
-
Return
Trueif other is the same castling type.- Parameters:
-
other (Any) – object to compare.
- Returns:
-
equality flag.
- Return type:
-
bool
- __repr__() str#
- __hash__() int#
- WHITE_KINGSIDE: CastlingType#
-
Castling type representing
WHITEkingside castling.
- WHITE_QUEENSIDE: CastlingType#
-
Castling type representing
WHITEqueenside castling.
- BLACK_KINGSIDE: CastlingType#
-
Castling type representing
BLACKkingside castling.
- BLACK_QUEENSIDE: CastlingType#
-
Castling type representing
BLACKqueenside castling.
- class Move(origin: Square, destination: Square, promote_to: PieceType | None = None)#
-
A chess move, defined by its origin and destination squares and an optional promotion piece type.
- __init__(origin: Square, destination: Square, promote_to: PieceType | None = None)#
-
Create a move from origin to destination.
- static castle(castling_type: CastlingType) Move#
-
Return the move corresponding to castling_type.
- Parameters:
-
castling_type (CastlingType) – one of the four castling constants.
- Returns:
-
appropriate king move for that castling.
- Return type:
>>> Move.castle(WHITE_KINGSIDE) == Move(E1, G1) True >>> Move.castle(WHITE_QUEENSIDE) == Move(E1, C1) True >>> Move.castle(BLACK_KINGSIDE) == Move(E8, G8) True >>> Move.castle(BLACK_QUEENSIDE) == Move(E8, C8) True
- static from_uci(uci: str) Move | None#
-
Parse a UCI long-algebraic
strand build a move.Null moves are supported as “0000”, and are represented as
None.- Parameters:
-
uci (str) – UCI string such as
"e2e4"or"b7b8q". - Returns:
-
move instance, or
Nonefor a null move. - Return type:
-
Move | None
- Raises:
-
ValueError – if uci is malformed or illegal.
>>> Move.from_uci("e2e4") == Move(E2, E4) True >>> Move.from_uci("a1d4") == Move(A1, D4) True >>> Move.from_uci("b7b8q") == Move(B7, B8, promote_to=QUEEN) True >>> Move.from_uci("0000") is None True
- static from_san(san: str, board: Board) Move#
-
Parse a SAN
strin the context of board.- Parameters:
-
-
san (str) – SAN text (e.g.
"Nf6","O-O","e4"). -
board (Board) – position used to disambiguate the SAN.
-
- Returns:
-
the corresponding move.
- Return type:
- Raises:
-
ValueError – if san is invalid for board.
>>> Move.from_san("e4", Board()) == Move(E2, E4) True >>> FEN = "r1bqkbnr/pppp1ppp/2n5/4p3/2B1P3/5N2/PPPP1PPP/RNBQK2R b KQkq - 3 3" >>> board = Board.from_fen(FEN) >>> Move.from_san("Nf6", board) == Move(G8, F6) True
- san(board: Board) str#
-
Return this move in SAN notation relative to board.
- Parameters:
-
board (Board) – position that provides context.
- Returns:
-
SAN string.
- Return type:
-
str
- Raises:
-
ValueError – if the move is illegal for board.
>>> Move(E2, E4).san(Board()) 'e4' >>> FEN = "r1bqkb1r/pppp1ppp/2n2n2/4p3/2B1P3/5N2/PPPP1PPP/RNBQK2R w KQkq - 4 4" >>> Move(E1, G1).san(Board.from_fen(FEN)) 'O-O'
- uci() str#
-
Return a
strof the UCI long algebraic notation representation of this move.- Returns:
-
UCI string.
- Return type:
-
str
>>> Move(E2, E4).uci() 'e2e4' >>> Move(A1, D4).uci() 'a1d4' >>> Move(B7, B8, promote_to=QUEEN).uci() 'b7b8q'
- __str__() str#
-
Alias for
Move.uci().>>> str(Move(E2, E4)) 'e2e4' >>> str(Move(A1, D4)) 'a1d4' >>> str(Move(B7, B8, promote_to=QUEEN)) 'b7b8q'
- property origin: Square#
-
The
Squarethe piece moves from.>>> Move(E2, E4).origin is E2 True >>> Move.from_uci("a1d4").origin is A1 True
- property destination: Square#
-
The
Squarethe piece moves to.>>> Move(E2, E4).destination is E4 True >>> Move.from_uci("a1d4").destination is D4 True
- property promotion: PieceType | None#
-
Promotion
PieceType, orNonefor a non-promotion.>>> Move.from_uci("b7b8q").promotion is QUEEN True >>> Move(E2, E4).promotion is None True
- is_promotion() bool#
-
Return
Trueif the move is a promotion.>>> Move.from_uci("b7b8q").is_promotion() True >>> Move(E2, E4).is_promotion() False
- is_capture(board: Board) bool#
-
Return
Trueif the move captures a piece on board.- Parameters:
-
board (Board) – position to check if this is a capture.
- Returns:
-
capture flag.
- Return type:
-
bool
>>> FEN = "r1bqkbnr/pppp1ppp/2n5/4p3/4P3/5N2/PPPP1PPP/RNBQKB1R w KQkq - 2 3" >>> board = Board.from_fen(FEN) >>> Move(F3, E5).is_capture(board) True
- is_castling(board: Board) bool#
-
Return
Trueif the move is a legal castling move on board.- Parameters:
-
board (Board) – position to check if this is a castling move.
- Returns:
-
castling flag.
- Return type:
-
bool
>>> FEN = "r1bqkb1r/pppp1ppp/2n2n2/4p3/2B1P3/5N2/PPPP1PPP/RNBQK2R w KQkq - 4 4" >>> board = Board.from_fen(FEN) >>> Move(E1, G1).is_castling(board) True >>> Move(E1, G1).is_castling(Board.empty()) False
- castling_type(board: Board) CastlingType | None#
-
If the move is castling, return its type; otherwise
None.- Parameters:
-
board (Board) – position used for classification.
- Returns:
-
corresponding castling type or
None. - Return type:
-
CastlingType | None
>>> FEN = "r1bqkb1r/pppp1ppp/2n2n2/4p3/2B1P3/5N2/PPPP1PPP/RNBQK2R w KQkq - 4 4" >>> board = Board.from_fen(FEN) >>> Move(E1, G1).castling_type(board) is WHITE_KINGSIDE True >>> Move(E1, G1).castling_type(Board.empty()) is None True
- __eq__(other: Any) bool#
-
Return
Trueif other has the same origin, destination and promotion value.- Parameters:
-
other (Any) – object to compare.
- Returns:
-
equality flag.
- Return type:
-
bool
>>> Move(E2, E4) == Move.from_uci("e2e4") True >>> Move(A7, A8) == Move.from_uci("a7a8q") False
- __hash__() int#
- __repr__() str#
- class CastlingRights(castling_types: Collection[CastlingType])#
-
A set of
CastlingTypevalues that encodes aBoard’s castling permissions.- __init__(castling_types: Collection[CastlingType]) None#
-
Initialize the object with castling_types.
- Parameters:
-
castling_types (Collection[CastlingType]) – iterable of castling constants to include.
- static from_fen(castling_fen: str) CastlingRights#
-
Build a
CastlingRightsobject from a FEN castling field.- Parameters:
-
castling_fen (str) –
"KQkq","KQ","-"… - Returns:
-
rights object matching castling_fen.
- Return type:
- Raises:
-
ValueError – if castling_fen is not valid FEN.
>>> CastlingRights.from_fen("KQkq") == ALL_CASTLING True >>> CastlingRights.from_fen("Qk") == CastlingRights([WHITE_QUEENSIDE, BLACK_KINGSIDE]) True >>> CastlingRights.from_fen("-") == NO_CASTLING True
- fen() str#
-
Returns the Forsyth-Edwards Notation
strrepresetnation of thisCastlingRights.- Returns:
-
"KQkq","-"or similar. - Return type:
-
str
>>> NO_CASTLING.fen() '-' >>> CastlingRights([WHITE_KINGSIDE, WHITE_QUEENSIDE]).fen() 'KQ' >>> ALL_CASTLING.fen() 'KQkq'
- __contains__(castling_type: CastlingType) bool#
-
Return
Trueif castling_type is present.- Parameters:
-
castling_type (CastlingType) – entry to test.
- Returns:
-
membership flag.
- Return type:
-
bool
>>> WHITE_KINGSIDE in ALL_CASTLING True >>> BLACK_KINGSIDE in CastlingRights([WHITE_QUEENSIDE]) False
- __iter__() Iterator[CastlingType]#
-
Iteratator over included
CastlingTypevalues
- __len__() int#
-
The number of castling types included
- __bool__() CastlingRights#
-
Returns
Trueif any castling rights are present, alias forCastlingRights.any()
- __add__(other: CastlingRights) CastlingRights#
-
Return the union of two castling-rights sets.
- Parameters:
-
other (CastlingRights) – rights to add.
- Returns:
-
combined rights.
- Return type:
>>> CastlingRights.from_fen("KQ") + CastlingRights.from_fen("kq") == ALL_CASTLING True
- __eq__(other: Any) bool#
-
Return
Trueif other has the identical rights set.
- __le__(other: CastlingRights) bool#
-
Return
Trueif this set is a subset of other.>>> CastlingRights.from_fen("KQ") <= CastlingRights.from_fen("KQkq") True >>> CastlingRights.from_fen("KQkq") <= CastlingRights.from_fen("KQkq") True
- __lt__(other: CastlingRights) bool#
-
Return
Trueif this set is a strict subset of other.>>> CastlingRights.from_fen("KQ") < CastlingRights.from_fen("KQkq") True >>> CastlingRights.from_fen("KQkq") < CastlingRights.from_fen("KQkq") False
- __gt__(other: CastlingRights) bool#
-
Return
Trueif this set is a strict superset of other.>>> CastlingRights.from_fen("KQkq") > CastlingRights.from_fen("KQ") True >>> CastlingRights.from_fen("KQkq") > CastlingRights.from_fen("KQkq") False
- __ge__(other: CastlingRights) bool#
-
Return
Trueif this set is a superset of, or equal to, other.>>> CastlingRights.from_fen("KQkq") >= CastlingRights.from_fen("KQ") True >>> CastlingRights.from_fen("KQkq") >= CastlingRights.from_fen("KQkq") True
- __str__() str#
-
Alias for
CastlingRights.fen()- Returns:
-
"KQkq","-"or similar. - Return type:
-
str
>>> str(NO_CASTLING) '-' >>> str(CastlingRights([WHITE_KINGSIDE, WHITE_QUEENSIDE])) 'KQ' >>> str(ALL_CASTLING) 'KQkq'
- full(color: Color | None = None) bool#
-
Return
Trueif all relevant rights are present.- Parameters:
-
color (Color | None) – optionally restrict the check to
WHITEorBLACK. - Returns:
-
completeness flag.
- Return type:
-
bool
>>> ALL_CASTLING.full() True >>> CastlingRights.from_fen("KQk").full() False >>> CastlingRights.from_fen("KQk").full(WHITE) True >>> NO_CASTLING.full() False
- any(color: Color | None = None) bool#
-
Return
Trueif any castling right is present.- Parameters:
-
color (Color | None) – optionally restrict the check to
WHITEorBLACK. - Returns:
-
presence flag.
- Return type:
-
bool
>>> ALL_CASTLING.any() True >>> CastlingRights.from_fen("KQk").any() True >>> CastlingRights.from_fen("K").any() True >>> CastlingRights.from_fen("K").any(BLACK) False >>> NO_CASTLING.any() False
- kingside(color: Color | None = None) bool#
-
Return
Trueif any kingside right is present.- Parameters:
-
color (Color | None) – optionally restrict the check to
WHITEorBLACK. - Returns:
-
kingside flag.
- Return type:
-
bool
>>> ALL_CASTLING.kingside() True >>> CastlingRights.from_fen("Q").kingside() False >>> CastlingRights.from_fen("K").kingside() True >>> CastlingRights.from_fen("K").kingside(BLACK) False >>> NO_CASTLING.kingside() False
- queenside(color: Color | None = None) bool#
-
Return
Trueif any queenside right is present.- Parameters:
-
color (Color | None) – optionally restrict the check to
WHITEorBLACK. - Returns:
-
queenside flag.
- Return type:
-
bool
>>> ALL_CASTLING.queenside() True >>> CastlingRights.from_fen("Q").queenside() True >>> CastlingRights.from_fen("K").queenside() False >>> CastlingRights.from_fen("Q").queenside(BLACK) False >>> NO_CASTLING.queenside() False
- __repr__() str#
- __hash__() int#
- ALL_CASTLING: CastlingRights#
-
Castling rights which include all types of castling
- NO_CASTLING: CastlingRights#
-
Castling rights which include no types of castling
- class Board#
-
A mutable chess position.
A
Boardrepresents a configuration of chess pieces as a mapping of eachSquareto optional anPiece. TheBoardclass includes attributes forCastlingRights, the existence of an en-passantSquare, and theColorfor the turn of the current player. Also holds the half-move clock and full-move number each as anint.The
Boardclass provides an interface for generatingMoveobjects representing legal actions for a turn, as well as applying and undoing these moves.- static from_fen(fen: str) Board#
-
Build a board from a
strof a Forsyth-Edwards Notation (FEN) representation of a position.The FEN is not required to include a halfmove clock or fullmove number. The default values for these are 0 and 1.
- Parameters:
-
fen (str) – full FEN record.
- Returns:
-
board represented by fen.
- Return type:
- Raises:
-
ValueError – if fen is malformed.
>>> board = Board.from_fen("rnbqkbnr/pp1ppppp/8/2p5/4P3/5N2/PPPP1PPP/RNBQKB1R b KQkq - 1 2") >>> print(board) r n b q k b n r p p - p p p p p - - - - - - - - - - p - - - - - - - - - P - - - - - - - - N - - P P P P - P P P R N B Q K B - R
- static empty() Board#
-
Creates a completely empty
Board, with no pieces on it.- Returns:
-
An empty position
- Return type:
-
:class:
Board
- property halfmove_clock: int#
-
Gets the current halfmove clock as an
int. This represents the number of ply that have passed since a capture or pawn advance.
- property fullmove_number: int#
-
Gets the current fullmove number as an
int. This represents the total number of turns each player has taken since the start of a game.
- property en_passant_square: Square | None#
-
Gets the current en passant
Square, if it exists. Otherwise returnsNone.
- property castling_rights: CastlingRights#
-
Gets the current
CastlingRightsof thisBoard.
- legal_moves() list[Move]#
-
Generates a
listof legalMoveobjects for thisBoard’s position.- Returns:
-
A list of legal moves for this position.
- Return type:
-
list[Move]
>>> Board().legal_moves() [<Move: b1a3>, <Move: b1c3>, <Move: g1f3>, <Move: g1h3>, <Move: a2a3>, <Move: a2a4>, <Move: b2b3>, <Move: b2b4>, <Move: c2c3>, <Move: c2c4>, <Move: d2d3>, <Move: d2d4>, <Move: e2e3>, <Move: e2e4>, <Move: f2f3>, <Move: f2f4>, <Move: g2g3>, <Move: g2g4>, <Move: h2h3>, <Move: h2h4>]
- apply(move: Move | None) None#
-
Applies the given
Moveto thisBoard.The
Moveargument is not checked to be legal outside of checking if the origin has a Piece.Nonecan be passed as the argument to skip a turn.- Parameters:
-
move (Move | None) – The move to apply, or
Nonefor a null move. - Raises:
-
ValueError – if the origin of move does not have a piece.
>>> board = Board() >>> board.apply(Move(E2, E4)) >>> print(board) r n b q k b n r p p p p p p p p - - - - - - - - - - - - - - - - - - - - P - - - - - - - - - - - P P P P - P P P R N B Q K B N R
- Raises:
-
ValueErrorif the givenMove’s origin is an empty square.
- undo() Move | None#
-
Undoes the last
Moveapplied to thisBoard.- Returns:
-
The last move applied to this board.
- Return type:
-
Move | None
- Raises:
-
AttributeErrorif there are no moves to undo. This is true whenBoard.historyhas a len() of 0
>>> board = Board() >>> board.apply(Move(E2, E4)) >>> board.apply(Move(E7, E5)) >>> board.undo() == Move(E7, E5) True >>> print(board) r n b q k b n r p p p p p p p p - - - - - - - - - - - - - - - - - - - - P - - - - - - - - - - - P P P P - P P P R N B Q K B N R
- Raises:
-
AttributeErrorif there are no moves to undo.
- fen() str#
-
Gets the Forsyth-Edwards Notation representation as a
strof thisBoard.- Returns:
-
A FEN representing the position
- Return type:
-
str
>>> board = Board() >>> board.fen() 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'
- copy() Board#
-
Returns a new
Boardwhich is an exact copy of thisBoard, including itsMovehistory.- Returns:
-
A deep copy of this position and its state.
- Return type:
- __getitem__(none: None) Bitboard#
- __getitem__(square: Square) Piece | None#
- __getitem__(piece_type: PieceType) Bitboard#
- __getitem__(color: Color) Bitboard#
- __getitem__(piece: Piece) Bitboard#
- __getitem__(piece_tuple: tuple[Color, PieceType]) Bitboard#
-
Index a
Boardwith a value. Can either index with aSquare,None, aPieceType, aColor, atuple[Color, PieceType], or aPiece,If given a
Square, returns thePieceat the square, orNoneif the square is unoccupied.>>> board = Board() >>> board[E2] is Piece(WHITE, PAWN) True >>> board[E4] is None True
If given
None, returns aBitboardof all empty squares.>>> board = Board() >>> print(board[None]) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
If given a
Color, returns aBitboardof all squares with a piece of that color>>> board = Board() >>> print(board[WHITE]) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
If given a
PieceType, returns aBitboardof all squares with a piece of that type.>>> board = Board() >>> print(board[PAWN]) 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
If given a
Pieceor a tuple of aColorand aPieceType, returns aBitboardof all squares with matching pieces.>>> board = Board() >>> board[WHITE, PAWN] == board[Piece(WHITE, PAWN)] True >>> print(board[WHITE, PAWN]) 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0
- __setitem__(square: Square, piece: Piece | None)#
-
Sets this
Boardto have the givenPieceand the indexedSquare. If set toNone, theSquarebecomes empty.>>> board = Board() >>> board[E2] = None >>> board[E4] = Piece(WHITE, PAWN) >>> print(board) r n b q k b n r p p p p p p p p - - - - - - - - - - - - - - - - - - - - P - - - - - - - - - - - P P P P - P P P R N B Q K B N R
- __delitem__(square: Square)#
-
Deletes any
Pieceat the specifiedSquare, leaving theSquareempty.>>> board = Board() >>> del board[E2] >>> print(board) r n b q k b n r p p p p p p p p - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - P P P P - P P P R N B Q K B N R
- __eq__(other: Any) bool#
-
Returns
Trueif compared with anotherBoardwith the same mapping ofSquaretoPieceobjects, equvilentCastlingRights, en-passantSquarevalues, and halfmove and fullmove clocks.To check if two
Boardinstances are “legally” equal, as in in terms of all of the above besides the halfmove and fullmove clocks, useutils.legally_equal()Two boards may be considered equal despite having different move histories.
- __hash__() int#
-
Performs a Zobrist hash of this Board.
- __contains__(piece: Piece | None) bool#
-
Returns
Trueif thisBoardhas the specifiedPiece. When givenNone, returnsTrueif there are any empy squares.
- property history: list[Move]#
-
Gets a
listofMoveobjects of everyMovewhich have been used withBoard.apply()and have not been undone withBoard.undo()for thisBoard>>> board = Board() >>> board.apply(Move(E2, E4)) >>> board.apply(Move(E7, E5)) >>> board.apply(Move(G1, F3)) >>> board.history [<Move: e2e4>, <Move: e7e5>, <Move: g1f3>]
- __str__()#
-
Returns an ASCII
strrepresentation of thisBoard.>>> print(str(Board())) r n b q k b n r p p p p p p p p - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - P P P P P P P P R N B Q K B N R
>>> FEN = "rnb3r1/R3Q3/2p5/1p1k1p1r/1n1P4/8/4P3/2K2B1r b - - 3 69" >>> board = Board.from_fen(FEN) >>> print(str(board)) r n b - - - r - R - - - Q - - - - - p - - - - - - p - k - p - r - n - P - - - - - - - - - - - - - - - - P - - - - - K - - B - r
- pretty(color_scheme: Board = Board.OAK, highlighted_squares: Bitboard = EMPTY_BB, targeted_squares: Bitboard = EMPTY_BB) str#
-
A pretty to-string method for terminal outputs.
Creates a
strrepresentation of thisBoardusing Unicode chess figurines and the providedBoard.ColorSchemeas a palette for the background and highlights.Bitboard’s can be specified for highlighting particular squares, as for example aMove’s origin, as well as for targetting certain squares, as for possibleMovedestinations.- Returns:
-
A rendering of this position as a UTF-8 string with
ANSIcolor codes - Return type:
-
str
- class ColorScheme#
-
A pallete of colors to be used with
Board.pretty()to stylize printed boards.
- LAGOON: Board.ColorScheme#
-
A light blue color pallete
- SLATE: Board.ColorScheme#
-
A slightly purplish, grey color pallete
- OAK: Board.ColorScheme#
-
A classical, wood styled color pallete
- WALNUT: Board.ColorScheme#
-
A less saturated wood styled color pallete
- GREEN: Board.ColorScheme#
-
A familiar green and white color pallete
- ROSE: Board.ColorScheme#
-
A pinkish red color pallete
- CLAY: Board.ColorScheme#
-
A dulled, rosy brown and grey color pallete
- STEEL: Board.ColorScheme#
-
A monochromatic grey color pallete
- class BoardStatus#
-
A predicate-like object that answers the question “is this board in status X?” Examples of statuses include check, check-mate, stalemate, and repetition or 50-move draw claims.
- __contains__(board: Board) bool#
-
Return
Trueif board satisfies this status.- Parameters:
-
board (Board) – position to test.
- Returns:
-
membership flag.
- Return type:
-
bool
Examples#
>>> Board() in DRAW False >>> FEN = "r1bqkb1r/pppp1Qpp/2n2n2/4p3/2B1P3/8/PPPP1PPP/RNB1K1NR b KQkq - 0 4" >>> board = Board.from_fen(FEN) >>> print(board) r - b q k b - r p p p p - Q p p - - n - - n - - - - - - p - - - - - B - P - - - - - - - - - - - P P P P - P P P R N B - K - N R
>>> board in CHECKMATE True
- __repr__() str#
- __eq__(other: Any) bool#
- CHECK: BoardStatus#
-
The side to move is in check
- MATE: BoardStatus#
-
The side to move has no legal moves.
- CHECKMATE: BoardStatus#
-
The side to move is in checkmate. The player is in check and has no legal moves.
- STALEMATE: BoardStatus#
-
The side to move is in stalemate. The player has no legal moves but is not in check.
- INSUFFICIENT_MATERIAL: BoardStatus#
-
There is not enough material for either player to be placed in checkmate.
- FIFTY_MOVE_TIMEOUT: BoardStatus#
-
Fifty full-moves have passed without a pawn move or capture.
- SEVENTY_FIVE_MOVE_TIMEOUT: BoardStatus#
-
Seventy five full-moves have passed without a pawn move or capture.
- THREEFOLD_REPETITION: BoardStatus#
-
The same position has occured at least three times.
- FIVEFOLD_REPETITION: BoardStatus#
-
The same position has occured at least five times.
- DRAW: BoardStatus#
-
Any position in which a player may claim a draw. This includes stalemate, insufficient material, a fifty move timeout, or threefold repetition.
- FORCED_DRAW: BoardStatus#
-
A forced draw. This includes stalemate, insufficient material, a seventy five move timeout, or fivefold repetition.