nerdchess package

Submodules

nerdchess.board module

This module represents a board in a game of chess.

class nerdchess.board.Board

Bases: object

Represents a board in a game of chess.

{
a: {
1: (Square), 2: (Square), etc…

}, b: {

1: (Square), 2: (Square), etc…

} etc…

}

letters

The letters of a board

Type:list
numbers

The numbers of a board

Type:list
squares

A dict of letters containing numbers with squares

Type:dict
castle(side, color)

Perform castling on a board.

Parameters:
  • side (CastleSide) – The side to castle to
  • color (Color) – The color performing the castle
Returns:

A new board with the processed move

Return type:

newboard

is_check(color=None)

Is one of the kings in check.

Parameters:color (optional) – The color to check for
Returns:The color of the king that is in check or False
Return type:color
is_checkmate()

Is one of the kings in checkmate.

Returns:Color of the king in mate or False
Return type:color
matrix()

Return a matrix of the board represented as a nested list.

new_board(move)

Create a new board from a supplied move.

This does not do any explicit validation on the move. It does set a piece’s captured status to True when needed. It does set a piece’s new position.

Parameters:move (Move) – The move to process
Returns:The new board
Return type:newboard
classmethod piece_list(square_dict, color=None)

Generate the current pieces on the board as a list.

Parameters:
  • square_dict (dict) – The dictionary of squares to get the list from.
  • color (Color) – Optional: The color to get the pieces for.
Yields:

Piece – A chess piece or pawn

setup_board(game_pieces, pawns)

Set up the pieces and pawns at their startpositions.

The lists passed can be created first with nerdchess.pieces.create_pieces and nerdchess.pieces.create_pawns.

Parameters:
  • game_pieces (list(Piece)) – A list of game pieces to set up
  • pawns (list(Pawns)) – A list of pawns to set up
class nerdchess.board.Square(selector, color, occupant=None)

Bases: object

Represents a square on a chessboard.

Parameters:
  • selector (String) – A selector of the square (eg. a1)
  • occupant (Piece) – Usually a piece or pawn, needs to implement __str__
selector

A selector of the square (eg. a1)

Type:String
occupant

Usually a piece or pawn, needs to implement __str__

Type:Piece

nerdchess.boardmove module

A move in the context of a board.

This module glues moves and a board together.

class nerdchess.boardmove.BoardMove(board, *args, **kwargs)

Bases: nerdchess.move.Move

Represents a move in the context of a board.

Inherits base class (Move) attributes. And adds some new ones.

Parameters:board (Board) – The board we’re playing on.
board

The boardcontext.

Type:Board
origin_sq

The origin square.

Type:Square
destination_sq

The destination square.

Type:Square
castle_side()

Return the side we’re castling to.

is_capturing()

Check if this move is capturing.

is_castling()

Check if this move is castling.

Returns:The color of the player castling, or False.
Return type:Color
process()

Process this move in the context of the board.

Returns:False if the move is incorrect Board: A new board
Return type:Bool
squares_between()

Get the squares between origin and destination of this move.

Yields:Square – The squares between origin and destination of this move.
class nerdchess.boardmove.CastleSide

Bases: enum.Enum

Enumerator with castling sides.

KING = 'kingside'
QUEEN = 'queenside'

nerdchess.boardrules module

Helps check for valid moves in the context of a board.

class nerdchess.boardrules.BoardRules(move)

Bases: object

Applies different boardrules.

Parameters:move – The boardmove to check against
move

The move we’re checking

valid

Is the checked move valid? see self.apply()

origin

The origin square of the move

destination

The destination square of the move

piece

The piece being moved

apply()

Apply boardrules based on the moved piece.

nerdchess.config module

Global config for nerdchess.

nerdchess.config.MOVE_REGEX

Regex to validate a move.

Type:Regex
nerdchess.config.numbers

List of numbers of a chessboard.

Type:list(int)
nerdchess.config.letterlijst

List of letters of a chessboard.

Type:list(letter(Enum))
class nerdchess.config.colors

Bases: enum.Enum

The colors of the players in a game of chess.

BLACK = 'b'
WHITE = 'w'
class nerdchess.config.letters

Bases: enum.Enum

The letters of a chess board.

A = 'a'
B = 'b'
C = 'c'
D = 'd'
E = 'e'
F = 'f'
G = 'g'
H = 'h'

nerdchess.game module

Start a game of chess.

This module offers a simple interface to start a game of chess. The game consists of:

  • 2 Players
  • A board
  • Pieces
  • Pawns

Example

