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
Color
instance.- 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
Color
to this one.>>> WHITE.opposite is BLACK True >>> BLACK.opposite is WHITE True
- __str__() str #
Serializes a
Color
to astr
of its name in title case.- Returns:
A
str
of theColor
’s name
>>> str(WHITE) "White" >>> str(BLACK) "Black"
- __invert__() Color #
Alias for
Color.opposite
, Allows~WHITE
syntax.
- __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
PieceType
to astr
of 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
PieceType
values. 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
WHITE
pieces, 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
True
when compared with anotherPiece
with the samePieceType
andColor
- __str__() str #
Serializes a
Piece
as a single ASCII characterstr
. Uses uppercase for anyPiece
that isWHITE
, and lowercase for anyPiece
that 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
Square
encoded by name (case‑insensitive).- Parameters:
name (str) – The name of the square
- Return type:
- Returns:
The corresponding
Square
- Raises:
ValueError
if 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
Bitboard
containing only thisSquare
.>>> A1.bb() == Bitboard([A1]) True >>> H3.bb() == Bitboard([H3]) True
- adjacent() Bitboard #
Creates a
Bitboard
of 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
None
if 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
None
if 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
None
if 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
None
if 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
None
if 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
None
if 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
None
if 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
None
if 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
- __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
Square
is included.- __init__(squares: Collection[Square])#
Initialize a
Bitboard
that contains squares.- Parameters:
squares (Collection[Square]) – squares to include in the new bitboard.
- __str__() str #
Return a
str
in which included squares are shown as1
and 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
Bitboard
from 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 < 0
orvalue >= 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
- __getitem__(square: Square) bool #
Return
True
if square is in thisBitboard
.- Parameters:
square (Square) – square to query.
- Returns:
membership flag.
- Return type:
bool
>>> bb = Bitboard([A1, B2, C3]) >>> bb[A1] True >>> bb[A2] False
- __setitem__(square: Square, value: bool)#
Add or remove square depending on value.
- Parameters:
square (Square) – square to modify.
value (bool) –
True
→ add,False
→ remove.
>>> bb = Bitboard([A1, B2, C3]) >>> bb2 = Bitboard([B2, C3]) >>> bb3 = Bitboard([B2, C3, C4]) >>> bb[A1] = False >>> bb == bb2 True >>> bb[C4] = True >>> bb == bb3 True
- __delitem__(square: Square)#
Remove square from the
Bitboard
.- Parameters:
square (Square) – square to delete.
>>> bb = Bitboard([A1, B2, C3]) >>> bb2 = Bitboard([B2, C3]) >>> del bb[A1] >>> bb == bb2 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
True
if other is aBitboard
with 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) Bitboard #
Return the intersection of two bitboards.
- Parameters:
other (Bitboard) – bitboard to intersect with.
- Returns:
squares common to both operands.
- Return type:
>>> Bitboard([A1]) & Bitboard([A1, A2]) == Bitboard([A1]) True >>> LIGHT_SQUARE_BB & DARK_SQUARE_BB == EMPTY_BB True
- __or__(other: Bitboard) Bitboard #
Return the union of two bitboards.
- Parameters:
other (Bitboard) – bitboard to union with.
- Returns:
squares contained in either operand.
- Return type:
>>> Bitboard([A1]) | Bitboard([A1, A2]) == Bitboard([A1, A2]) True >>> LIGHT_SQUARE_BB | DARK_SQUARE_BB == FULL_BB True
- __xor__(other: Bitboard) Bitboard #
Return the symmetric difference of two bitboards.
- Parameters:
other (Bitboard) – bitboard to XOR with.
- Returns:
squares in exactly one operand.
- Return type:
>>> Bitboard([A1]) ^ Bitboard([A1, A2]) == Bitboard([A2]) True >>> LIGHT_SQUARE_BB ^ DARK_SQUARE_BB == FULL_BB True
- __bool__() bool #
Return
True
if theBitboard
is 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
WHITE
orBLACK
.- 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
True
if 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
WHITE
kingside castling.
- WHITE_QUEENSIDE: CastlingType#
Castling type representing
WHITE
queenside castling.
- BLACK_KINGSIDE: CastlingType#
Castling type representing
BLACK
kingside castling.
- BLACK_QUEENSIDE: CastlingType#
Castling type representing
BLACK
queenside 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
str
and 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
None
for 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
str
in 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
str
of 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
Square
the piece moves from.>>> Move(E2, E4).origin is E2 True >>> Move.from_uci("a1d4").origin is A1 True
- property destination: Square#
The
Square
the piece moves to.>>> Move(E2, E4).destination is E4 True >>> Move.from_uci("a1d4").destination is D4 True
- property promotion: PieceType | None#
Promotion
PieceType
, orNone
for a non-promotion.>>> Move.from_uci("b7b8q").promotion is QUEEN True >>> Move(E2, E4).promotion is None True
- is_promotion() bool #
Return
True
if the move is a promotion.>>> Move.from_uci("b7b8q").is_promotion() True >>> Move(E2, E4).is_promotion() False
- is_capture(board: Board) bool #
Return
True
if 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
True
if 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
True
if 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
CastlingType
values 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
CastlingRights
object 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
str
represetnation 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
True
if 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
CastlingType
values
- __len__() int #
The number of castling types included
- __bool__() CastlingRights #
Returns
True
if 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
True
if other has the identical rights set.
- __le__(other: CastlingRights) bool #
Return
True
if 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
True
if 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
True
if 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
True
if 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
True
if all relevant rights are present.- Parameters:
color (Color | None) – optionally restrict the check to
WHITE
orBLACK
.- 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
True
if any castling right is present.- Parameters:
color (Color | None) – optionally restrict the check to
WHITE
orBLACK
.- 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
True
if any kingside right is present.- Parameters:
color (Color | None) – optionally restrict the check to
WHITE
orBLACK
.- 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
True
if any queenside right is present.- Parameters:
color (Color | None) – optionally restrict the check to
WHITE
orBLACK
.- 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
Board
represents a configuration of chess pieces as a mapping of eachSquare
to optional anPiece
. TheBoard
class includes attributes forCastlingRights
, the existence of an en-passantSquare
, and theColor
for the turn of the current player. Also holds the half-move clock and full-move number each as anint
.The
Board
class provides an interface for generatingMove
objects 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
str
of 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
CastlingRights
of thisBoard
.
- legal_moves() list[Move] #
Generates a
list
of legalMove
objects 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
Move
to thisBoard
.The
Move
argument is not checked to be legal outside of checking if the origin has a Piece.None
can be passed as the argument to skip a turn.- Parameters:
move (Move | None) – The move to apply, or
None
for 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:
ValueError
if the givenMove
’s origin is an empty square.
- undo() Move | None #
Undoes the last
Move
applied to thisBoard
.- Returns:
The last move applied to this board.
- Return type:
Move | None
- Raises:
AttributeError
if there are no moves to undo. This is true whenBoard.history
has 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:
AttributeError
if there are no moves to undo.
- fen() str #
Gets the Forsyth-Edwards Notation representation as a
str
of 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
Board
which is an exact copy of thisBoard
, including itsMove
history.- Returns:
A deep copy of this position and its state.
- Return type:
- __getitem__(none: None) Bitboard #
Index a
Board
with a value. Can either index with aSquare
,None
, aPieceType
, aColor
, atuple[Color, PieceType]
, or aPiece
,If given a
Square
, returns thePiece
at the square, orNone
if the square is unoccupied.>>> board = Board() >>> board[E2] is Piece(WHITE, PAWN) True >>> board[E4] is None True
If given
None
, returns aBitboard
of 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 :class:
Color
, returns aBitboard
of 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 :class:
PieceType
, returns aBitboard
of 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
Piece
or a tuple of aColor
and aPieceType
, returns aBitboard
of 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
Board
to have the givenPiece
and the indexedSquare
. If set toNone
, theSquare
becomes 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
Piece
at the specifiedSquare
, leaving theSquare
empty.>>> 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
True
if compared with anotherBoard
with the same mapping ofSquare
toPiece
objects, equvilentCastlingRights
, en-passantSquare
values, and halfmove and fullmove clocks.To check if two
Board
instances 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
True
if thisBoard
has the specifiedPiece
. When givenNone
, returnsTrue
if there are any empy squares.
- property history: list[Move]#
Gets a
list
ofMove
objects of everyMove
which 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
str
representation 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
str
representation of thisBoard
using Unicode chess figurines and the providedBoard.ColorScheme
as 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 possibleMove
destinations.- Returns:
A rendering of this position as a UTF-8 string with
ANSI
color 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
True
if 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.