Skip to content

MoritzSchwerer/py-chess

Repository files navigation

Py-Chess

This is a well performing C++ chess environment for RL. It should work almost identical to Pettingzoo's Chess Env. It is still missing draw by insufficient material. But other than that it is complete.

It exists because I needed a fast ChessEnv that can be accessed from python for my implementation of AlphaZero. I plan to improve upon the library in the future but it may never come to that.

Learning about Py-Chess

If you are here to learn how Py-Chess works then I got good news for you. The move generation part of the engine is very clean and you can read it in src/move_gen.hpp. This part was heavily inspired by Gigantua and the article.

Detecting 3-fold repetition is also interesting. Here I just use zobrist keys. They are just a way to hash a given board into 64 bits. For each board this key will be generated and stored in a map from hash to count. After each move we simply check if the current hash occured 3 times and if so we draw the game. The code for this part can be found in src/zobrist.hpp. An explanation of this can be found here.

If you are wondering about the hashing part. I use a mixer which I got from this blog. A mixer takes a non-zero number (seed) and then does some computation on that. Usually multiplications and XORs with a shifted version of itself. This results in very fast hash function that has low enough collision rate for our usecase.

As for the rest there is not much interesting stuff going on you use the move generation to validated incoming moves and to tell the agent which moves are legal and when you receive a move you just move around some state.

Optimizations

Baseline

Depth: 1 | Nodes: 20 | Time: 6.3929e-05 seconds Depth: 2 | Nodes: 400 | Time: 0.000321391 seconds Depth: 3 | Nodes: 8902 | Time: 0.00607044 seconds Depth: 4 | Nodes: 197281 | Time: 0.13826 seconds Depth: 5 | Nodes: 4865609 | Time: 3.18744 seconds

About

A high performance C++ chess library with python bindings

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published