views:

931

answers:

5

I was wondering if anyone knew of a "World's Smallest Chess Program". By this I mean a text based program that can play a game of chess following all the rules (castling, en passent, underpromotion, game ends with mate(stale), draw by repetition or insufficent material) in the least amount of code. I found some forums on this when I checked google but I couldn't find any real code for one. I was trying to wrap my head around how to optimize it. I mean I could probably get it down to about 200-250 lines of Java but I'm sure that isn't very good compared to what some of the genuises here could do. What's the shortest code you can come up with to play chess. (Not well mind you just find all possible moves and make a random move or just find the first legal response to the players move)

(You know not counting the whole int main()(printf("White Resigns 0-1");return 1;} chess engine)

+2  A: 

Lots of chess engines implement the xboard protocol. This is human readable, but can also be piped to for example Winboard for a graphical representation.

There are a lot of computer chess hobbyist sites. I can only say, google something like "chess engine xboard"

Thorarin
+11  A: 

From World Smallest Chess Program (sic):

10.2 The game is won by the player whose opponent declares he resigns. This immediately ends the game.

puts(const char *);main(){puts("I resign");return 0;}"

But seriously, from Micro-Max, a 133-line Chess Source:

As far as I am aware, this still makes micro-Max the smallest C Chess program in existence. A close competitor for this honor, Toledo, measures 2168 characters. Despite its smaller size, micro-Max seems to beat Toledo easily

Here's all 133 lines of it (invoking fair use under comment/review):

#define F(I,S,N) for(I=S;I<N;I++)
#define W(A) while(A)

int V=112,M=136,I=8e3,C=799,X,Y,Q,N,
d[]={-16,-15,-17,0,1,16,0,1,16,15,17,0,14,18,31,33,0,
     1,1,3,-1,3,5,9,
     7,-1,11,6,8,3,6},
b[128]={6,3,5,7,4,5,3,6};

char n[]=".?+pkltd?*?PKLTD";

D(k,q,l,e,x,n)
int k,q,l,e,x,n;
{
 int i=0,j,t,p,u,r,y,m=n>1|q>e?q:e,v,h,z;

 N++;
 do{
  u=b[i];
  if(u&k)
  {r=p=u&7;
   j=d[p+23];
   W(r=p>2&r<0?-r:-d[++j])
   {y=i;
    do
    {y+=r;if(y&M)break;
     t=b[y];if(t&k|p<3&!(r&7)-!t)break;
     v=99*d[t&7|16];
     if(v<0)m=I;
     if(m>=l)return m;

     if(h=n-(y!=x))
     {if(p<6)v+=b[i+8]-b[y+8];
      if(p==4)v+=9*(e>C?!((x^y)&68):-3);

      b[i]=0;b[y]=u;
      if(p<3)
      {v-=9*(((i-2)&M||b[i-2]!=u)+((i+2)&M||b[i+2]!=u)-1);
       if(y+r+1&128){b[y]|=7;v+=C;}
      }
      v=-D(24-k,-l,-m,z=-e-v,y,h);
      v-=v>I/3;
      if(x==9){if(v+I&&i==X&y==Y){Q=z;return l;}v=m;}
      b[i]=u;b[y]=t;

      if(v>m){m=v;if(x&8){X=i;Y=y;}}
     }
     t+=p<5;if(p<3&&6*k+(y&V)==128)t--;
    }W(!t);
   }
  }
 }W(i=i+9&~M);
 return m+I?m:-D(24-k,-I,I,0,x,1)/2;
}

main()
{
 int k=8,*p,c[9],d;

 F(X,0,8)
 {b[X+V]=(b[X]+=16)-8;b[X+16]=18;b[X+96]=9;
  F(Y,0,8)b[16*Y+X+8]=(X-4)*(X-4)+(Y-3.5)*(Y-3.5);
 }

 W(1)
 {F(N,0,121)printf(" %c",N&8&&(N+=7)?10:n[b[N]&15]);
  p=c;W((*p++=getchar())>10);
  if(*c-10){X=*c-16*c[1]+C;Y=c[2]-16*c[3]+C;}else
  {d=6;N=0;W(N<1e6)D(k,-I,I,Q,8,d++);}
  if(D(k,-I,I,Q,9,2)==I)k^=24;
 }
}

Here's one that implements under-promotions and I think it's only 100 lines (it even runs in codepad (!!!)) based on the comment:

micro-Max,
A chess program smaller than 2KB (of non-blank source), by H.G. Muller version 1.6 (1433 non-blank characters) features:

  • recursive negamax search
  • quiescence search with recaptures
  • recapture extensions
    • (internal) iterative deepening
  • best-move-first 'sorting'
  • full FIDE rules and move-legality checking

accepts under-promotions: type 1,2,3 (=R,B,N) after input move
(input buffer c[] & *P made global, K and N encoding swapped for this)

pageman
thanks thats incredibly awesome.Thats incredibly terse.
faceless1_14
I rather doubt this covers all the rules (underpromotion, capturing en passant, castling, draw by repetition of position, draw by too many turns with neither a pawn move nor capture, etc.), although it's too terse to tell by quick analysis.
David Thornley
@David Thornley found one on the same site that accepts underpromotions - will post it now :)
pageman
ya this appears to not support e.p and castling but its still ridiculous it would take me days to go through this and figure out how it works
faceless1_14
> #define W(A) while(A)I still cant figure out why anyone would do something like this... What's the point in absolutely unreadable source?
x3ro
I suspect the contest was minimum number of bytes of source.Would the most understandable program be the smallest, or is there a more verbose but more readable implementation?
Martin Beckett
And the legibility probably doesn't suffer even after the compiler has a go at it! ;P
JustJeff
@x3ro The purpose is to make the smallest code possible, it's a common technique in this type of programs designed to be not easy to maintain but small
victor hugo
@mgb there's a very detailed discussion of how this is implemented at http://home.hccnet.nl/h.g.muller/encode.html (go to the bottom and just keep clicking "next") also here's the variable index: http://home.hccnet.nl/h.g.muller/var.html and also a version of the program with longer more descriptive variables http://home.hccnet.nl/h.g.muller/maximax.txt
pageman
+2  A: 
apt-get install gnuchess
fortran
+8  A: 

Here is an article about the 1k chess for the Sinclair ZX81. Here is another article about it. And here it's playable in a browser with an java-applet emulator of the ZX81, amazin :)

epatel
Those old chess engines were cool. The Atari one reused its own code as graphics.
Nosredna
+1 wow. wow. wow. wow. wow. wow. Kuro5shin was right - this is the "greatest program EVAR written" http://www.kuro5hin.org/story/2001/8/10/12620/2164
pageman
I used to work with the guy who did Sargon. Dan Spracklen. http://en.wikipedia.org/wiki/Sargon_(chess) Check out the oral history in the references.
Nosredna
+2  A: 

Back in 2002, Douglas Banall wrote a chess implementation for a 5k competition (Programming anything impressive in less than 5k). His game included not only the AI, but also the complete graphics and UI. It is completely playable in a webbrowser: It can now be found and tested here.

Cassy