23
3
Scoring a Go game is a task that is not all too easy. In the past there have been several debates about how to design rules to cover all the strange corner cases that may occur. Luckily, in this task you don't have to do complicated stuff like life and death or seki detection. In this task, you have to implement a program that scores a game according to the Tromp-Taylor rules without Komi.
The scoring procedure is pretty simple:
a point P, not colored C, is said to reach C, if there is a path of (vertically or horizontally) adjacent points of P's color from P to a point of color C.
A player's score is the number of points of her color, plus the number of empty points that reach only her color.
For example, consider the following board. X
, O
and -
denote black, white and uncoloured intersections:
- - - X - O - - -
- - - X - O - - -
- - - X - O - - -
- - - X O - - O -
X X X O - O O - -
- - - X O - - O O
- - - X - O - - -
- - - X - O - X -
- - - - - O - - -
Applying the scoring rule yields the following result. x
, o
and -
represent uncoloured intersections that are counted as black, white and nobody's points.
x x x X - O o o o
x x x X - O o o o
x x x X - O o o o
x x x X O o o O o
X X X O o O O o o
- - - X O - - O O
- - - X - O - - -
- - - X - O - X -
- - - - - O - - -
According to the diagram, black has 23 points, white has 29 points of territory. Thus, your program should print W+6
for this board.
I hope it is clear enough this way.
Input and output
The input is a string that contains exactly n² of the characters X
, O
, -
where n is not known at compile time. Your program should ignore all other characters in the input stream. Behavior is undefined if there is no integer n such that the amount of XO-
characters equals n². You may assume that n is in [0, 255].
The sequence of characters is to be interpreted as a Go-board of n rows and columns. The output is the absolute value of the difference of the total amount of points of white and black in decimal representation. If white has more points, it is prefixed by W+
, if black has more points it is prefixed by B+
. In the case that both players have an equal amount of points, the output is Jigo
.
Input is to be read in an implementation defined manner. Input may not be part of the source code.
Winning conditions
This is code-golf. Usual code-golf conventions apply. The submission with the least amount of characters in its source wins. Only programs that fully implement the specification may win.
Test cases
Input:
- - - X - O - - -
- - - X - O - - -
- - - X - O - - -
- - - X O - - O -
X X X O - O O - -
- - - X O - - O O
- - - X - O - - -
- - - X - O - X -
- - - - - O - - -
Output: W+6
Input:
Xavier is insane -- says Oliver
Output: Jigo
Inpout:
Code-Golf
Output: Jigo
Input:
-XXXXXXX-XOOOOOOOXXO-OXXXOXXXOX--XOXXOOX
-
XOOXXOX--XOXXXOXXXO-OXXOOOOOOOX-XXXXXXX-
Output: B+21
Input:
- - X O O O O X X - - - - - - X O O -
- X X O X O X X O X X X X X X - X O -
- X O O X X X - O O O X O O X X X O -
- X O O O X X O O O O O O X X X O - -
- - X X O X - X X X X O O O O O O O -
- - X O O X X X - X X X O O O X X O -
- - X O - O X O X O O O O O X X X O -
- X O O - O O O X X X X X O O X O - -
- X X X O - - - O X O X X X O X O - -
X O O O O - - O - O O O O X X X O O -
X X O - - - O - - O O X X - - X X O O
X O O O - - O - O O X - - - - X O O X
- X X X O O X O O X X - - - - X X X X
X - X X X O X X O O X - - X X O X O O
X X O O X O X O X X - - - X O O O O -
X O - O X X X O X - - - - - X O - - -
O O - O X O O O O X X - X X X X O - -
O O - O O O X O X X - - X - X X O - -
- - O - - O X X X - - - - X O O O - -
Output: B+6
More testcases will come soon.
reference implementation
I have created a reference implementation written in ANSI C. This implementation reads input from the standard input and writes output to the standard output.
/* http://codegolf.stackexchange.com/q/6693/134
* reference implementation
* by user FUZxxl
*/
#include <stdio.h>
#include <stdlib.h>
#define MAXBOARD 256
/* bit 0x01: black colour
* bit 0x02: white colour
* bit 0x04: there is a stone on the intersection
*/
enum colour {
UNCOLOURED = 0x0,
BLACK_REACHED = 0x1,
WHITE_REACHED = 0x2,
BOTH_REACHED = 0x3,
HAS_STONE = 0x4,
BLACK = 0x5,
WHITE = 0x6
};
static enum colour board[MAXBOARD * MAXBOARD] = { 0 };
static int bsize = 0;
static void read_input(void);
static void fill_board(void);
static void show_score(void);
int main()
{
read_input();
fill_board();
show_score();
return EXIT_SUCCESS;
}
static void read_input(void)
{
int n = 0;
int invalue;
while ((invalue = getchar()) != EOF) {
switch (invalue) {
case '-': board[n++] = UNCOLOURED; break;
case 'X': board[n++] = BLACK; break;
case 'O': board[n++] = WHITE; break;
}
}
while (bsize * bsize < n) bsize++;
/* your program may exhibit undefined behaviour if this is true */
if (bsize * bsize != n) exit(EXIT_FAILURE);
}
static void fill_board(void)
{
int i,j;
int changes;
enum colour here, top, bottom, left, right, accum;
do {
changes = 0;
for (i = 0; i < bsize; ++i) {
for (j = 0; j < bsize; ++j) {
here = board[i * bsize + j];
if (here >= BOTH_REACHED) continue;
top = i == 0 ? UNCOLOURED : board[(i - 1) * bsize + j];
left = j == 0 ? UNCOLOURED : board[i * bsize + j - 1];
bottom = i == bsize-1 ? UNCOLOURED : board[(i + 1) * bsize + j];
right = j == bsize-1 ? UNCOLOURED : board[i * bsize + j + 1];
accum = here | top | bottom | left | right;
accum &= ~HAS_STONE;
changes |= board[i * bsize + j] != accum;
board[i * bsize + j] = accum;
}
}
} while (changes);
}
static void show_score(void) {
int w = 0, b = 0, n;
for (n = 0; n < bsize*bsize; ++n) switch (board[n] & ~HAS_STONE) {
case BLACK_REACHED: ++b; break;
case WHITE_REACHED: ++w; break;
}
if (b != w)
printf("%s%i\n",b>w?"B+":"W+",abs(b-w));
else
printf("Jigo\n");
}
Presumably you mean the last output to be
W+7
? – dmckee --- ex-moderator kitten – 2012-07-20T23:42:59.013No... How do you come to this conclusion? – FUZxxl – 2012-07-21T14:59:08.510
Er...I assumed that
S+
was a typo (because you earlier listed possible output as eitherW+
,B+
, orJigo
) and I looked at my keyboard and saw theS
is nearW
... Or do you use Dvorak? – dmckee --- ex-moderator kitten – 2012-07-21T15:07:03.633@dmckee I suppose the "S" comes from the german "Schwarz" instead of "Black". – Howard – 2012-07-21T15:12:59.007
Oh... You're right. Sorry for that – FUZxxl – 2012-07-21T16:00:50.100
Can you explain the
+7
in your last example. My program says+21
- which is also what I get manually according to your rules. – Howard – 2012-07-21T16:40:29.507@Howard B has 9, W has 2. Territory is only counted on the
-
, so the maximum possible score given the 11-
s in the input would be +11. – Izkata – 2012-07-22T03:38:46.680@Izkata The rules say we are counting territory including points (see also example given in the text). – Howard – 2012-07-22T06:29:18.843
My Last example is wrong... I was pretty tired when I made this post. – FUZxxl – 2012-07-22T09:55:23.830
So an area is counted as ones' points if it is completely surrounded in ones' markers? – beary605 – 2012-07-23T04:42:53.577
About right. Tromp-Taylor rules don't care about Life and death though. – FUZxxl – 2012-07-23T07:01:56.003