Full references

api module full references

class api.Move(type: int, piece, target: tuple, special_type=None)

Bases: object

Moves will be initiated when getting piece’s allowed moves, they can also be used as argument to move a piece There is different types of moves

FORBIDDEN_MOVE = 0
TO_EMPTY_MOVE = 1

A move to a empty case

KILL_MOVE = 2

A move that will kill an opponent’s piece

SPECIAL_MOVE = 3

Ex: for a castling move or a Pawn promoting move (in this case you should specify the wanted class as Pawn attribute, please refer to Pawn)

OVER_CHECK_MOVE = 4

Used to detect check situations (move where you’ll kill a Check piece)

LEADING_TO_CHECK_SITUATION_MOVE = 5

Used to detect check situations (move that will conduct to a Check of its own color, so it’s forbidden)

TEXTURES = {}
CASTLING_TYPE = 1

For SPECIAL_MOVE

EN_PASSANT_TYPE = 2

For SPECIAL_MOVE

TO_PROMOTE_TYPE = 3

special_type if it’s a Pawn promotion move. Be careful, the Move type won’t be SPECIAL_MOVE for promotions. To specify promotion, refer to Pawn

__init__(type: int, piece, target: tuple, special_type=None)
type

Move’s type (int)

piece

Move’s piece

target

Move’s position target (tuple)

special_type

Special type if type==SPECIAL_MOVE or if it’s a Pawn promotion move (value would be TO_PROMOTE_TYPE)

copy(new_piece)
property allowed
property texture
__str__()
class api.Piece(color: int, pos: tuple, board)

Bases: object

Base class for pieces

WHITE_TEXTURE = None
BLACK_TEXTURE = None
SCORE_VALUE = 0
WHITE = 0
BLACK = 1
INT_COLOR_TO_TEXT = {0: 'White', 1: 'Black'}
DIAGONALS_VECTORS = ((-1, -1), (1, -1), (-1, 1), (1, 1))
LINES_VECTORS = ((0, -1), (0, 1), (-1, 0), (1, 0))
KNIGHT_VECTOR = ((1, -2), (2, -1), (2, 1), (1, 2), (-1, 2), (-2, 1), (-2, -1), (-1, -2))
COLLISION_MOVES = (2, 4, 0)
__init__(color: int, pos: tuple, board)
color
pos
board
get_moves_allowed(skip_check_verification=False) list

To get every piece’s moves allowed with the current Board configuration (moves allowed this current turn)

case_allowed(case_pos: tuple, this_move_can_kill=True, skip_check_verification=False) Move

To test if this specific case is currently allowed for the piece

cases_allowed_around(skip_check_verification=False)
cases_allowed_in_diagonals(skip_check_verification=False)
cases_allowed_in_line(skip_check_verification=False)
cases_allowed_for_knight(skip_check_verification=False)
move(pos_or_move: tuple, skip_allowed_verif=False, call_new_turn=True) tuple

To move the piece to a position returns the target position if it worked, otherwise it returns None

property texture
copy(new_board)
__str__()
class api.Rook(color, pos: tuple, board)

Bases: Piece

Class for Rooks, please refer to Piece

NAME = 'Rook'
SCORE_VALUE = 5

Value for score evaluation

__init__(color, pos: tuple, board)
get_moves_allowed(skip_check_verification=False)
class api.Check(color, pos: tuple, board)

Bases: Piece

Class for Checks, please refer to Piece

NAME = 'Check'
IN_CHECK_TEXTURE = None
__init__(color, pos: tuple, board)
in_check_situation(hypothesis=None) bool

Detects if this Check is “in check”

get_moves_allowed(skip_check_verification=False)
class api.Queen(color, pos: tuple, board)

Bases: Piece

Class for Queens, please refer to Piece

NAME = 'Queen'
SCORE_VALUE = 10

Value for score evaluation

__init__(color, pos: tuple, board)
get_moves_allowed(skip_check_verification=False)
class api.Bishop(color, pos: tuple, board)

Bases: Piece

Class for Bishops, please refer to Piece

NAME = 'Bishop'
SCORE_VALUE = 3

Value for score evaluation

__init__(color, pos: tuple, board)
get_moves_allowed(skip_check_verification=False)
class api.Knight(color, pos: tuple, board)

