Remove deprecated code
[matilda.git] / src / constants.c
blob5ee70ba4c3eab422fdc146c061689bd903fdbe5d
1 /*
2 Initialization of game wide constants based on board size.
4 To use declare as external:
5 d16 komi;
6 u8 out_neighbors8[TOTAL_BOARD_SIZ];
7 u8 out_neighbors4[TOTAL_BOARD_SIZ];
8 move_seq neighbors_side[TOTAL_BOARD_SIZ];
9 move_seq neighbors_diag[TOTAL_BOARD_SIZ];
10 move_seq neighbors_3x3[TOTAL_BOARD_SIZ];
11 bool border_left[TOTAL_BOARD_SIZ];
12 bool border_right[TOTAL_BOARD_SIZ];
13 bool border_top[TOTAL_BOARD_SIZ];
14 bool border_bottom[TOTAL_BOARD_SIZ];
15 u8 distances_to_border[TOTAL_BOARD_SIZ];
16 move_seq nei_dst_3[TOTAL_BOARD_SIZ];
17 move_seq nei_dst_4[TOTAL_BOARD_SIZ];
18 u8 active_bits_in_byte[256];
21 #include "config.h"
23 #include <stdlib.h>
24 #include <string.h>
26 #include "board.h"
27 #include "flog.h"
28 #include "pat3.h"
29 #include "move.h"
30 #include "types.h"
32 d16 komi = DEFAULT_KOMI;
33 u8 out_neighbors8[TOTAL_BOARD_SIZ];
34 u8 out_neighbors4[TOTAL_BOARD_SIZ];
35 move_seq neighbors_side[TOTAL_BOARD_SIZ];
36 move_seq neighbors_diag[TOTAL_BOARD_SIZ];
37 move_seq neighbors_3x3[TOTAL_BOARD_SIZ];
38 bool border_left[TOTAL_BOARD_SIZ];
39 bool border_right[TOTAL_BOARD_SIZ];
40 bool border_top[TOTAL_BOARD_SIZ];
41 bool border_bottom[TOTAL_BOARD_SIZ];
42 u8 distances_to_border[TOTAL_BOARD_SIZ];
43 move_seq nei_dst_3[TOTAL_BOARD_SIZ];
44 move_seq nei_dst_4[TOTAL_BOARD_SIZ];
46 u8 active_bits_in_byte[256];
47 bool black_eye[65536];
48 bool white_eye[65536];
50 static bool board_constants_inited = false;
52 static u8 count_bits(u8 v) {
53 u8 bits = 0;
54 while (v > 0) {
55 if ((v & 1) == 1)
56 ++bits;
57 v /= 2;
59 return bits;
63 An eye is a point that may eventually become untakeable (without playing
64 at the empty intersection itself). Examples:
66 .bw .b. --- +--
67 b*b b*b b*b |*b
68 .bb .bb .b. |b.
70 static u8 _out_neighbors4(
71 u8 p[static 3][3]
72 ) {
73 u8 ret = 0;
75 if (p[1][0] == ILLEGAL) {
76 ++ret;
78 if (p[2][1] == ILLEGAL) {
79 ++ret;
81 if (p[1][2] == ILLEGAL) {
82 ++ret;
84 if (p[0][1] == ILLEGAL) {
85 ++ret;
88 return ret;
91 static u8 _black_neighbors4(
92 u8 p[static 3][3]
93 ) {
94 u8 ret = 0;
96 if (p[1][0] == BLACK_STONE) {
97 ++ret;
99 if (p[2][1] == BLACK_STONE) {
100 ++ret;
102 if (p[1][2] == BLACK_STONE) {
103 ++ret;
105 if (p[0][1] == BLACK_STONE) {
106 ++ret;
109 return ret;
112 static u8 _white_neighbors4(
113 u8 p[static 3][3]
115 u8 ret = 0;
117 if (p[1][0] == WHITE_STONE) {
118 ++ret;
120 if (p[2][1] == WHITE_STONE) {
121 ++ret;
123 if (p[1][2] == WHITE_STONE) {
124 ++ret;
126 if (p[0][1] == WHITE_STONE) {
127 ++ret;
130 return ret;
133 static u8 _black_neighbors8(
134 u8 p[static 3][3]
136 u8 ret = _black_neighbors4(p);
138 if (p[0][0] == BLACK_STONE) {
139 ++ret;
141 if (p[2][0] == BLACK_STONE) {
142 ++ret;
144 if (p[0][2] == BLACK_STONE) {
145 ++ret;
147 if (p[2][2] == BLACK_STONE) {
148 ++ret;
151 return ret;
154 static u8 _white_neighbors8(
155 u8 p[static 3][3]
157 u8 ret = _white_neighbors4(p);
159 if (p[0][0] == WHITE_STONE) {
160 ++ret;
162 if (p[2][0] == WHITE_STONE) {
163 ++ret;
165 if (p[0][2] == WHITE_STONE) {
166 ++ret;
168 if (p[2][2] == WHITE_STONE) {
169 ++ret;
172 return ret;
175 static void init_eye_table() {
176 u8 dst[3][3];
178 for (u32 i = 0; i < 65536; ++i) {
179 string_to_pat3(dst, i);
181 if (_out_neighbors4(dst) == 0) {
182 black_eye[i] = (_black_neighbors4(dst) == 4) && (_white_neighbors8(dst) < 2);
183 white_eye[i] = (_white_neighbors4(dst) == 4) && (_black_neighbors8(dst) < 2);
184 } else {
185 black_eye[i] = (_black_neighbors4(dst) + _out_neighbors4(dst) == 4) && (_white_neighbors8(dst) == 0);
186 white_eye[i] = (_white_neighbors4(dst) + _out_neighbors4(dst) == 4) && (_black_neighbors8(dst) == 0);
192 Initialize a series of constants based on the board size in use.
194 void board_constants_init() {
195 if (board_constants_inited) {
196 return;
199 board_constants_inited = true;
201 /* Adjacent neighbor positions */
202 init_moves_by_distance(neighbors_side, 1, false);
204 memset(border_left, false, TOTAL_BOARD_SIZ);
205 memset(border_right, false, TOTAL_BOARD_SIZ);
206 memset(border_top, false, TOTAL_BOARD_SIZ);
207 memset(border_bottom, false, TOTAL_BOARD_SIZ);
209 /* Diagonal neighbor positions */
210 for (d8 x = 0; x < BOARD_SIZ; ++x) {
211 for (d8 y = 0; y < BOARD_SIZ; ++y) {
212 move a = coord_to_move(x, y);
214 if (x == 0) {
215 border_left[a] = true;
217 if (x == BOARD_SIZ - 1) {
218 border_right[a] = true;
220 if (y == 0) {
221 border_top[a] = true;
223 if (y == BOARD_SIZ - 1) {
224 border_bottom[a] = true;
227 move c = 0;
228 for (d8 i = 0; i < BOARD_SIZ; ++i) {
229 for (d8 j = 0; j < BOARD_SIZ; ++j) {
230 if (abs(x - i) == 1 && abs(y - j) == 1) {
231 move b = coord_to_move(i, j);
232 neighbors_diag[a].coord[c] = b;
233 ++c;
238 neighbors_diag[a].count = c;
242 /* 3x3 positions excluding self */
243 memcpy(neighbors_3x3, neighbors_side, TOTAL_BOARD_SIZ * sizeof(move_seq));
244 for (move m = 0; m < TOTAL_BOARD_SIZ; ++m) {
245 copy_moves(&neighbors_3x3[m], &neighbors_diag[m]);
248 memset(out_neighbors4, 0, TOTAL_BOARD_SIZ);
249 memset(out_neighbors8, 0, TOTAL_BOARD_SIZ);
251 for (u8 i = 0; i < BOARD_SIZ; ++i) {
252 out_neighbors4[coord_to_move(i, 0)] = out_neighbors4[coord_to_move(0, i)] = out_neighbors4[coord_to_move(BOARD_SIZ - 1, i)] = out_neighbors4[coord_to_move(i, BOARD_SIZ - 1)] = 1;
253 out_neighbors8[coord_to_move(i, 0)] = out_neighbors8[coord_to_move(0, i)] = out_neighbors8[coord_to_move(BOARD_SIZ - 1, i)] = out_neighbors8[coord_to_move(i, BOARD_SIZ - 1)] = 3;
256 out_neighbors4[coord_to_move(0, 0)] = out_neighbors4[coord_to_move(BOARD_SIZ - 1, 0)] = out_neighbors4[coord_to_move(0, BOARD_SIZ - 1)] = out_neighbors4[coord_to_move(BOARD_SIZ - 1, BOARD_SIZ - 1)] = 2;
257 out_neighbors8[coord_to_move(0, 0)] = out_neighbors8[coord_to_move(BOARD_SIZ - 1, 0)] = out_neighbors8[coord_to_move(0, BOARD_SIZ - 1)] = out_neighbors8[coord_to_move(BOARD_SIZ - 1, BOARD_SIZ - 1)] = 5;
259 for (u16 i = 0; i < 256; ++i) {
260 active_bits_in_byte[i] = count_bits(i);
263 for (u8 i = 0; i < BOARD_SIZ; ++i) {
264 for (u8 j = 0; j < BOARD_SIZ; ++j) {
265 distances_to_border[coord_to_move(i, j)] = DISTANCE_TO_BORDER(i, j);
269 init_moves_by_distance(nei_dst_3, 3, false);
270 init_moves_by_distance(nei_dst_4, 4, false);
272 init_eye_table();