Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Shortest Chess Program

Author: Steffen Jakob

Date: 06:56:01 08/06/01

Go up one level in this thread


On August 06, 2001 at 05:01:17, Adam Oellermann wrote:

>Hi there,
>
>I ran across this the other day - excuse me if this is common knowledge.
>
>The following is the *full* source for a chess program. It is very weak,
>apparently just moving randomly, but is nonetheless impressive to me. WARNING:
>Although I have tested the program, I'm not sure what that #define with rm -r *
>is about. Take all necessary precautions...

return("rm -r *"[7]) and return("cd /"[4]) are only some unusual ways to return
zero :-)

If you run a preprocessor over the program and the some pretty printer you will
get something like this as a result which looks much more readable (i also
removed the "rm -rf" stuff):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


char a[8][9], b[8], g[8], *t;
int f (char *u)
{
    return (*u++ - 'a');
}

int j (char *x)
{
    return (*++x - '1');
}

int 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');
}

int m (int x, int y)
{
    return (((x > 7) + (x < 0) + (y > 7) + (y < 0)) ? x !=
            x : a[g[1] = y][*g = x] == ' ');
}

int 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':
                if (l (q, w))
                    return (0);
                if (l (z, w))
                    return (0);
                break;
            case ('L'):
                if (l (z, w))
                    return (0);
                if (l (z, p))
                    return (0);
                if (l (q, w))
                    return (0);
                if (l (q, p))
                    return (0);
                break;
            case ('T'):
                if (l (q, y))
                    return (0);
                if (l (x, w))
                    return (0);
                if (l (z, y))
                    return (0);
                if (l (x, p))
                    return (0);
                break;
            case ('P'):
                if (l (z, y + 2))
                    return (0);
                if (l (x + 2, p))
                    return (0);
                if (l (q, y + 2))
                    return (0);
                if (l (x - 2, p))
                    return (0);
                if (l (q, y - 2))
                    return (0);
                if (l (x - 2, y - 2))
                    return (0);
                if (l (x + 2, w))
                    return (0);
                if (l (z, y - 2))
                    return (0);
                break;
            case ('D'):
                if (l (z, p))
                    return (0);
                if (l (q, w))
                    return (0);
                if (l (q, p))
                    return (0);
                if (l (z, w))
                    return (0);
                if (l (z, y))
                    return (0);
                if (l (q, y))
                    return (0);
                if (l (x, w))
                    return (0);
                if (l (x, p))
                    return (0);
            }
        }
    }
    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':
                if (m (x, w))
                    return (0);
                break;
            case ('L'):
                if (m (z, w))
                    return (0);
                if (m (z, p))
                    return (0);
                if (m (q, w))
                    return (0);
                if (m (q, p))
                    return (0);
                break;
            case ('T'):
                if (m (q, y))
                    return (0);
                if (m (x, w))
                    return (0);
                if (m (z, y))
                    return (0);
                if (m (x, p))
                    return (0);
                break;
            case ('P'):
                if (m (z, y + 2))
                    return (0);
                if (m (x + 2, p))
                    return (0);
                if (m (q, y + 2))
                    return (0);
                if (m (x - 2, p))
                    return (0);
                if (m (q, y - 2))
                    return (0);
                if (m (x - 2, 2 * y - (y + 2)))
                    return (0);
                if (m (x + 2, w))
                    return (0);
                if (m (z, y - 2))
                    return (0);
                break;
            case ('D'):
                if (m (z, p))
                    return (0);
                if (m (q, w))
                    return (0);
                if (m (q, p))
                    return (0);
                if (m (z, w))
                    return (0);
                if (m (z, y))
                    return (0);
                if (m (q, y))
                    return (0);
                if (m (x, w))
                    return (0);
                if (m (x, p))
                    return (0);
            }
        }
    }
    return (1);
}

void
main ()
{
    int w, u, i;
    strcpy (*a, "tpldklpt");
    strcpy (a[7], "TPLDKLPT");
    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);
}



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.