Bases: Piece

Class for Knights, please refer to Piece

NAME = 'Knight'
SCORE_VALUE = 3

Value for score evaluation

__init__(color, pos: tuple, board)
get_moves_allowed(skip_check_verification=False)
class api.Pawn(color, pos: tuple, board)

Bases: Piece

Class for Pawns, please refer to Piece

NAME = 'Pawn'
SCORE_VALUE = 1

Value for score evaluation

__init__(color, pos: tuple, board)
promote_class_wanted
This attribute stores the piece class that will be used if the Pawn promotes
It can be changed whenever in the game, if no value is given we’ll warn it and use a Queen to promote the Pawn
get_moves_allowed(skip_check_verification=False)
class api.Case(square_type, content=None)

Bases: object

BLACK_TEXTURE = None
WHITE_TEXTURE = None
BLACK = 1
WHITE = 0
__init__(square_type, content=None)
class api.Board(pieces_by_pos=None, move_history=[], cur_color_turn=0, verbose=1)

Bases: object

Represents the whole game board, containing pieces and data about current and past turns

BACK_LINE_INIT_POSITIONS = {(0, 0): <class 'api.Rook'>, (1, 0): <class 'api.Knight'>, (2, 0): <class 'api.Bishop'>, (3, 0): <class 'api.Queen'>, (4, 0): <class 'api.Check'>, (5, 0): <class 'api.Bishop'>, (6, 0): <class 'api.Knight'>, (7, 0): <class 'api.Rook'>}
COLORS = (0, 1)
__init__(pieces_by_pos=None, move_history=[], cur_color_turn=0, verbose=1)
move_history
cur_color_turn
cur_color_turn_in_check

If the current playing color is in check

game_ended
winner
pieces_by_color

2-dimensional list representing pieces by color (self.pieces_by_color[0] for white pieces and self.pieces_by_color[0] for black pieces)

pieces_by_pos

Dict representing pieces, keys are positions (tuple) and values are pieces (Piece)

score_evaluation() dict

Returns the current score evaluation for each color | (basic calculation with a static value for each piece type)

get_piece_by_pos(pos)
is_allowed_move(piece: Piece, future_pos: tuple)
move_piece(piece, pos_or_move: tuple, skip_allowed_verif=False, call_new_turn=True) tuple

Enables you to move a piece instead of doing it with the Piece obj, returns None if the move isn’t allowed

create_hypothesis_board(pieces_with_pos_to_change={})
Allows you to create hypothesis boards, an independent copy of the current Board
Returns another Board obj

render module full references

class render.Gui(board: Board, colors_managed_by_gui=(0, 1), window_title='Chess Game', SCREEN_SIZE=(800, 800), FPS=60, verbose=1)

Bases: object

Class for the pygame’s gui, it will enable you to display the game and human players to move pieces

PIECE_TYPE_NAME_TO_OBJ = {'bishop': <class 'pygame_chess_api.api.Bishop'>, 'check': <class 'pygame_chess_api.api.Check'>, 'knight': <class 'pygame_chess_api.api.Knight'>, 'pawn': <class 'pygame_chess_api.api.Pawn'>, 'queen': <class 'pygame_chess_api.api.Queen'>, 'rook': <class 'pygame_chess_api.api.Rook'>}
ASSETS_FOLDER = '/home/docs/checkouts/readthedocs.org/user_builds/pygame-chess-api/checkouts/latest/src/pygame_chess_api/assets'
__init__(board: Board, colors_managed_by_gui=(0, 1), window_title='Chess Game', SCREEN_SIZE=(800, 800), FPS=60, verbose=1)
colors_managed
Tuple containing the colors managed by human moves with the gui.
The color(s) not in this tuple/list must be managed by AI with the parameter function function_for_AIs in method run_pygame_loop()
run_pygame_loop(function_for_AIs=None)

Run the Pygame gui loop to show and interact with the pygame window. The function_for_AIs must have one input: the board object We’ll call it when this is turn for a color that should not be managed by gui (an AI)

Obviously the move method called by your AI must be part of the given Board (Board.move_piece or a Piece.move)

get_mouse_pos()
draw_board()
generate_textures()
choose_a_pawn_promote(color: int)
mouse_left_clicked()
mouse_released()