Around January 2007 I remembered the similarities between C
and Javascript, and I was wondering if it would be possible to do a
translation of my recent winning entry (19th IOCCC,
Most Portable Chess Set), I did it on an afternoon and the result is the
current world's smallest chess program in Javascript, check also my
Tiny Chess for JS1K.
You can select the
promotion piece using the control. Some browsers will show you a
message of script being too slow, please indicate that you DON'T want to
stop the script.
See it on action, to play click piece to move and then click
destination square:
If you want to play with black's pieces, click this
to force a move, you will need to click every time after you do a move.
The source code
Most of the code is a resemblance of the original code,
except for the pointers translated to array-access. Recently I updated the core
with code from Toledo Nanochess Nov/20/2009.
Play level is fixed at 3-ply depth, you can modify it
(search on the source code for /*ply*/), be careful as every extra ply
is an order of magnitude more slow.
The JS1K contest started on
Sunday, August 1, 2010, organized by
Peter van der Zee with
Thomas Fuchs (author of
Scriptaculous),
Remy Sharp,
Patrick H. Lauke and
Christopher Williams as judges,
all expert Javascript programmers.
The contest's objective was simple, the participants should create the most
impressive Javascript program in 1024 bytes, that is 1 kilobyte, without using
any external file. The contest period closed on September 10.
I had news of the contest in August 8 and came to my mind
the challenge of writing a chess in 1K of Javascript, so based on my 2.2K
Javascript chess in six hours I managed to crunch it to 1K. I had to remove
castling, en passant and limit promotion to queen, leaving a simple chessboard
with letters for chessmen, click in origin square and target square to move
pieces.
Next day I sent it to the
contest, you can still play it online as the
entry #226. Take note that the 1018
octets of my Javascript code include an artificial intelligence and also the
user's interface and chessboard visualization. Not an easy thing as noted
by several
people.
First version of Tiny Chess
Two hours after publication, the enthusiast
Laxminarayan Kamath sent me an
e-mail suggesting me to put
Unicode
graphics and an optimization tip that I would use latter.
On Wednesday 11 in other six
hours of work I cutted more than 50 bytes, making enough space to put Unicode
graphics for the chess pieces, I sized up the chessboard for comfortable
visualization and also illuminated clicked square. Same day I sent this
update to the contest (it obtained the
number 298), and the graphics
surprised to
the public.
Some days after Spaniard
Román Cortés kindly shared with
me optimization tips for saving bytes, at same time I rewrote the artificial
intelligence to save space and with the extra space introduced variety in
gameplay (Math.random) also instant update of chessboard on final
click. This was the third update
(entry 435) and got excellent and
multitude of comments,
hundreds of
mentions in Twitter and thousands of visitors from
United States, Spanish, Chinese, Japanese,
Russian, French and German forums
and blogs. I've estimated that this version was played by at least 25,000
people around the world, this number was greatly surpassed by my last
version played by an estimate of 100,000 people.
The third version of Tiny Chess
An anonymous German reported that was easy to force my
program to fool's mate, I realized that an extra «intelligence» would solve
the problem and also I did illumination of last moved piece. This was the
fourth update
(entry 699).
Casually same day that I did the update, I received an
e-mail from English
Matt Round, he informed me of his
article about a game of my chess versus
the legendary 1K ZX81 Chess, and my chess drew instead of giving mate.
I knew immediately it could be enhanced, so I was on the task again, was very
difficult to «crunch» these last bytes (here I finally used Kamath's
optimization tip), I did it and was my last update
(entry 750) before contest closed.
And it won!, on September 25, 2010, Thomas Fuchs and Remy
Sharp demonstrated the winning JS1K entries at Berlin, Germany, during the
jsConf.eu conference, my Tiny Chess got 2nd
place, triggering a
wave of
mentions.
Here is the definitive version of my Javascript Chess in
1023 bytes, note that the original version was written in a single line that
has to be divided to put it here, also this is pure Javascript, must be
invocated by HTML markers such as this <body> <script
src="tiny_chess.js"> </body>
I managed to get a version with castling and en
passant in 1024 bytes (yes, it is possible!), but the visualization stood in
black and white, also the gameplay quality is bad, works as a prototype, but
was not nice enough for the contest.
It was archived (although I can publish it here if there are interesting
offers :)), in preference to an expanded version of my final entry with
castling and en passant, here is that version in 1251 bytes.
Go up on this page to download the ZIP file with the
source codes.
Other small Javascript chess programs
Douglas Bagnall from New Zealand wrote the first 5K
Javascript Chess Game for the now defunct
5K Javascript Contest.
Current smallest programs based on his source exceeds
4 kilobytes. These programs and others are available at
http://p4wn.sourceforge.net/.
The Mathematician John A. Moody from the Warwick University
at United Kingdom has also written his own
small Javascript
chess game in 2.7 kilobytes, currently doesn't detect illegal moves and
doesn't handle castling nor en passant.