chessgame = game.ChessGame(player_1, player_2)

class nerdchess.game.ChessGame(player_1, player_2, over=False)

Bases: object

Creates a new chessgame with players, pieces, and sets up the board.

Parameters:
  • name_1 (Player) – Player 1
  • name_2 (Player) – Player 2
  • over (Bool) – Whether the game is over
player_1

Player 1

Type:Player
player_2

Player 2

Type:Player
playerlist

A list of the two players

Type:list
board

The board the game is played on

Type:Board
pieces

A list of the pieces the game is played with

Type:list
pawns

A list of the pawns the game is played with

Type:list
move(player, move)

Process the move in a game of chess.

Parameters:
  • player – The player that made the move
  • move – The move representeed by squares (eg. e2e4)
Returns:

Was the move succesful?

Return type:

Bool

pass_turn()

Pass the turn to the other player.

nerdchess.move module

This module describes a move in a game of chess.

The move is not aware of the board context, but is aware of the boundaries of a board in general.

class nerdchess.move.Move(move, *args, **kwargs)

Bases: abc.ABC

Represents a move in a game of chess.

Parameters:move (String) – A string that’s tested with the regex ‘[a-h][1-8][a-h][1-8]’
text

String representation of the move r’[a-h][1-8][a-h][1-8]’

Type:String
origin

String representation of the origin square

Type:String
destination

String representation of the destination square

Type:String
horizontal

Amount of horizontal steps in the move

Type:int
vertical

Amount of vertical steps in the move

Type:int
indices

Origin/destination letter(x)/number(y) mapped to their list position

Type:dict
classmethod from_position(position, steps)

Create a move based on the current position and steps (hori/verti).

Parameters:
  • position (String) – The current position (eg. a1)
  • steps (tuple(int, int)) – The steps taken in the move
Returns:

A new move instance

Return type:

Move

get_steps()

Return the horizontal/vertical steps of the move.

is_diagonal()

Is the move diagonal.

is_horizontal()

Is the move horizontal (only).

is_vertical()

Is the move vertical (only).

square_selectors_between()

Return selectors of squares between the origin and destination.

Returns:A list of selectors of squares.
Return type:list(String)

nerdchess.pieces module

This module describes what pieces and pawns look like.

class nerdchess.pieces.Bishop(color, captured=False)

Bases: nerdchess.pieces.Piece

Represents a bishop in a game of chess.

move_pattern()

Movevement pattern of a piece.

Returns:Move pattern as list of tuples
Return type:list(tuple(int, int), )
start_position()

Start positions of a piece.

class nerdchess.pieces.King(color, captured=False)

Bases: nerdchess.pieces.Piece

Represents a king in a game of chess.

move_pattern()

Movevement pattern of a piece.

Returns:Move pattern as list of tuples
Return type:list(tuple(int, int), )
start_position()

Start positions of a piece.

class nerdchess.pieces.Knight(color, captured=False)

Bases: nerdchess.pieces.Piece

Represents a knight in a game of chess.

move_pattern()

Movevement pattern of a piece.

Returns:Move pattern as list of tuples
Return type:list(tuple(int, int), )
start_position()

Start positions of a piece.

class nerdchess.pieces.Pawn(color, captured=False)

Bases: nerdchess.pieces.Piece

Represents a pawn in a game of chess.

move_pattern()

Movevement pattern of a piece.

Returns:Move pattern as list of tuples
Return type:list(tuple(int, int), )
start_position()

Start positions of a piece.

class nerdchess.pieces.Piece(color, captured=False)

Bases: abc.ABC

Baseclass of a game piece (pawns and pieces).

Parameters:
  • color (colors) – The color of the piece
  • captured (Bool) – Is the piece captured?
color

The color of the piece

Type:colors
position

The position of the piece

Type:String
captured

Is the piece captured?

Type:Bool
allowed_moves(board=False)

Transform the move pattern into a list of allowed moves.

Returns:
A list of allowed moves for the piece
doesn’t factor in other pieces on the board.
Return type:list(Move, )
diagonal_pattern()

Return a diagonal movement pattern as a list (bishops).

move_pattern()

Movevement pattern of a piece.

Returns:Move pattern as list of tuples
Return type:list(tuple(int, int), )
start_position()

Start positions of a piece.

straight_pattern()

Return a straight movement pattern as a list (rooks).

class nerdchess.pieces.Queen(color, captured=False)

Bases: nerdchess.pieces.Piece

Represents a queen in a game of chess.

move_pattern()

Movevement pattern of a piece.

