ChessPy

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 […]

ChessPy

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.

I'm afraid I've only just begun the AJAX chess project, and I'll be working on it in my spare time. So it may be a while before a release.

For non-commercial use it is free in return for a credit. I'm not sure about commerical use yet, but I may end up just releasing it on some liberal open source license. I'll need to give it some thought!

Thanks for writing the module. I am willing to help development of the module. I wrote a full PGN parser and database program in the past, so I can extend the module so that it can handle variations as well. Just let me know if you are interested.

How's work on the AJAX chess website? There is a site, www.lutanho.net, that contains a javascript chess library. Maybe you could use that as well. I am planning on including it on the website of my chessclub, so that people can setup chess diagrams through the web.

Please do extend it as you see fit. I don't have time to do much work on it, but any improvements will be gratefully received! I deliberately made provision for exotics flavors of chess - the board width and height are parameters and not hard coded as 8 - so hopefuly it wont be too painful to adapt. Not sure how well I stuck to that through the entire module though.

I'm afraid I've abandoned the AJAX chess idea. Just for lack of time, but I may pick it up again at some point in the future...

Email me if you need more information - my email address is in the About page.

2) The method does not test if the enemy king can attack the current side's king. This leads to a bug in which the white King (for example) can take a pawn being protected by the black King, leaving the two Kings adjacent which is (of course) illegal.

Once again, thanks for writing this code. It has been very helpful to me.

Mister Will McGugan, is it possible to hard-code chess moves into this module, so that it'll look as if there's an AI? So, for example, if I type e4 and press enter, the computer would respond with a hard-coded move. I want to inject the best possible moves for Black (if human player is White) and for White (if human player is Black). And to get the best possible moves, I'll analyze them with the best chess engine in the world, that is Houdini 2.0c at the time of this writing. It'll be a long time until I manage to do this, but I love chess so that is going to be fun.