ChessBoard - v2.05

ChessBoard is a Python implementation of the FIDE laws of chess. The main goal is to implement all applicable rules in a simple, straightforward way. The intention is not to be fast but to be easy to understand and to be complete. Many other implementation has known problems with castling, stalemate or other more or less special rules.

The ChessBoard logic is contained in one class with a simple set of public methods.

Features

  • The moves of the pieces
  • Castling
  • En passant
  • Check detection
  • Checkmate detection
  • Stalemate detection
  • Draw by the fifty moves rule detection
  • Draw by the three repetitions rule detection
  • Get valid locations support
  • Import and export of Forsyth-Edwards Notation strings.
  • Add text moves in the AN, SAN and LAN standards.
  • Export moves in the AN, SAN and LAN standards.
  • Undo and Redo.
  • Goto a specified move.

What's new in 2.05?

  • Fixed a bug in the getFEN() method. (Thanks to Alfredo Alessandrini)

What's new in 2.04?

  • Fixed another bug in the setFEN() method. (Thanks to Alfredo Alessandrini)

What's new in 2.03?

  • Fixed a bug in the setFEN() method.

What's new in 2.02?

  • Added the method getLastMove()
  • Changed the behavior of the promotion value. The promotion value set by setPromotion is always remembered until
    setPromotion(0) resets it.
  • Added the method getPromotion() to get the current promotion value.
  • Added the method getLastMoveType() to indicate if the last move was a "special move" like en passant or castling.
  • Fixed a bug in getLastTextMove(...). It now returns the correct code for castling.
  • Fixed a bug in the addMove() method. It pushed the state before it determined the game result causing it to loose the game result when using undo redo.

What's the use?

With the included example you and a friend can play a game of chess. But that is not the real reason to why I wrote this. Whatever chess software you are planning to write you almost always need to be able to determine valid chess moves and to detect the end of the game. You need this for network games, web based games, chess engines, PGN-viewers, chess training software etc. etc.

Short note about coordinates

ChessBoard preferably use its own internal coordinates in the communication with the client (for example in addMove and getValidMoves). The coordinates starts at (0,0) in the upper left corner. Ex: (0,0) = a8, (7,7) = h1. This is the same coordinate system that is use in the matrix returned by the getBoard method.

New since version 2.0 is that you also can add moves by using Algebraic Notation. The new method addTextMove understands three different standards; Algebraic Notation (AN), Standard Algebraic Notation (SAN, used in PGN-files) and Long Algebraic Notation (LAN).

The popular opening e2-e4 is done by calling addMove((4,6),(4,4)) but can also be made by calling addTextMove("e4"), addTextMove("e2e4"), addTextMove("e2-e4") or addTextMove("Pe2-e4").

Screenshot

Related links

Downloads

Thanks

  • Special thanks to Jasper Stolte for testing and suggesting improvements!

Copyright notice


Comment this project:

Your name:
Your email (hidden):
Message:
Enter the validation code :
Private! (visible for webmaster only)

Max 2012-11-07 00:19:27

Does Cheessboard run under Python 2 or 3?

Thanks Max

gadfium 2012-06-12 04:40:45

moveRook is too sloppy checking for whether castling should be disabled. It disables castling if any rook of the appropriate colour moves on the a (or h) file. It should check if a rook moves from a1 or h1 (for White) or from a8 or h8 for Black.

gadfium 2012-06-12 04:37:25

Further problems with the castling routines. Any character after O-O or O-O-O breaks it. In the game I found, 0-0-0# was played, ie castling with mate to the other player.

gadfium 2012-05-22 04:16:47

setFEN does not handle en passant positions correctly. The following is a quick fix:

epadjust = -1 #fix an off-by-one error in the index below
if fparts1 = = 'w': epadjust = 1
newstate += str("87654321".index(fparts3[1])+epadjust)

gadfium 2012-05-20 03:54:06

The game does not handle the case of castling which puts the opposing player in check ie 'O-O+' or 'O-O-O+'. It accepts it without the check sign on the end. I have changed +parseTextMove to

if txt = = "O-O" or txt = = "O-O+":

and similarly for "O-O-O"

View all