fn_rules.c

filter: Statementstmt 96.2% 177 / 184 Branchbran 76.7% 89 / 116 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.6% 266 / 300 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Fanorona.xs ↑ Summary fn_moves.c »
1 /* fn_rules.c - a game: a board, the moves that made it, and how it ends.
2 *
3 * PERL-FREE, as the other two files are.
4 *
5 * THE RULES THIS FILE IMPLEMENTS, quoted from the sources the distribution
6 * names in its documentation:
7 *
8 * "The game ends when one player captures all stones of the opponent."
9 * "The game is a draw if no player is able to win. In practice this is
10 * achieved by repetition of the same position with the same player to
11 * move."
12 * "There does not exist any documentation stating what the outcome of the
13 * game would be if a player is not able to move. If this occurs during a
14 * game we define that a player who is not able to move forfeits the game."
15 *
16 * and three readings where the sources give no number:
17 *
18 * - REPETITION DRAWS AT THE THIRD OCCURRENCE, counted between moves only. A
19 * position in the middle of a chain is not a position.
20 * - A QUIET LIMIT. A game is drawn after a set number of moves running,
21 * both sides counted, that took nothing.
22 * - A PLY CAP far above anything a game reaches, as a guard.
23 *
24 * A MOVE IS COMPLETE WHEN THE TURN PASSES: after a paika, after a stop, and
25 * after a step that left nothing to follow. Only then is anything judged, the
26 * history written and the counts moved. A step in the middle of a chain
27 * changes the board and nothing else.
28 *
29 * THEY ARE ASKED IN THIS ORDER, because one move can satisfy two: no stones,
30 * no move, repetition, the quiet limit, the cap.
31 *
32 * REPETITION LOOKS BACK ONLY AS FAR AS THE LAST CAPTURE. A capture takes a
33 * stone off the board for good, so no position before it can come again; the
34 * quiet count is exactly how many positions are worth comparing.
35 */
36  
37 #include <stdlib.h>
38 #include <string.h>
39 #include "fn_abi.h"
40  
41 40294115 static const struct fn_abi *E(void) { return fn_abi_table(); }
42  
43 /* What one game_step or game_stop changed, so that game_undo restores it. */
44 struct played {
45 struct fn_undo undo;
46 int quiet;
47 int ply;
48 int outcome;
49 };
50  
51 struct fn_game {
52 struct fn_board *board;
53 struct fn_variant variant;
54 int outcome;
55 int ply; /* moves completed */
56 int quiet; /* of them, how many running took nothing */
57 int took; /* whether the move in progress has taken */
58 unsigned long long *key; /* key[i] is the position after i moves */
59 int key_room;
60 struct played *stack; /* one a step or stop */
61 int stack_n;
62 int stack_room;
63 unsigned char *took_at; /* took, as it was before each stack entry */
64 };
65  
66 static int FN_GAMES_LIVE = 0;
67  
68 464875 static void fn_variant_default(struct fn_variant *v)
69 {
70 464875 ▶ if (!v) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!v) return; 0 464875 464875
71 464875 memset(v, 0, sizeof(*v));
72 464875 v->quiet_limit = FN_QUIET_DEFAULT;
73 464875 v->repeat = FN_REPEAT_DEFAULT;
74 464875 v->ply_cap = FN_PLY_CAP_DEFAULT;
75 }
76  
77 /* A limit below zero means the default. Zero turns the quiet limit and
78 * repetition OFF, which a table of forced results needs; the cap cannot be
79 * turned off, because the history is an array as long as it. */
80 464874 static void variant_clamp(struct fn_variant *v)
81 {
82 464874 ▶ if (v->quiet_limit < 0) v->quiet_limit = FN_QUIET_DEFAULT;
Branch: 1 branch
BranchTrueFalseTotal
if (v->quiet_limit < 0) v->quiet_limit = FN_QUIET_DEFAULT; 190 464684 464874
83 464874 ▶ if (v->repeat < 0) v->repeat = FN_REPEAT_DEFAULT;
Branch: 1 branch
BranchTrueFalseTotal
if (v->repeat < 0) v->repeat = FN_REPEAT_DEFAULT; 190 464684 464874
84 464874 ▶ if (v->repeat == 1) v->repeat = 2;
Branch: 1 branch
BranchTrueFalseTotal
if (v->repeat == 1) v->repeat = 2; 1 464873 464874
85 464874 ▶ if (v->ply_cap <= 0 || v->ply_cap > FN_PLY_CAP_MAX) v->ply_cap = FN_PLY_CAP_DEFAULT;
Branch: 2 branches
BranchTrueFalseTotal
if (v->ply_cap <= 0 || v->ply_cap > FN_PLY_CAP_MAX) v->ply_cap = FN_PLY_CAP_DEFAULT; 464874 0 464874
if (v->ply_cap <= 0 || v->ply_cap > FN_PLY_CAP_MAX) v->ply_cap = FN_PLY_CAP_DEFAULT; 0 464874 464874
86 464874 }
87  
88  
89 948579 static int fn_repeats(const struct fn_game *g)
90 {
91 948579 unsigned long long now = g->key[g->ply];
92 948579 int n = 1, i;
93 1274007 ▶ for (i = g->ply - 2; i >= 0 && i >= g->ply - g->quiet; i -= 2)
Branch: 2 branches
BranchTrueFalseTotal
for (i = g->ply - 2; i >= 0 && i >= g->ply - g->quiet; i -= 2) 1226459 47548 1274007
for (i = g->ply - 2; i >= 0 && i >= g->ply - g->quiet; i -= 2) 325428 901031 1226459
94 325428 ▶ if (g->key[i] == now) n++;
Branch: 1 branch
BranchTrueFalseTotal
if (g->key[i] == now) n++; 911 324517 325428
95 948579 return n;
96 }
97  
98 /* Judges the position a completed move has left. */
99 8211521 static int judge(const struct fn_game *g)
100 {
101 8211521 const struct fn_abi *e = E();
102 8211521 const struct fn_board *b = g->board;
103 8211521 int side = e->side(b);
104 8211521 ▶ if (e->count(b, side) == 0 || e->count(b, FN_OTHER(side)) == 0) return FN_BY_STONES;
Branch: 2 branches
BranchTrueFalseTotal
if (e->count(b, side) == 0 || e->count(b, FN_OTHER(side)) == 0) return FN_BY_STONES; 7696182 515339 8211521
if (e->count(b, side) == 0 || e->count(b, FN_OTHER(side)) == 0) return FN_BY_STONES; 1 7696181 7696182
105 7696181 ▶ if (e->play_steps(b, NULL, 0) == 0) return FN_BY_NO_MOVES;
Branch: 1 branch
BranchTrueFalseTotal
if (e->play_steps(b, NULL, 0) == 0) return FN_BY_NO_MOVES; 33 7696148 7696181
106 7696148 ▶ if (g->variant.repeat && fn_repeats(g) >= g->variant.repeat) return FN_DRAW_REPETITION;
Branch: 2 branches
BranchTrueFalseTotal
if (g->variant.repeat && fn_repeats(g) >= g->variant.repeat) return FN_DRAW_REPETITION; 948554 6747594 7696148
if (g->variant.repeat && fn_repeats(g) >= g->variant.repeat) return FN_DRAW_REPETITION; 11 948543 948554
107 7696137 ▶ if (g->variant.quiet_limit && g->quiet >= g->variant.quiet_limit) return FN_DRAW_QUIET;
Branch: 2 branches
BranchTrueFalseTotal
if (g->variant.quiet_limit && g->quiet >= g->variant.quiet_limit) return FN_DRAW_QUIET; 948219 6747918 7696137
if (g->variant.quiet_limit && g->quiet >= g->variant.quiet_limit) return FN_DRAW_QUIET; 20 948199 948219
108 7696117 ▶ if (g->ply >= g->variant.ply_cap) return FN_DRAW_PLY_CAP;
Branch: 1 branch
BranchTrueFalseTotal
if (g->ply >= g->variant.ply_cap) return FN_DRAW_PLY_CAP; 904 7695213 7696117
109 7695213 return FN_ONGOING;
110 }
111  
112 464874 static struct fn_game *fn_game_new(const struct fn_board *b, const struct fn_variant *v)
113 {
114 464874 const struct fn_abi *e = E();
115 464874 struct fn_game *g = (struct fn_game *) calloc(1, sizeof(struct fn_game));
116 464874 ▶ if (!g) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!g) return NULL; 0 464874 464874
117 464874 ▶ if (v) g->variant = *v; else fn_variant_default(&g->variant);
Branch: 1 branch
BranchTrueFalseTotal
if (v) g->variant = *v; else fn_variant_default(&g->variant); 464874 0 464874
118 464874 variant_clamp(&g->variant);
119 464874 ▶ g->board = b ? e->board_copy(b) : e->board_new(FN_COLS_MAX, FN_ROWS_MAX);
Branch: 1 branch
BranchTrueFalseTotal
g->board = b ? e->board_copy(b) : e->board_new(FN_COLS_MAX, FN_ROWS_MAX); 464874 0 464874
120 464874 g->key_room = g->variant.ply_cap + 2;
121 464874 g->key = (unsigned long long *) calloc((size_t) g->key_room, sizeof(unsigned long long));
122 464874 ▶ if (!g->board || !g->key) {
Branch: 2 branches
BranchTrueFalseTotal
if (!g->board || !g->key) { 464874 0 464874
if (!g->board || !g->key) { 0 464874 464874
123 0 ▶ if (g->board) e->board_drop(g->board);
Branch: 1 branch
BranchTrueFalseTotal
if (g->board) e->board_drop(g->board); 0 0 0
124 0 free(g->key);
125 0 free(g);
126 0 return NULL;
127 }
128 /* a game begins between moves: a chain the board came with is dropped */
129 464874 e->chain_set(g->board, -1, -1, 0);
130 464874 g->key[0] = e->key(g->board);
131 464874 g->outcome = judge(g);
132 464874 FN_GAMES_LIVE++;
133 464874 return g;
134 }
135  
136 33893 static struct fn_game *fn_game_copy(const struct fn_game *g)
137 {
138 33893 const struct fn_abi *e = E();
139 struct fn_game *n;
140 33893 ▶ if (!g) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!g) return NULL; 0 33893 33893
141 33893 n = (struct fn_game *) malloc(sizeof(struct fn_game));
142 33893 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 33893 33893
143 33893 *n = *g;
144 33893 n->board = e->board_copy(g->board);
145 33893 n->key = (unsigned long long *) malloc((size_t) g->key_room * sizeof(unsigned long long));
146 33893 n->stack = NULL;
147 33893 n->took_at = NULL;
148 33893 ▶ if (g->stack_room) {
Branch: 1 branch
BranchTrueFalseTotal
if (g->stack_room) { 23134 10759 33893
149 23134 n->stack = (struct played *) malloc((size_t) g->stack_room * sizeof(struct played));
150 23134 n->took_at = (unsigned char *) malloc((size_t) g->stack_room);
151 }
152 33893 ▶ if (!n->board || !n->key || (g->stack_room && (!n->stack || !n->took_at))) {
Branch: 5 branches
BranchTrueFalseTotal
if (!n->board || !n->key || (g->stack_room && (!n->stack || !n->took_at))) { 33893 0 33893
if (!n->board || !n->key || (g->stack_room && (!n->stack || !n->took_at))) { 33893 0 33893
if (!n->board || !n->key || (g->stack_room && (!n->stack || !n->took_at))) { 23134 10759 33893
if (!n->board || !n->key || (g->stack_room && (!n->stack || !n->took_at))) { 23134 0 23134
if (!n->board || !n->key || (g->stack_room && (!n->stack || !n->took_at))) { 0 23134 23134
153 0 ▶ if (n->board) e->board_drop(n->board);
Branch: 1 branch
BranchTrueFalseTotal
if (n->board) e->board_drop(n->board); 0 0 0
154 0 free(n->key); free(n->stack); free(n->took_at); free(n);
155 0 return NULL;
156 }
157 33893 memcpy(n->key, g->key, (size_t) g->key_room * sizeof(unsigned long long));
158 33893 ▶ if (g->stack_n) {
Branch: 1 branch
BranchTrueFalseTotal
if (g->stack_n) { 23134 10759 33893
159 23134 memcpy(n->stack, g->stack, (size_t) g->stack_n * sizeof(struct played));
160 23134 memcpy(n->took_at, g->took_at, (size_t) g->stack_n);
161 }
162 33893 FN_GAMES_LIVE++;
163 33893 return n;
164 }
165  
166 498767 static void fn_game_drop(struct fn_game *g)
167 {
168 498767 ▶ if (!g) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!g) return; 0 498767 498767
169 498767 (E())->board_drop(g->board);
170 498767 free(g->key);
171 498767 free(g->stack);
172 498767 free(g->took_at);
173 498767 free(g);
174 498767 FN_GAMES_LIVE--;
175 }
176  
177 9 static int fn_games_live(void) { return FN_GAMES_LIVE; }
178  
179 24108607 static const struct fn_board *fn_game_board(const struct fn_game *g) { return g->board; }
180 510 ▶static void fn_game_variant(const struct fn_game *g, struct fn_variant *out) { if (out) *out = g->variant; }
Branch: 1 branch
BranchTrueFalseTotal
static void fn_game_variant(const struct fn_game *g, struct fn_variant *out) { if (out) *out = g->variant; } 510 0 510
181 4490590 static int fn_game_ply(const struct fn_game *g) { return g->ply; }
182 9887 static int fn_game_quiet(const struct fn_game *g) { return g->quiet; }
183 10544769 static int fn_game_outcome(const struct fn_game *g) { return g->outcome; }
184 4471012 static int fn_game_depth(const struct fn_game *g) { return g->stack_n; }
185  
186 /* The side that won, or 0. A game won on stones is won by the side that has
187 * some: after a move that is always the mover, but a game may be BEGUN from a
188 * position in which one side has none, and then it is whoever has. A game won
189 * because the side to move is stuck is won by the other side. */
190 433063 static int fn_game_winner(const struct fn_game *g)
191 {
192 433063 const struct fn_abi *e = E();
193 433063 int side = e->side(g->board);
194 433063 ▶ if (g->outcome == FN_BY_STONES)
Branch: 1 branch
BranchTrueFalseTotal
if (g->outcome == FN_BY_STONES) 432108 955 433063
195 432108 ▶ return e->count(g->board, side) == 0 ? FN_OTHER(side) : side;
Branch: 1 branch
BranchTrueFalseTotal
return e->count(g->board, side) == 0 ? FN_OTHER(side) : side; 432107 1 432108
196 955 ▶ if (g->outcome == FN_BY_NO_MOVES) return FN_OTHER(side);
Branch: 1 branch
BranchTrueFalseTotal
if (g->outcome == FN_BY_NO_MOVES) return FN_OTHER(side); 8 947 955
197 947 return 0;
198 }
199  
200 9 static unsigned long long fn_game_key_at(const struct fn_game *g, int ply)
201 {
202 9 ▶ if (ply < 0 || ply > g->ply) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (ply < 0 || ply > g->ply) return 0; 9 0 9
if (ply < 0 || ply > g->ply) return 0; 0 9 9
203 9 return g->key[ply];
204 }
205  
206 6154641 static int fn_game_steps(const struct fn_game *g, struct fn_step *out, int max)
207 {
208 6154641 ▶ if (g->outcome != FN_ONGOING) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (g->outcome != FN_ONGOING) return 0; 3 6154638 6154641
209 6154638 return (E())->play_steps(g->board, out, max);
210 }
211  
212 11783 static int fn_game_can_stop(const struct fn_game *g)
213 {
214 11783 ▶ if (g->outcome != FN_ONGOING) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (g->outcome != FN_ONGOING) return 0; 2 11781 11783
215 11781 return (E())->can_stop(g->board);
216 }
217  
218 /* Room for one more entry on the stack, or 0. */
219 8410172 static int stack_grow(struct fn_game *g)
220 {
221 struct played *s;
222 unsigned char *t;
223 int room;
224 8410172 ▶ if (g->stack_n < g->stack_room) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (g->stack_n < g->stack_room) return 1; 7943726 466446 8410172
225 466446 ▶ room = g->stack_room ? g->stack_room * 2 : 64;
Branch: 1 branch
BranchTrueFalseTotal
room = g->stack_room ? g->stack_room * 2 : 64; 204 466242 466446
226 466446 s = (struct played *) realloc(g->stack, (size_t) room * sizeof(struct played));
227 466446 ▶ if (!s) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!s) return 0; 0 466446 466446
228 466446 g->stack = s;
229 466446 t = (unsigned char *) realloc(g->took_at, (size_t) room);
230 466446 ▶ if (!t) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!t) return 0; 0 466446 466446
231 466446 g->took_at = t;
232 466446 g->stack_room = room;
233 466446 return 1;
234 }
235  
236 /* The turn has just passed: count the move, write the position, judge it. */
237 7746647 static void move_done(struct fn_game *g)
238 {
239 7746647 g->ply++;
240 7746647 ▶ g->quiet = g->took ? 0 : g->quiet + 1;
Branch: 1 branch
BranchTrueFalseTotal
g->quiet = g->took ? 0 : g->quiet + 1; 6232652 1513995 7746647
241 7746647 g->took = 0;
242 7746647 g->key[g->ply] = (E())->key(g->board);
243 7746647 g->outcome = judge(g);
244 7746647 }
245  
246 8410172 static void remember(struct fn_game *g, struct played *p)
247 {
248 8410172 p->quiet = g->quiet;
249 8410172 p->ply = g->ply;
250 8410172 p->outcome = g->outcome;
251 8410172 g->took_at[g->stack_n] = (unsigned char) g->took;
252 8410172 }
253  
254 /* THE ONE DOOR A STEP COMES THROUGH. Refuses and changes nothing, or plays. */
255 8320476 static int fn_game_step(struct fn_game *g, int from, int dir, int kind, int *why)
256 {
257 8320476 const struct fn_abi *e = E();
258 struct fn_step s;
259 struct played *p;
260 int reason, removed;
261 8320476 ▶ if (why) *why = FN_WHY_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (why) *why = FN_WHY_OK; 6888249 1432227 8320476
262 8320476 ▶ if (g->outcome != FN_ONGOING) return FN_PLAY_OVER;
Branch: 1 branch
BranchTrueFalseTotal
if (g->outcome != FN_ONGOING) return FN_PLAY_OVER; 1 8320475 8320476
263 8320475 reason = e->play_why(g->board, from, dir, kind, &s);
264 8320475 ▶ if (reason != FN_WHY_OK) {
Branch: 1 branch
BranchTrueFalseTotal
if (reason != FN_WHY_OK) { 31 8320444 8320475
265 31 ▶ if (why) *why = reason;
Branch: 1 branch
BranchTrueFalseTotal
if (why) *why = reason; 31 0 31
266 31 return FN_PLAY_ILLEGAL;
267 }
268 8320444 ▶ if (!stack_grow(g)) return FN_PLAY_ILLEGAL;
Branch: 1 branch
BranchTrueFalseTotal
if (!stack_grow(g)) return FN_PLAY_ILLEGAL; 0 8320444 8320444
269 8320444 p = &g->stack[g->stack_n];
270 8320444 remember(g, p);
271 8320444 removed = e->play_step(g->board, &s, &p->undo);
272 8320444 g->stack_n++;
273 8320444 ▶ if (removed > 0) g->took = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (removed > 0) g->took = 1; 2087792 6232652 8320444
274 8320444 ▶ if (e->chain_at(g->board) < 0) move_done(g);
Branch: 1 branch
BranchTrueFalseTotal
if (e->chain_at(g->board) < 0) move_done(g); 7656919 663525 8320444
275 8320444 return FN_PLAY_OK;
276 }
277  
278 89730 static int fn_game_stop(struct fn_game *g)
279 {
280 89730 const struct fn_abi *e = E();
281 struct played *p;
282 89730 ▶ if (g->outcome != FN_ONGOING) return FN_PLAY_OVER;
Branch: 1 branch
BranchTrueFalseTotal
if (g->outcome != FN_ONGOING) return FN_PLAY_OVER; 1 89729 89730
283 89729 ▶ if (!e->can_stop(g->board)) return FN_PLAY_ILLEGAL;
Branch: 1 branch
BranchTrueFalseTotal
if (!e->can_stop(g->board)) return FN_PLAY_ILLEGAL; 1 89728 89729
284 89728 ▶ if (!stack_grow(g)) return FN_PLAY_ILLEGAL;
Branch: 1 branch
BranchTrueFalseTotal
if (!stack_grow(g)) return FN_PLAY_ILLEGAL; 0 89728 89728
285 89728 p = &g->stack[g->stack_n];
286 89728 remember(g, p);
287 89728 e->play_stop(g->board, &p->undo);
288 89728 g->stack_n++;
289 89728 move_done(g);
290 89728 return FN_PLAY_OK;
291 }
292  
293 /* Takes back the last step or stop. 0 when there is none. */
294 8328896 static int fn_game_undo(struct fn_game *g)
295 {
296 struct played *p;
297 8328896 ▶ if (g->stack_n == 0) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (g->stack_n == 0) return 0; 171 8328725 8328896
298 8328725 g->stack_n--;
299 8328725 p = &g->stack[g->stack_n];
300 8328725 (E())->play_undo(g->board, &p->undo);
301 8328725 g->quiet = p->quiet;
302 8328725 g->ply = p->ply;
303 8328725 g->outcome = p->outcome;
304 8328725 g->took = g->took_at[g->stack_n];
305 8328725 return 1;
306 }
307  
308 /* ---- installed into the table by fn_engine.c ---------------------------------- */
309  
310 void fn_rules_install(struct fn_abi *t);
311  
312 46 void fn_rules_install(struct fn_abi *t)
313 {
314 46 t->variant_default = fn_variant_default;
315 46 t->game_new = fn_game_new;
316 46 t->game_copy = fn_game_copy;
317 46 t->game_drop = fn_game_drop;
318 46 t->games_live = fn_games_live;
319 46 t->game_board = fn_game_board;
320 46 t->game_variant = fn_game_variant;
321 46 t->game_ply = fn_game_ply;
322 46 t->game_quiet = fn_game_quiet;
323 46 t->game_depth = fn_game_depth;
324 46 t->game_outcome = fn_game_outcome;
325 46 t->game_winner = fn_game_winner;
326 46 t->game_key_at = fn_game_key_at;
327 46 t->repeats = fn_repeats;
328 46 t->game_steps = fn_game_steps;
329 46 t->game_can_stop = fn_game_can_stop;
330 46 t->game_step = fn_game_step;
331 46 t->game_stop = fn_game_stop;
332 46 t->game_undo = fn_game_undo;
333 46 }
« Fanorona.xs ↑ Summary fn_moves.c »