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
This blog post was posted to It's All Geek to Me on Sunday June 18th, 2006 at 5:55PM
 

16 Responses to "ChessPy"

  • Paul Swanson
    June 19th, 2006, 11:09 a.m.

    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?

  • June 19th, 2006, 7:15 p.m.

    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!

  • August 24th, 2006, noon

    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

  • August 24th, 2006, 9:17 p.m.

    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

  • August 25th, 2006, 12:11 p.m.

    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

  • Dan
    March 19th, 2009, 7:47 p.m.

    Is this no longer available?

  • March 19th, 2009, 8:05 p.m.

    It is now, I had forgotten to move some files over from my old blog…

  • Tom A.
    June 3rd, 2009, 7:47 a.m.

    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.

  • Tom A.
    June 3rd, 2009, 7:48 a.m.

    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.

  • Ashish
    December 7th, 2009, 12:10 a.m.

    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!

  • Noah
    October 27th, 2011, 12:47 a.m.

    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

  • Roy
    November 25th, 2011, 2:56 a.m.

    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?

  • November 26th, 2011, 3:25 p.m.

    Roy,

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

    Will

  • Roy
    December 6th, 2011, 4:31 p.m.

    Mister McGugan, can you give me some hints on how should I go and implement that kind of AI functionality?

  • Tom
    December 4th, 2012, 7:32 p.m.

    @Roy LOL! Mister McGugan, can you tell me how to adjust your module to solve chess?

  • Oriol
    March 11th, 2014, 9:41 p.m.

    Classic Roy

Leave a Comment

You can use bbcode in the comment: e.g. [b]This is bold[/b], [url]http://www.willmcgugan.com[/url], [code python]import this[/code]
Preview Posting...
Previewing comment, please wait a moment...

My Tweets

Will McGugan

My name is Will McGugan. I am an unabashed geek, an author, a hacker and a Python expert – amongst other things!

Search for Posts
Possibly related posts
Tags
Popular Tags
 
Archives
2013
 
Recent Comments
Sir, can you give me full code.?
Trying to implement this code. What are the requirements? Does it need a certain version of PHP, etc? Do I ...
Hi Will Can't seem to run any pyopengl applications on my Win7 64bit setup. Using python 3.3 but keep getting ...
Great work, is it possible to jump to a specified picture?
Thanks for the lesson and library, but: What if I wanted to limit the frames as well? Is it really ...
- The novice on Master time with PyGame
 
© 2008 Will McGugan.

A technoblog blog, design by Will McGugan