

This is a fully working chess program with graphics. In 1024 bytes of JS. o_0 - archon810
http://js1k.com/demo/435

======
wensing
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();

~~~
euccastro
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?

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

~~~
bd
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>

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

~~~
StavrosK
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?

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

~~~
adamtj
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.

------
michael_dorfman
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.

------
halo
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.

~~~
pella
...only 672 bytes. source code:

<http://users.ox.ac.uk/~uzdm0006/scans/1kchess/>

and ZX-81 emulator to play the game:

[http://www.zx81stuff.org.uk/zx81/emulate.php?tzx=0/1KZXChess...](http://www.zx81stuff.org.uk/zx81/emulate.php?tzx=0/1KZXChess\(Artic\).tzx.zip%400&title=White+queen+pawn+moved+from+1K+ZX+Chess)

------
ilitirit
Anyone wanna have a stab at explaining how it works?

~~~
dabeeeenster
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!

~~~
kenjackson
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.

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

~~~
mechanical_fish
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_.

~~~
SageRaven
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.

------
antirez
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.

~~~
iamwil
I lost. :(

------
joey_bananas
1024 bytes... and a 60 meg browser.

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

------
StavrosK
Looks like someone just won the contest...

~~~
phoboslab
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>

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

------
eliben
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!

~~~
pavs
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.

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

~~~
fizx
<http://nanochess.110mb.com/chess4.html>

Much older than this article, but same idea.

------
clueinfool
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>

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

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

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

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

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

------
wensing
Title is wrong: it's 1014 bytes.

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

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

------
pella
similar ...

<http://news.ycombinator.com/item?id=1034359>

------
SeanLuke
Castling is broken.

------
seles
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)

~~~
wensing
Feel free to improve on it.

