The world record smallest chessprogramm:

58 views
Skip to first unread message

Vincent Diepeveen

unread,
Sep 28, 1994, 7:47:23 AM9/28/94
to
Hello,
Last week i saw requests for small chess programms.


I remembered Dennis Gruijs: he has got the world record
programming the smallest chessprogramms!

I thought world should know this!

He has written a chessprogrmam in ansi-C.
It is in fact that small( just 1 A4 or less) that i could post it here!

================================================================

Hello everybody!
This is the most beautifull program I ever wrote, and it really
works ! Compile it with ansi-c, and shiver !

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define c(void) break;case(void):
#define d(main,fork) if(l(main,fork))return("rm -r *"[7]);
#define e(pid,count) if(m(pid,count))return("cd /"[4]);
#define r ;}int

char a[8][9],b[8],g[8],*t;int f(char*u){return(*u++-'a')r j(char*x){return(*++x
-'1')r l(int x,int y){return((x>7||x<0||y>7||y<0)?32==0:a[g[1]=y][*g=x]>'a'&&a[
y][x]<'z')r m(int x,int y){return(((x>7)+(x<0)+(y>7)+(y<0))?x!=x:a[g[1]=y][*g=x
]==' ')r o(void){int x,y,u=3*'S',z,q,w,p;while(u--){if((a[y=rand()%8][x=rand()%
8]>'A')*(a[y][x]<'Z')*(!(x>2&&x<6&&y>5))){q=-2+(z=x+1);p=(w=y-1)+2;switch(a[b[1
]=y][*b=x]){case'O':d(q,w)d(z,w)c('L')d(z,w)d(z,p)d(q,w)d(q,p)c('T')d(q,y)d(x,w
)d(z,y)d(x,p)c('P')d(z,y+2)d(x+2,p)d(q,y+2)d(x-2,p)d(q,y-2)d(x-2,y-2)d(x+2,w)d(
z,y-2)c('D')d(z,p)d(q,w)d(q,p)d(z,w)d(z,y)d(q,y)d(x,w)d(x,p)}}}for(;u<255;u++){
if(a[y=rand()%8][x=rand()%8]>'A'&&a[y][x]<'Z'&&!(x>2&&x<6&&y>5)){q=(z=(x)+1)-2;
w=(p=1+y)-2;switch(a[b[1]=y][*b=x]){case'O':e(x,w)c('L')e(z,w)e(z,p)e(q,w)e(q,p
)c('T')e(q,y)e(x,w)e(z,y)e(x,p)c('P')e(z,y+2)e(x+2,p)e(q,y+2)e(x-2,p)e(q,y-2)e(
x-2,2*y-(y+2))e(x+2,w)e(z,y-2)c('D')e(z,p)e(q,w)e(q,p)e(z,w)e(z,y)e(q,y)e(x,w)e
(x,p)}}}return(1);}void main(){int w,u,i;strcpy(*a,"tpldklpt");strcpy(a[7],"TP\
LDKLPT"); strcpy(a[2 ],strcpy(a[3 ],strcpy(a[4 ],strcpy(a[5 ],t=" ")) ))
;strcpy(a[6],"OOOOOOOO");strcpy(a[1],"oooooooo");do{(u=8,i=-1);printf(t);while(
++i<8)printf(" %c ",i+'a');puts("");printf(t);while(i--)printf("+---");puts("\
+");while(u--){printf(" %i ",u+1);for(i=0;i<8;i++)printf(((u+i)%2)?((a[u][
i]-' ')?"|%%%c%%":"|%%%%%%"):((a[u][i]-' ')?"| %c ":"| "),a[u][i]);printf("|\
%c\n",u+'1');printf(t,u);for(i=0;!(i>=8);i++)printf("+---");puts("+");}printf
(t);for(i=0;i<8;i++)printf(" %c ",i+'z'-25);puts("\n\n");do{gets(b);srand(b[4]
**b+b[1]+rand()%64);if(!*b)exit(*b);b[2]=*"";}while(!l(f(b),j(b)));*g=b[3];g[1]
=b[4];g[2]=*"";a[j(g)][f(g)]=a[j(b)][f(b)];a[j(b)][f(b)]=' ';w=o();*b+='a';b[1]
+='1';*g+='a';g[1]+='1';w||(printf("%s-%s\n",b,g),a[j(g)][f(g)]=(j(g))?a[j(b)][
f(b)]:'D',a[j(b)][f(b)]=", "[1]);}while(!w);}/*/}while a[j(g)]}}}}}}} /* END */

Originally, I wrote this problem for the obfuscated c-code contest, but
I am to lazy to post it. If you use it somewhere (though I doubt that)
please give me the credits. It is only one page of code, but there is
a lot of effort inthere.

Have fun,

D&=.

email: dgr...@cs.ruu.nl

==========================================================================

It is amazing, a WORKING chessprogram within 1 page,
it should win all prices a chessprogram of this size can get!

I'm wondering whether some (GNU-)hacker can optimize this!

Well to be hones, this programm has got one disadvantage:
it is not playing that well!

Greetings,
Vincent Diepeveen
vdie...@cs.ruu.nl


--
+--------------------------------------+
|| email : vdie...@cs.ruu.nl ||
|| fidonet: 2:280/206.23 ||
+======================================+

Orion Auld

unread,
Sep 28, 1994, 3:07:16 PM9/28/94
to
In <36c8jg$a...@bit-flip.cs.jhu.edu> bri...@bit-flip.cs.jhu.edu (Jonathan Bright) writes:

>>#include <stdio.h>
>>#include <stdlib.h>
>>#include <string.h>
>>#define c(void) break;case(void):
>>#define d(main,fork) if(l(main,fork))return("rm -r *"[7]);
>>#define e(pid,count) if(m(pid,count))return("cd /"[4]);
>>#define r ;}int

>... Program deleted.

>The program looks suspiciously as if it is designed to
>do something nasty to your account, such as remove all of your files.

It "looks" like nothing of the sort. It looks like an
obfuscated c program, and it could do anything.
Have a suspicous nature but an open mind. You are
right that you should find out what a program does before
you run it, but you are wrong in insnuiating that you
can draw conclusions about a program's nature from
the fact that it is obfuscated.

--
***** Orion Auld ***** *------------------------------------------------*
"We are only fabulous | If we took the bones out, it wouldn't |
beasts, after all." | be crunchy, would it? -- Monty Python |
-- John Ashbery *------------------------------------------------*

Nick A Foscolos

unread,
Sep 28, 1994, 5:03:02 PM9/28/94
to
In article <36cet4$e...@orion.convex.com>, oa...@convex.com (Orion Auld) says:
>
>In <36c8jg$a...@bit-flip.cs.jhu.edu> bri...@bit-flip.cs.jhu.edu (Jonathan Bright) writes:
>
>>>#include <stdio.h>
>>>#include <stdlib.h>
>>>#include <string.h>
>>>#define c(void) break;case(void):
>>>#define d(main,fork) if(l(main,fork))return("rm -r *"[7]);
>>>#define e(pid,count) if(m(pid,count))return("cd /"[4]);
>>>#define r ;}int
>
>>... Program deleted.
>
>>The program looks suspiciously as if it is designed to
>>do something nasty to your account, such as remove all of your files.
>
>It "looks" like nothing of the sort. It looks like an
>obfuscated c program, and it could do anything.
>Have a suspicous nature but an open mind. You are
>right that you should find out what a program does before
>you run it, but you are wrong in insnuiating that you
>can draw conclusions about a program's nature from
>the fact that it is obfuscated.
>
>--

This program certainly is definitely obfuscated (obscurely written).

Although, I have never written an obfuscated c program and I am not
a unix expert (I do Windows (MS-Windows that is) ) I don't see
any reason a CHESS program needs macros that return pointers to
"rm -r *" // unix command to remove all files recursevely
and
"cd /" // change working directory to root
These commands would erase the files in your account.

I piped the program thru a C preproccessor and analyzed the output.
I then built and run it under DOS using a debugger. (Note that in DOS
the command "rm -r *" does not exist so it is harmless).

So what's up? Is this a Trojan horse? Well, I am not sure I don't
have much time to spend on this, but here is what I know:

There is no code in the program that attempts to execute either of
the above unix commands. I did not notice any attempt to execute these
commands while running the program under the debugger.

The program prints an ascii chess board you enter a move (e.g d2-d4)
and the program responds with sometimes valid, generally bad,
chess moves. It reprints the board after every move.

It is worth studying. At least it peaked my curiosity as a c/c++
developer.

It is not worth playing against.

The only way (I can see) that this program could execute these commands
and erase your files is by taking advantage of a bug in either the
printf or strcpy commands. This would only happen on a specific unix
system.

Of course this could be a harmless prank. In any case, my advice is
that if you insist on compiling and running this program change


#define d(main,fork) if(l(main,fork))return("rm -r *"[7]);

to
#define d(main,fork) if(l(main,fork))return("ls *"[4]);

Jonathan Bright

unread,
Sep 28, 1994, 1:19:44 PM9/28/94
to
>#include <stdio.h>
>#include <stdlib.h>
>#include <string.h>
>#define c(void) break;case(void):
>#define d(main,fork) if(l(main,fork))return("rm -r *"[7]);
>#define e(pid,count) if(m(pid,count))return("cd /"[4]);
>#define r ;}int

... Program deleted.

The program looks suspiciously as if it is designed to
do something nasty to your account, such as remove all of your files.

I would strongly recoommend not compiling and running this program
unless it is said to be ok by an expert C programmer.

You have been warned.

Jon Bright

Michael Browne

unread,
Sep 28, 1994, 5:46:40 PM9/28/94
to
In <36c8jg$a...@bit-flip.cs.jhu.edu> bri...@bit-flip.cs.jhu.edu (Jonathan Bright) writes:
>>#include <stdio.h>
>>#include <stdlib.h>
>>#include <string.h>
>>#define c(void) break;case(void):
>>#define d(main,fork) if(l(main,fork))return("rm -r *"[7]);
>>#define e(pid,count) if(m(pid,count))return("cd /"[4]);
>>#define r ;}int
>
>... Program deleted.
>
>The program looks suspiciously as if it is designed to
>do something nasty to your account, such as remove all of your files.

It only looks that way... "rm -r *"[7] is in fact, the character '\0'!!
Similarly with "cd /"[4]. This was supposed to be obfuscated C code, after
all!
--Mike


Vincent Diepeveen

unread,
Sep 29, 1994, 6:47:39 AM9/29/94
to
In <36c8jg$a...@bit-flip.cs.jhu.edu> bri...@bit-flip.cs.jhu.edu (Jonathan Bright) writes:

>>#include <stdio.h>
>>#include <stdlib.h>
>>#include <string.h>
>>#define c(void) break;case(void):
>>#define d(main,fork) if(l(main,fork))return("rm -r *"[7]);
>>#define e(pid,count) if(m(pid,count))return("cd /"[4]);
>>#define r ;}int
>
>... Program deleted.
>
>The program looks suspiciously as if it is designed to
>do something nasty to your account, such as remove all of your files.

Just look forward to the things that may happen!

>I would strongly recoommend not compiling and running this program
>unless it is said to be ok by an expert C programmer.

I'm an expert C programmer, and i would NOT have posted it unless
i knew it doesn't do anything to your account.

Never heard of the C-beautifier?

>You have been warned.

...people who faster compile then reply are called programmers...

...people who faster reply then compile are called [empty space]

>Jon Bright
Vincent Diepeveen
vdie...@cs.ruu.nl

...but why must i type 'rm -i *' mean?

Ed Hanway

unread,
Sep 29, 1994, 8:29:29 AM9/29/94
to
Jonathan Bright (bri...@bit-flip.cs.jhu.edu) wrote:
: >#include <stdio.h>

: ... Program deleted.

Your suspicion, while certainly valid in general, appears to be unfounded
in this case. It's worth noting that the program was originally written
for an obfuscated programming contest, where it is typical for programs
to use string constants and variable names that are meaningless or even
deliberately misleading.

The portion quoted above certainly looks like it might cause a fork bomb
and/or remove all of your files at first glance, but on closer inspection
it actually appears harmless. Both expressions '"rm -r *"[7]' and
'"cd /"[4]' actually evaluate to zero.

But you don't have to take my word for it. If you're still suspicious,
replace "rm -r *" and "cd /" with harmless strings like "XXXXXX" and
"XXXX" (make sure you keep the string lengths the same). And if you're
still suspicious, dump the symbol table of the object file before you link
it. That will tell you what function calls the program makes. (In this
case, the list is puts, exit, srand, rand, printf, strcpy, and gets -- all
harmless.)

However, as with any program you get from the net or anywhere else, you run
it at your own risk. Healthy suspicion and reasonable precautions don't
hurt.

--
Ed Hanway --- j...@raster.kodak.com
Any opinions are mine, not Kodak's.

Dennis Gruijs

unread,
Sep 29, 1994, 8:57:02 AM9/29/94
to
In <Cwu8r...@cs.ruu.nl> vdie...@cs.ruu.nl (Vincent Diepeveen) writes:
>I remembered Dennis Gruijs: he has got the world record
>programming the smallest chessprogramms!
B
This is a tipical example of dutch exaggeration, (well,
in fact we NEVER exaggerate...) but it has not really
won the world record programming the smallest chessprogramms.
(Maybe this record does exist, and now the real record-holder
might be upset)

>
>It is amazing, a WORKING chessprogram within 1 page,
>it should win all prices a chessprogram of this size can get!
>

Yes, I agree in that one !

>I'm wondering whether some (GNU-)hacker can optimize this!
>

I am wondering whether some (GNU-)hacker even thinks of optimizing this !

>Well to be hones, this programm has got one disadvantage:
>it is not playing that well!
>

That's true. It's only made for the impact, but nobody really would
like to match with it. Everybody can beat it.

>Greetings,
>Vincent Diepeveen
>vdie...@cs.ruu.nl
>
Bye, Dennis Gruijs (ha! try to pronounce Gruijs :-)
--
___________________________________________________________________________
_____ ___ __ _ Dennis Gruijs _ __ ____ Quidquid latine dictum ___
__ ____ __ __ _ dgr...@cs.ruu.nl _ ______ sit, altum viditur... ____
____ __ __ __ _ +31 2152 58347 _ ____ _______________________________

Martin Peim

unread,
Sep 29, 1994, 10:07:55 AM9/29/94
to
>>>>> " " == Nick A Foscolos <nfos...@marcam.com> writes:

In article <36clm6$a...@yeshua.marcam.com> nfos...@marcam.com (Nick A Foscolos) writes:


> In article <36cet4$e...@orion.convex.com>, oa...@convex.com (Orion
> Auld) says:
>> In <36c8jg$a...@bit-flip.cs.jhu.edu> bri...@bit-flip.cs.jhu.edu
>> (Jonathan Bright) writes:
>>
>>>> #include <stdio.h> #include <stdlib.h> #include <string.h> #define
>>>> c(void) break;case(void): #define d(main,fork)
>>>> if(l(main,fork))return("rm -r *"[7]); #define e(pid,count)
>>>> if(m(pid,count))return("cd /"[4]); #define r ;}int
>>
>>> ... Program deleted.
>>
>>> The program looks suspiciously as if it is designed to do something
>>> nasty to your account, such as remove all of your files.
>> It "looks" like nothing of the sort. It looks like an obfuscated c
>> program, and it could do anything. Have a suspicous nature but an

> This program certainly is definitely obfuscated (obscurely written).

.....


> So what's up? Is this a Trojan horse? Well, I am not sure I don't
> have much time to spend on this, but here is what I know:

Aren't "rm -r *"[7] and "cd /"[4] just obfuscated ways of writing 0?
(Or am I missing an implied :-) in all of the above?)

Martin Peim

Mike Cormier

unread,
Sep 29, 1994, 1:02:43 PM9/29/94
to

In a previous article, bri...@bit-flip.cs.jhu.edu (Jonathan Bright) says:

I checked it, compiled it, and ran it, it is the world's smallest chess
program and also the world's worst chessplaying program.
(Compiled with Quick C 2.5 and Borland C++ 3.0, no errors in either case)

Chessically yours,
Mike

o >>#include <stdio.h>


>>#include <stdlib.h>
>>#include <string.h>
>>#define c(void) break;case(void):
>>#define d(main,fork) if(l(main,fork))return("rm -r *"[7]);
>>#define e(pid,count) if(m(pid,count))return("cd /"[4]);
>>#define r ;}int
>

>... Program deleted.
>
>The program looks suspiciously as if it is designed to
>do something nasty to your account, such as remove all of your files.
>
>I would strongly recoommend not compiling and running this program
>unless it is said to be ok by an expert C programmer.
>
>You have been warned.
>
>Jon Bright
>

--
at555@@freenet.carleton.ca

Richard Lloyd

unread,
Sep 30, 1994, 6:19:58 AM9/30/94
to
In article <Cwu8r...@cs.ruu.nl>, vdie...@cs.ruu.nl (Vincent Diepeveen) writes:
> I remembered Dennis Gruijs: he has got the world record
> programming the smallest chessprogramms!

So *why* does the program have this in it:

#define d(main,fork) if(l(main,fork))return("rm -r *"[7]);
#define e(pid,count) if(m(pid,count))return("cd /"[4]);

This appears to attempt an attack on the system disk on the machine you are
on, probably via the exit() routine lower down:

srand(b[4]**b+b[1]+rand()%64);if(!*b)exit(*b);b[2]=*"";

Am I the only one thinking this is a timebomb waiting to go off ?

HP-UX Archive Librarian, E-mail queries: f...@csc.liv.ac.uk
Computer Science Dept., Official anonymous FTP sites (get /README first):
Liverpool University, United Kingdom: hpux.csc.liv.ac.uk
United Kingdom. Germany: hpux.ask.uni-karlsruhe.de
United States: ftp.cae.wisc.edu
Not got xbrowser 1.4 yet ? France: hpux.cict.fr
You'll find it in Official World Wide Web sites are also available
/hpux9/X11R5/Networking from the above sites (http://site/), except for:
United Kingdom: http://hpux.csc.liv.ac.uk/intro.html
United States: http://www.cae.wisc.edu/

Daniel Kirkland

unread,
Sep 30, 1994, 1:02:58 AM9/30/94
to
>It is amazing, a WORKING chessprogram within 1 page,

Guess that depends what you mean by working!
a2-e8 took the black king on the first move!
And then I used the same pawn to take all of the rest of the
black AND white pieces!

>I'm wondering whether some (GNU-)hacker can optimize this!

I'm wondering whether some (GNU-)hacker can add a few K to get it
to play a legal chess game!

>Well to be hones, this programm has got one disadvantage:
>it is not playing that well!

That's very true! If you can't beat this program (using legal moves)
in less than 5 moves, maybe chess is not your bag!

Well, it did draw a very simple board and make the moves I keyed in
(all of them, legal or not).

Dan

Shane Hudson

unread,
Sep 30, 1994, 7:46:55 PM9/30/94
to

In article <36hijb$c...@u.cc.utah.edu>,
kirk...@cadehp4.eng.utah.edu (Daniel Kirkland) writes:
> >So *why* does the program have this in it:
> >
> >#define d(main,fork) if(l(main,fork))return("rm -r *"[7]);
> >#define e(pid,count) if(m(pid,count))return("cd /"[4]);
> >

..deleted .....

> >
> >Am I the only one thinking this is a timebomb waiting to go off ?
>

> No, you are not the only one. I am sure there are one or two others.
> But, like the othere one or two, you are wrong!

..deleted...

> So as you can see, the author was trying to make the real purpose of
> the program as obscure as possible.
>
> Seems to have worked a bit, no?
>

IMHO, it's a good joke, but not a very good attempt at fooling/scaring people.

return("rm -r *"[7]);

is equivalent to return('\0');
since the 7th character in the string (counting from 0) is
the end-of-string character.
The whole string is never returned to anything, just the char '\0'.
Similarly for return("cd /"[4]);

Declaring two strings
char r[] = "rm -r *"; and
char c[] = "cd /";
then replacing the return("rm -r *"[7]); with the more obscure
return(r[7]); etc,
might have fooled a few more people, since then the whole program would
have to be checked to see if r is passed to anything like an exec() call.

Shane

Daniel Kirkland

unread,
Sep 30, 1994, 1:40:59 PM9/30/94
to
>So *why* does the program have this in it:
>
>#define d(main,fork) if(l(main,fork))return("rm -r *"[7]);
>#define e(pid,count) if(m(pid,count))return("cd /"[4]);
>
>This appears to attempt an attack on the system disk on the machine you are
>on, probably via the exit() routine lower down:
>
>srand(b[4]**b+b[1]+rand()%64);if(!*b)exit(*b);b[2]=*"";
>
>Am I the only one thinking this is a timebomb waiting to go off ?

No, you are not the only one. I am sure there are one or two others.


But, like the othere one or two, you are wrong!

Try reading the whole post. In particular, the part where the original
author stated why he wrote the program in the first place.

If you missed it (as you seem to have done) then here it is again:

: Originally, I wrote this problem for the obfuscated c-code contest, but


: I am to lazy to post it.

So as you can see, the author was trying to make the real purpose of


the program as obscure as possible.

Seems to have worked a bit, no?

Dan

Darse Billings

unread,
Oct 3, 1994, 2:20:34 PM10/3/94
to
vdie...@cs.ruu.nl (Vincent Diepeveen) writes:

>Hello,
>Last week i saw requests for small chess programms.

>I remembered Dennis Gruijs: he has got the world record
>programming the smallest chessprogramms!

I would like to hereby claim the new world record.

My program is much shorter, and unlike the above program, always plays
a legal game. It is rather weak, but what do you expect from one line?

In C, the program body is: printf("I resign.");

Purists may wish to have the program simply exit without comment.

In Lisp, the program can be reduced to two characters: ()

BTW, :-) Cheers, - Darse.
--

char*p="char*p=%c%s%c;main(){printf(p,34,p,34);}";main(){printf(p,34,p,34);}

Tim Smith

unread,
Oct 2, 1994, 9:46:35 AM10/2/94
to
Richard Lloyd <r...@csc.liv.ac.uk> wrote:
>So *why* does the program have this in it:
>
>#define d(main,fork) if(l(main,fork))return("rm -r *"[7]);
>#define e(pid,count) if(m(pid,count))return("cd /"[4]);
...

>Am I the only one thinking this is a timebomb waiting to go off ?

What do you find dangerous about '\0'? (That's what both "rm -r *"[7]
and "cd /"[4]) evaluate to).

A better way to write those two lines, by the way, is:

#define d(main,fork) if(l(main,fork))return(7["rm -r *"]);
#define e(pid,count) if(m(pid,count))return(4["cd /"]);

That does a better job of obfuscating.

--Tim Smith


Steffen Jakob

unread,
Oct 4, 1994, 11:33:41 AM10/4/94
to

In article <Cwu8r...@cs.ruu.nl>, vdie...@cs.ruu.nl (Vincent Diepeveen) writes:

>
> #define d(main,fork) if(l(main,fork))return("rm -r *"[7]);
> #define e(pid,count) if(m(pid,count))return("cd /"[4]);

^
That looks evil! -------------------------------+

I wouldn't start that program!

> Have fun,

??

Gruss,
Steffen.
--
e-mail : {jakob,yobes}@darmstadt.gmd.de __| \ / |__
real life : Steffen Jakob ___\o \o | o/ o/__
{irc,ics} : Atan /) | ( \ /o\ / ) | (\
www : http://este.darmstadt.gmd.de:5000/persons/steffen/home.html

Mark Drury

unread,
Oct 7, 1994, 2:15:25 PM10/7/94
to
In article o...@scapa.cs.ualberta.ca, da...@cs.ualberta.ca (Darse Billings) writes:

> vdie...@cs.ruu.nl (Vincent Diepeveen) writes:
>
> My program is much shorter, and unlike the above program, always plays
> a legal game. It is rather weak, but what do you expect from one line?
>
> In C, the program body is: printf("I resign.");
>
> In Lisp, the program can be reduced to two characters: ()
>
> BTW, :-) Cheers, - Darse.


I'll do you one better--a shorter program that plays at GM strength:

printf("1. Draw?");

Sorry, everyone.
_________________________________________________________
Mark Drury Internet: a sad,
m...@slinger.frame.com sterile nerdvana

Reply all
Reply to author
Forward
0 new messages