Can You Beat 1024 Bytes of JavaScript at Chess?
The first time I played The Kilobyte's Gambit, I lost in four moves. Chess is hard and I'm bad at it, but I didn't expect to lose to a program that's running on 1024 bytes of JavaScript. I played a few more times, losing within six moves every game, before I decided that The Kilobyte's Gambit would always get the best of me. The new game is an ingenious bit of design-combining chess with the aesthetics of 1980s Apple II games and The Queen's Gambit.
The Kilobyte's Gambit is hosted on Vole.wtf, a site that hearkens back to the messy, early days of the internet.
The site is just trying to make the internet a little bit more fun and interesting," site creator Matt Round told Motherboard in an email.
Chess is popular and old and for as long as the internet's been around, people have been trying to design better and more elegant chess programs. Programmers are constantly trying to write chess programs that take less and less space. One of the most famous is programmer Oscar Toledo G's 1K chess.
Round's The Kilobyte's Gambit uses a modified version of Toledo G's chess engine running with a Queen's Gambit themed user interface. After watching The Queen's Gambit the idea of doing something with one of those tiny chess engines came to mind, so I got in touch and Oscar kindly gave permission," Round said. Before I had the chance to draw anything, I saw Pinot W. Ichwandardi's MS-DOS game mockup and knew that any graphics I'd do simply wouldn't be that good, he'd done a wonderful job, luckily he also said it was fine to use his work. So I modified a 1.25KB chess engine to make it smaller, and reworked the graphics to suit the game."
What follows is the entirety of what round calls the game's brain," it's code.
for(B=y=u=b=0,x=10,z=15,I=[],l=[];l[B]=("ustvrtsuqqqqqqqq"+"yyyyyyyy}{|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);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=Th||12|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(I,B=b,c,y=E);E=1-G|A<7|D|!S|R|o1e4;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{X(8,0,y,V);X(8,0,y,1)};Z=(U)=>{b=U;I[b]&8?W(I,B=b):X(0,0,y,1)}"
That code contains everything The Kilobyte's Gambit needs to dominate a chess match. Included above are setting up the board and pieces, checking your moves are legal, and deciding how to respond. It doesn't flag up checkmate/stalemate though, you'll have to work that out for yourself," Round wrote on vole.wtf. Looking 4 moves ahead, a points system considers factors such as the value of pieces, the strength of areas of the board, and speed of capture/victory. It calls an external function to update the display, and the display code calls functions to trigger moves."
It's a chess program so devious that even its creator has a hard time beating it. Out of dozens/hundreds of attempts I've beaten it 3 or 4 times, but I'm terrible at chess and haven't improved much," Round said.