Constants for the side to move or the color of a piece.
- chess.WHITE = 0¶
- chess.BLACK = 1¶
You can get the opposite color using color ^ 1.
- chess.NONE = 0¶
The castling flags
- chess.CASTLING_NONE = 0¶
can be combined bitwise.
- chess.CASTLING_WHITE = CASTLING_WHITE_QUEENSIDE | CASTLING_WHITE_KINGSIDE¶
- chess.CASTLING_BLACK = CASTLING_BLACK_QUEENSIDE | CASTLING_BLACK_KINGSIDE¶
- chess.CASTLING = CASTLING_WHITE | CASTLING_BLACK¶
- chess.A1 = 0¶
- chess.B1 = 1¶
and so on to
- chess.H8 = 63¶
Gets the file index of square where 0 is the a file.
Gets the rank index of the square where 0 is the first rank.
- class chess.Piece(piece_type, color)¶
A piece with type and color.
The piece type.
The piece color.
Gets the symbol P, N, B, R, Q or K for white pieces or the lower-case variants for the black pieces.
- classmethod from_symbol(symbol)¶
Creates a piece instance from a piece symbol.
Raises ValueError if the symbol is invalid.
- class chess.Move(from_square, to_square, promotion=0)¶
Represents a move from a square to a square and possibly the promotion piece type.
Castling moves are identified only by the movement of the king.
Null moves are supported.
The source square.
The target square.
Gets an UCI string for the move.
For example a move from A7 to A8 would be a7a8 or a7a8q if it is a promotion to a queen. The UCI representatin of null moves is 0000.
- classmethod from_uci(uci)¶
Parses an UCI string.
Raises ValueError if the UCI string is invalid.
- classmethod null()¶
Gets a null move.
A null move just passes the turn to the other side (and possibly forfeits en-passant capturing). Null moves evaluate to False in boolean contexts.
>>> bool(chess.Move.null()) False
- chess.STARTING_FEN = 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1'¶
The FEN notation of the standard chess starting position.
- class chess.Bitboard(fen=None)¶
A bitboard and additional information representing a position.
Provides move generation, validation, parsing, attack generation, game end detection, move counters and the capability to make and unmake moves.
The bitboard is initialized to the starting position, unless otherwise specified in the optional fen argument.
The side to move.
Bitmask of castling rights.
The potential en-passant square on the third or sixth rank or 0. It does not matter if en-passant would actually be possible on the next move.
Counts move pairs. Starts at 1 and is incremented after every move of the black side.
The number of half moves since the last capture or pawn move.
- pseudo_legal_moves = PseudoLegalMoveGenerator(self)¶
A dynamic list of pseudo legal moves.
Pseudo legal moves might leave or put the king in check, but are otherwise valid. Null moves are not pseudo legal. Castling moves are only included if they are completely legal.
For performance moves are generated on the fly and only when nescessary. The following operations do not just generate everything but map to more efficient methods.
>>> len(board.pseudo_legal_moves) 20
>>> bool(board.pseudo_legal_moves) True
>>> move in board.pseudo_legal_moves True
- legal_moves = LegalMoveGenerator(self)¶
A dynamic list of completely legal moves, much like the pseudo legal move list.
Restores the starting position.
Gets the piece at the given square.
Gets the piece type at the given square.
Removes a piece from the given square if present.
- set_piece_at(square, piece)¶
Sets a piece at the given square. An existing piece is replaced.
- is_attacked_by(color, square)¶
Checks if the given side attacks the given square. Pinned pieces still count as attackers.
- attackers(color, square)¶
Gets a set of attackers of the given color for the given square.
Returns a set of squares.
Checks if the current side to move is in check.
Checks if the given move would move would leave the king in check or put it into check.
Checks if the king of the other side is attacked. Such a position is not valid and could only be reached by an illegal move.
Checks if the game is over due to checkmate, stalemate or insufficient mating material.
Checks if the current position is a checkmate.
Checks if the current position is a stalemate.
Checks for a draw due to insufficient mating material.
Updates the position with the given move and puts it onto a stack.
Null moves just increment the move counters, switch turns and forfeit en passant capturing.
No validation is performed. For performance moves are assumed to be at least pseudo legal. Otherwise there is no guarantee that the previous board state can be restored. To check it yourself you can use:
>>> move in board.pseudo_legal_moves True
Restores the previous position and returns the last move from the stack.
Gets the last move from the move stack.
Parses the given EPD string and uses it to set the position.
If present the hmvc and the fmvn are used to set the half move clock and the ply. Otherwise 0 and 1 are used.
Returns a dictionary of parsed operations. Values can be strings, integers, floats or move objects.
Raises ValueError if the EPD string is invalid.
Gets an EPD representation of the current position.
EPD operations can be given as keyword arguments. Supported operands are strings, integers, floats and moves. All other operands are converted to strings.
hmvc and fmvc are not included by default. You can use:
>>> board.epd(hmvc=board.half_moves, fmvc=board.ply) 'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - hmvc 0; fmvc 1;'
Parses a FEN and sets the position from it.
Rasies ValueError if the FEN string is invalid.
Gets the FEN representation of the position.
Uses the current position as the context to parse a move in standard algebraic notation and return the corresponding move object.
The returned move is guaranteed to be either legal or a null move.
Raises ValueError if the SAN is invalid or ambigous.
Parses a move in standard algebraic notation, makes the move and puts it on the the move stack.
Raises ValueError if neither legal nor a null move.
Returns the move.
Gets the standard algebraic notation of the given move in the context of the current position.
There is no validation. It is only guaranteed to work if the move is legal or a null move.
Gets a bitmask of possible problems with the position. Move making, generation and validation are only guaranteed to work on a completely valid board.
Returns a Zobrist hash of the current position.
A zobrist hash is an exclusive or of pseudo random values picked from an array. Which values are picked is decided by features of the position, such as piece positions, castling rights and en-passant squares. For this implementation an array of 781 values is required.
The default behaviour is to use values from POLYGLOT_RANDOM_ARRAY, which makes for hashes compatible with polyglot opening books.