Toledo Javascript Chess Game

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.
Play it online:
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.
A brief description of the inner workings of this code can be found on the page of Toledo Chess 1 or check my Toledo Nanochess book.
<script>//(c)2009 Oscar Toledo G.
var B,i,y,u,b,I=[],G=120,x=10,z=15,M=1e4,l=[5,3,4,6,2,4,3,5,1,1,1,1,1,1,1,1,9,9
X(w,c,h,e,S,s){var t,o,L,E,d,O=e,N=-M*M,K=78-h<<x,p,g,n,m,A,q,r,C,J,a=y?-x:x;
:S;if(!r&(!!q|A<3||!!g)||(r+1&z^y)>9&&q|A>2){if(m=!(r-2&7))return y^=8,I[G--]=
-N,h+1,I[G+1],J=q|A>1?0:p,s);if(!(h||s-1|B-O|i-n|p-b|L<-M))return W(),G--,u=J;
<0)return y^=8,G--,L;if(!h)i=n,B=O,b=p;}N=L;}n+=J||(g=p,m=p<O?g-3:g+2,I[m]<z|I[
++O>98?O=20:e-O);return y^=8,G--,N+M*M&&N>-K+1924|d?N:0;}B=i=y=u=0;while(B++<
"<table cellspacing=0 align=center>",i=18;i<100;a+=++i%10-9?
"<th width=40 height=40 onclick=Y("+i+") style='border:2px solid #aae' id=o"+i+
" bgcolor=#"+(i*.9&1?"9090d0>":"c0c0ff>"):(i++,"<tr>"));
a+="<th colspan=8><select id=t><option>Q<option>R<option>B";
function W(){B=b;for(p=21;p<99;++p)if(q=document.getElementById("o"+p)){q.
innerHTML="<img width=40 src="+(I[p]&z)+".gif>";q.
function Y(s){i=(I[s]^y)&z;if(i>8){b=s;W();}else if(B&&i<9){b=s;i=I[B]&z;if((i&

Source code forks

This Javascript chess game has currently several forks, same core but different graphical interfaces:

Tiny chess for the JS1K contest

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
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
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 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>
// Tiny Chess (c)2010 Óscar Toledo G.
.charCodeAt(y++)-64:7);function X(c,h,e,s){c^=8;for(var o,S,C,A,R,T,G,d=e&&X(c,0
o-9?l[61+G]:49;do if(!(R=I[T+=l[C]])&&!!G|A<3||(R+1^c)>9&&G|A>2){if(!(R-2&7)
)return K;n=G|(c?T>29:T<91)?o:6^c;S=(R&&l[R&7|32]*2-h-G)+(n-o?110:!G&&(A<2)+1);
-1e4))return W(),c&&setTimeout("X(8,0,2),X(8,0,1)",75);I[O]=o;I[T]=R}if(S>N||!h
T=O,(G||A>2|(c?O>78:O<41)&!R)&&++C*--A))}return-K+768<N|d&&N}function W(){i=
"<th width=60 height=60 onclick='I[b="+u+
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.
// Chess (c)2010 Óscar Toledo G.
"yyyyyyyy}{|~z|{}@G@TSb~?A6J57IKJT576,+-48HLSUmgukgg OJNMLK  IDHGFE")
function X(c,h,e,S,s){c^=8;for(var T,o,L,E,D,O=20,G,N=-1e8,n,g,d=S&&X(c,0)>1e4,
|A>2){if(!(2-R&7))return K;for(E=n=G|I[T-a]-7?o&z:6^c;E;E=!E&&!d&&!(g=T,D=T<O?g
,E=G|A>1?0:T,S,L-N);if(!(h||S-1|B-O|T-b|L<-1e4))return W(y=E),c&&setTimeout(
:(B=O,b=T,0))return N}}}while(!R&G>2||(T=O,G|A>2|z<o&!R&&++C*--A))}return-K+
768<N|d&&N}function W(){i="<table>";for(u=18;u<98;document.body.innerHTML=i+=++
u%x-9?"<th width=60 height=60 onclick='I[b="+u+
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
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.

Related links

Last modified: Dec/29/2017