June 18, 2006 will

ChessPy

Knight

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.

Download Python Chess Module
Use Markdown for formatting
*Italic* **Bold** `inline code` Links to [Google](http://www.google.com) > This is a quote > ```python import this ```
your comment will be previewed here
gravatar
Paul Swanson

While I can't offer any development help I am currently looking for exactly what you've proposed: an AJAX chess application. Everything that is similar on sourceforge is dead or just junk.

PS. The ChessPy you've developed; what licensing terms are you releasing it under?

gravatar
will

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!

gravatar
Hans Then

Hi Will,

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.

Regards,

Hans

gravatar
Will

Hi Hans,

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.

Regards,

Will

gravatar
Hans Then

Hi Will,

Okay thanks.

I have already started adding support for variations and moving though them. Is it okay to release the whole of the module under an Open Source license?

Hans

gravatar
Dan
Is this no longer available?
gravatar
WIll McGugan
It is now, I had forgotten to move some files over from my old blog…
gravatar
Tom A.
Hi,

I used your python chess module to make a small chessbot for fun. It was very helpful, but there are a couple of errors I've found in the is_check() method (line 1246).

1) lines 1259-1265 (testing if Knights put the King in check) do not work. I don't know what you were trying to accomplish with the code you had written so I changed it to:
         for hop in Knight.MOVES:
            hop = Coord(*hop)
            if not (king_pos - hop) in self: continue
            if self[ king_pos - hop ]==None: continue
            if self[king_pos-hop].colour==side: continue
            if self[king_pos-hop].__class__.__name__ != 'Knight': continue
       	    return True


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.
gravatar
Tom A.
I forgot to mention that I resolved problem #2 by adding the following code:

 enemy_king_pos = self.find_king(Opponent(side)).position
if(  abs(king_pos.col-enemy_king_pos.col)<=1 and abs(king_pos.row-enemy_king_pos.row)<=1 ):
    return True

Not elegant by any means, but it seems to have done the trick.
gravatar
Ashish
Hi Will i have used few of your chess modules in my university project also i have tweaked it a little and it works great in python shell, wanted to thank you for it!
gravatar
Noah
Hi i am wondering if it is possible for you to make the program be able to play moves against you? I was just curious, Noah
gravatar
Roy
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.

Any ideas on how should I adjust your module to suit my needs?
gravatar
Will McGugan
Roy,

There's no functionality for that, but you could certainly build upon ChessPy…

Will
gravatar
Roy
Mister McGugan, can you give me some hints on how should I go and implement that kind of AI functionality?
gravatar
Tom
@Roy LOL! Mister McGugan, can you tell me how to adjust your module to solve chess?
gravatar
Oriol
Classic Roy
gravatar
CJ

Hi, It seems that the download link does not work.

Is there any other place from where I can download it?

Thanks