Skip to content

Instantly share code, notes, and snippets.

@fuzxxl fuzxxl/goscore.c forked from anonymous/goscore.c
Last active Aug 29, 2015

Embed
What would you like to do?
/*
* access the array b at row d, column e. Allow extra index parts before the
* closing bracket. Usually used as I] to get b[d * g + e]. All occurances of
* this macros are commented in the next parts.
*/
#define I b[d*g+e
/* int */ a; /* updated content of cell at b[d*g+e] */
/* int */ b[65536]; /* array containing the board */
/* int */ c; /* cleared if the flood-fill fills something, score */
/* int */ d; /* variable for the input loop, y coordinate */
/* int */ e; /* x coordinate */
/* int */ f; /* number of positions used up in b */
/* int */ g; /* size of the board, e.g. 19 for a 19x19 board */
/*
* The board itself contains at each position the bitwise or of the following
* values:
*
* 1 -- intersection coloured white
* 2 -- intersection coloured black
* 4 -- intersection occupied
*
* With a value of "0" meaning "empty intersection".
*/
/* int */ main()
{
/* read in the go board. Because EOF == -1, getchar() + 1 is 0 on EOF */
for(; d = getchar() + 1; f++)
b[f]
= d - 80 /* 'O' + 1 */
? d - 89 /* 'X' + 1 */
? d - 46 /* '-' + 1; if empty, set b[f] = 0 */
&& f-- /* if no match, undo effect of f++ */
: 5 /* if black, b[f] = 4 + 1; */
: 6, /* if white, b[f] = 4 + 2; */
g += g * g < f; /* incrementally compute board size as sqrt(f) */
/*
* Flood-fill algorithm. Iterate as long as c cleared (ie. something
* was filled). !c++ evaluates to 1 if c was 0 (cleared) before and sets
* c again.
*/
while (!c--) /* as long as we change parts of the board */
/* iterate over the board with coordinates (e,d) backwards */
for (d = g; d--;) for (e = g; e--;)
I] < 3 ? a = /* if b[d*g+e] is unoccupied, set a to */
3&( /* the occupied bit cleared */
I] /* b[d*g+e] */
| !!d * I - g] /* if d != 0, b[(d-1)*g + e] */
| !!e * I - 1] /* if e != 0, b[d*g + e - 1] */
| (d < g - 1) * I + g] /* if d < g - 1, b[(d+1)*g + e] */
| (e < g - 1) * I + 1]),/* if e < g - 1, b[d*g + e + 1] */
c &= I] == a, /* if value of b[d*g+e] changed, clear c */
I] = a /* write back cell content */
: 0; /* do nothing otherwise */
/* count score walking backwards down the array */
while (f--)
/* if black, count 1; if white, count -1 */
c += b[f] % 2 - b[f] / 2 % 2;
/* print the result, print Jigo if c == 0 */
printf(c ? "%c+%i" : "Jigo", c > 0 ? 66 : 87, abs(c));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.