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
- FIDE - Laws of Chess - The laws of chess.
- FIDE about SAN - Standard Algebraic Notation, see appendix E.
- Forsyth-Edwards Notation - Wikiedia about FEN-strings.
- Python - The python programming language.
- PyGame - Game development for Python.
Downloads
- ChessBoard_2.05.tar.gz - Current ChessBoard version including example and graphics.
- ChessBoard_2.05.zip - Same as the above but in zip-format.
- ChessBoard.py - The core ChessBoard 2.05 module.
- HowToUseChessBoard.txt - Short description of the public methods. Also included in the above package.
Thanks
- Special thanks to Jasper Stolte for testing and suggesting improvements!
Copyright notice
- Chess graphics are released under the Creative Commons License and made by Paul Gorman .
- Source code are GPL and made by me (John Eriksson).
Comment this project:
Vuk 2011-08-08 16:28:54
Hi John. Are you still developing this software?
Two bugs in getFEN()
1.
- bug1.py
from ChessBoard import ChessBoard
cb = ChessBoard()
cb.addTextMove('e4')
cb.addTextMove('h5')
print cb.getFEN()
$ python bug1.py
Traceback (most recent call last):
File "bug1.py", line 5, in
print cb.getFEN()
File "/home/user/tmp/test/ChessBoard/ChessBoard.py", line 932, in getFEN
elif turn "w" and (self._board[y][x-1] 'P' or self._board[y][x+1] == 'P'):
IndexError: list index out of range
2.
- bug2.py
from ChessBoard import ChessBoard
cb = ChessBoard()
cb.addTextMove('e4')
print cb.getFEN()
$ python bug2.py
rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq - 0 1
Correct FEN is
rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1
(see http://en.wikipedia.org/wiki/Forsyth%E2%80%93Edwards_Notation)
Extremely cool & useful lib. But something is wrong:
>>> cb = ChessBoard.ChessBoard()
>>> fen = 'rnbQ3k/p1p3pp/r7/1p2pp1n/1P3PPP/b2P1N2/P1P5/RNB1KB1R b KQ - 1 1'
>>> cb.setFEN(fen)
>>> cb.getBoard()
[['r', 'n', 'b', 'Q', '.', '.', '.', 'k'],
['p', '.', 'p', '.', '.', '.', 'p', 'p'],
['r', '.', '.', '.', '.', '.', '.', '.'],
['.', 'p', '.', '.', 'p', 'p', '.', 'n'],
['.', 'P', '.', '.', '.', 'P', 'P', 'P'],
['b', '.', '.', 'P', '.', 'N', '.', '.'],
['P', '.', 'P', '.', '.', '.', '.', '.'],
['R', 'N', 'B', '.', 'K', 'B', '.', 'R']]
>>> cb.isGameOver()
False
>>> cb.getGameResult()
0
Black should be checkmate...
I'd really appreciate a bugfix.
Thanks for the interesting project.
I have a 7 year old son who brings his game notations from the tournament games written down in score sheets. I have to enter them in a computer and send them over to his coach via email. My son's notations are NOT entirely error free, so I have to validate as I enter them. How do I use this project for entering the game by typing in the notation?
Thanks in advance
Thanks alot for the zip! Your project is looking really good!
I just finished beating myself in it. ;-)
I really like your 'Slingshot' game too! I hope you're still working
on it!
BTW: How do you extract .tgz files? Can you only do it on linux or something?
