ChessPy
Sunday, June 18th, 2006
A while ago I wrote a chess module for Python. Now this is not a chess-playing module, rather it parses chess moves, stores board positions and works out legal moves. It can also read and write PGN files. I can fairly confidently declare myself as an expert in this kind of thing, having written the PC chess game Chess Commander.
Chess is something that is very well defined, the rules are laid out in black and white, but there are enough juicy problems and gotchas to make it interesting to work on. It was the special case moves such as en passant and castling that were most tricky. I made extensive use of Python’s generators extensively which did simplify things a great deal.
There was already a python chess module in existence, by Eric Max Francis, but this version supports stalemate / draw rules and FEN output. It was originally written just for the hell of it, but I have since decided to dust it off and use it to create an AJAX chess website - which is why I present it here for feedback.
I’d be interested in help with optimization. It was designed to be flexible (ie possibly support chess variations) and speed was a secondary considerations. But if my AJAX chess site is successful it could be doing a lot of processing, so any tips would be appreciated! Although they may be moot since Psyco gives a significant speed boost.