Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does this cryptic C code snippet do?

Tags:

c

I’m reading some C code that can be found at https://home.hccnet.nl/h.g.muller/umax4_8.c. There, in main(), it has the following:

N=-1;W(++N<121)
    printf("%c",N&8&&(N+=7)?10:".?+nkbrq?*?NKBRQ"[b[N]&15]);

I don’t understand what this printf() call is doing, but somehow it outputs a chess board to the terminal.

Any idea?

like image 728
SmallChess Avatar asked Dec 16 '10 04:12

SmallChess


1 Answers

Basically, this:

for (n = 0; n < 121; ++n) {
    if (n & 8) {
        n += 7;
        putchar('\n');
    } else {
        putchar(".?+nkbrq?*?NKBRQ"[b[n] & 15]);
    }
}

What that does is, after every 8 board items, print a newline; otherwise, print out the board item indicated by b[n].

like image 85
Chris Jester-Young Avatar answered Sep 29 '22 22:09

Chris Jester-Young