Javascript Chess
In 2002 I (Douglas Bagnall) wrote a javascript chess game for the 5k competition. People are still curious about it, so it has finally gained its own sourceforge site.
The code is now in the public domain. That means you can do whatever you want with it; there is no copyright.
News
2005-03-23: Sven Vahar's fancy version in CVS
The improved board that Sven Vahar made in December 2004 is now available in CVS, and as a zip download.
2005-03-04: Unicode version by Antony Lesuisse
Antony Lesuisse replaced the external gifs with unicode glyphs, creating a completely self-contained page of only 4385 bytes. It has been added to CVS.
If you don't see a row of chess pieces here (♔♕♖♗♝♞♟), you might have problems using the page - it depends on your browser's font rendering.
2004-12-14: New version by Sven Vahar
Sven Vahar has created a cleaner interface with help text, with both Estonian and English versions.
Different Versions
The code has forked a few times already, due mainly to a lack of version control. I think this is an OK thing with such a tiny program, allowing experiments and varying objectives. If you want to take it in a new direction, please do. New forks are welcome in the CVS tree. The main branches are:
- The smallest possible page. Less than 3k of code should be possible.
- The smallest page using images.
- Better play, more readable code, with debugging stuff, hopefully somewhat modular.
- Something inbetween - good but small.
- Personal or experimental branches, such as Ron Winter's untangled version, and Sven Vehar's prettier interface.
- An actionscript branch, if I ever get round to it, compilable with ming
How it works
It recursively explores the tree of possible moves, keeping score along the way. It tries to take as many pieces as possible and get into the centre early. That's about all. Its endgame strategy is to win before the endgame.
Bugs
It doesn't always lose graciously, often trying to move out of checkmate. You could take that as a concession, but it is irksome.
To do
(incomplete list)
- Fix the bug.
- The small ("5k") branch can be shrunk in a number of ways. It uses a principal variation search algorithm, which could be replaced by a smaller plainer alpha-beta search (I tried principal variation in search of speed, and never got round to switching back). Someone claims compression tricks could save 744 bytes. Better encoding of piece names will save space and time.
- The big version could be reoptimised for speed rather than space. Different searches and evaluations could be tried (it really is naive now). The recursion could be removed. Some kind of hackery could be used to get past the javascript timeout in browsers. Some of the variable names are a bit stupid.
- The pictures could be better.
- It could also, very easily, be ported to actionscript, and compiled for flash players. The core engine is simple ECMAscript, so I can't see to much trouble with porting it. It may be possible to have two separate wrappers for the same engine, so developments in javascript are usable in flash.
- One day it might get a name.
Similar Projects
Neil Pearce has written a javascript chess game which can be found at internet.com and other places.
There was one by David Moore in the 2001 5k competition. Unfortunately it won't work in my browsers.
The most famous small chess implentation is probably the 1k ZX81 game. Not only is it short, it ran in only 1024 bytes of ram (javascript has megabytes to play with). On the other hand, it didn't know all the rules.
License (or lack thereof)
Most popular free licenses are longer than the code, and often refer to things that don't apply well to a webpage (such as the source/binary distinction). So it is public domain.