| 1 |
|
| /* bd_rules.c - a game: a board, the moves that made it, and how it ends. |
| 2 |
|
| * |
| 3 |
|
| * PERL-FREE, as the other bd_*.c files are. |
| 4 |
|
| * |
| 5 |
|
| * THE RULES THIS FILE IMPLEMENTS, quoted from the source the distribution |
| 6 |
|
| * names in its documentation: |
| 7 |
|
| * |
| 8 |
|
| * 11. "The king wins the game on reaching any of the marked corner squares. |
| 9 |
|
| * The attackers win if they capture the king." |
| 10 |
|
| * 12. "The game is drawn if a position is repeated, if a player cannot move, |
| 11 |
|
| * or if the players otherwise agree it." |
| 12 |
|
| * |
| 13 |
|
| * and three decisions where that text is silent or would give a result nobody |
| 14 |
|
| * means: |
| 15 |
|
| * |
| 16 |
|
| * - "REPEATED" IS THE THIRD TIME, with the same side to move, as draughts |
| 17 |
|
| * and chess count it. bd_variant.repeat says which occurrence; the literal |
| 18 |
|
| * reading of the sentence is 2. |
| 19 |
|
| * - ATTACKERS WITH NO PIECE LEFT HAVE LOST. Read literally rule 12 draws |
| 20 |
|
| * that game, because a side with nothing "cannot move". The sentence is |
| 21 |
|
| * for a side that is blocked. |
| 22 |
|
| * - A GAME THAT WILL NOT END IS DRAWN AT A PLY CAP. The attackers can seal |
| 23 |
|
| * all four corners, and then nothing above ends the game. |
| 24 |
|
| * |
| 25 |
|
| * Agreement and resignation are not here. They are something two people do, |
| 26 |
|
| * and the caller records them. |
| 27 |
|
| * |
| 28 |
|
| * THE ORDER THE ENDINGS ARE ASKED IN IS PART OF THE RULE, because one move can |
| 29 |
|
| * satisfy two of them: |
| 30 |
|
| * |
| 31 |
|
| * 1. the win the move itself made: the king home, or the king taken |
| 32 |
|
| * 2. the attackers have no piece |
| 33 |
|
| * 3. the side to move has no legal move |
| 34 |
|
| * 4. the position has occurred for the Nth time |
| 35 |
|
| * 5. the ply cap |
| 36 |
|
| * |
| 37 |
|
| * So a king who reaches a corner on the move that also repeats a position has |
| 38 |
|
| * won, and taking the last attacker is a win and not a blocked side. |
| 39 |
|
| */ |
| 40 |
|
| |
| 41 |
|
| #include <stdlib.h> |
| 42 |
|
| #include <string.h> |
| 43 |
|
| #include "bd_abi.h" |
| 44 |
|
| |
| 45 |
100703270 |
| static const struct bd_abi *E(void) { return bd_abi_table(); } |
| 46 |
|
| |
| 47 |
|
| /* ---- the game ------------------------------------------------------------------ |
| 48 |
|
| * |
| 49 |
|
| * Everything about a ply is kept by its index: the key of the position after |
| 50 |
|
| * it, the outcome as it stood then, whether the move that made it captured, |
| 51 |
|
| * and the undo that takes it back. Index 0 is the starting position and has no |
| 52 |
|
| * move behind it. |
| 53 |
|
| * |
| 54 |
|
| * THE ARRAYS HOLD ply_cap + 1 ENTRIES AND CANNOT OVERFLOW: at the cap the |
| 55 |
|
| * outcome is a draw and game_do refuses, so the index never passes it. |
| 56 |
|
| */ |
| 57 |
|
| |
| 58 |
|
| struct bd_game { |
| 59 |
|
| struct bd_board *board; |
| 60 |
|
| struct bd_variant variant; |
| 61 |
|
| int cap; /* the ply cap, as clamped */ |
| 62 |
|
| int ply; /* moves made so far */ |
| 63 |
|
| unsigned long long *key; /* [0 .. cap] */ |
| 64 |
|
| int *outcome; /* [0 .. cap] */ |
| 65 |
|
| unsigned char *captured; /* [0 .. cap], 1 if that move captured */ |
| 66 |
|
| struct bd_undo *undo; /* [1 .. cap], undo[i] takes ply i back */ |
| 67 |
|
| }; |
| 68 |
|
| |
| 69 |
|
| static int GAMES_LIVE = 0; |
| 70 |
|
| |
| 71 |
|
| /* A rule set as a game will use it. A cap of nothing or less means the |
| 72 |
|
| * default, and so does a cap above what the arrays are allowed to be; a |
| 73 |
|
| * repeat below 2 would draw every game at its first position, and means 2. */ |
| 74 |
17096 |
| static void variant_clamped(struct bd_variant *out, const struct bd_variant *v) |
| 75 |
|
| { |
| 76 |
17096 |
▶ | if (v) *out = *v; |
Branch: 1 branch
| Branch | True | False | Total |
| if (v) *out = *v; |
17096 |
0 |
17096 |
|
| 77 |
0 |
| else (E())->variant_default(out); |
| 78 |
17096 |
▶ | if (out->ply_cap <= 0 || out->ply_cap > BD_PLY_CAP_MAX) out->ply_cap = BD_PLY_CAP_DEFAULT; |
Branch: 2 branches
| Branch | True | False | Total |
| if (out->ply_cap <= 0 || out->ply_cap > BD_PLY_CAP_MAX) out->ply_cap = BD_PLY_CAP_DEFAULT; |
17094 |
2 |
17096 |
| if (out->ply_cap <= 0 || out->ply_cap > BD_PLY_CAP_MAX) out->ply_cap = BD_PLY_CAP_DEFAULT; |
1 |
17093 |
17094 |
|
| 79 |
17096 |
▶ | if (out->repeat < 2) out->repeat = 2; |
Branch: 1 branch
| Branch | True | False | Total |
| if (out->repeat < 2) out->repeat = 2; |
2 |
17094 |
17096 |
|
| 80 |
17096 |
| } |
| 81 |
|
| |
| 82 |
|
| /* ---- repetition ----------------------------------------------------------------- |
| 83 |
|
| * |
| 84 |
|
| * How many times the current position has occurred, this time included. |
| 85 |
|
| * |
| 86 |
|
| * THE WALK STOPS AT THE LAST CAPTURE. Pieces only ever leave the board, so a |
| 87 |
|
| * position from before a capture has more pieces on it than any after and |
| 88 |
|
| * cannot be the one on the board now. Stopping there changes no answer; it |
| 89 |
|
| * only means a long game does not search its whole length on every move. |
| 90 |
|
| * |
| 91 |
|
| * A KEY MATCH IS TRUSTED. Two different positions sharing a 64-bit key would |
| 92 |
|
| * be a draw called in error, and nothing here guards against it. |
| 93 |
|
| */ |
| 94 |
24685425 |
| static int bd_repeats(const struct bd_game *g) |
| 95 |
|
| { |
| 96 |
|
| unsigned long long now; |
| 97 |
24685425 |
| int i, n = 0; |
| 98 |
24685425 |
▶ | if (!g) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!g) return 0; |
0 |
24685425 |
24685425 |
|
| 99 |
24685425 |
| now = g->key[g->ply]; |
| 100 |
221010458 |
▶ | for (i = g->ply; i >= 0; i--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = g->ply; i >= 0; i--) { |
212906129 |
8104329 |
221010458 |
|
| 101 |
212906129 |
▶ | if (g->key[i] == now) n++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (g->key[i] == now) n++; |
24722300 |
188183829 |
212906129 |
|
| 102 |
212906129 |
▶ | if (g->captured[i]) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (g->captured[i]) break; |
16581096 |
196325033 |
212906129 |
|
| 103 |
|
| } |
| 104 |
24685425 |
| return n; |
| 105 |
|
| } |
| 106 |
|
| |
| 107 |
|
| /* ---- the outcome ------------------------------------------------------------------ */ |
| 108 |
|
| |
| 109 |
8909 |
| static int king_home_somewhere(const struct bd_game *g) |
| 110 |
|
| { |
| 111 |
|
| int f, r; |
| 112 |
70069 |
▶ | for (r = 0; r < BD_SIZE; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < BD_SIZE; r++) { |
61460 |
8609 |
70069 |
|
| 113 |
490444 |
▶ | for (f = 0; f < BD_SIZE; f++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (f = 0; f < BD_SIZE; f++) { |
429284 |
61160 |
490444 |
|
| 114 |
429284 |
| int sq = (E())->square_of(f, r); |
| 115 |
429284 |
▶ | int edge = f == 0 || r == 0 || f == BD_SIZE - 1 || r == BD_SIZE - 1; |
Branch: 4 branches
| Branch | True | False | Total |
| int edge = f == 0 || r == 0 || f == BD_SIZE - 1 || r == BD_SIZE - 1; |
367824 |
61460 |
429284 |
| int edge = f == 0 || r == 0 || f == BD_SIZE - 1 || r == BD_SIZE - 1; |
314814 |
53010 |
367824 |
| int edge = f == 0 || r == 0 || f == BD_SIZE - 1 || r == BD_SIZE - 1; |
262345 |
52469 |
314814 |
| int edge = f == 0 || r == 0 || f == BD_SIZE - 1 || r == BD_SIZE - 1; |
43385 |
218960 |
262345 |
|
| 116 |
429284 |
▶ | if ((E())->at(g->board, sq) != BD_KING) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((E())->at(g->board, sq) != BD_KING) continue; |
420357 |
8927 |
429284 |
|
| 117 |
8927 |
▶ | if ((E())->is_corner(sq) || (g->variant.escape_edge && edge)) return 1; |
Branch: 3 branches
| Branch | True | False | Total |
| if ((E())->is_corner(sq) || (g->variant.escape_edge && edge)) return 1; |
8628 |
299 |
8927 |
| if ((E())->is_corner(sq) || (g->variant.escape_edge && edge)) return 1; |
312 |
8316 |
8628 |
| if ((E())->is_corner(sq) || (g->variant.escape_edge && edge)) return 1; |
1 |
311 |
312 |
|
| 118 |
|
| } |
| 119 |
|
| } |
| 120 |
8609 |
| return 0; |
| 121 |
|
| } |
| 122 |
|
| |
| 123 |
|
| /* endings 2 to 5: the ones that are read off the position and its history and |
| 124 |
|
| * not off the move that was just made */ |
| 125 |
254939 |
| static int judge_rest(const struct bd_game *g) |
| 126 |
|
| { |
| 127 |
254939 |
▶ | if ((E())->count(g->board, BD_ATTACKER) == 0) return BD_BY_NO_PIECES; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((E())->count(g->board, BD_ATTACKER) == 0) return BD_BY_NO_PIECES; |
54 |
254885 |
254939 |
|
| 128 |
254885 |
▶ | if ((E())->gen_moves(g->board, &g->variant, NULL, 0) == 0) return BD_DRAW_NO_MOVE; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((E())->gen_moves(g->board, &g->variant, NULL, 0) == 0) return BD_DRAW_NO_MOVE; |
27 |
254858 |
254885 |
|
| 129 |
254858 |
▶ | if (bd_repeats(g) >= g->variant.repeat) return BD_DRAW_REPETITION; |
Branch: 1 branch
| Branch | True | False | Total |
| if (bd_repeats(g) >= g->variant.repeat) return BD_DRAW_REPETITION; |
57 |
254801 |
254858 |
|
| 130 |
254801 |
▶ | if (g->ply >= g->cap) return BD_DRAW_PLY_CAP; |
Branch: 1 branch
| Branch | True | False | Total |
| if (g->ply >= g->cap) return BD_DRAW_PLY_CAP; |
422 |
254379 |
254801 |
|
| 131 |
254379 |
| return BD_ONGOING; |
| 132 |
|
| } |
| 133 |
|
| |
| 134 |
|
| /* A position nobody moved into, so there is no move to read a win from. No |
| 135 |
|
| * king on the board: he has been captured. A king standing where he wins: he |
| 136 |
|
| * has won. Otherwise it is judged like any other. */ |
| 137 |
8917 |
| static int judge_start(const struct bd_game *g) |
| 138 |
|
| { |
| 139 |
8917 |
▶ | if ((E())->count(g->board, BD_KING) == 0) return BD_BY_CAPTURE; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((E())->count(g->board, BD_KING) == 0) return BD_BY_CAPTURE; |
8 |
8909 |
8917 |
|
| 140 |
8909 |
▶ | if (king_home_somewhere(g)) return BD_BY_CORNER; |
Branch: 1 branch
| Branch | True | False | Total |
| if (king_home_somewhere(g)) return BD_BY_CORNER; |
300 |
8609 |
8909 |
|
| 141 |
8609 |
| return judge_rest(g); |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
249021 |
| static int judge_move(const struct bd_game *g, int flags) |
| 145 |
|
| { |
| 146 |
249021 |
▶ | if (flags & BD_KING_HOME) return BD_BY_CORNER; |
Branch: 1 branch
| Branch | True | False | Total |
| if (flags & BD_KING_HOME) return BD_BY_CORNER; |
1954 |
247067 |
249021 |
|
| 147 |
247067 |
▶ | if (flags & BD_KING_TAKEN) return BD_BY_CAPTURE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (flags & BD_KING_TAKEN) return BD_BY_CAPTURE; |
737 |
246330 |
247067 |
|
| 148 |
246330 |
| return judge_rest(g); |
| 149 |
|
| } |
| 150 |
|
| |
| 151 |
|
| /* ---- lifecycle -------------------------------------------------------------------- */ |
| 152 |
|
| |
| 153 |
17096 |
| static void bd_game_drop(struct bd_game *g) |
| 154 |
|
| { |
| 155 |
17096 |
▶ | if (!g) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!g) return; |
0 |
17096 |
17096 |
|
| 156 |
17096 |
▶ | if (g->board) (E())->board_drop(g->board); |
Branch: 1 branch
| Branch | True | False | Total |
| if (g->board) (E())->board_drop(g->board); |
17096 |
0 |
17096 |
|
| 157 |
17096 |
| free(g->key); |
| 158 |
17096 |
| free(g->outcome); |
| 159 |
17096 |
| free(g->captured); |
| 160 |
17096 |
| free(g->undo); |
| 161 |
17096 |
| free(g); |
| 162 |
17096 |
| GAMES_LIVE--; |
| 163 |
|
| } |
| 164 |
|
| |
| 165 |
17096 |
| static struct bd_game *game_alloc(const struct bd_variant *v) |
| 166 |
|
| { |
| 167 |
17096 |
| struct bd_game *g = (struct bd_game *) calloc(1, sizeof(struct bd_game)); |
| 168 |
|
| size_t slots; |
| 169 |
17096 |
▶ | if (!g) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!g) return NULL; |
0 |
17096 |
17096 |
|
| 170 |
17096 |
| GAMES_LIVE++; |
| 171 |
17096 |
| variant_clamped(&g->variant, v); |
| 172 |
17096 |
| g->cap = g->variant.ply_cap; |
| 173 |
17096 |
| slots = (size_t) g->cap + 1; |
| 174 |
17096 |
| g->key = (unsigned long long *) calloc(slots, sizeof(unsigned long long)); |
| 175 |
17096 |
| g->outcome = (int *) calloc(slots, sizeof(int)); |
| 176 |
17096 |
| g->captured = (unsigned char *) calloc(slots, 1); |
| 177 |
17096 |
| g->undo = (struct bd_undo *) calloc(slots, sizeof(struct bd_undo)); |
| 178 |
17096 |
▶ | if (!g->key || !g->outcome || !g->captured || !g->undo) { |
Branch: 4 branches
| Branch | True | False | Total |
| if (!g->key || !g->outcome || !g->captured || !g->undo) { |
17096 |
0 |
17096 |
| if (!g->key || !g->outcome || !g->captured || !g->undo) { |
17096 |
0 |
17096 |
| if (!g->key || !g->outcome || !g->captured || !g->undo) { |
17096 |
0 |
17096 |
| if (!g->key || !g->outcome || !g->captured || !g->undo) { |
0 |
17096 |
17096 |
|
| 179 |
0 |
| bd_game_drop(g); |
| 180 |
0 |
| return NULL; |
| 181 |
|
| } |
| 182 |
17096 |
| return g; |
| 183 |
|
| } |
| 184 |
|
| |
| 185 |
|
| /* A game from a copy of `b`, or from the set-up when `b` is NULL. The board |
| 186 |
|
| * handed in is left alone and may be dropped at once. */ |
| 187 |
8917 |
| static struct bd_game *bd_game_new(const struct bd_board *b, const struct bd_variant *v) |
| 188 |
|
| { |
| 189 |
8917 |
| struct bd_game *g = game_alloc(v); |
| 190 |
8917 |
▶ | if (!g) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!g) return NULL; |
0 |
8917 |
8917 |
|
| 191 |
8917 |
▶ | g->board = b ? (E())->board_copy(b) : (E())->board_new(); |
Branch: 1 branch
| Branch | True | False | Total |
| g->board = b ? (E())->board_copy(b) : (E())->board_new(); |
7875 |
1042 |
8917 |
|
| 192 |
8917 |
▶ | if (!g->board) { bd_game_drop(g); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!g->board) { bd_game_drop(g); return NULL; } |
0 |
8917 |
8917 |
|
| 193 |
8917 |
| g->ply = 0; |
| 194 |
8917 |
| g->key[0] = (E())->key(g->board); |
| 195 |
8917 |
| g->captured[0] = 0; |
| 196 |
8917 |
| g->outcome[0] = judge_start(g); |
| 197 |
8917 |
| return g; |
| 198 |
|
| } |
| 199 |
|
| |
| 200 |
8179 |
| static struct bd_game *bd_game_copy(const struct bd_game *src) |
| 201 |
|
| { |
| 202 |
|
| struct bd_game *g; |
| 203 |
|
| size_t slots; |
| 204 |
8179 |
▶ | if (!src) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!src) return NULL; |
0 |
8179 |
8179 |
|
| 205 |
8179 |
| g = game_alloc(&src->variant); |
| 206 |
8179 |
▶ | if (!g) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!g) return NULL; |
0 |
8179 |
8179 |
|
| 207 |
8179 |
| g->board = (E())->board_copy(src->board); |
| 208 |
8179 |
▶ | if (!g->board) { bd_game_drop(g); return NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!g->board) { bd_game_drop(g); return NULL; } |
0 |
8179 |
8179 |
|
| 209 |
8179 |
| slots = (size_t) src->cap + 1; |
| 210 |
8179 |
| g->ply = src->ply; |
| 211 |
8179 |
| memcpy(g->key, src->key, slots * sizeof(unsigned long long)); |
| 212 |
8179 |
| memcpy(g->outcome, src->outcome, slots * sizeof(int)); |
| 213 |
8179 |
| memcpy(g->captured, src->captured, slots); |
| 214 |
8179 |
| memcpy(g->undo, src->undo, slots * sizeof(struct bd_undo)); |
| 215 |
8179 |
| return g; |
| 216 |
|
| } |
| 217 |
|
| |
| 218 |
7 |
| static int bd_games_live(void) { return GAMES_LIVE; } |
| 219 |
|
| |
| 220 |
|
| /* ---- reading a game --------------------------------------------------------------- */ |
| 221 |
|
| |
| 222 |
28864273 |
▶ | static const struct bd_board *bd_game_board(const struct bd_game *g) { return g ? g->board : NULL; } |
Branch: 1 branch
| Branch | True | False | Total |
| static const struct bd_board *bd_game_board(const struct bd_game *g) { return g ? g->board : NULL; } |
28864273 |
0 |
28864273 |
|
| 223 |
28125 |
▶ | static int bd_game_ply(const struct bd_game *g) { return g ? g->ply : 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| static int bd_game_ply(const struct bd_game *g) { return g ? g->ply : 0; } |
28125 |
0 |
28125 |
|
| 224 |
6 |
▶ | static int bd_game_cap(const struct bd_game *g) { return g ? g->cap : 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| static int bd_game_cap(const struct bd_game *g) { return g ? g->cap : 0; } |
6 |
0 |
6 |
|
| 225 |
25045917 |
▶ | static int bd_game_outcome(const struct bd_game *g) { return g ? g->outcome[g->ply] : BD_ONGOING; } |
Branch: 1 branch
| Branch | True | False | Total |
| static int bd_game_outcome(const struct bd_game *g) { return g ? g->outcome[g->ply] : BD_ONGOING; } |
25045917 |
0 |
25045917 |
|
| 226 |
|
| |
| 227 |
1808904 |
| static void bd_game_variant(const struct bd_game *g, struct bd_variant *out) |
| 228 |
|
| { |
| 229 |
1808904 |
▶ | if (g && out) *out = g->variant; |
Branch: 2 branches
| Branch | True | False | Total |
| if (g && out) *out = g->variant; |
1808904 |
0 |
1808904 |
| if (g && out) *out = g->variant; |
1808904 |
0 |
1808904 |
|
| 230 |
1808904 |
| } |
| 231 |
|
| |
| 232 |
260143 |
| static int bd_winner_of(int outcome) |
| 233 |
|
| { |
| 234 |
260143 |
▶ | if (outcome == BD_BY_CAPTURE) return BD_ATTACKERS; |
Branch: 1 branch
| Branch | True | False | Total |
| if (outcome == BD_BY_CAPTURE) return BD_ATTACKERS; |
112373 |
147770 |
260143 |
|
| 235 |
147770 |
▶ | if (outcome == BD_BY_CORNER || outcome == BD_BY_NO_PIECES) return BD_DEFENDERS; |
Branch: 2 branches
| Branch | True | False | Total |
| if (outcome == BD_BY_CORNER || outcome == BD_BY_NO_PIECES) return BD_DEFENDERS; |
24425 |
123345 |
147770 |
| if (outcome == BD_BY_CORNER || outcome == BD_BY_NO_PIECES) return BD_DEFENDERS; |
55 |
24370 |
24425 |
|
| 236 |
24370 |
| return -1; |
| 237 |
|
| } |
| 238 |
|
| |
| 239 |
4188 |
| static int bd_game_winner(const struct bd_game *g) { return bd_winner_of(bd_game_outcome(g)); } |
| 240 |
|
| |
| 241 |
|
| /* The key of the position after `ply` moves, or 0 for a ply the game has not |
| 242 |
|
| * reached. */ |
| 243 |
9 |
| static unsigned long long bd_game_key_at(const struct bd_game *g, int ply) |
| 244 |
|
| { |
| 245 |
9 |
▶ | if (!g || ply < 0 || ply > g->ply) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!g || ply < 0 || ply > g->ply) return 0; |
9 |
0 |
9 |
| if (!g || ply < 0 || ply > g->ply) return 0; |
9 |
0 |
9 |
| if (!g || ply < 0 || ply > g->ply) return 0; |
0 |
9 |
9 |
|
| 246 |
9 |
| return g->key[ply]; |
| 247 |
|
| } |
| 248 |
|
| |
| 249 |
|
| /* The moves of the side to move, and NONE once the game is over: a finished |
| 250 |
|
| * game has no legal move, whatever the pieces could do. */ |
| 251 |
109141 |
| static int bd_game_moves(const struct bd_game *g, int *out, int max) |
| 252 |
|
| { |
| 253 |
109141 |
▶ | if (!g || g->outcome[g->ply] != BD_ONGOING) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!g || g->outcome[g->ply] != BD_ONGOING) return 0; |
109141 |
0 |
109141 |
| if (!g || g->outcome[g->ply] != BD_ONGOING) return 0; |
252 |
108889 |
109141 |
|
| 254 |
108889 |
| return (E())->gen_moves(g->board, &g->variant, out, max); |
| 255 |
|
| } |
| 256 |
|
| |
| 257 |
|
| /* ---- playing ---------------------------------------------------------------------- |
| 258 |
|
| * |
| 259 |
|
| * game_do IS A RULES CALL, the first in this distribution that refuses. A |
| 260 |
|
| * move in a finished game is BD_PLAY_OVER; a move the side to move may not |
| 261 |
|
| * make is BD_PLAY_ILLEGAL; and in both cases the game is exactly as it was. |
| 262 |
|
| */ |
| 263 |
249052 |
| static int bd_game_do(struct bd_game *g, int mv, int *flags_out) |
| 264 |
|
| { |
| 265 |
|
| int flags, at; |
| 266 |
249052 |
▶ | if (flags_out) *flags_out = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (flags_out) *flags_out = 0; |
249052 |
0 |
249052 |
|
| 267 |
249052 |
▶ | if (!g) return BD_PLAY_ILLEGAL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!g) return BD_PLAY_ILLEGAL; |
0 |
249052 |
249052 |
|
| 268 |
249052 |
▶ | if (g->outcome[g->ply] != BD_ONGOING) return BD_PLAY_OVER; |
Branch: 1 branch
| Branch | True | False | Total |
| if (g->outcome[g->ply] != BD_ONGOING) return BD_PLAY_OVER; |
25 |
249027 |
249052 |
|
| 269 |
249027 |
▶ | if (!(E())->is_legal(g->board, &g->variant, mv)) return BD_PLAY_ILLEGAL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(E())->is_legal(g->board, &g->variant, mv)) return BD_PLAY_ILLEGAL; |
6 |
249021 |
249027 |
|
| 270 |
|
| |
| 271 |
249021 |
| at = g->ply + 1; |
| 272 |
249021 |
| flags = (E())->do_move(g->board, &g->variant, mv, &g->undo[at]); |
| 273 |
249021 |
| g->ply = at; |
| 274 |
249021 |
| g->key[at] = (E())->key(g->board); |
| 275 |
249021 |
| g->captured[at] = (flags & BD_DID_CAPTURE) ? 1 : 0; |
| 276 |
249021 |
| g->outcome[at] = judge_move(g, flags); |
| 277 |
|
| |
| 278 |
249021 |
▶ | if (flags_out) *flags_out = flags; |
Branch: 1 branch
| Branch | True | False | Total |
| if (flags_out) *flags_out = flags; |
249021 |
0 |
249021 |
|
| 279 |
249021 |
| return BD_PLAY_OK; |
| 280 |
|
| } |
| 281 |
|
| |
| 282 |
|
| /* game_do with two questions left out, for a search: see the header. The |
| 283 |
|
| * outcome is judged in the same order with "no move" skipped, and a position |
| 284 |
|
| * that is both blocked and something else is a draw either way. */ |
| 285 |
24656694 |
| static void bd_game_push(struct bd_game *g, int mv, int *flags_out) |
| 286 |
|
| { |
| 287 |
|
| int flags, at, outcome; |
| 288 |
24656694 |
▶ | if (flags_out) *flags_out = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (flags_out) *flags_out = 0; |
0 |
24656694 |
24656694 |
|
| 289 |
24656694 |
▶ | if (!g || g->ply >= g->cap) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!g || g->ply >= g->cap) return; |
24656694 |
0 |
24656694 |
| if (!g || g->ply >= g->cap) return; |
0 |
24656694 |
24656694 |
|
| 290 |
|
| |
| 291 |
24656694 |
| at = g->ply + 1; |
| 292 |
24656694 |
| flags = (E())->do_move(g->board, &g->variant, mv, &g->undo[at]); |
| 293 |
24656694 |
| g->ply = at; |
| 294 |
24656694 |
| g->key[at] = (E())->key(g->board); |
| 295 |
24656694 |
| g->captured[at] = (flags & BD_DID_CAPTURE) ? 1 : 0; |
| 296 |
|
| |
| 297 |
24656694 |
▶ | if (flags & BD_KING_HOME) outcome = BD_BY_CORNER; |
Branch: 1 branch
| Branch | True | False | Total |
| if (flags & BD_KING_HOME) outcome = BD_BY_CORNER; |
120778 |
24535916 |
24656694 |
|
| 298 |
24535916 |
▶ | else if (flags & BD_KING_TAKEN) outcome = BD_BY_CAPTURE; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (flags & BD_KING_TAKEN) outcome = BD_BY_CAPTURE; |
111423 |
24424493 |
24535916 |
|
| 299 |
24424493 |
▶ | else if ((E())->count(g->board, BD_ATTACKER) == 0) outcome = BD_BY_NO_PIECES; |
Branch: 1 branch
| Branch | True | False | Total |
| else if ((E())->count(g->board, BD_ATTACKER) == 0) outcome = BD_BY_NO_PIECES; |
0 |
24424493 |
24424493 |
|
| 300 |
24424493 |
▶ | else if (bd_repeats(g) >= g->variant.repeat) outcome = BD_DRAW_REPETITION; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (bd_repeats(g) >= g->variant.repeat) outcome = BD_DRAW_REPETITION; |
103 |
24424390 |
24424493 |
|
| 301 |
24424390 |
▶ | else if (g->ply >= g->cap) outcome = BD_DRAW_PLY_CAP; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (g->ply >= g->cap) outcome = BD_DRAW_PLY_CAP; |
23663 |
24400727 |
24424390 |
|
| 302 |
24400727 |
| else outcome = BD_ONGOING; |
| 303 |
24656694 |
| g->outcome[at] = outcome; |
| 304 |
|
| |
| 305 |
24656694 |
▶ | if (flags_out) *flags_out = flags; |
Branch: 1 branch
| Branch | True | False | Total |
| if (flags_out) *flags_out = flags; |
0 |
24656694 |
24656694 |
|
| 306 |
|
| } |
| 307 |
|
| |
| 308 |
|
| /* Takes the last move back. Everything kept for that ply is simply left |
| 309 |
|
| * behind: the index steps down and the entries above it are dead until a new |
| 310 |
|
| * move writes over them. Returns 0 when there is no move to take back. */ |
| 311 |
24680335 |
| static int bd_game_undo(struct bd_game *g) |
| 312 |
|
| { |
| 313 |
24680335 |
▶ | if (!g || g->ply == 0) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!g || g->ply == 0) return 0; |
24680335 |
0 |
24680335 |
| if (!g || g->ply == 0) return 0; |
249 |
24680086 |
24680335 |
|
| 314 |
24680086 |
| (E())->undo_move(g->board, &g->undo[g->ply]); |
| 315 |
24680086 |
| g->ply--; |
| 316 |
24680086 |
| return 1; |
| 317 |
|
| } |
| 318 |
|
| |
| 319 |
|
| /* ---- installed into the table by bd_engine.c ------------------------------------- */ |
| 320 |
|
| |
| 321 |
|
| void bd_rules_install(struct bd_abi *t); |
| 322 |
|
| |
| 323 |
59 |
| void bd_rules_install(struct bd_abi *t) |
| 324 |
|
| { |
| 325 |
59 |
| t->game_new = bd_game_new; |
| 326 |
59 |
| t->game_copy = bd_game_copy; |
| 327 |
59 |
| t->game_drop = bd_game_drop; |
| 328 |
59 |
| t->games_live = bd_games_live; |
| 329 |
|
| |
| 330 |
59 |
| t->game_board = bd_game_board; |
| 331 |
59 |
| t->game_variant = bd_game_variant; |
| 332 |
59 |
| t->game_ply = bd_game_ply; |
| 333 |
59 |
| t->game_cap = bd_game_cap; |
| 334 |
59 |
| t->game_outcome = bd_game_outcome; |
| 335 |
59 |
| t->game_winner = bd_game_winner; |
| 336 |
59 |
| t->winner_of = bd_winner_of; |
| 337 |
59 |
| t->game_key_at = bd_game_key_at; |
| 338 |
59 |
| t->game_moves = bd_game_moves; |
| 339 |
59 |
| t->repeats = bd_repeats; |
| 340 |
|
| |
| 341 |
59 |
| t->game_do = bd_game_do; |
| 342 |
59 |
| t->game_undo = bd_game_undo; |
| 343 |
59 |
| t->game_push = bd_game_push; |
| 344 |
59 |
| } |