Package: chess-python-pip
IntroductionPythonimplementation of the chess gamein less than 1000 lines of code.For engines/agents to play against check my other projectchess-aiThere is also a work in progress for the cli based game (python -m chess_python.game).Usagepip install chess-python-pip#examples/example_01.pyfromchess_python.chessimportChesschess=Chess(fen="rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 0")# Defaultprint(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 | hDEV section:Performance trackingUsingpython3 -m cProfile -o prof.txt tree.py -hfor profiling andsnakeviz prof.txtto
visualize.Perft(3) initial positionrnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1(8902
positions, reference time 0.05 s withpython-chess:1.25 s (first)0.79 s (movingget_positions_attacking_piecesto optimizer initialization and update, not when
checking if move is legal)0.70 s removingnp.arrayfrom list of moves (does not make a lot of sense)0.55 s removing more np.arrays0.51 s removing np.unravel0.47 s using only lists inget_allowed_moves_by_piece0.39 s with revampedget_index_trajectory0.35 with customdeepcopy0.18 s with using list for board instead of numpy!!0.15 s further tweaksTODO:Include tests for perft in different positionsInclude utils if there is a mismatch in positions tree with reference implementationImprove performance:Keep track of index where there are pieces in optimizer levelMove 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 functionscyclomatic complexity inis_legal_moveAutomate release with github action to pipExplore pypyExplore deepcopy for tree generation, it takes a lot of time (only copying board improves
performance)Simplify castling