pythonchess

Copyright (c) 2002 Jyrki Alakuijala and Hannu Helminen

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License version 2 as published by the Free Software Foundation.

Introduction

pythonchess is a computer chess program for human vs. computer games. pythonchess is a complete chess program with alpha-beta search, hashing, pondering, iterative deepening, etc. implemented in Python programming language.

Python is well-suited for almost anything else but complex small-grained computation like chess programming. Here is my contribution trying to bend and strecth the limits of Python. This project was started as a Python learning and pair programming exercise with my great friend and colleague, Hannu Helminen. After building an initial chess engine together, I continued refining it, and also added the graphical user interface. The initial concept of this project was to try to find out how clean and simple a chess program could be. As usual, the goal changed during the development and I concentrated more on improving the playing strength, which made the code less obvious.

The program is divided into image files, one file for each piece type, separately for both colors, and source files. Two source files are included. board.py includes the user interface, while nchess6.py includes the unit tests and the chess engine. Unfortunately, the piece images are provided as gif files. This is only because tk does not support any patent free image type having the alpha channel.

The following shows an image of the chess program graphical UI.

Image of the graphics user interface of the pythonchess program

Methods

Many of the algorithms and heuristics used in typical chess programs are applied in pythonchess. These include pondering, attack maps, locked piece calculations, king safety, pawn structure evaluation, king mobility evaluation, evaluation hashing, general mobility, space, queen safety, material, relative material difference, iterative deepening, and move ordering by iterative deepening.

A unit test set was written to test the engine. Many of them are still present in the module nchess6.py and should be helpful in further development. The user interface is around 450 lines of Python code. The chess engine and the test cases are about 2000 and 1500 lines, respectively.

Results

The game can play respectable speed chess considering its core technology. However, the search speed is quite bad and will fail in slower play. I believe that the ELO count level of this software on a modern PC is around 1250 in a 5 min game and probably around 1100 in a slower game.

pythonchess has beaten me several times, but I still win it in a carefully played game. Without any solid evidence I consider myself an ELO 1500 level player.

The evaluator seems to be quite comparable in quality to Fritz 6. I have one win against Fritz, when both were playing at 1-ply. I guess Fritz 6 still had quiescence search on, deepening the search up to 11 plys or so quite often. The search in Fritz 6 is just 10000 times faster, making it a little better player when the plys are not limited artificially. The pythonchess evaluator can calculate only around 100 positions in second on a 1 GHz PC.

Discussion

The two most important missing features in pythonchess, which are quite often implemented in a typical chess program, are probabaly the transition hash table and null move heuristic. I believe that if we are able to do 3-plys only, these should not provide much improvement. I feel that for now the most important improvement area is the search speed, and I believe there is not much that can be done to it.

Considering the clarity of Python syntax, the resulting software may be interesting for CS and IT students and also other people who do want to understand the anatomy of a chess program. With the help of abstraction power of Python, Computer chess programming can be simultaneously surprisingly simple and still so complex and beautiful.

I do recommend for programming students and professionals for accepting the challenge of writing a chess program. It has been a great learning experience for me, and will most likely work for you, too. Together with Hannu we spent about half man month to implement the current system. I had some previous chess programming experience in writing a partial C++ chess program a few years back (totalling two weeks of C++ coding), that helped a little.

After finishing the 0.2 version project, I have written the engine for a commercial chess program for mobile phones, Deep Pocket Computer Chess Program and several other board games on chessboard, including chaturanga, losing chess, pocket knight chess. These games are available at a mobile operator or java games portal near you. I will still keep improving this Python version, too.

Enjoy the game!

Download

pythonchess-0.6.tgz version 0.6, 2003-04-02, assertion problem fixed

Old versions

pythonchess-0.5.tgz version 0.5, 2003-04-01, slightly improved UI with animated moves and progress bar, minor search strategy and evaluation improvements

pythonchess-0.4.tgz version 0.4, 2002-12-07, removed bugs, improved UI, compatible with Python 1.5 (with Tkinter).

pythonchess-0.3.tgz version 0.3, 2002-09-26, fix for crashes

pythonchess-0.2.tgz version 0.2, 2002-09-24, opening library and personalities added

pychess-0.1.tgz version 0.1, 2002-04-02, pondering fault fixed

pychess.tgz version 0.0 - 2002-03-30

I have written a minimal alternative chess engine in Python for Mark Dufour. He has added it into his Shed Skin (an optimizing Python-to-C++ compiler) benchmark. That chess engine was more than 10 times faster when compiled with Shed Skin. At this stage, Shed Skin needs more work to integrate libraries such as Tkinter into the compiler, so pythonchess cannot be compiled yet with it. The engine in the Shed Skin benchmark is smaller, cleaner and more easy to extend, so one could consider that as a more appropriate starting point to learn chess programming than pythonchess. One has to remember that that engine is just a bare-bones engine, no GUI, no nothing.

This page, including this line, is optimized for Lynx.

Valid XHTML 1.0!

hitcount

Hitcounting provided by www.digits.com.