News for hackers. Stuff that matters.
Re:This is a fully working chess program with graphics. In 1... by wensing on Monday September 06, 2010 @10:04AM (#1666675)
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();
Re:This is a fully working chess program with graphics. In 1... by baby on Monday September 06, 2010 @10:15AM (#1666706)
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/

Re:This is a fully working chess program with graphics. In 1... by Torn on Monday September 06, 2010 @10:40AM (#1666752)
I'm getting empty character boxes instead of chess symbols, even if I force UTF-8 in Chrome or IE. Any ideas?
Re:This is a fully working chess program with graphics. In 1... by bd on Monday September 06, 2010 @11:18AM (#1666814)
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).

Re:This is a fully working chess program with graphics. In 1... by baby on Tuesday September 07, 2010 @07:33AM (#1668602)
"once my Arial got broken".

funny sentence :D

Re:This is a fully working chess program with graphics. In 1... by gowiththeflow on Monday September 06, 2010 @01:51PM (#1667073)
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.
Re:This is a fully working chess program with graphics. In 1... by superk on Monday September 06, 2010 @01:20PM (#1667021)
Extra points for the function call:

X(c, h, e, s)

Re:This is a fully working chess program with graphics. In 1... by euccastro on Monday September 06, 2010 @12:35PM (#1666938)
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?

Re:This is a fully working chess program with graphics. In 1... by palish on Monday September 06, 2010 @12:51PM (#1666974)
Probably because he wanted to keep 1-letter variable names, and 1024 is more interesting than 1022.
Re:This is a fully working chess program with graphics. In 1... by euccastro on Monday September 06, 2010 @01:44PM (#1667065)
It's 1014, though.
Re:This is a fully working chess program with graphics. In 1... by palish on Monday September 06, 2010 @03:05PM (#1667210)
Okay, then it was to pad the js file to 1024 bytes, because 1024 bytes is more interesting than 1014 bytes.
Re:This is a fully working chess program with graphics. In 1... by DougBTX on Monday September 06, 2010 @02:38PM (#1667149)
Also setting i twice, I'm not sure why.
Re:This is a fully working chess program with graphics. In 1... by bd on Monday September 06, 2010 @03:32PM (#1667252)
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 "[email protected]")

- 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

Re:This is a fully working chess program with graphics. In 1... by euccastro on Monday September 06, 2010 @03:25PM (#1667236)
Good catch, although that would be easier to discount as an oversight.

[Edit: sibling post explains it.]

Re:This is a fully working chess program with graphics. In 1... by jpwagner on Monday September 06, 2010 @01:22PM (#1667027)
would you mind re-pasting the beautified code from the locally saved source? there are characters missing in the pasted blob (notice the italics.)
Re:This is a fully working chess program with graphics. In 1... by sandipagr on Monday September 06, 2010 @02:01PM (#1667089)
If only I could understand what is going on in the code...the variable names make my head explode :(
Re:This is a fully working chess program with graphics. In 1... by pfreet on Monday September 06, 2010 @01:44PM (#1667064)
Not a very good one :-) I just got to checkmate in 6 moves!
Re:This is a fully working chess program with graphics. In 1... by adamtj on Monday September 06, 2010 @09:37AM (#1666614)
It's not quite fully working. Either that, or I just can't figure out how to castle. Even so, it's very impressive.
Re:This is a fully working chess program with graphics. In 1... by StavrosK on Monday September 06, 2010 @09:46AM (#1666634)
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?
Re:This is a fully working chess program with graphics. In 1... by CGamesPlay on Monday September 06, 2010 @09:59AM (#1666663)
The title here on HN is "fully working", which isn't true.
Re:This is a fully working chess program with graphics. In 1... by adamtj on Monday September 06, 2010 @10:06AM (#1666684)
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.
Re:This is a fully working chess program with graphics. In 1... by kiiski on Monday September 06, 2010 @10:09AM (#1666689)
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).
Re:This is a fully working chess program with graphics. In 1... by wensing on Monday September 06, 2010 @10:12AM (#1666694)
I wonder how many characters of code it would take.
Re:This is a fully working chess program with graphics. In 1... by niyazpk on Monday September 06, 2010 @12:02PM (#1666888)
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.
Re:This is a fully working chess program with graphics. In 1... by Isofarro on Monday September 06, 2010 @12:03PM (#1666889)
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.
Re:This is a fully working chess program with graphics. In 1... by jpwagner on Monday September 06, 2010 @12:19PM (#1666908)
...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

Re:This is a fully working chess program with graphics. In 1... by ars on Monday September 06, 2010 @03:18PM (#1667232)
Also doesn't detect stalemate.
Re:This is a fully working chess program with graphics. In 1... by jpwagner on Monday September 06, 2010 @03:27PM (#1667241)
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?

Re:This is a fully working chess program with graphics. In 1... by ars on Monday September 06, 2010 @03:50PM (#1667284)
No, it just sits there.

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

Re:This is a fully working chess program with graphics. In 1... by michael_dorfman on Monday September 06, 2010 @11:04AM (#1666790)
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.

Re:This is a fully working chess program with graphics. In 1... by halo on Monday September 06, 2010 @10:02AM (#1666671)
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.
Re:This is a fully working chess program with graphics. In 1... by pella on Monday September 06, 2010 @11:06AM (#1666793)
Re:This is a fully working chess program with graphics. In 1... by caf on Monday September 06, 2010 @10:18AM (#1666715)
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!
Re:This is a fully working chess program with graphics. In 1... by ilitirit on Monday September 06, 2010 @10:35AM (#1666749)
Anyone wanna have a stab at explaining how it works?
Re:This is a fully working chess program with graphics. In 1... by dabeeeenster on Monday September 06, 2010 @03:24PM (#1667235)
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!
Re:This is a fully working chess program with graphics. In 1... by kenjackson on Monday September 06, 2010 @06:48PM (#1667567)
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.

Re:This is a fully working chess program with graphics. In 1... by dsplittgerber on Monday September 06, 2010 @09:53AM (#1666652)
Brilliant. Reminds of the old 4k demo competitions - craft at its best.
Re:This is a fully working chess program with graphics. In 1... by mechanical_fish on Monday September 06, 2010 @09:58AM (#1666662)
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.

Re:This is a fully working chess program with graphics. In 1... by SageRaven on Monday September 06, 2010 @01:26PM (#1667034)
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.
Re:This is a fully working chess program with graphics. In 1... by antirez on Monday September 06, 2010 @11:34AM (#1666843)
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.
Re:This is a fully working chess program with graphics. In 1... by iamwil on Monday September 06, 2010 @12:33PM (#1666933)
I lost. :(
Re:This is a fully working chess program with graphics. In 1... by joey_bananas on Monday September 06, 2010 @11:33AM (#1666840)
1024 bytes... and a 60 meg browser.
Re:This is a fully working chess program with graphics. In 1... by iamwil on Monday September 06, 2010 @12:34PM (#1666936)
Yes, and you can play anywhere in the world, on all different operating systems and platforms.
Re:This is a fully working chess program with graphics. In 1... by philjackson on Monday September 06, 2010 @12:42PM (#1666956)
Plus all of that libc. Not to mention the megabytes of kernel. How big is a bootloader these days?
Re:This is a fully working chess program with graphics. In 1... by StavrosK on Monday September 06, 2010 @09:42AM (#1666626)
Looks like someone just won the contest...
Re:This is a fully working chess program with graphics. In 1... by phoboslab on Monday September 06, 2010 @10:35AM (#1666747)
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

Re:This is a fully working chess program with graphics. In 1... by StavrosK on Monday September 06, 2010 @07:50PM (#1667638)
Jesus, those are all amazing. It was very nice of the Jump'n'Run guy to include the commented source.
Re:This is a fully working chess program with graphics. In 1... by cakeface on Tuesday September 07, 2010 @04:22PM (#1669979)
I really like this one too http://js1k.com/demo/12
Re:This is a fully working chess program with graphics. In 1... by AndyKelley on Tuesday September 14, 2010 @03:15AM (#1689800)
Syntax highlighting Quine is great! Although quine part was pretty easy :P
Re:This is a fully working chess program with graphics. In 1... by julian37 on Monday September 06, 2010 @05:42PM (#1667469)
He won the IOCCC a number of times before.

http://nanochess.110mb.com/

Re:This is a fully working chess program with graphics. In 1... by eliben on Monday September 06, 2010 @11:24AM (#1666824)
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!

Re:This is a fully working chess program with graphics. In 1... by pavs on Monday September 06, 2010 @12:03PM (#1666891)
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.

Re:This is a fully working chess program with graphics. In 1... by Twisol on Monday September 06, 2010 @08:50PM (#1667715)
Apparently, I play chess worse than a pseudo-random 1012-byte JS chess bot. Burn...
Re:This is a fully working chess program with graphics. In 1... by elblanco on Monday September 06, 2010 @01:35PM (#1667047)
Perhaps someone should look at modifying this for a 4k competition with good AI and all of the movement rules.
Re:This is a fully working chess program with graphics. In 1... by fizx on Monday September 06, 2010 @03:02PM (#1667201)
http://nanochess.110mb.com/chess4.html

Much older than this article, but same idea.

Re:This is a fully working chess program with graphics. In 1... by clueinfool on Tuesday September 07, 2010 @11:06AM (#1669038)
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

Re:This is a fully working chess program with graphics. In 1... by Jach on Monday September 06, 2010 @05:42PM (#1667470)
It's trivial to defeat with the four-move checkmate, but still an interesting piece of code.
Re:This is a fully working chess program with graphics. In 1... by daralthus on Monday September 06, 2010 @11:42AM (#1666859)
well I don't want to be in his place debugging this: http://tinypic.com/r/24zknc9/7 :D
Re:This is a fully working chess program with graphics. In 1... by euccastro on Monday September 06, 2010 @12:39PM (#1666950)
How is that position illegal? A black pawn could have eaten some piece at h3, then advanced two squares to be promoted to queen.
Re:This is a fully working chess program with graphics. In 1... by daralthus on Monday September 06, 2010 @02:40PM (#1667152)
oops my bad.. seems like you are right. I'm really ashamed now.. I go and play more as a punishment:D
Re:This is a fully working chess program with graphics. In 1... by AndyKelley on Monday September 13, 2010 @05:55AM (#1686067)
Nothing like something like this to come along every now and then and completely destroy my hubris.
Re:This is a fully working chess program with graphics. In 1... by wensing on Monday September 06, 2010 @10:15AM (#1666709)
Title is wrong: it's 1014 bytes.
Re:This is a fully working chess program with graphics. In 1... by dansingerman on Monday September 06, 2010 @11:02AM (#1666785)
Can castling and/or en passant be squeezed into 10 bytes?
Re:This is a fully working chess program with graphics. In 1... by jacquesm on Thursday September 09, 2010 @09:41PM (#1677587)
The 'best' page is really useful sometimes, I completely missed this one. What a gem!
Re:This is a fully working chess program with graphics. In 1... by pella on Monday September 06, 2010 @11:52AM (#1666876)
Re:This is a fully working chess program with graphics. In 1... by SeanLuke on Monday September 06, 2010 @03:40PM (#1667267)
Castling is broken.
Re:This is a fully working chess program with graphics. In 1... by seles on Monday September 06, 2010 @10:11AM (#1666693)
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)
Re:This is a fully working chess program with graphics. In 1... by wensing on Monday September 06, 2010 @10:20AM (#1666722)
Feel free to improve on it.