| 1 |
|
| /* sen_moves.c - what a roll allows, and a move made. |
| 2 |
|
| * |
| 3 |
|
| * PERL-FREE, like sen_engine.c, and it reaches the board only through the |
| 4 |
|
| * table: this file does not know how a position is stored. |
| 5 |
|
| * |
| 6 |
|
| * THIS FILE'S SCOPE. The moves a roll allows the side to move: forward, the |
| 7 |
|
| * backward fallback, the swap, protection, the blockade, the approach to house |
| 8 |
|
| * 26, and the exit chains of the marked houses beyond it. Then a move MADE: |
| 9 |
|
| * apply and its undo, the forfeit, whose turn it is afterwards, and how a game |
| 10 |
|
| * ends. |
| 11 |
|
| * |
| 12 |
|
| * IT READS FIELDS AND NEVER A SET'S NAME. Nothing in this file knows which rule |
| 13 |
|
| * set it is playing, nor which marked house is which beyond the one the track |
| 14 |
|
| * cannot be passed: 26, which every set shares, and 15, where the walk begins. |
| 15 |
|
| * A house that is safe is a bit of the struct; what a piece on a marked house |
| 16 |
|
| * may do is a chain of the struct. |
| 17 |
|
| */ |
| 18 |
|
| |
| 19 |
|
| #include <stddef.h> |
| 20 |
|
| #include "sen_abi.h" |
| 21 |
|
| |
| 22 |
|
| static const struct sen_abi *SEN = NULL; |
| 23 |
|
| |
| 24 |
|
| /* The house every piece must stand on before it goes further. */ |
| 25 |
|
| #define HAPPINESS 26 |
| 26 |
|
| |
| 27 |
|
| /* ---- runs ---------------------------------------------------------------------- |
| 28 |
|
| * |
| 29 |
|
| * A RUN is a maximal stretch of consecutive house NUMBERS held by one side. Houses |
| 30 |
|
| * 10 and 11 are one run; houses 1 and 20, one above the other on the grid, are |
| 31 |
|
| * not. |
| 32 |
|
| */ |
| 33 |
|
| |
| 34 |
7462125 |
| static void sen_run_of(const struct sen_board *b, int house, int *lo, int *hi) |
| 35 |
|
| { |
| 36 |
7462125 |
| int p = (SEN->at)(b, house); |
| 37 |
7462125 |
| int l = house, h = house; |
| 38 |
7893143 |
▶ | while (l > 1 && (SEN->at)(b, l - 1) == p) l--; |
Branch: 2 branches
| Branch | True | False | Total |
| while (l > 1 && (SEN->at)(b, l - 1) == p) l--; |
7887196 |
5947 |
7893143 |
| while (l > 1 && (SEN->at)(b, l - 1) == p) l--; |
431018 |
7456178 |
7887196 |
|
| 39 |
9456904 |
▶ | while (h < SEN_HOUSES && (SEN->at)(b, h + 1) == p) h++; |
Branch: 2 branches
| Branch | True | False | Total |
| while (h < SEN_HOUSES && (SEN->at)(b, h + 1) == p) h++; |
9456904 |
0 |
9456904 |
| while (h < SEN_HOUSES && (SEN->at)(b, h + 1) == p) h++; |
1994779 |
7462125 |
9456904 |
|
| 40 |
7462125 |
| *lo = l; |
| 41 |
7462125 |
| *hi = h; |
| 42 |
7462125 |
| } |
| 43 |
|
| |
| 44 |
|
| /* Whether the enemy piece on `house` may not be swapped, judged on the board as |
| 45 |
|
| * it stands BEFORE the move: the house the mover leaves holds the mover's own |
| 46 |
|
| * piece and is never part of the defender's run. */ |
| 47 |
2513398 |
| static int sen_protected_at(const struct sen_board *b, const struct sen_rules *r, int house) |
| 48 |
|
| { |
| 49 |
|
| int lo, hi; |
| 50 |
2513398 |
▶ | if (r->protect == SEN_PROTECT_NONE) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->protect == SEN_PROTECT_NONE) return 0; |
155 |
2513243 |
2513398 |
|
| 51 |
2513243 |
| sen_run_of(b, house, &lo, &hi); |
| 52 |
2513243 |
▶ | if (hi - lo + 1 < 2) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (hi - lo + 1 < 2) return 0; |
1815180 |
698063 |
2513243 |
|
| 53 |
698063 |
▶ | if (r->protect == SEN_PROTECT_RUN) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->protect == SEN_PROTECT_RUN) return 1; |
424502 |
273561 |
698063 |
|
| 54 |
273561 |
▶ | return house == lo || house == hi; |
Branch: 2 branches
| Branch | True | False | Total |
| return house == lo || house == hi; |
33815 |
239746 |
273561 |
| return house == lo || house == hi; |
33365 |
450 |
33815 |
|
| 55 |
|
| } |
| 56 |
|
| |
| 57 |
|
| /* Whether a move from `from` to `to`, either way, PASSES an enemy run of |
| 58 |
|
| * `blockade` houses or more: a house of the run lies strictly between them. A |
| 59 |
|
| * side's own runs never block it. */ |
| 60 |
19613722 |
| static int sen_blocked(const struct sen_board *b, const struct sen_rules *r, |
| 61 |
|
| int enemy, int from, int to) |
| 62 |
|
| { |
| 63 |
19613722 |
| int lo = from < to ? from : to; |
| 64 |
19613722 |
| int hi = from < to ? to : from; |
| 65 |
|
| int x; |
| 66 |
19613722 |
▶ | if (r->blockade == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->blockade == 0) return 0; |
561622 |
19052100 |
19613722 |
|
| 67 |
51580354 |
▶ | for (x = lo + 1; x < hi; x++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (x = lo + 1; x < hi; x++) { |
33181088 |
18399266 |
51580354 |
|
| 68 |
|
| int rl, rh; |
| 69 |
33181088 |
▶ | if ((SEN->at)(b, x) != enemy) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((SEN->at)(b, x) != enemy) continue; |
28236422 |
4944666 |
33181088 |
|
| 70 |
4944666 |
| sen_run_of(b, x, &rl, &rh); |
| 71 |
4944666 |
▶ | if (rh - rl + 1 >= r->blockade) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rh - rl + 1 >= r->blockade) return 1; |
652834 |
4291832 |
4944666 |
|
| 72 |
|
| } |
| 73 |
18399266 |
| return 0; |
| 74 |
|
| } |
| 75 |
|
| |
| 76 |
17460089 |
| static void sen_set_move(struct sen_move *m, int from, int to, int roll, int swap, int back, |
| 77 |
|
| int off, int walk) |
| 78 |
|
| { |
| 79 |
17460089 |
| m->from = from; |
| 80 |
17460089 |
| m->to = to; |
| 81 |
17460089 |
| m->roll = roll; |
| 82 |
17460089 |
| m->swap = swap; |
| 83 |
17460089 |
| m->back = back; |
| 84 |
17460089 |
| m->off = off; |
| 85 |
17460089 |
| m->walk = walk; |
| 86 |
17460089 |
| m->reserved[0] = 0; |
| 87 |
17460089 |
| } |
| 88 |
|
| |
| 89 |
|
| /* One candidate that COUNTS along the track, forward or back. 1 and the move |
| 90 |
|
| * written when it is one. */ |
| 91 |
19680163 |
| static int sen_consider(const struct sen_board *b, const struct sen_rules *r, int roll, |
| 92 |
|
| int from, int to, int back, struct sen_move *m) |
| 93 |
|
| { |
| 94 |
19680163 |
| int me = SEN_PIECE_OF((SEN->side)(b)); |
| 95 |
19680163 |
▶ | int enemy = SEN_PIECE_OF(SEN_OTHER((SEN->side)(b))); |
Branch: 1 branch
| Branch | True | False | Total |
| int enemy = SEN_PIECE_OF(SEN_OTHER((SEN->side)(b))); |
9755224 |
9924939 |
19680163 |
|
| 96 |
|
| int there; |
| 97 |
|
| |
| 98 |
19680163 |
▶ | if (to < 1 || to > SEN_HOUSES) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (to < 1 || to > SEN_HOUSES) return 0; |
19613722 |
66441 |
19680163 |
| if (to < 1 || to > SEN_HOUSES) return 0; |
0 |
19613722 |
19613722 |
|
| 99 |
19613722 |
▶ | if (sen_blocked(b, r, enemy, from, to)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sen_blocked(b, r, enemy, from, to)) return 0; |
652834 |
18960888 |
19613722 |
|
| 100 |
18960888 |
| there = (SEN->at)(b, to); |
| 101 |
18960888 |
▶ | if (there == me) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (there == me) return 0; |
2880917 |
16079971 |
18960888 |
|
| 102 |
16079971 |
▶ | if (there == enemy) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (there == enemy) { |
2747624 |
13332347 |
16079971 |
|
| 103 |
2747624 |
▶ | if (r->safe & (1UL << to)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->safe & (1UL << to)) return 0; |
236505 |
2511119 |
2747624 |
|
| 104 |
2511119 |
▶ | if (sen_protected_at(b, r, to)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sen_protected_at(b, r, to)) return 0; |
696885 |
1814234 |
2511119 |
|
| 105 |
|
| } |
| 106 |
15146581 |
| sen_set_move(m, from, to, roll, there == enemy, back, 0, 0); |
| 107 |
15146581 |
| return 1; |
| 108 |
|
| } |
| 109 |
|
| |
| 110 |
|
| /* Where a forward roll from `from` lands, or 0 when it is not a move: no piece |
| 111 |
|
| * goes past 26 without standing on it. Under SEN_REACH_ANY a piece on 25 lands |
| 112 |
|
| * on 26 whatever the roll. */ |
| 113 |
20345242 |
| static int sen_forward_to(const struct sen_rules *r, int from, int roll) |
| 114 |
|
| { |
| 115 |
20345242 |
| int to = from + roll; |
| 116 |
20345242 |
▶ | if (to <= HAPPINESS) return to; |
Branch: 1 branch
| Branch | True | False | Total |
| if (to <= HAPPINESS) return to; |
18841317 |
1503925 |
20345242 |
|
| 117 |
1503925 |
▶ | if (r->reach26 == SEN_REACH_ANY && from == HAPPINESS - 1) return HAPPINESS; |
Branch: 2 branches
| Branch | True | False | Total |
| if (r->reach26 == SEN_REACH_ANY && from == HAPPINESS - 1) return HAPPINESS; |
1187438 |
316487 |
1503925 |
| if (r->reach26 == SEN_REACH_ANY && from == HAPPINESS - 1) return HAPPINESS; |
515513 |
671925 |
1187438 |
|
| 118 |
988412 |
| return 0; |
| 119 |
|
| } |
| 120 |
|
| |
| 121 |
|
| /* ---- the marked houses' exits ------------------------------------------------- |
| 122 |
|
| * |
| 123 |
|
| * The walk: SEN_REBIRTH, else the first EMPTY house below it. Either side's |
| 124 |
|
| * piece makes a house full, and the walk never swaps. On a consistent board it |
| 125 |
|
| * always finds one: the mover stands on 26 or above, and the other pieces, at |
| 126 |
|
| * most 2 * SEN_PIECES_MAX - 1 of them, cannot fill the fifteen houses from 1. |
| 127 |
|
| */ |
| 128 |
|
| |
| 129 |
646736 |
| static int sen_walk_down(const struct sen_board *b) |
| 130 |
|
| { |
| 131 |
|
| int x; |
| 132 |
1376887 |
▶ | for (x = SEN_REBIRTH; x >= 1; x--) |
Branch: 1 branch
| Branch | True | False | Total |
| for (x = SEN_REBIRTH; x >= 1; x--) |
1376886 |
1 |
1376887 |
|
| 133 |
1376886 |
▶ | if ((SEN->at)(b, x) == SEN_EMPTY) return x; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((SEN->at)(b, x) == SEN_EMPTY) return x; |
646735 |
730151 |
1376886 |
|
| 134 |
1 |
| return 0; |
| 135 |
|
| } |
| 136 |
|
| |
| 137 |
|
| /* A piece on `from`, 26 or beyond, reads its chain for the roll. AN EXIT NEVER |
| 138 |
|
| * COUNTS ALONG THE TRACK, so no blockade judges it, and it never swaps: it lands |
| 139 |
|
| * on an empty house or nowhere. */ |
| 140 |
3849155 |
| static int sen_exit_move(const struct sen_board *b, const struct sen_rules *r, int roll, |
| 141 |
|
| int from, struct sen_move *m) |
| 142 |
|
| { |
| 143 |
|
| int i; |
| 144 |
3849155 |
| const signed char *chain = r->exit[from - SEN_EXIT_FIRST][roll - 1]; |
| 145 |
4002040 |
▶ | for (i = 0; i < SEN_CHAIN_MAX; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < SEN_CHAIN_MAX; i++) { |
4002040 |
0 |
4002040 |
|
| 146 |
4002040 |
| int e = chain[i]; |
| 147 |
4002040 |
▶ | if (e == SEN_X_OFF) { sen_set_move(m, from, -1, roll, 0, 0, 1, 0); return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == SEN_X_OFF) { sen_set_move(m, from, -1, roll, 0, 0, 1, 0); return 1; } |
1171798 |
2830242 |
4002040 |
|
| 148 |
2830242 |
▶ | if (e == SEN_X_END || e == SEN_X_STAY) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (e == SEN_X_END || e == SEN_X_STAY) return 0; |
1406791 |
1423451 |
2830242 |
| if (e == SEN_X_END || e == SEN_X_STAY) return 0; |
112196 |
1294595 |
1406791 |
|
| 149 |
1294595 |
▶ | if (e == SEN_X_WALK) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e == SEN_X_WALK) { |
646736 |
647859 |
1294595 |
|
| 150 |
646736 |
| int to = sen_walk_down(b); |
| 151 |
646736 |
▶ | if (to) { sen_set_move(m, from, to, roll, 0, 0, 0, 1); return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (to) { sen_set_move(m, from, to, roll, 0, 0, 0, 1); return 1; } |
646735 |
1 |
646736 |
|
| 152 |
1 |
| continue; |
| 153 |
|
| } |
| 154 |
647859 |
▶ | if ((SEN->at)(b, e) == SEN_EMPTY) { sen_set_move(m, from, e, roll, 0, 0, 0, 0); return 1; } |
Branch: 1 branch
| Branch | True | False | Total |
| if ((SEN->at)(b, e) == SEN_EMPTY) { sen_set_move(m, from, e, roll, 0, 0, 0, 0); return 1; } |
494975 |
152884 |
647859 |
|
| 155 |
|
| } |
| 156 |
0 |
| return 0; |
| 157 |
|
| } |
| 158 |
|
| |
| 159 |
6956156 |
| static int sen_moves(const struct sen_board *b, const struct sen_rules *r, int roll, |
| 160 |
|
| int only, struct sen_move *out, int max) |
| 161 |
|
| { |
| 162 |
|
| struct sen_move m; |
| 163 |
6956156 |
| int me, h, n = 0, pass; |
| 164 |
|
| |
| 165 |
6956156 |
▶ | if (roll < 1 || roll > SEN_ROLL_MAX) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (roll < 1 || roll > SEN_ROLL_MAX) return -1; |
6956155 |
1 |
6956156 |
| if (roll < 1 || roll > SEN_ROLL_MAX) return -1; |
0 |
6956155 |
6956155 |
|
| 166 |
6956155 |
▶ | if (!(SEN->rules_ok)(r)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(SEN->rules_ok)(r)) return -1; |
0 |
6956155 |
6956155 |
|
| 167 |
6956155 |
| me = SEN_PIECE_OF((SEN->side)(b)); |
| 168 |
|
| |
| 169 |
14848181 |
▶ | for (pass = 0; pass < 2 && n == 0; pass++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (pass = 0; pass < 2 && n == 0; pass++) { |
13912310 |
935871 |
14848181 |
| for (pass = 0; pass < 2 && n == 0; pass++) { |
7892056 |
6020254 |
13912310 |
|
| 170 |
7892056 |
▶ | if (pass == 1 && !r->backward) break; |
Branch: 2 branches
| Branch | True | False | Total |
| if (pass == 1 && !r->backward) break; |
935901 |
6956155 |
7892056 |
| if (pass == 1 && !r->backward) break; |
30 |
935871 |
935901 |
|
| 171 |
244652806 |
▶ | for (h = 1; h <= SEN_HOUSES; h++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (h = 1; h <= SEN_HOUSES; h++) { |
236760780 |
7892026 |
244652806 |
|
| 172 |
|
| int to, ok; |
| 173 |
236760780 |
▶ | if ((SEN->at)(b, h) != me) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((SEN->at)(b, h) != me) continue; |
210639171 |
26121609 |
236760780 |
|
| 174 |
26121609 |
▶ | if (only && h != only) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (only && h != only) continue; |
1550459 |
24571150 |
26121609 |
| if (only && h != only) continue; |
1266652 |
283807 |
1550459 |
|
| 175 |
24854957 |
▶ | if (h >= HAPPINESS) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (h >= HAPPINESS) { |
4186382 |
20668575 |
24854957 |
|
| 176 |
4186382 |
▶ | if (pass == 1) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (pass == 1) continue; |
337227 |
3849155 |
4186382 |
|
| 177 |
3849155 |
| ok = sen_exit_move(b, r, roll, h, &m); |
| 178 |
|
| } |
| 179 |
|
| else { |
| 180 |
20668575 |
▶ | to = pass == 0 ? sen_forward_to(r, h, roll) : h - roll; |
Branch: 1 branch
| Branch | True | False | Total |
| to = pass == 0 ? sen_forward_to(r, h, roll) : h - roll; |
20345242 |
323333 |
20668575 |
|
| 181 |
20668575 |
▶ | if (pass == 0 && to == 0) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (pass == 0 && to == 0) continue; |
20345242 |
323333 |
20668575 |
| if (pass == 0 && to == 0) continue; |
988412 |
19356830 |
20345242 |
|
| 182 |
19680163 |
| ok = sen_consider(b, r, roll, h, to, pass, &m); |
| 183 |
|
| } |
| 184 |
23529318 |
▶ | if (!ok) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!ok) continue; |
6069229 |
17460089 |
23529318 |
|
| 185 |
17460089 |
▶ | if (out && n < max) out[n] = m; |
Branch: 2 branches
| Branch | True | False | Total |
| if (out && n < max) out[n] = m; |
17460089 |
0 |
17460089 |
| if (out && n < max) out[n] = m; |
17460089 |
0 |
17460089 |
|
| 186 |
17460089 |
| n++; |
| 187 |
|
| } |
| 188 |
|
| } |
| 189 |
6956155 |
| return n; |
| 190 |
|
| } |
| 191 |
|
| |
| 192 |
|
| /* ---- a move made ---------------------------------------------------------------- |
| 193 |
|
| * |
| 194 |
|
| * WHOSE TURN IT IS AFTERWARDS is decided here and nowhere else: the extra throw |
| 195 |
|
| * belongs to the THROW, so a roll of 1, 4 or 5 keeps the side whatever was done |
| 196 |
|
| * with it, a forfeit included, and a 2 or 3 passes it. |
| 197 |
|
| */ |
| 198 |
|
| |
| 199 |
34318939 |
| static int sen_won(const struct sen_board *b, int side) |
| 200 |
|
| { |
| 201 |
34318939 |
| return (SEN->count)(b, side) == 0; |
| 202 |
|
| } |
| 203 |
|
| |
| 204 |
4614155 |
| static int sen_apply(struct sen_board *b, const struct sen_rules *r, |
| 205 |
|
| const struct sen_move *m, struct sen_undo *u) |
| 206 |
|
| { |
| 207 |
4614155 |
| int side = (SEN->side)(b); |
| 208 |
4614155 |
| int me = SEN_PIECE_OF(side); |
| 209 |
4614155 |
▶ | int enemy = SEN_PIECE_OF(SEN_OTHER(side)); |
Branch: 1 branch
| Branch | True | False | Total |
| int enemy = SEN_PIECE_OF(SEN_OTHER(side)); |
2435813 |
2178342 |
4614155 |
|
| 210 |
|
| int i; |
| 211 |
|
| |
| 212 |
4614155 |
| u->kind = SEN_UNDO_NONE; |
| 213 |
4614155 |
| u->bound = 0; |
| 214 |
23070775 |
▶ | for (i = 0; i < 4; i++) u->reserved[i] = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 4; i++) u->reserved[i] = 0; |
18456620 |
4614155 |
23070775 |
|
| 215 |
4614155 |
▶ | if (!m || (SEN->at)(b, m->from) != me) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!m || (SEN->at)(b, m->from) != me) return 0; |
4614155 |
0 |
4614155 |
| if (!m || (SEN->at)(b, m->from) != me) return 0; |
1 |
4614154 |
4614155 |
|
| 216 |
4614154 |
▶ | if (!m->off && (m->to < 1 || m->to > SEN_HOUSES)) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!m->off && (m->to < 1 || m->to > SEN_HOUSES)) return 0; |
4371598 |
242556 |
4614154 |
| if (!m->off && (m->to < 1 || m->to > SEN_HOUSES)) return 0; |
4371598 |
0 |
4371598 |
| if (!m->off && (m->to < 1 || m->to > SEN_HOUSES)) return 0; |
0 |
4371598 |
4371598 |
|
| 217 |
|
| |
| 218 |
4614154 |
| u->kind = SEN_UNDO_MOVE; |
| 219 |
4614154 |
| u->side = side; |
| 220 |
4614154 |
| u->ply = (SEN->ply)(b); |
| 221 |
4614154 |
| u->from = m->from; |
| 222 |
4614154 |
▶ | u->to = m->off ? -1 : m->to; |
Branch: 1 branch
| Branch | True | False | Total |
| u->to = m->off ? -1 : m->to; |
4371598 |
242556 |
4614154 |
|
| 223 |
4614154 |
| u->swapped = 0; |
| 224 |
|
| |
| 225 |
4614154 |
| (SEN->lift)(b, m->from); |
| 226 |
4614154 |
▶ | if (!m->off) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!m->off) { |
4371598 |
242556 |
4614154 |
|
| 227 |
4371598 |
▶ | if ((SEN->at)(b, m->to) == enemy) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((SEN->at)(b, m->to) == enemy) { |
618077 |
3753521 |
4371598 |
|
| 228 |
618077 |
| (SEN->put)(b, m->from, enemy); |
| 229 |
618077 |
| u->swapped = 1; |
| 230 |
|
| } |
| 231 |
4371598 |
| (SEN->put)(b, m->to, me); |
| 232 |
|
| } |
| 233 |
4614154 |
| (SEN->set_ply)(b, u->ply + 1); |
| 234 |
|
| |
| 235 |
4614154 |
▶ | if (sen_won(b, side)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sen_won(b, side)) return 1; |
73107 |
4541047 |
4614154 |
|
| 236 |
4541047 |
▶ | if (r && r->throw_on_26 && !m->off && m->from < HAPPINESS && m->to == HAPPINESS) { |
Branch: 5 branches
| Branch | True | False | Total |
| if (r && r->throw_on_26 && !m->off && m->from < HAPPINESS && m->to == HAPPINESS) { |
4541047 |
0 |
4541047 |
| if (r && r->throw_on_26 && !m->off && m->from < HAPPINESS && m->to == HAPPINESS) { |
2351728 |
2189319 |
4541047 |
| if (r && r->throw_on_26 && !m->off && m->from < HAPPINESS && m->to == HAPPINESS) { |
2285508 |
66220 |
2351728 |
| if (r && r->throw_on_26 && !m->off && m->from < HAPPINESS && m->to == HAPPINESS) { |
2163509 |
121999 |
2285508 |
| if (r && r->throw_on_26 && !m->off && m->from < HAPPINESS && m->to == HAPPINESS) { |
193640 |
1969869 |
2163509 |
|
| 237 |
193640 |
| u->bound = HAPPINESS; |
| 238 |
193640 |
| return 1; |
| 239 |
|
| } |
| 240 |
4347407 |
▶ | if ((SEN->extra)(m->roll) != 1) (SEN->set_side)(b, SEN_OTHER(side)); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((SEN->extra)(m->roll) != 1) (SEN->set_side)(b, SEN_OTHER(side)); |
1990551 |
2356856 |
4347407 |
|
| 241 |
4347407 |
| return 1; |
| 242 |
|
| } |
| 243 |
|
| |
| 244 |
253330 |
| static void sen_forfeit(struct sen_board *b, int roll, struct sen_undo *u) |
| 245 |
|
| { |
| 246 |
253330 |
| int side = (SEN->side)(b); |
| 247 |
|
| int i; |
| 248 |
253330 |
| u->kind = SEN_UNDO_FORFEIT; |
| 249 |
253330 |
| u->side = side; |
| 250 |
253330 |
| u->ply = (SEN->ply)(b); |
| 251 |
253330 |
| u->from = u->to = 0; |
| 252 |
253330 |
| u->swapped = 0; |
| 253 |
253330 |
| u->bound = 0; |
| 254 |
1266650 |
▶ | for (i = 0; i < 4; i++) u->reserved[i] = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < 4; i++) u->reserved[i] = 0; |
1013320 |
253330 |
1266650 |
|
| 255 |
253330 |
| (SEN->set_ply)(b, u->ply + 1); |
| 256 |
253330 |
▶ | if ((SEN->extra)(roll) != 1) (SEN->set_side)(b, SEN_OTHER(side)); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((SEN->extra)(roll) != 1) (SEN->set_side)(b, SEN_OTHER(side)); |
141349 |
111981 |
253330 |
|
| 257 |
253330 |
| } |
| 258 |
|
| |
| 259 |
3760113 |
| static void sen_unapply(struct sen_board *b, const struct sen_undo *u) |
| 260 |
|
| { |
| 261 |
3760113 |
▶ | if (!u || u->kind == SEN_UNDO_NONE) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!u || u->kind == SEN_UNDO_NONE) return; |
3760113 |
0 |
3760113 |
| if (!u || u->kind == SEN_UNDO_NONE) return; |
0 |
3760113 |
3760113 |
|
| 262 |
3760113 |
▶ | if (u->kind == SEN_UNDO_MOVE) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (u->kind == SEN_UNDO_MOVE) { |
3741877 |
18236 |
3760113 |
|
| 263 |
3741877 |
| int me = SEN_PIECE_OF(u->side); |
| 264 |
3741877 |
▶ | int enemy = SEN_PIECE_OF(SEN_OTHER(u->side)); |
Branch: 1 branch
| Branch | True | False | Total |
| int enemy = SEN_PIECE_OF(SEN_OTHER(u->side)); |
1935539 |
1806338 |
3741877 |
|
| 265 |
3741877 |
▶ | if (u->to > 0) (SEN->put)(b, u->to, u->swapped ? enemy : SEN_EMPTY); |
Branch: 2 branches
| Branch | True | False | Total |
| if (u->to > 0) (SEN->put)(b, u->to, u->swapped ? enemy : SEN_EMPTY); |
3645505 |
96372 |
3741877 |
| if (u->to > 0) (SEN->put)(b, u->to, u->swapped ? enemy : SEN_EMPTY); |
506179 |
3139326 |
3645505 |
|
| 266 |
3741877 |
| (SEN->put)(b, u->from, me); |
| 267 |
|
| } |
| 268 |
3760113 |
| (SEN->set_side)(b, u->side); |
| 269 |
3760113 |
| (SEN->set_ply)(b, u->ply); |
| 270 |
|
| } |
| 271 |
|
| |
| 272 |
|
| /* ---- how a game stands ------------------------------------------------------------ |
| 273 |
|
| * |
| 274 |
|
| * After the move that wins, the side does not change, so the winner is the |
| 275 |
|
| * side to move when its pieces are all home, and otherwise the other side when |
| 276 |
|
| * its are. |
| 277 |
|
| */ |
| 278 |
|
| |
| 279 |
14857851 |
| static int sen_winner(const struct sen_board *b) |
| 280 |
|
| { |
| 281 |
14857851 |
| int side = (SEN->side)(b); |
| 282 |
14857851 |
▶ | if (sen_won(b, side)) return side; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sen_won(b, side)) return side; |
10917 |
14846934 |
14857851 |
|
| 283 |
14846934 |
▶ | if (sen_won(b, SEN_OTHER(side))) return SEN_OTHER(side); |
Branch: 1 branch
| Branch | True | False | Total |
| if (sen_won(b, SEN_OTHER(side))) return SEN_OTHER(side); |
4 |
14846930 |
14846934 |
|
| 284 |
14846930 |
| return -1; |
| 285 |
|
| } |
| 286 |
|
| |
| 287 |
11116095 |
| static int sen_how(const struct sen_board *b) |
| 288 |
|
| { |
| 289 |
11116095 |
▶ | if (sen_winner(b) >= 0) return SEN_BY_HOME; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sen_winner(b) >= 0) return SEN_BY_HOME; |
8235 |
11107860 |
11116095 |
|
| 290 |
11107860 |
▶ | if ((SEN->ply)(b) >= SEN_PLY_CAP) return SEN_BY_PLY_CAP; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((SEN->ply)(b) >= SEN_PLY_CAP) return SEN_BY_PLY_CAP; |
7 |
11107853 |
11107860 |
|
| 291 |
11107853 |
| return 0; |
| 292 |
|
| } |
| 293 |
|
| |
| 294 |
11114682 |
| static int sen_status(const struct sen_board *b) |
| 295 |
|
| { |
| 296 |
11114682 |
| int how = sen_how(b); |
| 297 |
11114682 |
▶ | if (how == SEN_BY_HOME) return SEN_WON; |
Branch: 1 branch
| Branch | True | False | Total |
| if (how == SEN_BY_HOME) return SEN_WON; |
6824 |
11107858 |
11114682 |
|
| 298 |
11107858 |
▶ | if (how == SEN_BY_PLY_CAP) return SEN_DRAWN; |
Branch: 1 branch
| Branch | True | False | Total |
| if (how == SEN_BY_PLY_CAP) return SEN_DRAWN; |
5 |
11107853 |
11107858 |
|
| 299 |
11107853 |
| return SEN_ONGOING; |
| 300 |
|
| } |
| 301 |
|
| |
| 302 |
3 |
| static int sen_ply_cap(void) { return SEN_PLY_CAP; } |
| 303 |
|
| |
| 304 |
|
| /* ---- the walk ---------------------------------------------------------------------- */ |
| 305 |
|
| |
| 306 |
11880 |
| static unsigned long long sen_walk_from(struct sen_board *b, const struct sen_rules *r, |
| 307 |
|
| int depth, int only) |
| 308 |
|
| { |
| 309 |
|
| struct sen_move m[SEN_MOVES_MAX]; |
| 310 |
|
| struct sen_undo u; |
| 311 |
11880 |
| unsigned long long leaves = 0; |
| 312 |
|
| int roll, n, i; |
| 313 |
|
| |
| 314 |
11880 |
▶ | if (depth <= 0 || sen_status(b) != SEN_ONGOING) return 1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (depth <= 0 || sen_status(b) != SEN_ONGOING) return 1; |
760 |
11120 |
11880 |
| if (depth <= 0 || sen_status(b) != SEN_ONGOING) return 1; |
0 |
760 |
760 |
|
| 315 |
4560 |
▶ | for (roll = 1; roll <= SEN_ROLL_MAX; roll++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (roll = 1; roll <= SEN_ROLL_MAX; roll++) { |
3800 |
760 |
4560 |
|
| 316 |
3800 |
| n = sen_moves(b, r, roll, only, m, SEN_MOVES_MAX); |
| 317 |
3800 |
▶ | if (n > SEN_MOVES_MAX) n = SEN_MOVES_MAX; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > SEN_MOVES_MAX) n = SEN_MOVES_MAX; |
0 |
3800 |
3800 |
|
| 318 |
3800 |
▶ | if (n <= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (n <= 0) { |
40 |
3760 |
3800 |
|
| 319 |
40 |
| sen_forfeit(b, roll, &u); |
| 320 |
40 |
| leaves += sen_walk_from(b, r, depth - 1, 0); |
| 321 |
40 |
| sen_unapply(b, &u); |
| 322 |
40 |
| continue; |
| 323 |
|
| } |
| 324 |
15560 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
11800 |
3760 |
15560 |
|
| 325 |
11800 |
| sen_apply(b, r, &m[i], &u); |
| 326 |
11800 |
| leaves += sen_walk_from(b, r, depth - 1, u.bound); |
| 327 |
11800 |
| sen_unapply(b, &u); |
| 328 |
|
| } |
| 329 |
|
| } |
| 330 |
760 |
| return leaves; |
| 331 |
|
| } |
| 332 |
|
| |
| 333 |
40 |
| static unsigned long long sen_walk(struct sen_board *b, const struct sen_rules *r, int depth) |
| 334 |
|
| { |
| 335 |
40 |
▶ | if (!b || !(SEN->rules_ok)(r)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!b || !(SEN->rules_ok)(r)) return 0; |
40 |
0 |
40 |
| if (!b || !(SEN->rules_ok)(r)) return 0; |
0 |
40 |
40 |
|
| 336 |
40 |
| return sen_walk_from(b, r, depth, 0); |
| 337 |
|
| } |
| 338 |
|
| |
| 339 |
|
| /* ---- what a house is ------------------------------------------------------------ |
| 340 |
|
| * |
| 341 |
|
| * The same rules the generator applies, asked of one house. A run is measured |
| 342 |
|
| * from the piece that stands there, so an empty house is refused first: the |
| 343 |
|
| * run of empty houses around it is no run of anybody's. |
| 344 |
|
| */ |
| 345 |
|
| |
| 346 |
6617 |
| static int sen_held(const struct sen_board *b, const struct sen_rules *r, int house) |
| 347 |
|
| { |
| 348 |
|
| int p; |
| 349 |
6617 |
▶ | if (house < 1 || house > SEN_HOUSES) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (house < 1 || house > SEN_HOUSES) return 0; |
6617 |
0 |
6617 |
| if (house < 1 || house > SEN_HOUSES) return 0; |
0 |
6617 |
6617 |
|
| 350 |
6617 |
▶ | if (!(SEN->rules_ok)(r)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(SEN->rules_ok)(r)) return 0; |
0 |
6617 |
6617 |
|
| 351 |
6617 |
| p = (SEN->at)(b, house); |
| 352 |
6617 |
▶ | return p == SEN_PIECE_OF(SEN_SIDE_LIGHT) || p == SEN_PIECE_OF(SEN_SIDE_DARK); |
Branch: 2 branches
| Branch | True | False | Total |
| return p == SEN_PIECE_OF(SEN_SIDE_LIGHT) || p == SEN_PIECE_OF(SEN_SIDE_DARK); |
3421 |
3196 |
6617 |
| return p == SEN_PIECE_OF(SEN_SIDE_LIGHT) || p == SEN_PIECE_OF(SEN_SIDE_DARK); |
3299 |
122 |
3421 |
|
| 353 |
|
| } |
| 354 |
|
| |
| 355 |
2355 |
| static int sen_protected_house(const struct sen_board *b, const struct sen_rules *r, int house) |
| 356 |
|
| { |
| 357 |
2355 |
▶ | if (!sen_held(b, r, house)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sen_held(b, r, house)) return 0; |
76 |
2279 |
2355 |
|
| 358 |
2279 |
| return sen_protected_at(b, r, house); |
| 359 |
|
| } |
| 360 |
|
| |
| 361 |
4262 |
| static int sen_in_blockade(const struct sen_board *b, const struct sen_rules *r, int house) |
| 362 |
|
| { |
| 363 |
|
| int lo, hi; |
| 364 |
4262 |
▶ | if (!sen_held(b, r, house)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!sen_held(b, r, house)) return 0; |
46 |
4216 |
4262 |
|
| 365 |
4216 |
▶ | if (r->blockade == 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (r->blockade == 0) return 0; |
0 |
4216 |
4216 |
|
| 366 |
4216 |
| sen_run_of(b, house, &lo, &hi); |
| 367 |
4216 |
| return hi - lo + 1 >= r->blockade; |
| 368 |
|
| } |
| 369 |
|
| |
| 370 |
|
| /* ---- the table -------------------------------------------------------------- */ |
| 371 |
|
| |
| 372 |
80 |
| void sen_moves_install(struct sen_abi *t) |
| 373 |
|
| { |
| 374 |
80 |
| SEN = t; |
| 375 |
80 |
| t->moves = sen_moves; |
| 376 |
80 |
| t->apply = sen_apply; |
| 377 |
80 |
| t->forfeit = sen_forfeit; |
| 378 |
80 |
| t->unapply = sen_unapply; |
| 379 |
80 |
| t->status = sen_status; |
| 380 |
80 |
| t->how = sen_how; |
| 381 |
80 |
| t->winner = sen_winner; |
| 382 |
80 |
| t->ply_cap = sen_ply_cap; |
| 383 |
80 |
| t->walk = sen_walk; |
| 384 |
80 |
| t->protected_at = sen_protected_house; |
| 385 |
80 |
| t->in_blockade = sen_in_blockade; |
| 386 |
80 |
| } |