Board

This module has the Board class.

class board.Board[source]

Board class

add_piece(piece: PieceType, *, color: Color = Color.WHITE, position: tuple) None[source]

Add piece to the board.

Parameters
  • piece (PieceType) – Bishop, King, Knight, Pawn, Queen or Rook

  • color (Color) – Color.WHITE or Color.BLACK (default is Color.WHITE)

  • position (tuple) – (file, rank) for position

add_piece_set() None[source]

Adds piece set with default positions by color to the board.

property board: dict

Returns the board dictionary with 64 key-value pairs. Keys are two-item tuples, (file, rank), and values are None (empty positions) or Piece (Bishop, King, Knight, Pawn, Queen or Rook). A read-only property.

Getter

board dictionary

Type

dict

Raises

AttributeError – setting read-only property

check_end_position(start: tuple, end: tuple) tuple[source]

Check for capture or empty at end position and returns corresponding Status.

Parameters
  • start (tuple) – (file, rank) for start position

  • end (tuple) – (file, rank) for end position

Returns

Status.CAPTURE, Status.INVALID or Status.VALID

Return type

Status

convert_position()[source]

Converts an algebriac notation string, 'a1', to a tuple, (97, 1), or vice versa.

Parameters

value (str | tuple) – two-character string or (file, rank)

Raises
  • NotImplementedError – Types other than string or tuple.

  • ValueError – Not a two-character string or tuple.

Returns

(file, rank) or two-character string

Return type

tuple | string

has_empty_positions(start: tuple, end: tuple) bool[source]

Determines empty positions between start and end positions and returns Boolean.

Parameters
  • start (tuple) – (file, rank) for start position

  • end (tuple) – (file, rank) for end position

Returns

True or False

Return type

bool

static is_diagonal_move(start: tuple, end: tuple) bool[source]

Determines diagonal move and returns Boolean.

Parameters
  • start (tuple) – (file, rank) for start position

  • end (tuple) – (file, rank) for end position

Returns

True or False

Return type

bool

static is_short_move(start: int, end: int, *, limit: int = 1) bool[source]

Determines short move (one or two positions) and returns Boolean.

Parameters
  • start (int) – integer for end position

  • end (int) – integer for start position

  • limit (int) – integer, 1 (default value) or 2

Raises

ValueError – invalid limit values

Returns

True or False

Return type

bool

swap_positions(start: tuple, end: tuple) None[source]

Swap two positions on the board.

Parameters
  • start (tuple) – (file, rank) for start position

  • end (tuple) – (file, rank) for end position

validate_move_bishop(start: tuple, end: tuple) tuple[source]

Validate move for bishop. One to seven positions, diagonally. Returns Status.INVALID or Status.VALID with an empty tuple, or Status.CAPTURE with (file, rank) of captured piece.

Parameters
  • start (tuple) – (file, rank) for start position

  • end (tuple) – (file, rank) for end position

Returns

(Status, ()) or (Status, (file, rank))

Return type

tuple

validate_move_king(start: tuple, end: tuple) tuple[source]

Validate move for the king. One position, any direction, or two positions horizontally when castling with a rook. Returns Status.INVALID or Status.VALID with an empty tuple, Status.CASTLING with (file, rank) of rook, or Status.CAPTURE with (file, rank) of captured piece.

Parameters
  • start (tuple) – (file, rank) for start position

  • end (tuple) – (file, rank) for end position

Returns

(Status, ()) or (Status, (file, rank))

Return type

tuple

validate_move_knight(start: tuple, end: tuple) tuple[source]

Validate move for knight. Move two positions, horizontally or vertically, and one position over. Returns Status.INVALID or Status.VALID with an empty tuple, or Status.CAPTURE with (file, rank) of captured piece.

Parameters
  • start (tuple) – (file, rank) for start position

  • end (tuple) – (file, rank) for end position

Returns

(Status, ()) or (Status, (file, rank))

Return type

tuple

validate_move_pawn(start: tuple, end: tuple) tuple[source]

Validate move for pawn. Forward one position, two positions (first move only), or diagonally one position for en passant or capture. Returns Status.INVALID or Status.VALID with an empty tuple, or Status.CAPTURE or Status.EN_PASSANT with (file, rank) of captured piece.

Parameters
  • start (tuple) – (file, rank) for start position

  • end (tuple) – (file, rank) for end position

Returns

(Status, ()) or (Status, (file, rank))

Return type

tuple

validate_move_queen(start: tuple, end: tuple) tuple[source]

Validate move for the queen. One to seven positions, any direction. Returns Status.INVALID or Status.VALID with an empty tuple, or Status.CAPTURE with (file, rank) of captured piece.

Parameters
  • start (tuple) – (file, rank) for start position

  • end (tuple) – (file, rank) for end position

Returns

(Status, ()) or (Status, (file, rank))

Return type

tuple

validate_move_rook(start: tuple, end: tuple) tuple[source]

Validate move for rook. One to seven positions, horizontal or vertical. Returns Status.INVALID or Status.VALID with an empty tuple, or Status.CAPTURE with (file, rank) of captured piece.

Parameters
  • start (tuple) – (file, rank) for start position

  • end (tuple) – (file, rank) for end position

Returns

(Status, ()) or (Status, (file, rank))

Return type

tuple