Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
This is a fully working chess program with graphics. In 1024 bytes of JS. o_0 (js1k.com)
189 points by archon810 on Sept 6, 2010 | hide | past | favorite | 68 comments


It's not quite fully working. Either that, or I just can't figure out how to castle. Even so, it's very impressive.


It says in the description that it doesn't support castling or en passant. Also, wtf, the guy wrote a chess program with AI in 1 kb and you're complaining about castling?


The title here on HN is "fully working", which isn't true.


Agreed, I wasn't complaining, I was just pointing out a minor inaccuracy in the title. It's very impressive and doesn't need to be oversold.


Castling is pretty important move in chess. You can hardly claim an implementation to be fully working without it (although it wasn't the author claiming that).


I wonder how many characters of code it would take.


The problem with castling is that for a move that is only once done in a chess game we have to keep track of a couple of variables and do a check for every move in the tree. It is rather unpleasant to code and it will increase the program size and decrease the speed (by a bit I mean). May be that is because they ended up not implementing castling.


it's one of three special cases where it isn't moving a piece from and to a square. The other being capturing enpassant, and queening a pawn. Certainly cannot claim to be complete without being able to play a legal move.


...promoting a pawn

...and then there's the 50 move and repetition draws

...and a non-chess player may not realize how vital a knowledge of openings is

but it basically works


Also doesn't detect stalemate.


Also doesn't detect stalemate.

umm...are you saying that when in a position where it's impossible to make a move, it still moves?


No, it just sits there.

Does it not report win/loose? I only played one game.


My favorite is still Peter Jennings' Micro-Chess, from 1976, which ran on a KIM-1 (6502) machine and used 1.1K of RAM.

http://users.telenet.be/kim1-6502/microchess/microchess.html

I had the PET 2001 version (which ran in 7K) and played pretty competent chess, all things considered-- disassembling that, back in the day, was a major boost to my software development education.


Reminded me of 1k ZX Chess (http://en.wikipedia.org/wiki/1K_ZX_Chess), an implementation of chess with AI for the ZX-81, a home computer which had a whopping 1k of RAM.



That's actually a more impressive achievement, since Z80 assembler is far less expressive than Javascript. And all the display output had to be in that, too!


Anyone wanna have a stab at explaining how it works?


It's recursive. Probably easier to get your head around the recursive solution to the towers of hanoi (http://en.wikipedia.org/wiki/Tower_of_Hanoi) and then start on the Chess code!


I really don't think the recursion is the hard part to understand. It's what each variable and magic number means. A while ago someone had a post of the exponentiation function written with single letter variables. It was difficult to know what it was doing. With proper variable names it would be apparent.

I think what people are asking is basically, could someone convert this to a program that is actually meant to be read, while still maintaining its exact control flow and logic.


In case you don't take the time to have your mind blown, here's the source (1014 characters):

for(B=i=y=u=b=i=5-5,x=10,I=[],l=[];l[B]="ECDFBDCEAAAAAAAAIIIIIIIIMKLNJLKM@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[i++]:7);function X(c,h,e,s){e^=8;for(var o,S,C,A,R,T,G,n,N=-1e8,O=20;++O<99;)if((o=I[T=O])&&(G=o^e)<7){A=G--&2?8:4;C=o-9?l[61+G]:49;do if(!(R=I[T+=l[C]])&&!!G|A<3||(R+1^e)>9&&G|A>2){if(!(R-2&7))return 78-h<<x;n=G|(e?T>29:T<91)?o:6^e;S=(R&&l[R&7|32]-h-G)+(G?0:n-o?55:(A<2)+1);if(c>h||1<c&c==h&&S>2){I[T]=n;I[O]=0;S-=X(c,h+1,e,S-N);if(!(h||c-1|B-O|T-b|S<-1e4))return W(B=0),e&&setTimeout("X(2,0,8),X(1,0,8)",50);I[O]=o;I[T]=R}if(S>N||!h&S==N&&Math.random()<.5)if(N=S,c>1)if(h?s-S<0:(B=O,b=T,0))return S}while(!R&G>2||(T=O,(G||A>2|(e?O>78:O<41)&!R)&&++C--A))}return N}function W(){i="<table>";for(u=18;u<98;i+=++u%x-9?"<th width=60 height=60 onclick='I[b="+u+"]>8?W(B=b):X(1,0,0)'style='font-size:50px'bgcolor=#"+(u-B?u.9&1||9:"d")+"0f0e0>&#"+(I[u]?9808+l[67+I[u]]:160)+";":u++&&"<tr>");document.body.innerHTML=i+"</table>"}W();

Beautified:

   for (B = i = y = u = b = i = 5 - 5, x = 10, I = [], l = []; l[B] = "ECDFBDCEAAAAAAAAIIIIIIIIMKLNJLKM@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[i++] : 7);
   
   function X(c, h, e, s) {
       e ^= 8;
       for (var o, S, C, A, R, T, G, n, N = -1e8, O = 20; ++O < 99;) if ((o = I[T = O]) && (G = o ^ e) < 7) {
           A = G-- & 2 ? 8 : 4;
           C = o - 9 ? l[61 + G] : 49;
           do
           if (!(R = I[T += l[C]]) && !! G | A < 3 || (R + 1 ^ e) > 9 && G | A > 2) {
               if (!(R - 2 & 7)) return 78 - h << x;
               n = G | (e ? T > 29 : T < 91) ? o : 6 ^ e;
               S = (R && l[R & 7 | 32] - h - G) + (G ? 0 : n - o ? 55 : (A < 2) + 1);
               if (c > h || 1 < c & c == h && S > 2) {
                   I[T] = n;
                   I[O] = 0;
                   S -= X(c, h + 1, e, S - N);
                   if (!(h || c - 1 | B - O | T - b | S < -1e4)) return W(B = 0), e && setTimeout("X(2,0,8),X(1,0,8)", 50);
                   I[O] = o;
                   I[T] = R
               }
               if (S > N || !h & S == N && Math.random() < .5) if (N = S, c > 1) if (h ? s - S < 0 : (B = O, b = T, 0)) return S
           }
           while (!R & G > 2 || (T = O, (G || A > 2 | (e ? O > 78 : O < 41) & !R) && ++C--A))
       }
       return N
   }
   function W() {
       i = "<table>";
       for (u = 18; u < 98; i += ++u % x - 9 ? "<th width=60 height=60 onclick='I[b=" + u + "]>8?W(B=b):X(1,0,0)'style='font-size:50px'bgcolor=#" + (u - B ? u.9 & 1 || 9 : "d") + "0f0e0>&#" + (I[u] ? 9808 + l[67 + I[u]] : 160) + ";" : u++ && "<tr>");
       document.body.innerHTML = i + "</table>"
   }
   W();


if someone's wondering how he displays chess symbols that's how :

http://en.wikipedia.org/wiki/Chess_symbols_in_Unicode

ps : also it's better to beautifier the code to (try to) understand it :

http://jsbeautifier.org/


I'm getting empty character boxes instead of chess symbols, even if I force UTF-8 in Chrome or IE. Any ideas?


Maybe some font problem? How do you see chess symbols in Wikipedia page?

It's possible to have missing or corrupted Unicode font (once my Arial got broken and I had to get it somewhere from the web).


"once my Arial got broken".

funny sentence :D


Have you patched your uxtheme.dll and are using a custom .msstyle? I'm guessing the default font of your theme may have an impact.


for(B=i=y=u=b=i=5-5, [...]

Is there a point in saying 5-5 instead of 0, other than wasting two characters for the fun of it?


Probably because he wanted to keep 1-letter variable names, and 1024 is more interesting than 1022.


It's 1014, though.


Okay, then it was to pad the js file to 1024 bytes, because 1024 bytes is more interesting than 1014 bytes.


Also setting i twice, I'm not sure why.


It seems the author likes to use variable names to encode messages:

- "Biyubi" looks like his nickname (on his chess site [1] contact mail is "biyubi@gmail.com")

- there is also "o, S, C, A, R, T, G" which are his first name and surname initials

- and already mentioned "X(c, h, e, s)"

[1] http://nanochess.110mb.com/author.html


Good catch, although that would be easier to discount as an oversight.

[Edit: sibling post explains it.]


Extra points for the function call:

X(c, h, e, s)


would you mind re-pasting the beautified code from the locally saved source? there are characters missing in the pasted blob (notice the italics.)


If only I could understand what is going on in the code...the variable names make my head explode :(


Not a very good one :-) I just got to checkmate in 6 moves!


Brilliant. Reminds of the old 4k demo competitions - craft at its best.


It reminds me of Atari 2600 chess, which used to be able to beat me [1] using 4k of ROM and 128 bytes of RAM [2].

---

[1] This is not a particularly remarkable achievement even today, of course.

[2] Note to people younger than twenty: this is not a typo.


I used to get repeatedly trounced by "Video Chess" on the TI-99/4A. Don't know how large that game was, but being from '79 it couldn't have been that large. Not that I was (or am) any good at chess -- if I fired up that game under MESS today it would probably still trounce me.


it's easy to defeat but definitely not random, it will win most of the times against people that just know the rules but rarely played.


I lost. :(


1024 bytes... and a 60 meg browser.


Yes, and you can play anywhere in the world, on all different operating systems and platforms.


Plus all of that libc. Not to mention the megabytes of kernel. How big is a bootloader these days?


Looks like someone just won the contest...


Chess is damn impressive, but so is Tetris ( http://js1k.com/demo/663 ) or a Jump'n'Run ( http://js1k.com/demo/635 ). There are also a lot of very beautiful graphic demos like http://js1k.com/demo/171 or http://js1k.com/demo/49

Shameless plug: My syntax highlighting Quine: http://js1k.com/demo/223


Jesus, those are all amazing. It was very nice of the Jump'n'Run guy to include the commented source.


I really like this one too http://js1k.com/demo/12


Syntax highlighting Quine is great! Although quine part was pretty easy :P


He won the IOCCC a number of times before.

http://nanochess.110mb.com/


Well done, but no castling support and the the AI seems pseudo-random :-)

Not to take credit from this achievement though, it's a commendable hack!


As someone who have been involved with chess engine community for the last 15 years or so (not anymore) I can tell you that, until recently, almost every single new engine, that has been written from scratch instead of copying an OS implementation, almost always left out en passant and castling in its first few releases. Even when they did implement those rules in to the engine, they did so grudgingly or as an after thought. I wonder why.

Now, everyone just copies the few dozens of OS options available out there and use their move generator which support all the rules, they just tweak with the piece values and algorithm to give a "personal touch" to the engine.


Apparently, I play chess worse than a pseudo-random 1012-byte JS chess bot. Burn...


Perhaps someone should look at modifying this for a 4k competition with good AI and all of the movement rules.


http://nanochess.110mb.com/chess4.html

Much older than this article, but same idea.


Here is a very small program, well-known in chess circles because it plays at nearly 1800 elo, which is good enough to beat strong club players.

http://home.hccnet.nl/h.g.muller/max-src2.html


It's trivial to defeat with the four-move checkmate, but still an interesting piece of code.


well I don't want to be in his place debugging this: http://tinypic.com/r/24zknc9/7 :D


How is that position illegal? A black pawn could have eaten some piece at h3, then advanced two squares to be promoted to queen.


oops my bad.. seems like you are right. I'm really ashamed now.. I go and play more as a punishment:D


Nothing like something like this to come along every now and then and completely destroy my hubris.


Title is wrong: it's 1014 bytes.


Can castling and/or en passant be squeezed into 10 bytes?


The 'best' page is really useful sometimes, I completely missed this one. What a gem!



Castling is broken.


Nice, but it isn't very good, lost to Fools Mate (it took a knight then got mated) (1 look ahead would see it, queen and bishop just attack pawn near king)


Feel free to improve on it.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: