forked from pasky/pachi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ownermap.c
138 lines (122 loc) · 3.5 KB
/
ownermap.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "board.h"
#include "debug.h"
#include "move.h"
#include "mq.h"
#include "ownermap.h"
static char *
printhook(struct board *board, coord_t c, char *s, char *end, void *data)
{
struct board_ownermap *ownermap = data;
if (!ownermap) {
strcat(s, ". ");
return s + 2;
}
const char chr[] = ":XO,"; // dame, black, white, unclear
const char chm[] = ":xo,";
char ch = chr[board_ownermap_judge_point(ownermap, c, GJ_THRES)];
if (ch == ',') { // less precise estimate then?
ch = chm[board_ownermap_judge_point(ownermap, c, 0.67)];
}
s += snprintf(s, end - s, "%c ", ch);
return s;
}
void
board_print_ownermap(struct board *b, FILE *f, struct board_ownermap *ownermap)
{
board_print_custom(b, stderr, printhook, ownermap);
}
void
board_ownermap_fill(struct board_ownermap *ownermap, struct board *b)
{
ownermap->playouts++;
foreach_point(b) {
enum stone color = board_at(b, c);
if (color == S_NONE)
color = board_get_one_point_eye(b, c);
ownermap->map[c][color]++;
} foreach_point_end;
}
void
board_ownermap_merge(int bsize2, struct board_ownermap *dst, struct board_ownermap *src)
{
dst->playouts += src->playouts;
for (int i = 0; i < bsize2; i++)
for (int j = 0; j < S_MAX; j++)
dst->map[i][j] += src->map[i][j];
}
float
board_ownermap_estimate_point(struct board_ownermap *ownermap, coord_t c)
{
assert(ownermap->map);
int b = ownermap->map[c][S_BLACK];
int w = ownermap->map[c][S_WHITE];
int total = ownermap->playouts;
return 1.0 * (b - w) / total;
}
enum point_judgement
board_ownermap_judge_point(struct board_ownermap *ownermap, coord_t c, floating_t thres)
{
assert(ownermap->map);
int n = ownermap->map[c][S_NONE];
int b = ownermap->map[c][S_BLACK];
int w = ownermap->map[c][S_WHITE];
int total = ownermap->playouts;
if (n >= total * thres)
return PJ_DAME;
else if (n + b >= total * thres)
return PJ_BLACK;
else if (n + w >= total * thres)
return PJ_WHITE;
else
return PJ_UNKNOWN;
}
void
board_ownermap_judge_groups(struct board *b, struct board_ownermap *ownermap, struct group_judgement *judge)
{
assert(ownermap->map);
assert(judge->gs);
memset(judge->gs, GS_NONE, board_size2(b) * sizeof(judge->gs[0]));
foreach_point(b) {
enum stone color = board_at(b, c);
group_t g = group_at(b, c);
if (!g) continue;
enum point_judgement pj = board_ownermap_judge_point(ownermap, c, judge->thres);
// assert(judge->gs[g] == GS_NONE || judge->gs[g] == pj);
if (pj == PJ_UNKNOWN) {
/* Fate is uncertain. */
judge->gs[g] = GS_UNKNOWN;
} else if (judge->gs[g] != GS_UNKNOWN) {
/* Update group state. */
enum gj_state new;
// Comparing enum types, casting (int) avoids compiler warnings
if ((int)pj == (int)color) {
new = GS_ALIVE;
} else if ((int)pj == (int)stone_other(color)) {
new = GS_DEAD;
} else { assert(pj == PJ_DAME);
/* Exotic! */
new = GS_UNKNOWN;
}
if (judge->gs[g] == GS_NONE) {
judge->gs[g] = new;
} else if (judge->gs[g] != new) {
/* Contradiction. :( */
judge->gs[g] = GS_UNKNOWN;
}
}
} foreach_point_end;
}
void
groups_of_status(struct board *b, struct group_judgement *judge, enum gj_state s, struct move_queue *mq)
{
foreach_point(b) { /* foreach_group, effectively */
group_t g = group_at(b, c);
if (!g || g != c) continue;
assert(judge->gs[g] != GS_NONE);
if (judge->gs[g] == s)
mq_add(mq, g, 0);
} foreach_point_end;
}