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?
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).
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.
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.
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!
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();
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)
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.
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.
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.
73 comments
[ 6.1 ms ] story [ 143 ms ] thread...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
umm...are you saying that when in a position where it's impossible to make a move, it still moves?
Does it not report win/loose? I only played one game.
Shameless plug: My syntax highlighting Quine: http://js1k.com/demo/223
http://nanochess.110mb.com/
---
[1] This is not a particularly remarkable achievement even today, of course.
[2] Note to people younger than twenty: this is not a typo.
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...
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:
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/
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).
funny sentence :D
Is there a point in saying 5-5 instead of 0, other than wasting two characters for the fun of it?
[Edit: sibling post explains it.]
- "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
X(c, h, e, s)
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.
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.
Not to take credit from this achievement though, it's a commendable hack!
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.
http://news.ycombinator.com/item?id=1034359
Much older than this article, but same idea.
http://home.hccnet.nl/h.g.muller/max-src2.html