Core

Colors

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.

Piece types

chess.NONE = 0
chess.PAWN
chess.KNIGHT
chess.BISHOP
chess.ROOK
chess.QUEEN
chess.KING

Castling rights

The castling flags

chess.CASTLING_NONE = 0
chess.CASTLING_WHITE_KINGSIDE
chess.CASTLING_BLACK_KINGSIDE
chess.CASTLING_WHITE_QUEENSIDE
chess.CASTLING_BLACK_QUEENSIDE

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

Squares

chess.A1 = 0
chess.B1 = 1

and so on to

chess.H8 = 63
chess.SQUARES = [A1, B1, ..., G8, H8]
chess.SQUARE_NAMES = ['a1', 'b1', ..., 'g8', 'h8']
chess.file_index(square)

Gets the file index of square where 0 is the a file.

chess.FILE_NAMES = ['a', 'b', ..., 'g', 'h']
chess.rank_index(square)

Gets the rank index of the square where 0 is the first rank.

Pieces

class chess.Piece(piece_type, color)

A piece with type and color.

piece_type

The piece type.

color

The piece color.

symbol()

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.

Moves

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.

from_square

The source square.

to_square

The target square.

promotion

The promotion piece type.

uci()

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

Bitboard

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.

turn

The side to move.

castling_rights

Bitmask of castling rights.

ep_square

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.

fullmove_number

Counts move pairs. Starts at 1 and is incremented after every move of the black side.

halfmove_clock

The number of half moves since the last capture or pawn move.

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.

reset()

Restores the starting position.

clear()

Clears the board.

Resets move stacks and move counters. The side to move is white. There are no rooks or kings, so castling is not allowed.

In order to be in a valid status() at least kings need to be put on the board. This is required for move generation and validation to work properly.

piece_at(square)

Gets the piece at the given square.

piece_type_at(square)

Gets the piece type at the given square.

remove_piece_at(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.

is_check()

Checks if the current side to move is in check.

is_into_check(move)

Checks if the given move would move would leave the king in check or put it into check.

was_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.

is_game_over()

Checks if the game is over due to checkmate, stalemate, insufficient mating material, the seventyfive-move rule or fivefold repitition.

is_checkmate()

Checks if the current position is a checkmate.

is_stalemate()

Checks if the current position is a stalemate.

is_insufficient_material()

Checks for a draw due to insufficient mating material.

is_seventyfive_moves()

Since the first of July 2014 a game is automatically drawn (without a claim by one of the players) if the half move clock since a capture or pawn move is equal to or grather than 150. Other means to end a game take precedence.

is_fivefold_repitition()

Since the first of July 2014 a game is automatically drawn (without a claim by one of the players) if a position occurs for the fifth time on consecutive alternating moves.

can_claim_draw()

Checks if the side to move can claim a draw by the fifty-move rule or by threefold repitition.

can_claim_fifty_moves()

Draw by the fifty-move rule can be claimed once the clock of halfmoves since the last capture or pawn move becomes equal or greater to 100 and the side to move still has a legal move they can make.

can_claim_threefold_repitition()

Draw by threefold repitition can be claimed if the position on the board occured for the third time or if such a repitition is reached with one of the possible legal moves.

push(move)

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
pop()

Restores the previous position and returns the last move from the stack.

peek()

Gets the last move from the move stack.

set_epd(epd)

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 fullmove number. 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.

epd(**operations)

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.halfmove_clock, fmvc=board.fullmove_number)
'rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - hmvc 0; fmvc 1;'
set_fen(fen)

Parses a FEN and sets the position from it.

Rasies ValueError if the FEN string is invalid.

fen()

Gets the FEN representation of the position.

parse_san(san)

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.

push_san(san)

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.

san(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.

status()

Gets a bitmask of possible problems with the position. Move making, generation and validation are only guaranteed to work on a completely valid board.

zobrist_hash(array=None)

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.