Skip to main content

Python implementation of the game of chess.

Project description

Introduction

Python implementation of the chess game in less than 1000 lines of code.

For engines/agents to play against check my other project chess-ai

There is also a work in progress for the cli based game (python -m chess_python.game).

Usage

pip install chess-python-pip
#examples/example_01.py
from chess_python.chess import Chess
chess = Chess(fen="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 0") # Default
print(chess)
print(chess.legal_moves())
chess.move("e2e4")
print(chess)

outputs:

Player to move: White
Move count: 0
8 || r | n | b | q | k | b | n | r |
-----------------------------------
7 || p | p | p | p | p | p | p | p |
-----------------------------------
6 ||   |   |   |   |   |   |   |   |
-----------------------------------
5 ||   |   |   |   |   |   |   |   |
-----------------------------------
4 ||   |   |   |   |   |   |   |   |
-----------------------------------
3 ||   |   |   |   |   |   |   |   |
-----------------------------------
2 || P | P | P | P | P | P | P | P |
-----------------------------------
1 || R | N | B | Q | K | B | N | R |
-----------------------------------
-----------------------------------
  || a | b | c | d | e | f | g | h

['b1a3', 'b1c3', 'g1f3', 'g1h3', 'a2a3', 'a2a4', 'b2b3', 'b2b4', 'c2c3', 'c2c4', 'd2d3', 'd2d4', 'e2e3', 'e2e4', 'f2f3', 'f2f4', 'g2g3', 'g2g4', 'h2h3', 'h2h4']
Player to move: Black
Move count: 1
8 || r | n | b | q | k | b | n | r |
-----------------------------------
7 || p | p | p | p | p | p | p | p |
-----------------------------------
6 ||   |   |   |   |   |   |   |   |
-----------------------------------
5 ||   |   |   |   |   |   |   |   |
-----------------------------------
4 ||   |   |   |   | P |   |   |   |
-----------------------------------
3 ||   |   |   |   |   |   |   |   |
-----------------------------------
2 || P | P | P | P |   | P | P | P |
-----------------------------------
1 || R | N | B | Q | K | B | N | R |
-----------------------------------
-----------------------------------
  || a | b | c | d | e | f | g | h

DEV section:

Performance tracking

Using python3 -m cProfile -o prof.txt tree.py -h for profiling and snakeviz prof.txt to visualize.

Perft(3) initial position rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 (8902 positions, reference time 0.05 s with python-chess:

  • 1.25 s (first)
  • 0.79 s (moving get_positions_attacking_pieces to optimizer initialization and update, not when checking if move is legal)
  • 0.70 s removing np.array from list of moves (does not make a lot of sense)
  • 0.55 s removing more np.arrays
  • 0.51 s removing np.unravel
  • 0.47 s using only lists in get_allowed_moves_by_piece
  • 0.39 s with revamped get_index_trajectory
  • 0.35 with custom deepcopy
  • 0.18 s with using list for board instead of numpy!!
  • 0.15 s further tweaks

TODO:

  • Include tests for perft in different positions
  • Include utils if there is a mismatch in positions tree with reference implementation
  • Improve performance:
    • Keep track of index where there are pieces in optimizer level
    • Move and unmake move (implenting unmake also requires using a copy of the board, which performance wise does not improve anything)
  • Improve overall code quality (clarity, choose right data structure for the job):
    • Public vs private functions
    • cyclomatic complexity in is_legal_move
  • Automate release with github action to pip
  • Explore pypy
  • Explore deepcopy for tree generation, it takes a lot of time (only copying board improves performance)
  • Simplify castling

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

chess_python_pip-0.1.8.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

chess_python_pip-0.1.8-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file chess_python_pip-0.1.8.tar.gz.

File metadata

  • Download URL: chess_python_pip-0.1.8.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.2 Linux/5.15.0-1021-azure

File hashes

Hashes for chess_python_pip-0.1.8.tar.gz
Algorithm Hash digest
SHA256 136994ef404090b9c8ac3eec2f01a70c7df58503198d21bd3aca59a9c810bb76
MD5 79e27bfe5db815595cb5c9bbc6a76518
BLAKE2b-256 b75ee60e41f0a2569cadceec57b9e96e6d944d8eea6f890c6c3c5422b340a990

See more details on using hashes here.

File details

Details for the file chess_python_pip-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: chess_python_pip-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 11.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.2 Linux/5.15.0-1021-azure

File hashes

Hashes for chess_python_pip-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 f2ffd7a33b61a035c2bc0868ba2738e44cc6865018c89433a7a2c89ad759b546
MD5 8bc2e187234f9db35dfc03473a613954
BLAKE2b-256 2098f41feb9bf54cc82a3cfdb406df8e20400a55cddd81601a9f4f8eb280e43b

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page