Returns:Move pattern as list of tuples
Return type:list(tuple(int, int), )
start_position()

Start positions of a piece.

class nerdchess.pieces.Rook(color, captured=False)

Bases: nerdchess.pieces.Piece

Represents a rook in a game of chess.

move_pattern()

Movevement pattern of a piece.

Returns:Move pattern as list of tuples
Return type:list(tuple(int, int), )
start_position()

Start positions of a piece.

nerdchess.pieces.create_pawns()

Create a standard set of pawns.

nerdchess.pieces.create_pieces()

Create a standard set of chess pieces.

nerdchess.player module

Represent a player in a game of chess.

class nerdchess.player.Player(name, color, turn=True, *args, **kwargs)

Bases: object

Represents a player in a chessgame.

Parameters:
  • name – The name of the player
  • color – The color of the player
  • turn – Whether it’s the players turn
name

The name of the player

color

The color of the player

turn

Is it the players turn?

classmethod create_two(name_1, name_2, color_input)

Create two players and base the colors off of the one assigned to 1.

Parameters:
  • name_1 – The name of player 1
  • name_2 – The name of player 2
  • color_input – The color to assign to player 1
Returns:

The players that are going to play the game

Return type:

Tuple(Player, Player)

nerdchess.player.random_color()

Return a random color.

Module contents

A chess engine written in Python.

The goal of this project is to offer simple tools to simulate a game of chess, by offering the necessary objects like a board, pieces, pawns and players standalone. But also a complete chessgame that implements these objects and offers an interface to control the game.

The package includes a small commandline chess game, but the main goal is to offer an interface to create chess games from everywhere. It should be just as easy to implement the same games of chess in a web-application with Flask, a commandline interface or some graphical desktop interface.

It’s still the idea to make it possible to write AI’s againt this package. But for now I’m just going to finish it’s basic functionality and try to keep it in mind as much as I can while making design decisions.

I’m not aiming for this to be some widely used package, and am mostly making it for fun and learning. Expect things to change a lot, and your applications to break if you don’t freeze versions might you decide to use this. At least in it’s current state.

nerdchess.main()

Play a game of chess.

The main function of this module starts a really basic game of chess on the commandline. Moves are input in the format ‘e2e4’.

Examples

>>> import nerdchess
>>> nerdchess.main()
What is the name of player 1?henk
With which color should henk start, (w)hite, (b)lack or (r)andom?     r
What is the name of player 2?blaat
-  ---  ---  ---  ---  ---  ---  ---  ---
8  [♖]  [♘]  [♗]  [♕]  [♔]  [♗]  [♘]  [♖]
7  [♙]  [♙]  [♙]  [♙]  [♙]  [♙]  [♙]  [♙]
6  [ ]  [ ]  [ ]  [ ]  [ ]  [ ]  [ ]  [ ]
5  [ ]  [ ]  [ ]  [ ]  [ ]  [ ]  [ ]  [ ]
4  [ ]  [ ]  [ ]  [ ]  [ ]  [ ]  [ ]  [ ]
3  [ ]  [ ]  [ ]  [ ]  [ ]  [ ]  [ ]  [ ]
2  [♟]  [♟]  [♟]  [♟]  [♟]  [♟]  [♟]  [♟]
1  [♜]  [♞]  [♝]  [♛]  [♚]  [♝]  [♞]  [♜]
X  _a_  _b_  _c_  _d_  _e_  _f_  _g_  _h_
-  ---  ---  ---  ---  ---  ---  ---  ---
What's your move, blaat?: e2e4
-  ---  ---  ---  ---  ---  ---  ---  ---
8  [♖]  [♘]  [♗]  [♕]  [♔]  [♗]  [♘]  [♖]
7  [♙]  [♙]  [♙]  [♙]  [♙]  [♙]  [♙]  [♙]
6  [ ]  [ ]  [ ]  [ ]  [ ]  [ ]  [ ]  [ ]
5  [ ]  [ ]  [ ]  [ ]  [ ]  [ ]  [ ]  [ ]
4  [ ]  [ ]  [ ]  [ ]  [♟]  [ ]  [ ]  [ ]
3  [ ]  [ ]  [ ]  [ ]  [ ]  [ ]  [ ]  [ ]
2  [♟]  [♟]  [♟]  [♟]  [ ]  [♟]  [♟]  [♟]
1  [♜]  [♞]  [♝]  [♛]  [♚]  [♝]  [♞]  [♜]
X  _a_  _b_  _c_  _d_  _e_  _f_  _g_  _h_
-  ---  ---  ---  ---  ---  ---  ---  ---