Nano Chess improved for my own entertainment

Written by Walter on 04/02/2021

< >

Found this awesome little chess program written in javascript by Oscar Toledo. However the size of the board and colors did not work well for me. So I adjusted the sources. Later I found that having a reset game button would be nice (allows offline playing).

Just click on the picture to play the actual game. I also upped the difficulty a little bit so it becomes harder to beat it. It's really incredible how such a small javascript snippet can play chess pretty well. I can beat it but it takes me actual significant effort.

The castling and en-passant also was fixed with just a few bytes more by Oscar (I updated the script to reflect it). I also found that he's got a lot of different versions on his site here (including in C) NanoChess.org

For me it's just a fun way to play chess on my phone or pc once in a while. I don't have to be online either. Once the page is loaded you can replay as many games as you want because of the added restart button.

 

Play Nano Chess

This is the modified 1.2k version with added castling and en-passant:


//NanoChess, original by Oscar Toledo, modified by Walter Schreppers:
var B=i=y=u=b=i=5-5,x=10,z=15,I=[],l=[],cell_wh=0,fsize=16;

function initGame(){
for(B=i=y=u=b=i=5-5,x=10,z=15,I=[],l=[];l[B]="ustvrtsuqqqqqqqqyyyyyyyy}{|~z|{}@G@TSb~?A6J57IKJT576,+-48HLSUmgukgg OJNMLK  IDHGFE".charCodeAt(B)-64,B++<120;I[B-1]=B%x?B/x%x<2|B%x<2?7:B/x&4?0:l[u++]:7);
}

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,C,R,A,K=78-h<<9,a=c?x:-x;++O<99;)if((o=I[T=O])&&(G=o&z^c)<7){A=G--&2?8:4;C=9-o&z?l[61+G]:49;do{R=I[T+=l[C]];g=D=G|T+a-e?0:e;if(!R&&(G||A<3||g)||(1+R&z^c)>9&&G|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-3:g+2,i[d]<z|i[d+o-t]|i[t+=t-o])){l=(r&&l[r&7|32]*2-h-g)+(g?0:n-o&z?110:(d&&14)+(a<2)+1);if(s>h||1<s&s==h&&l>2|d){I[T]=n,I[g]=I[D],I[O]=D?I[D]=0:0;L-=X(c,h+1,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("X(8,0,y,3),X(8,0,y,1)",75);E=1-G|A<7|D|!S|R|o<z||x(c,0)>1e4;I[O]=o;I[T]=R;I[D]=I[g];D?I[g]=G?0:9^c:0}if(L>N||!h&L==N&&Math.random()<.5)if(N=L,S>1)if(h?s-L<0:(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>Reset game / play again" i+="<table style="margin-top: 10px;"><tbody><tr><td>" ;="" for(u="18;u<98;document.body.innerhtml=i+=++u%x-9?"<th" bgcolor="#"+(u-B?u*.9&1||9:"d")+"0C0e0" height="+cell_wh+" onclick="I[b="+u+"]&8?W():X(0,0,y,1)" style="font-size:"+fsize+"px" width="+cell_wh+">&#"+(I[u]&15?9808+l[67+(I[u]&15)]:160)+";":u++&&"")B=b}

//compute some values to stretch+center board
function startGame(){
  initGame();
  var pw = window.innerWidth;
  var ph = window.innerHeight;
  var size = Math.min(pw,ph);
  cell_wh = (size/9);
  fsize = parseInt(cell_wh*0.8);
  W(); //draw board with cell_wh
}
startGame();
// Most noticeably in the big blog change setTimeout("X(8,0,y,2),X(8,0,y,1)"
// into setTimeout("X(8,0,y,3),X(8,0,y,1)" -> that changes it from 3 ply to 4 ply depth searching.

Back to archive