Pieces

This module has the Piece base class, Bishop, King, Knight, Pawn, Rook, and Queen subclasses, and several factory methods for creating pieces.

class pieces.Piece(*, color: Color = Color.WHITE, position: Optional[tuple] = None)[source]

Base class to derive the Bishop, King, Knight, Pawn, Rook, and Queen subclasses.

Parameters
  • color (Color) – Color.WHITE (default value) or Color.BLACK

  • position (tuple) – two-item integer tuple (default is None)

__repr__() str[source]

Returns string as “<piece name> (color=<bool>, position=<None|tuple>)”.

__str__() str[source]

Returns string as “<piece name> <color name>[ <position>]”.

property color: Color

Piece color.

Getter

True for white, False for black

Type

bool

Raises

AttributeError – read-only property

property first_move: bool

First move for the piece.

Getter

True for first move, False if not

Type

bool

Raises

AttributeError – read-only property

property position: None | tuple

Position on the board. When position is updated for first time, first move will change from True to False.

Getter

None (not set) or (file, rank)

Type

None | tuple

Setter

(file, rank)

Type

tuple

class pieces.Bishop(*, color: Color = Color.WHITE, position: Optional[tuple] = None)[source]

A bishop can move diagonally from one to seven positions.

class pieces.King(color: Color = Color.WHITE, position: Optional[tuple] = None)[source]

A king can move one position in any direction, or two positions horizontally when castling with a rook.

property check: bool

Check by another piece.

Getter

True if checked, False if not (default is False)

Type

bool

Setter

True or False

Type

bool

class pieces.Knight(*, color: Color = Color.WHITE, position: Optional[tuple] = None)[source]

A knight can jump two positions in one direction and one position over.

class pieces.Pawn(*, color: Color = Color.WHITE, position: Optional[tuple] = None)[source]

A pawn can only move foward. One or two positions on the first move, one position on or after the first move, and diagonally one position for capture or en passant.

class pieces.Queen(*, color: Color = Color.WHITE, position: Optional[tuple] = None)[source]

A queen can move one to seven positions in any direction.

class pieces.Rook(*, color: Color = Color.WHITE, position: Optional[tuple] = None)[source]

A rook can move one to seven positions horizontally or vertically.

pieces.factory_piece(piece: PieceType, *, color: Color = Color.WHITE, position: Optional[tuple] = None) Piece[source]

Factory method to create a piece.

Parameters
  • piece (PieceType) – BISHOP, KING, KNIGHT, PAWN, QUEEN, ROOK

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

  • position (tuple) – (file, rank) (default is None)

Returns

specified piece

Return type

Piece

Raises

ValueError – invalid PieceType for piece

pieces.factory_set(*, color: Color = Color.WHITE) Iterable[source]

Factory method to create a set of 16 pieces with assigned positions based on color. Calls the factory_piece() method to create indivdual pieces.

Parameters

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

Returns

map object with 16 tuples, ((file, rank), Piece)

Return type

Iterable