Toledo Javascript Chess
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.
Recently I updated the core with code from Toledo Nanochess Mar/07/2009.
Play it online:
Most of the code is a resemblance of the original code, except for the pointers translated to array-access. You can select the promotion piece using the control.
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. Some browsers will show you an message of script being too slow, please indicate that you DON'T want to stop the script.
See it on action:
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
A brief description of the inner workings of this code can be found on the page of Toledo Chess 1.
<script>//(c)2009 Oscar Toledo G.
var B,i,y,u,b,I=[],G,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,9,
9,9,9,9,9,13,11,12,14,10,12,11,13,0,11,0,34,33,55,94,0,1,2,3,3,2,1,0,-1,1,-10,
10,-11,-9,9,11,10,20,-9,-11,-10,-20,-21,-19,-12,-8,8,12,19,21];function X(f,w,
c,h,e,S,s){var t,o,L,E,d,O=f,N=-M*M,K=128-h<<10,p,g,n,m,H,A,q,r,C,J,a=y?-x:x;
y^=8;d=!!w||!!s&&X(21,0,0,0,0,1,0)>M;H=G;do{if(o=I[p=O]){q=o&z^y;if(q<7){A=
q--&2?8:4;C=o-9&z?[53,47,61,51,47,47][q]:57;do{r=I[p+=l[C]];if(!w|p==w&&q|A>2|!
r){g=q|p+a-e?0:e;if(!r&(q>0|A<3||!!g)||(r+1&z^y)>9){m=0;if(!(r-2&7))return y^=
8,G=O,K;n=o&z;t=q|p>28&p<91?n+1:(n+=2,7^y);while(n-t){I[p]=n,E=I[O]=m?(I[g]=I[m
],I[m]=0):g?I[g]=0:0;L=(1-q?l[(p-p%x)/x+37]-l[(O-O%x)/x+37]+l[p%x+38]-l[O%x+38
]+(o&16)/2:!!m*9)+(!q?l[p%x+38]-1+!(I[p-1]^n)+!(I[p+1]^n)+l[(n&7)+32]*9-99+!!g*
99+(1==A&&(E=p,1)):0)+(r?l[(r&7)+32]*9-h-q:0)+!(I[p-a]&z^y^9);L-=s>h||s==h&(L>
z&1<s|d)?X(H,s>h|d?0:p,L,h+1,E,N,s):0;if(!(h||S|B-O|i-n|p-b|L<-M))return u=E;J=
q!=1||A<7||!!m||!s|d|!!r|o<z||X(21,0,0,0,0,1,0)>M;I[O]=o;I[p]=r;m?(I[m]=I[g],I[
g]=0):g?I[g]=9^y:0;if(L>N){N=L;G=O;if(!h&S&&s)i=n,B=O,b=p;if(h&&c-L<S)return y^=
8,L;}n+=J||(g=p,m=p<O?g-3:g+2,I[m]<z|!!I[p+=p-O]|!!I[p<O?m+1:m-1]);}}}}while(!r&
q>2||(p=O,q|A>2|(o&16&&!r)&&++C&&--A));}}}while(++O>98?O=20:f-O);return y^=8,N+
M*M&&N>-K+1924|d?N:0;}B=i=y=u=0;while(B++<120)I[B-1]=B%x?B/x%x<2|B%x<2?7:B/x&4?
0:l[i++]|16:7;for(a="<table align=center border=0>",B=0;B<8;B++)for(a+="<tr>",i=
21;i<29;i++)a+="<td width=40 height=40 onclick=Y("+(B*x+i)+") id=o"+(B*x+i)+
" style='border:2px solid transparent' bgcolor=#"+(i+B&1?"d0d0ff":"7070b0")+
" align=center>";
a+="<tr><td colspan=8 align=center><select id=t><option>Q<option>R<option>B";
document.write(a+"<option>N</select></table>");
function W(){for(p=21;p<99;p+=p%x-8?1:3)document.getElementById("o"+p).
innerHTML="<img width=40 src="+(I[p]&z)+".gif>";B=0;}W();function Z(s,y){
document.getElementById("o"+s).style.borderColor=y?"yellow":"transparent";}
function Y(s){i=(I[s]^y)&z;if(i>8){if(B)Z(B,0);Z(B=s,1);}else if(B&&i<9){
Z(b=s,1);i=I[B]&z;if((i&7)==1&(b<29|b>90))i=14-document.getElementById("t").
selectedIndex^y;X(21,0,0,0,u,0,1);Z(B,0);Z(b,0);W();if(y)window.setTimeout(
"X(21,0,0,0,u,1,2/*ply*/),X(21,0,0,0,u,0,1),W()",250);}}</script>
Other Javascript chess programs
Douglas Bagnall wrote the first 5K Javascript Chess Game, current smallest programs based on his source exceeds 4 kilobytes. These programs and others are available at http://p4wn.sourceforge.net/
Last modified: Aug/11/2009