Toledo Java Chess Game

Between March 16 and 20, 2009, I've translated Toledo Nanochess to the Java language, with the latest version of the source code it crunches at exactly 2835 bytes, this makes it the world's smallest chess program in Java, the compiled class sizes up at 4538 bytes, it is necessary to add 967 bytes of graphics (reused from Toledo Javascript Chess).
See it on action, to play click the piece to move and then click destination square:
Here would appear Toledo Java Chess in execution but your browser doesn't support Java.
The promotion piece is fixed to queen and currently there is no way to play with black pieces.
Only tested in Windows with the Java VM 1.6 from Sun.

The source code

The Java language inherits the C and C++ languages syntax and it is cousin of the Javascript language, so was relatively easy to do the translation of Toledo Nanochess.
Most of the code is a resemblance of the original code, except for the pointers translated to array-access.
The differences are noticeable on the source code when comparing C and Java, the latter is more verbose, it is not possible to assign an integer to a boolean and vice-versa, by example, doesn't exist comma operator, and the 2nd edition of Java made obsolete various graphical interface features. Still the program inherits the obfuscated nature of the original.
Play level is fixed at 4-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.
//(c)2010 Oscar Toledo G.
import java.applet.*;import java.awt.event.*;import java.awt.*;
public class toledo_chess extends Applet implements MouseListener{
int B,i,y,u,b,I[]=new int[411],G=120,l[]={5,3,4,6,2,4,3,5,1,1,1,1,1,1,1,1,9,9,9
};Image[]im;static final int x=10,z=15,M=10000;
public void init(){for(B=i=y=u=b=0;B++<120;)I[B-1]=B%x!=0?B/x%x<2|B%x<2?7:(B/x&
4)!=0?0:l[i++]|16:7;im=new Image[16];for(;b<15;b++)if(b<7|b>8)im[b]=this.
public void stop(){removeMouseListener(this);}
public void update(Graphics g){paint(g);}
public void paint(Graphics g){int x,y,c=21,a;boolean n=false;for(y=0;y<320;y+=
40){for(x=0;x<320;x+=40){g.drawImage(im[I[c]&z],x,y,40,40,n?new Color(144,144,
208):new Color(192,192,255),this);if(c==B){g.setColor(new Color(255,255,0));g.
void Z(){paint(getGraphics());}public void mouseExited(MouseEvent e){}public
void mousePressed(MouseEvent e){}public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseReleased(MouseEvent e){int s=e.getX()/40+(e.getY()/40*x+21);i=
(I[s]^y)&z;if(i>8){B=s;Z();}else if(B!=0&&i<9){b=s;i=I[B]&z;if((i&7)==1&(b<29|b
public String getAppletInfo(){return"Toledo Java Chess by Oscar Toledo G.";}
int X(int w,int c,int h,int e,int S,int s){int t,o,L,E,d,O=e,N=-M*M,K=78-h<<x,p
,g,n,m,A,q,r,C,a=y>0?-x:x;boolean D,J;y^=8;G++;D=w>0||s>0&&s>=h&&X(0,0,0,21,0,0
9&&q>0|A>2){if((r&7)==2){y^=8;I[G--]=O;return K;}m=0;n=o&z;J=true;E=I[p-a]&z;if
;}else if(g>0)I[g]=0;L-=X(s>h|D?0:p,L-N,h+1,I[G+1],E=q>0|A>1?0:p,s);if(h<1&s==1
&&B==O&i==n&p==b&L>-M){G--;return u=E;}J=q!=1||A<7||m>0||s<1|D|r>0|o<z||X(0,0,0
,21,0,0)>M;I[O]=o;I[p]=r;if(m>0){I[m]=I[g];I[g]=0;}else if(g>0)I[g]=9^y;}if(L>N
){I[G]=O;if(s>1){if(h>0&&c-L<0){y^=8;G--;return L;}if(h<1){i=n;B=O;b=p;}}N=L;}
while(e!=O);y^=8;G--;return N!=-M*M&&N>-K+1924|D?N:0;}}
Java has a tendency to be a little slow because it is translated to an intermediate language and then interpreted or compiled in execution time.

How to compile it

To compile the source code it is necessary to have the Java Development Kit available from
At the directory of the Java compiler and with the source code in the same directory, enter this in the command line:
javac -d . -Xlint:deprecation -g:none -target 1.5
This will generate the smallest possible class file, then it can be invocated by an HTML file and executed by a web browser:
<applet code="toledo_chess.class" width="320" height="320">
Of course this HTML code (name it something like chess.html) should be in the same directory as the toledo_chess.class file, and remember to put the GIF graphics available from the ZIP file of Toledo Javascript Chess.

Related links

Last modified: Oct/10/2015