| 1 |
|
| /* fn_moves.c - one stone, one step: where it may go and what it takes; and |
| 2 |
|
| * then the chain of steps that is a whole move. |
| 3 |
|
| * |
| 4 |
|
| * PERL-FREE, as fn_engine.c is. |
| 5 |
|
| * |
| 6 |
|
| * THE RULES THIS FILE IMPLEMENTS, quoted from the sources the distribution |
| 7 |
|
| * names in its documentation: |
| 8 |
|
| * |
| 9 |
|
| * "A paika move consists of moving one stone along a line to an adjacent |
| 10 |
|
| * intersection." |
| 11 |
|
| * "Capturing moves are obligatory and have to be played in preference to |
| 12 |
|
| * paika moves." And: "If any capturing moves are possible anywhere on the |
| 13 |
|
| * board, then a capturing move must be made. If multiple captures are |
| 14 |
|
| * possible, you can choose which to do." |
| 15 |
|
| * Approach: "moving the capturing stone to a point adjacent to an opponent's |
| 16 |
|
| * stone, which must be on the continuation of the capturing stone's |
| 17 |
|
| * movement line." |
| 18 |
|
| * Withdrawal: "the capturing stone moves from a point adjacent to the |
| 19 |
|
| * opponent's stone, away from the stone along the continuation of the line |
| 20 |
|
| * between them." |
| 21 |
|
| * "All opponent pieces in line beyond that stone (as long as there is no |
| 22 |
|
| * interruption by an empty point or an own stone) are captured as well." |
| 23 |
|
| * "It is not allowed to capture by approach and withdrawal at the same time |
| 24 |
|
| * ... the player has to choose". |
| 25 |
|
| * |
| 26 |
|
| * and two readings where the sources are brief: |
| 27 |
|
| * |
| 28 |
|
| * - A STEP THAT COULD TAKE, TAKES. Capture is obligatory, so there is no |
| 29 |
|
| * moving there and declining: a step is a paika only when it takes |
| 30 |
|
| * nothing by either method. |
| 31 |
|
| * - THE OBLIGATION IS GLOBAL AND THE CHOICE IS FREE. If any stone can take, |
| 32 |
|
| * no stone may make a paika; but which capture is made is the mover's, |
| 33 |
|
| * and the largest has no priority. |
| 34 |
|
| * |
| 35 |
|
| * THE LINE IS READ OUTWARD FROM THE STEP. Nothing here scans the board for |
| 36 |
|
| * lines: an approach looks at the point beyond `to`, a withdrawal at the point |
| 37 |
|
| * behind `from`, and each walks on from there. |
| 38 |
|
| */ |
| 39 |
|
| |
| 40 |
|
| #include <string.h> |
| 41 |
|
| #include "fn_abi.h" |
| 42 |
|
| |
| 43 |
|
| /* fn_engine.c owns the struct; this file reaches it through the same table |
| 44 |
|
| * every other consumer uses, which keeps the two files honest about the |
| 45 |
|
| * interface. */ |
| 46 |
3934995491 |
| static const struct fn_abi *E(void) { return fn_abi_table(); } |
| 47 |
|
| |
| 48 |
|
| #define AT(b, sq) ((E())->at((b), (sq))) |
| 49 |
|
| #define NEXT(b, sq, dir) ((E())->next((b), (sq), (dir))) |
| 50 |
|
| |
| 51 |
|
| /* A stone has at most eight ways to go and each is at most two entries. */ |
| 52 |
|
| #define STONE_STEPS_MAX 16 |
| 53 |
|
| |
| 54 |
|
| /* ---- the line ------------------------------------------------------------------ |
| 55 |
|
| * |
| 56 |
|
| * The enemy stones from `start` onward in `dir`, nearest first, until the |
| 57 |
|
| * line breaks. `start` may be -1, which is the edge and an empty line. |
| 58 |
|
| * |
| 59 |
|
| * A diagonal walk never has to ask whether it may go on: every point on a |
| 60 |
|
| * diagonal is a strong point, so NEXT answers -1 only at the edge. |
| 61 |
|
| */ |
| 62 |
596166258 |
| static int line_from(const struct fn_board *b, int start, int dir, int foe, int *out) |
| 63 |
|
| { |
| 64 |
596166258 |
| int n = 0, sq = start; |
| 65 |
622297439 |
▶ | while (sq >= 0 && n < FN_LINE_MAX && AT(b, sq) == foe) { |
Branch: 3 branches
| Branch | True | False | Total |
| while (sq >= 0 && n < FN_LINE_MAX && AT(b, sq) == foe) { |
487170844 |
135126595 |
622297439 |
| while (sq >= 0 && n < FN_LINE_MAX && AT(b, sq) == foe) { |
487170844 |
0 |
487170844 |
| while (sq >= 0 && n < FN_LINE_MAX && AT(b, sq) == foe) { |
26131181 |
461039663 |
487170844 |
|
| 66 |
26131181 |
| out[n++] = sq; |
| 67 |
26131181 |
| sq = NEXT(b, sq, dir); |
| 68 |
|
| } |
| 69 |
596166258 |
| return n; |
| 70 |
|
| } |
| 71 |
|
| |
| 72 |
299899353 |
| static void step_fill(struct fn_step *s, int from, int to, int dir, int kind, const int *taken, int n) |
| 73 |
|
| { |
| 74 |
|
| int i; |
| 75 |
299899353 |
| s->from = from; |
| 76 |
299899353 |
| s->to = to; |
| 77 |
299899353 |
| s->dir = dir; |
| 78 |
299899353 |
| s->kind = kind; |
| 79 |
299899353 |
| s->taken_n = n; |
| 80 |
2399194824 |
▶ | for (i = 0; i < FN_LINE_MAX; i++) s->taken[i] = i < n ? taken[i] : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| for (i = 0; i < FN_LINE_MAX; i++) s->taken[i] = i < n ? taken[i] : 0; |
26110422 |
2073185049 |
2099295471 |
| for (i = 0; i < FN_LINE_MAX; i++) s->taken[i] = i < n ? taken[i] : 0; |
2099295471 |
299899353 |
2399194824 |
|
| 81 |
299899353 |
| } |
| 82 |
|
| |
| 83 |
|
| /* The steps of one stone, judging nothing but the geometry. Returns how many |
| 84 |
|
| * there are and writes at most `max`. */ |
| 85 |
69191598 |
| static int fn_steps_of(const struct fn_board *b, int sq, struct fn_step *out, int max) |
| 86 |
|
| { |
| 87 |
69191598 |
| int stone = AT(b, sq); |
| 88 |
69191598 |
| int foe, dir, n = 0; |
| 89 |
69191598 |
▶ | if (!FN_IS_STONE(stone)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!FN_IS_STONE(stone)) return 0; |
32354676 |
36836922 |
69191598 |
| if (!FN_IS_STONE(stone)) return 0; |
0 |
32354676 |
32354676 |
|
| 90 |
69191598 |
| foe = FN_OTHER(stone); |
| 91 |
622724382 |
▶ | for (dir = 0; dir < FN_DIRS; dir++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (dir = 0; dir < FN_DIRS; dir++) { |
553532784 |
69191598 |
622724382 |
|
| 92 |
|
| int ahead[FN_LINE_MAX], behind[FN_LINE_MAX]; |
| 93 |
|
| int a, w; |
| 94 |
553532784 |
| int to = NEXT(b, sq, dir); |
| 95 |
553532784 |
▶ | if (to < 0 || AT(b, to) != FN_EMPTY) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (to < 0 || AT(b, to) != FN_EMPTY) continue; |
344925019 |
208607765 |
553532784 |
| if (to < 0 || AT(b, to) != FN_EMPTY) continue; |
56506256 |
288418763 |
344925019 |
|
| 96 |
288418763 |
| a = line_from(b, NEXT(b, to, dir), dir, foe, ahead); |
| 97 |
288418763 |
| w = line_from(b, NEXT(b, sq, (dir + 4) % FN_DIRS), (dir + 4) % FN_DIRS, foe, behind); |
| 98 |
288418763 |
▶ | if (a) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (a) { |
10754016 |
277664747 |
288418763 |
|
| 99 |
10754016 |
▶ | if (out && n < max) step_fill(&out[n], sq, to, dir, FN_APPROACH, ahead, a); |
Branch: 2 branches
| Branch | True | False | Total |
| if (out && n < max) step_fill(&out[n], sq, to, dir, FN_APPROACH, ahead, a); |
10754016 |
0 |
10754016 |
| if (out && n < max) step_fill(&out[n], sq, to, dir, FN_APPROACH, ahead, a); |
10754016 |
0 |
10754016 |
|
| 100 |
10754016 |
| n++; |
| 101 |
|
| } |
| 102 |
288418763 |
▶ | if (w) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (w) { |
6447298 |
281971465 |
288418763 |
|
| 103 |
6447298 |
▶ | if (out && n < max) step_fill(&out[n], sq, to, dir, FN_WITHDRAWAL, behind, w); |
Branch: 2 branches
| Branch | True | False | Total |
| if (out && n < max) step_fill(&out[n], sq, to, dir, FN_WITHDRAWAL, behind, w); |
6447298 |
0 |
6447298 |
| if (out && n < max) step_fill(&out[n], sq, to, dir, FN_WITHDRAWAL, behind, w); |
6447298 |
0 |
6447298 |
|
| 104 |
6447298 |
| n++; |
| 105 |
|
| } |
| 106 |
288418763 |
▶ | if (!a && !w) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (!a && !w) { |
277664747 |
10754016 |
288418763 |
| if (!a && !w) { |
271760700 |
5904047 |
277664747 |
|
| 107 |
271760700 |
▶ | if (out && n < max) step_fill(&out[n], sq, to, dir, FN_PAIKA, ahead, 0); |
Branch: 2 branches
| Branch | True | False | Total |
| if (out && n < max) step_fill(&out[n], sq, to, dir, FN_PAIKA, ahead, 0); |
271760700 |
0 |
271760700 |
| if (out && n < max) step_fill(&out[n], sq, to, dir, FN_PAIKA, ahead, 0); |
271760700 |
0 |
271760700 |
|
| 108 |
271760700 |
| n++; |
| 109 |
|
| } |
| 110 |
|
| } |
| 111 |
69191598 |
| return n; |
| 112 |
|
| } |
| 113 |
|
| |
| 114 |
22525942 |
| static int fn_can_capture(const struct fn_board *b, int side) |
| 115 |
|
| { |
| 116 |
22525942 |
| const struct fn_abi *e = E(); |
| 117 |
|
| struct fn_step tmp[STONE_STEPS_MAX]; |
| 118 |
|
| int rank, file, i, n; |
| 119 |
22525942 |
▶ | if (!FN_IS_STONE(side)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!FN_IS_STONE(side)) return 0; |
11539356 |
10986586 |
22525942 |
| if (!FN_IS_STONE(side)) return 0; |
1 |
11539355 |
11539356 |
|
| 120 |
123020895 |
▶ | for (rank = 0; rank < e->rows(b); rank++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (rank = 0; rank < e->rows(b); rank++) { |
104284803 |
18736092 |
123020895 |
|
| 121 |
1022960652 |
▶ | for (file = 0; file < e->cols(b); file++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (file = 0; file < e->cols(b); file++) { |
922465698 |
100494954 |
1022960652 |
|
| 122 |
922465698 |
| int sq = e->square_of(file, rank); |
| 123 |
922465698 |
▶ | if (AT(b, sq) != side) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (AT(b, sq) != side) continue; |
889144058 |
33321640 |
922465698 |
|
| 124 |
33321640 |
| n = fn_steps_of(b, sq, tmp, STONE_STEPS_MAX); |
| 125 |
177569251 |
▶ | for (i = 0; i < n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) |
148037460 |
29531791 |
177569251 |
|
| 126 |
148037460 |
▶ | if (tmp[i].kind != FN_PAIKA) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (tmp[i].kind != FN_PAIKA) return 1; |
3789849 |
144247611 |
148037460 |
|
| 127 |
|
| } |
| 128 |
|
| } |
| 129 |
18736092 |
| return 0; |
| 130 |
|
| } |
| 131 |
|
| |
| 132 |
|
| /* THE OBLIGATION. Every step of every stone is found first; if one of them |
| 133 |
|
| * takes, the paikas are dropped from the list, wherever they stand. */ |
| 134 |
14151933 |
| static int fn_gen_steps(const struct fn_board *b, struct fn_step *out, int max) |
| 135 |
|
| { |
| 136 |
14151933 |
| const struct fn_abi *e = E(); |
| 137 |
|
| struct fn_step tmp[STONE_STEPS_MAX]; |
| 138 |
14151933 |
| int side = e->side(b); |
| 139 |
14151933 |
| int must = fn_can_capture(b, side); |
| 140 |
14151933 |
| int rank, file, i, n, found = 0; |
| 141 |
84865372 |
▶ | for (rank = 0; rank < e->rows(b); rank++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (rank = 0; rank < e->rows(b); rank++) { |
70713439 |
14151933 |
84865372 |
|
| 142 |
706650742 |
▶ | for (file = 0; file < e->cols(b); file++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (file = 0; file < e->cols(b); file++) { |
635937303 |
70713439 |
706650742 |
|
| 143 |
635937303 |
| int sq = e->square_of(file, rank); |
| 144 |
635937303 |
▶ | if (AT(b, sq) != side) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (AT(b, sq) != side) continue; |
603856523 |
32080780 |
635937303 |
|
| 145 |
32080780 |
| n = fn_steps_of(b, sq, tmp, STONE_STEPS_MAX); |
| 146 |
146199573 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
114118793 |
32080780 |
146199573 |
|
| 147 |
114118793 |
▶ | if (must && tmp[i].kind == FN_PAIKA) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (must && tmp[i].kind == FN_PAIKA) continue; |
49684883 |
64433910 |
114118793 |
| if (must && tmp[i].kind == FN_PAIKA) continue; |
40010791 |
9674092 |
49684883 |
|
| 148 |
74108002 |
▶ | if (out && found < max) out[found] = tmp[i]; |
Branch: 2 branches
| Branch | True | False | Total |
| if (out && found < max) out[found] = tmp[i]; |
33771996 |
40336006 |
74108002 |
| if (out && found < max) out[found] = tmp[i]; |
33771996 |
0 |
33771996 |
|
| 149 |
74108002 |
| found++; |
| 150 |
|
| } |
| 151 |
|
| } |
| 152 |
|
| } |
| 153 |
14151933 |
| return found; |
| 154 |
|
| } |
| 155 |
|
| |
| 156 |
27704633 |
| static int fn_step_build(const struct fn_board *b, int from, int dir, int kind, struct fn_step *out) |
| 157 |
|
| { |
| 158 |
|
| int ahead[FN_LINE_MAX]; |
| 159 |
27704633 |
| int stone, to, n = 0; |
| 160 |
27704633 |
▶ | if (dir < 0 || dir >= FN_DIRS) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (dir < 0 || dir >= FN_DIRS) return 0; |
27704633 |
0 |
27704633 |
| if (dir < 0 || dir >= FN_DIRS) return 0; |
1 |
27704632 |
27704633 |
|
| 161 |
27704632 |
▶ | if (kind != FN_PAIKA && kind != FN_APPROACH && kind != FN_WITHDRAWAL) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (kind != FN_PAIKA && kind != FN_APPROACH && kind != FN_WITHDRAWAL) return 0; |
19328733 |
8375899 |
27704632 |
| if (kind != FN_PAIKA && kind != FN_APPROACH && kind != FN_WITHDRAWAL) return 0; |
9345020 |
9983713 |
19328733 |
| if (kind != FN_PAIKA && kind != FN_APPROACH && kind != FN_WITHDRAWAL) return 0; |
1 |
9345019 |
9345020 |
|
| 162 |
27704631 |
| stone = AT(b, from); |
| 163 |
27704631 |
▶ | if (!FN_IS_STONE(stone)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!FN_IS_STONE(stone)) return 0; |
11853875 |
15850756 |
27704631 |
| if (!FN_IS_STONE(stone)) return 0; |
3 |
11853872 |
11853875 |
|
| 164 |
27704628 |
| to = NEXT(b, from, dir); |
| 165 |
27704628 |
▶ | if (to < 0 || AT(b, to) != FN_EMPTY) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (to < 0 || AT(b, to) != FN_EMPTY) return 0; |
27704626 |
2 |
27704628 |
| if (to < 0 || AT(b, to) != FN_EMPTY) return 0; |
1 |
27704625 |
27704626 |
|
| 166 |
27704625 |
▶ | if (kind == FN_APPROACH) |
Branch: 1 branch
| Branch | True | False | Total |
| if (kind == FN_APPROACH) |
9983713 |
17720912 |
27704625 |
|
| 167 |
9983713 |
| n = line_from(b, NEXT(b, to, dir), dir, FN_OTHER(stone), ahead); |
| 168 |
17720912 |
▶ | else if (kind == FN_WITHDRAWAL) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (kind == FN_WITHDRAWAL) |
9345019 |
8375893 |
17720912 |
|
| 169 |
9345019 |
| n = line_from(b, NEXT(b, from, (dir + 4) % FN_DIRS), (dir + 4) % FN_DIRS, FN_OTHER(stone), ahead); |
| 170 |
27704625 |
▶ | if (kind != FN_PAIKA && !n) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (kind != FN_PAIKA && !n) return 0; |
19328732 |
8375893 |
27704625 |
| if (kind != FN_PAIKA && !n) return 0; |
16750913 |
2577819 |
19328732 |
|
| 171 |
10953712 |
▶ | if (out) step_fill(out, from, to, dir, kind, ahead, n); |
Branch: 1 branch
| Branch | True | False | Total |
| if (out) step_fill(out, from, to, dir, kind, ahead, n); |
10937339 |
16373 |
10953712 |
|
| 172 |
10953712 |
| return 1; |
| 173 |
|
| } |
| 174 |
|
| |
| 175 |
10102855 |
| static int fn_step_why(const struct fn_board *b, int from, int dir, int kind, struct fn_step *out) |
| 176 |
|
| { |
| 177 |
10102855 |
| const struct fn_abi *e = E(); |
| 178 |
|
| struct fn_step either; |
| 179 |
|
| int stone, to, takes; |
| 180 |
10102855 |
▶ | if (!e->on_board(b, from)) return FN_WHY_OFF_BOARD; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e->on_board(b, from)) return FN_WHY_OFF_BOARD; |
1 |
10102854 |
10102855 |
|
| 181 |
10102854 |
▶ | if (dir < 0 || dir >= FN_DIRS) return FN_WHY_BAD_STEP; |
Branch: 2 branches
| Branch | True | False | Total |
| if (dir < 0 || dir >= FN_DIRS) return FN_WHY_BAD_STEP; |
10102854 |
0 |
10102854 |
| if (dir < 0 || dir >= FN_DIRS) return FN_WHY_BAD_STEP; |
1 |
10102853 |
10102854 |
|
| 182 |
10102853 |
▶ | if (kind != FN_PAIKA && kind != FN_APPROACH && kind != FN_WITHDRAWAL) return FN_WHY_BAD_STEP; |
Branch: 3 branches
| Branch | True | False | Total |
| if (kind != FN_PAIKA && kind != FN_APPROACH && kind != FN_WITHDRAWAL) return FN_WHY_BAD_STEP; |
1706418 |
8396435 |
10102853 |
| if (kind != FN_PAIKA && kind != FN_APPROACH && kind != FN_WITHDRAWAL) return FN_WHY_BAD_STEP; |
585972 |
1120446 |
1706418 |
| if (kind != FN_PAIKA && kind != FN_APPROACH && kind != FN_WITHDRAWAL) return FN_WHY_BAD_STEP; |
1 |
585971 |
585972 |
|
| 183 |
10102852 |
| stone = AT(b, from); |
| 184 |
10102852 |
▶ | if (!FN_IS_STONE(stone)) return FN_WHY_NO_STONE; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!FN_IS_STONE(stone)) return FN_WHY_NO_STONE; |
4429342 |
5673510 |
10102852 |
| if (!FN_IS_STONE(stone)) return FN_WHY_NO_STONE; |
49828 |
4379514 |
4429342 |
|
| 185 |
10053024 |
▶ | if (stone != e->side(b)) return FN_WHY_NOT_YOURS; |
Branch: 1 branch
| Branch | True | False | Total |
| if (stone != e->side(b)) return FN_WHY_NOT_YOURS; |
10591 |
10042433 |
10053024 |
|
| 186 |
10042433 |
| to = NEXT(b, from, dir); |
| 187 |
10042433 |
▶ | if (to < 0) return FN_WHY_NO_LINE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (to < 0) return FN_WHY_NO_LINE; |
4056 |
10038377 |
10042433 |
|
| 188 |
10038377 |
▶ | if (AT(b, to) != FN_EMPTY) return FN_WHY_OCCUPIED; |
Branch: 1 branch
| Branch | True | False | Total |
| if (AT(b, to) != FN_EMPTY) return FN_WHY_OCCUPIED; |
1865 |
10036512 |
10038377 |
|
| 189 |
10036512 |
▶ | if (kind != FN_PAIKA) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (kind != FN_PAIKA) { |
1662195 |
8374317 |
10036512 |
|
| 190 |
1662195 |
▶ | if (!fn_step_build(b, from, dir, kind, out)) return FN_WHY_TAKES_NOTHING; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!fn_step_build(b, from, dir, kind, out)) return FN_WHY_TAKES_NOTHING; |
2262 |
1659933 |
1662195 |
|
| 191 |
1659933 |
| return FN_WHY_OK; |
| 192 |
|
| } |
| 193 |
8374317 |
| takes = fn_step_build(b, from, dir, FN_APPROACH, &either) |
| 194 |
8374317 |
▶ | || fn_step_build(b, from, dir, FN_WITHDRAWAL, &either); |
Branch: 2 branches
| Branch | True | False | Total |
| || fn_step_build(b, from, dir, FN_WITHDRAWAL, &either); |
8374134 |
183 |
8374317 |
| || fn_step_build(b, from, dir, FN_WITHDRAWAL, &either); |
134 |
8374000 |
8374134 |
|
| 195 |
8374317 |
▶ | if (takes) return FN_WHY_NOT_A_PAIKA; |
Branch: 1 branch
| Branch | True | False | Total |
| if (takes) return FN_WHY_NOT_A_PAIKA; |
317 |
8374000 |
8374317 |
|
| 196 |
8374000 |
▶ | if (fn_can_capture(b, stone)) return FN_WHY_MUST_CAPTURE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (fn_can_capture(b, stone)) return FN_WHY_MUST_CAPTURE; |
921 |
8373079 |
8374000 |
|
| 197 |
8373079 |
| fn_step_build(b, from, dir, FN_PAIKA, out); |
| 198 |
8373079 |
| return FN_WHY_OK; |
| 199 |
|
| } |
| 200 |
|
| |
| 201 |
|
| /* ---- do and undo ---------------------------------------------------------------- |
| 202 |
|
| * |
| 203 |
|
| * do_step takes the step at its word about what is taken, and records what |
| 204 |
|
| * actually stood on each point so that undo puts back exactly that. |
| 205 |
|
| */ |
| 206 |
|
| |
| 207 |
15703548 |
| static int fn_do_step(struct fn_board *b, const struct fn_step *s, struct fn_undo *u) |
| 208 |
|
| { |
| 209 |
15703548 |
| const struct fn_abi *e = E(); |
| 210 |
15703548 |
| int stone, i, n, removed = 0; |
| 211 |
15703548 |
▶ | if (u) memset(u, 0, sizeof(*u)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (u) memset(u, 0, sizeof(*u)); |
15703548 |
0 |
15703548 |
|
| 212 |
15703548 |
▶ | if (!s) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!s) return -1; |
0 |
15703548 |
15703548 |
|
| 213 |
15703548 |
| stone = AT(b, s->from); |
| 214 |
15703548 |
▶ | if (!FN_IS_STONE(stone) || AT(b, s->to) != FN_EMPTY) return -1; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!FN_IS_STONE(stone) || AT(b, s->to) != FN_EMPTY) return -1; |
6967246 |
8736302 |
15703548 |
| if (!FN_IS_STONE(stone) || AT(b, s->to) != FN_EMPTY) return -1; |
6967246 |
0 |
6967246 |
| if (!FN_IS_STONE(stone) || AT(b, s->to) != FN_EMPTY) return -1; |
0 |
15703548 |
15703548 |
|
| 215 |
15703548 |
| n = s->taken_n; |
| 216 |
15703548 |
▶ | if (n < 0) n = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 0) n = 0; |
0 |
15703548 |
15703548 |
|
| 217 |
15703548 |
▶ | if (n > FN_LINE_MAX) n = FN_LINE_MAX; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > FN_LINE_MAX) n = FN_LINE_MAX; |
0 |
15703548 |
15703548 |
|
| 218 |
15703548 |
▶ | if (u) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (u) { |
15703548 |
0 |
15703548 |
|
| 219 |
15703548 |
| u->step = *s; |
| 220 |
15703548 |
| u->step.taken_n = n; |
| 221 |
15703548 |
| u->did = 1; |
| 222 |
15703548 |
| u->stone = stone; |
| 223 |
15703548 |
| u->side = e->side(b); |
| 224 |
15703548 |
| u->key = e->key(b); |
| 225 |
|
| } |
| 226 |
15703548 |
| e->lift(b, s->from); |
| 227 |
15703548 |
| e->put(b, s->to, stone); |
| 228 |
19848415 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
4144867 |
15703548 |
19848415 |
|
| 229 |
4144867 |
| int was = AT(b, s->taken[i]); |
| 230 |
4144867 |
▶ | if (u) u->was[i] = FN_IS_STONE(was) ? was : FN_EMPTY; |
Branch: 3 branches
| Branch | True | False | Total |
| if (u) u->was[i] = FN_IS_STONE(was) ? was : FN_EMPTY; |
4144867 |
0 |
4144867 |
| if (u) u->was[i] = FN_IS_STONE(was) ? was : FN_EMPTY; |
2157729 |
1987138 |
4144867 |
| if (u) u->was[i] = FN_IS_STONE(was) ? was : FN_EMPTY; |
2157729 |
0 |
2157729 |
|
| 231 |
4144867 |
▶ | if (!FN_IS_STONE(was)) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!FN_IS_STONE(was)) continue; |
2157729 |
1987138 |
4144867 |
| if (!FN_IS_STONE(was)) continue; |
0 |
2157729 |
2157729 |
|
| 232 |
4144867 |
| e->lift(b, s->taken[i]); |
| 233 |
4144867 |
| removed++; |
| 234 |
|
| } |
| 235 |
15703548 |
| e->set_side(b, FN_OTHER(e->side(b))); |
| 236 |
15703548 |
| return removed; |
| 237 |
|
| } |
| 238 |
|
| |
| 239 |
15625767 |
| static void fn_undo_step(struct fn_board *b, const struct fn_undo *u) |
| 240 |
|
| { |
| 241 |
15625767 |
| const struct fn_abi *e = E(); |
| 242 |
|
| int i; |
| 243 |
15625767 |
▶ | if (!u || !u->did) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!u || !u->did) return; |
15625767 |
0 |
15625767 |
| if (!u || !u->did) return; |
0 |
15625767 |
15625767 |
|
| 244 |
15625767 |
| e->set_side(b, u->side); |
| 245 |
19691866 |
▶ | for (i = 0; i < u->step.taken_n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < u->step.taken_n; i++) |
4066099 |
15625767 |
19691866 |
|
| 246 |
4066099 |
▶ | if (FN_IS_STONE(u->was[i])) e->put(b, u->step.taken[i], u->was[i]); |
Branch: 2 branches
| Branch | True | False | Total |
| if (FN_IS_STONE(u->was[i])) e->put(b, u->step.taken[i], u->was[i]); |
2117657 |
1948442 |
4066099 |
| if (FN_IS_STONE(u->was[i])) e->put(b, u->step.taken[i], u->was[i]); |
2117657 |
0 |
2117657 |
|
| 247 |
15625767 |
| e->lift(b, u->step.to); |
| 248 |
15625767 |
| e->put(b, u->step.from, u->stone); |
| 249 |
|
| } |
| 250 |
|
| |
| 251 |
1824 |
| static unsigned long long fn_perft_steps(struct fn_board *b, int depth) |
| 252 |
|
| { |
| 253 |
|
| struct fn_step list[FN_STEPS_MAX]; |
| 254 |
|
| struct fn_undo u; |
| 255 |
1824 |
| unsigned long long total = 0; |
| 256 |
|
| int n, i; |
| 257 |
1824 |
▶ | if (depth <= 0) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (depth <= 0) return 1; |
1 |
1823 |
1824 |
|
| 258 |
1823 |
| n = fn_gen_steps(b, list, FN_STEPS_MAX); |
| 259 |
1823 |
▶ | if (depth == 1) return (unsigned long long) n; |
Branch: 1 branch
| Branch | True | False | Total |
| if (depth == 1) return (unsigned long long) n; |
1523 |
300 |
1823 |
|
| 260 |
2114 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
1814 |
300 |
2114 |
|
| 261 |
1814 |
| fn_do_step(b, &list[i], &u); |
| 262 |
1814 |
| total += fn_perft_steps(b, depth - 1); |
| 263 |
1814 |
| fn_undo_step(b, &u); |
| 264 |
|
| } |
| 265 |
300 |
| return total; |
| 266 |
|
| } |
| 267 |
|
| |
| 268 |
|
| /* ---- the chain, version 3 --------------------------------------------------------- |
| 269 |
|
| * |
| 270 |
|
| * THE RULES, quoted from the same sources: |
| 271 |
|
| * |
| 272 |
|
| * "it is allowed to continue capturing with the same piece ... (Even if a |
| 273 |
|
| * move consists of multiple movements of one single piece it still counts |
| 274 |
|
| * as a single move.)" |
| 275 |
|
| * "the player may stop capturing after any number of opponent pieces are |
| 276 |
|
| * captured." |
| 277 |
|
| * "It is not allowed to make a capturing move in the same direction as the |
| 278 |
|
| * capturing move directly before ... A player is allowed to play a |
| 279 |
|
| * capturing sequence two times into the same direction if a capturing |
| 280 |
|
| * movement in another direction is done in between. The last movement |
| 281 |
|
| * direction of the capturing move in the previous turn ... does not |
| 282 |
|
| * influence possible capturing directions in the current turn." |
| 283 |
|
| * "The current capturing piece is not allowed to arrive on the same |
| 284 |
|
| * intersection twice during a capturing sequence." The source's own |
| 285 |
|
| * example refuses a return to the point the chain STARTED from. |
| 286 |
|
| * "you must keep moving the same piece" |
| 287 |
|
| * |
| 288 |
|
| * and one reading: every step of a chain after the first must take. A stone |
| 289 |
|
| * that has taken may stop; it may not then make a paika. |
| 290 |
|
| * |
| 291 |
|
| * THE DIRECTION RULE LOOKS BACK ONE STEP AND NO FURTHER, and nothing of a |
| 292 |
|
| * chain outlives it: closing clears all three fields. |
| 293 |
|
| */ |
| 294 |
|
| |
| 295 |
5028011 |
| static unsigned long long bit_of(int sq) |
| 296 |
|
| { |
| 297 |
5028011 |
| int bit = (E())->point_bit(sq); |
| 298 |
5028011 |
▶ | return bit < 0 ? 0 : (1ULL << bit); |
Branch: 1 branch
| Branch | True | False | Total |
| return bit < 0 ? 0 : (1ULL << bit); |
5028011 |
0 |
5028011 |
|
| 299 |
|
| } |
| 300 |
|
| |
| 301 |
|
| /* The steps that may follow in an open chain. */ |
| 302 |
3789172 |
| static int chain_steps(const struct fn_board *b, struct fn_step *out, int max) |
| 303 |
|
| { |
| 304 |
3789172 |
| const struct fn_abi *e = E(); |
| 305 |
|
| struct fn_step tmp[STONE_STEPS_MAX]; |
| 306 |
3789172 |
| int at = e->chain_at(b); |
| 307 |
3789172 |
| int last = e->chain_dir(b); |
| 308 |
3789172 |
| int i, n, found = 0; |
| 309 |
3789172 |
▶ | if (at < 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (at < 0) return 0; |
0 |
3789172 |
3789172 |
|
| 310 |
3789172 |
| n = fn_steps_of(b, at, tmp, STONE_STEPS_MAX); |
| 311 |
23396849 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
19607677 |
3789172 |
23396849 |
|
| 312 |
19607677 |
▶ | if (tmp[i].kind == FN_PAIKA) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (tmp[i].kind == FN_PAIKA) continue; |
16327591 |
3280086 |
19607677 |
|
| 313 |
3280086 |
▶ | if (tmp[i].dir == last) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (tmp[i].dir == last) continue; |
77191 |
3202895 |
3280086 |
|
| 314 |
3202895 |
▶ | if (e->chain_seen(b, tmp[i].to)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->chain_seen(b, tmp[i].to)) continue; |
477013 |
2725882 |
3202895 |
|
| 315 |
2725882 |
▶ | if (out && found < max) out[found] = tmp[i]; |
Branch: 2 branches
| Branch | True | False | Total |
| if (out && found < max) out[found] = tmp[i]; |
1036428 |
1689454 |
2725882 |
| if (out && found < max) out[found] = tmp[i]; |
1036428 |
0 |
1036428 |
|
| 316 |
2725882 |
| found++; |
| 317 |
|
| } |
| 318 |
3789172 |
| return found; |
| 319 |
|
| } |
| 320 |
|
| |
| 321 |
13866773 |
| static int fn_play_steps(const struct fn_board *b, struct fn_step *out, int max) |
| 322 |
|
| { |
| 323 |
13866773 |
▶ | if ((E())->chain_at(b) >= 0) return chain_steps(b, out, max); |
Branch: 1 branch
| Branch | True | False | Total |
| if ((E())->chain_at(b) >= 0) return chain_steps(b, out, max); |
647322 |
13219451 |
13866773 |
|
| 324 |
13219451 |
| return fn_gen_steps(b, out, max); |
| 325 |
|
| } |
| 326 |
|
| |
| 327 |
101512 |
| static int fn_can_stop(const struct fn_board *b) |
| 328 |
|
| { |
| 329 |
101512 |
| return (E())->chain_at(b) >= 0; |
| 330 |
|
| } |
| 331 |
|
| |
| 332 |
11004818 |
| static int fn_play_why(const struct fn_board *b, int from, int dir, int kind, struct fn_step *out) |
| 333 |
|
| { |
| 334 |
11004818 |
| const struct fn_abi *e = E(); |
| 335 |
|
| struct fn_step either; |
| 336 |
11004818 |
| int at = e->chain_at(b); |
| 337 |
|
| int stone, to; |
| 338 |
11004818 |
▶ | if (at < 0) return fn_step_why(b, from, dir, kind, out); |
Branch: 1 branch
| Branch | True | False | Total |
| if (at < 0) return fn_step_why(b, from, dir, kind, out); |
10038025 |
966793 |
11004818 |
|
| 339 |
966793 |
▶ | if (!e->on_board(b, from)) return FN_WHY_OFF_BOARD; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!e->on_board(b, from)) return FN_WHY_OFF_BOARD; |
0 |
966793 |
966793 |
|
| 340 |
966793 |
▶ | if (dir < 0 || dir >= FN_DIRS) return FN_WHY_BAD_STEP; |
Branch: 2 branches
| Branch | True | False | Total |
| if (dir < 0 || dir >= FN_DIRS) return FN_WHY_BAD_STEP; |
966793 |
0 |
966793 |
| if (dir < 0 || dir >= FN_DIRS) return FN_WHY_BAD_STEP; |
0 |
966793 |
966793 |
|
| 341 |
966793 |
▶ | if (kind != FN_PAIKA && kind != FN_APPROACH && kind != FN_WITHDRAWAL) return FN_WHY_BAD_STEP; |
Branch: 3 branches
| Branch | True | False | Total |
| if (kind != FN_PAIKA && kind != FN_APPROACH && kind != FN_WITHDRAWAL) return FN_WHY_BAD_STEP; |
942307 |
24486 |
966793 |
| if (kind != FN_PAIKA && kind != FN_APPROACH && kind != FN_WITHDRAWAL) return FN_WHY_BAD_STEP; |
420482 |
521825 |
942307 |
| if (kind != FN_PAIKA && kind != FN_APPROACH && kind != FN_WITHDRAWAL) return FN_WHY_BAD_STEP; |
0 |
420482 |
420482 |
|
| 342 |
966793 |
| stone = AT(b, from); |
| 343 |
966793 |
▶ | if (!FN_IS_STONE(stone)) return FN_WHY_NO_STONE; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!FN_IS_STONE(stone)) return FN_WHY_NO_STONE; |
447224 |
519569 |
966793 |
| if (!FN_IS_STONE(stone)) return FN_WHY_NO_STONE; |
39481 |
407743 |
447224 |
|
| 344 |
927312 |
▶ | if (stone != e->side(b)) return FN_WHY_NOT_YOURS; |
Branch: 1 branch
| Branch | True | False | Total |
| if (stone != e->side(b)) return FN_WHY_NOT_YOURS; |
20521 |
906791 |
927312 |
|
| 345 |
906791 |
▶ | if (from != at) return FN_WHY_NOT_THE_STONE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (from != at) return FN_WHY_NOT_THE_STONE; |
11810 |
894981 |
906791 |
|
| 346 |
894981 |
| to = NEXT(b, from, dir); |
| 347 |
894981 |
▶ | if (to < 0) return FN_WHY_NO_LINE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (to < 0) return FN_WHY_NO_LINE; |
277 |
894704 |
894981 |
|
| 348 |
894704 |
▶ | if (AT(b, to) != FN_EMPTY) return FN_WHY_OCCUPIED; |
Branch: 1 branch
| Branch | True | False | Total |
| if (AT(b, to) != FN_EMPTY) return FN_WHY_OCCUPIED; |
348 |
894356 |
894704 |
|
| 349 |
894356 |
▶ | if (dir == e->chain_dir(b)) return FN_WHY_SAME_DIRECTION; |
Branch: 1 branch
| Branch | True | False | Total |
| if (dir == e->chain_dir(b)) return FN_WHY_SAME_DIRECTION; |
179 |
894177 |
894356 |
|
| 350 |
894177 |
▶ | if (e->chain_seen(b, to)) return FN_WHY_REVISIT; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->chain_seen(b, to)) return FN_WHY_REVISIT; |
249 |
893928 |
894177 |
|
| 351 |
893928 |
▶ | if (kind == FN_PAIKA) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (kind == FN_PAIKA) { |
201 |
893727 |
893928 |
|
| 352 |
201 |
▶ | if (fn_step_build(b, from, dir, FN_APPROACH, &either) |
Branch: 1 branch
| Branch | True | False | Total |
| if (fn_step_build(b, from, dir, FN_APPROACH, &either) |
134 |
67 |
201 |
|
| 353 |
201 |
▶ | || fn_step_build(b, from, dir, FN_WITHDRAWAL, &either)) return FN_WHY_NOT_A_PAIKA; |
Branch: 1 branch
| Branch | True | False | Total |
| || fn_step_build(b, from, dir, FN_WITHDRAWAL, &either)) return FN_WHY_NOT_A_PAIKA; |
37 |
97 |
134 |
|
| 354 |
97 |
| return FN_WHY_PAIKA_IN_CHAIN; |
| 355 |
|
| } |
| 356 |
893727 |
▶ | if (!fn_step_build(b, from, dir, kind, out)) return FN_WHY_TAKES_NOTHING; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!fn_step_build(b, from, dir, kind, out)) return FN_WHY_TAKES_NOTHING; |
285 |
893442 |
893727 |
|
| 357 |
893442 |
| return FN_WHY_OK; |
| 358 |
|
| } |
| 359 |
|
| |
| 360 |
104323 |
| static void undo_keep_chain(const struct fn_board *b, struct fn_undo *u, int stop) |
| 361 |
|
| { |
| 362 |
104323 |
| const struct fn_abi *e = E(); |
| 363 |
104323 |
▶ | if (!u) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!u) return; |
0 |
104323 |
104323 |
|
| 364 |
104323 |
| u->reserved[FN_UNDO_PLAYED] = 1; |
| 365 |
104323 |
| u->reserved[FN_UNDO_AT] = e->chain_at(b); |
| 366 |
104323 |
| u->reserved[FN_UNDO_DIR] = e->chain_dir(b); |
| 367 |
104323 |
| u->reserved[FN_UNDO_STOP] = stop; |
| 368 |
104323 |
| u->reserved64[FN_UNDO_SEEN] = e->chain_mask(b); |
| 369 |
|
| } |
| 370 |
|
| |
| 371 |
15696721 |
| static int fn_play_step(struct fn_board *b, const struct fn_step *s, struct fn_undo *u) |
| 372 |
|
| { |
| 373 |
15696721 |
| const struct fn_abi *e = E(); |
| 374 |
|
| struct fn_undo local; |
| 375 |
|
| unsigned long long seen; |
| 376 |
|
| int mover, was_open, removed; |
| 377 |
15696721 |
▶ | if (!u) u = &local; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!u) u = &local; |
0 |
15696721 |
15696721 |
|
| 378 |
15696721 |
| mover = e->side(b); |
| 379 |
15696721 |
| was_open = e->chain_at(b) >= 0; |
| 380 |
15696721 |
| seen = e->chain_mask(b); |
| 381 |
|
| { |
| 382 |
|
| /* the chain as it was, before do_step clears the record */ |
| 383 |
15696721 |
| int at = e->chain_at(b), dir = e->chain_dir(b); |
| 384 |
15696721 |
| removed = fn_do_step(b, s, u); |
| 385 |
15696721 |
▶ | if (removed < 0) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (removed < 0) return -1; |
0 |
15696721 |
15696721 |
|
| 386 |
15696721 |
| u->reserved[FN_UNDO_PLAYED] = 1; |
| 387 |
15696721 |
| u->reserved[FN_UNDO_AT] = at; |
| 388 |
15696721 |
| u->reserved[FN_UNDO_DIR] = dir; |
| 389 |
15696721 |
| u->reserved[FN_UNDO_STOP] = 0; |
| 390 |
15696721 |
| u->reserved64[FN_UNDO_SEEN] = seen; |
| 391 |
|
| } |
| 392 |
15696721 |
▶ | if (removed == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (removed == 0) { |
12653894 |
3042827 |
15696721 |
|
| 393 |
|
| /* a paika: do_step has passed the turn, and no chain survives one */ |
| 394 |
12653894 |
| e->chain_set(b, -1, -1, 0); |
| 395 |
12653894 |
| return 0; |
| 396 |
|
| } |
| 397 |
|
| /* it took: the turn stays with the mover and the chain is on this stone */ |
| 398 |
3042827 |
| e->set_side(b, mover); |
| 399 |
3042827 |
▶ | if (!was_open) seen = bit_of(s->from); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!was_open) seen = bit_of(s->from); |
1985184 |
1057643 |
3042827 |
|
| 400 |
3042827 |
| seen |= bit_of(s->to); |
| 401 |
3042827 |
| e->chain_set(b, s->to, s->dir, seen); |
| 402 |
3042827 |
▶ | if (chain_steps(b, NULL, 0) == 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (chain_steps(b, NULL, 0) == 0) { |
1979445 |
1063382 |
3042827 |
|
| 403 |
1979445 |
| e->chain_set(b, -1, -1, 0); |
| 404 |
1979445 |
| e->set_side(b, FN_OTHER(mover)); |
| 405 |
|
| } |
| 406 |
3042827 |
| return removed; |
| 407 |
|
| } |
| 408 |
|
| |
| 409 |
104324 |
| static int fn_play_stop(struct fn_board *b, struct fn_undo *u) |
| 410 |
|
| { |
| 411 |
104324 |
| const struct fn_abi *e = E(); |
| 412 |
104324 |
▶ | if (u) memset(u, 0, sizeof(*u)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (u) memset(u, 0, sizeof(*u)); |
104324 |
0 |
104324 |
|
| 413 |
104324 |
▶ | if (e->chain_at(b) < 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->chain_at(b) < 0) return 0; |
1 |
104323 |
104324 |
|
| 414 |
104323 |
▶ | if (u) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (u) { |
104323 |
0 |
104323 |
|
| 415 |
104323 |
| u->did = 1; |
| 416 |
104323 |
| u->side = e->side(b); |
| 417 |
104323 |
| u->key = e->key(b); |
| 418 |
|
| } |
| 419 |
104323 |
| undo_keep_chain(b, u, 1); |
| 420 |
104323 |
| e->chain_set(b, -1, -1, 0); |
| 421 |
104323 |
| e->set_side(b, FN_OTHER(e->side(b))); |
| 422 |
104323 |
| return 1; |
| 423 |
|
| } |
| 424 |
|
| |
| 425 |
15719577 |
| static void fn_play_undo(struct fn_board *b, const struct fn_undo *u) |
| 426 |
|
| { |
| 427 |
15719577 |
| const struct fn_abi *e = E(); |
| 428 |
15719577 |
▶ | if (!u || !u->did) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!u || !u->did) return; |
15719577 |
0 |
15719577 |
| if (!u || !u->did) return; |
0 |
15719577 |
15719577 |
|
| 429 |
15719577 |
▶ | if (u->reserved[FN_UNDO_STOP]) e->set_side(b, u->side); |
Branch: 1 branch
| Branch | True | False | Total |
| if (u->reserved[FN_UNDO_STOP]) e->set_side(b, u->side); |
100630 |
15618947 |
15719577 |
|
| 430 |
15618947 |
| else fn_undo_step(b, u); |
| 431 |
15719577 |
| e->chain_set(b, u->reserved[FN_UNDO_AT], u->reserved[FN_UNDO_DIR], u->reserved64[FN_UNDO_SEEN]); |
| 432 |
|
| } |
| 433 |
|
| |
| 434 |
|
| /* ---- whole moves ------------------------------------------------------------------- |
| 435 |
|
| * |
| 436 |
|
| * A walk of the step tree, depth first. A move ends after every step: a paika |
| 437 |
|
| * is a move, a step that closed its own chain is a move, and a step that left |
| 438 |
|
| * the chain open is a move too, because the mover may stop there. So every |
| 439 |
|
| * node of the tree below the root is one move, and a chain's shorter |
| 440 |
|
| * beginnings are met before its longer ones. |
| 441 |
|
| */ |
| 442 |
|
| |
| 443 |
|
| struct walk { |
| 444 |
|
| struct fn_move cur; |
| 445 |
|
| struct fn_move *out; |
| 446 |
|
| int max; |
| 447 |
|
| unsigned long long found; |
| 448 |
|
| unsigned long long cap; |
| 449 |
|
| }; |
| 450 |
|
| |
| 451 |
1024064 |
| static void walk_from(struct fn_board *b, struct walk *w, struct fn_step *list, int n) |
| 452 |
|
| { |
| 453 |
1024064 |
| const struct fn_abi *e = E(); |
| 454 |
|
| struct fn_step next[STONE_STEPS_MAX]; |
| 455 |
|
| struct fn_undo u; |
| 456 |
|
| int i, m; |
| 457 |
5764859 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
4740814 |
1024045 |
5764859 |
|
| 458 |
4740814 |
▶ | if (w->cap && w->found >= w->cap) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (w->cap && w->found >= w->cap) return; |
4719041 |
21773 |
4740814 |
| if (w->cap && w->found >= w->cap) return; |
19 |
4719022 |
4719041 |
|
| 459 |
4740795 |
▶ | if (w->cur.n >= FN_CHAIN_MAX) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->cur.n >= FN_CHAIN_MAX) return; |
0 |
4740795 |
4740795 |
|
| 460 |
4740795 |
▶ | if (w->cur.n == 0) w->cur.from = list[i].from; |
Branch: 1 branch
| Branch | True | False | Total |
| if (w->cur.n == 0) w->cur.from = list[i].from; |
4597613 |
143182 |
4740795 |
|
| 461 |
4740795 |
| w->cur.dir[w->cur.n] = (unsigned char) list[i].dir; |
| 462 |
4740795 |
| w->cur.kind[w->cur.n] = (unsigned char) list[i].kind; |
| 463 |
4740795 |
| w->cur.n++; |
| 464 |
4740795 |
| w->cur.taken += list[i].taken_n; |
| 465 |
4740795 |
| fn_play_step(b, &list[i], &u); |
| 466 |
4740795 |
▶ | if (w->out && w->found < (unsigned long long) w->max) w->out[w->found] = w->cur; |
Branch: 2 branches
| Branch | True | False | Total |
| if (w->out && w->found < (unsigned long long) w->max) w->out[w->found] = w->cur; |
2358760 |
2382035 |
4740795 |
| if (w->out && w->found < (unsigned long long) w->max) w->out[w->found] = w->cur; |
2358760 |
0 |
2358760 |
|
| 467 |
4740795 |
| w->found++; |
| 468 |
4740795 |
▶ | if (e->chain_at(b) >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->chain_at(b) >= 0) { |
99023 |
4641772 |
4740795 |
|
| 469 |
99023 |
| m = chain_steps(b, next, STONE_STEPS_MAX); |
| 470 |
99023 |
| walk_from(b, w, next, m); |
| 471 |
|
| } |
| 472 |
4740795 |
| fn_play_undo(b, &u); |
| 473 |
4740795 |
| w->cur.taken -= list[i].taken_n; |
| 474 |
4740795 |
| w->cur.n--; |
| 475 |
|
| } |
| 476 |
|
| } |
| 477 |
|
| |
| 478 |
925041 |
| static unsigned long long walk_moves(struct fn_board *b, struct fn_move *out, int max, unsigned long long cap) |
| 479 |
|
| { |
| 480 |
|
| struct fn_step list[FN_STEPS_MAX]; |
| 481 |
|
| struct walk w; |
| 482 |
|
| int n; |
| 483 |
925041 |
| memset(&w, 0, sizeof(w)); |
| 484 |
925041 |
| w.out = out; |
| 485 |
925041 |
| w.max = max; |
| 486 |
925041 |
| w.cap = cap; |
| 487 |
925041 |
▶ | if ((E())->chain_at(b) >= 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((E())->chain_at(b) >= 0) return 0; |
0 |
925041 |
925041 |
|
| 488 |
925041 |
| n = fn_gen_steps(b, list, FN_STEPS_MAX); |
| 489 |
925041 |
| walk_from(b, &w, list, n); |
| 490 |
925041 |
| return w.found; |
| 491 |
|
| } |
| 492 |
|
| |
| 493 |
462738 |
| static unsigned long long fn_count_moves(struct fn_board *b, unsigned long long cap) |
| 494 |
|
| { |
| 495 |
462738 |
| return walk_moves(b, NULL, 0, cap); |
| 496 |
|
| } |
| 497 |
|
| |
| 498 |
|
| /* THE WALK STOPS ONE PAST `max`. A position can have more whole moves than |
| 499 |
|
| * anyone will wait for, so "how many there are" is answered only as far as |
| 500 |
|
| * the caller's buffer plus one: enough to know the buffer was short, and no |
| 501 |
|
| * further. count_moves is the call that counts. */ |
| 502 |
462303 |
| static int fn_gen_moves(struct fn_board *b, struct fn_move *out, int max) |
| 503 |
|
| { |
| 504 |
462303 |
▶ | if (max < 0) max = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (max < 0) max = 0; |
0 |
462303 |
462303 |
|
| 505 |
462303 |
| return (int) walk_moves(b, out, max, (unsigned long long) max + 1ULL); |
| 506 |
|
| } |
| 507 |
|
| |
| 508 |
2358760 |
| static int fn_move_steps(struct fn_board *b, const struct fn_move *m, struct fn_step *out) |
| 509 |
|
| { |
| 510 |
2358760 |
| const struct fn_abi *e = E(); |
| 511 |
|
| struct fn_undo undo[FN_CHAIN_MAX]; |
| 512 |
|
| struct fn_step s; |
| 513 |
2358760 |
| int at, i, ok = 1; |
| 514 |
2358760 |
▶ | if (!m || m->n < 1 || m->n > FN_CHAIN_MAX || e->chain_at(b) >= 0) return -1; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!m || m->n < 1 || m->n > FN_CHAIN_MAX || e->chain_at(b) >= 0) return -1; |
2358760 |
0 |
2358760 |
| if (!m || m->n < 1 || m->n > FN_CHAIN_MAX || e->chain_at(b) >= 0) return -1; |
2358760 |
0 |
2358760 |
| if (!m || m->n < 1 || m->n > FN_CHAIN_MAX || e->chain_at(b) >= 0) return -1; |
2358760 |
0 |
2358760 |
| if (!m || m->n < 1 || m->n > FN_CHAIN_MAX || e->chain_at(b) >= 0) return -1; |
0 |
2358760 |
2358760 |
|
| 515 |
2358760 |
| at = m->from; |
| 516 |
4948397 |
▶ | for (i = 0; i < m->n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < m->n; i++) { |
2589637 |
2358760 |
4948397 |
|
| 517 |
2589637 |
▶ | if (fn_play_why(b, at, m->dir[i], m->kind[i], &s) != FN_WHY_OK) { ok = 0; break; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (fn_play_why(b, at, m->dir[i], m->kind[i], &s) != FN_WHY_OK) { ok = 0; break; } |
0 |
2589637 |
2589637 |
|
| 518 |
2589637 |
▶ | if (out) out[i] = s; |
Branch: 1 branch
| Branch | True | False | Total |
| if (out) out[i] = s; |
2589637 |
0 |
2589637 |
|
| 519 |
2589637 |
| fn_play_step(b, &s, &undo[i]); |
| 520 |
2589637 |
| at = s.to; |
| 521 |
|
| } |
| 522 |
4948397 |
▶ | while (i-- > 0) fn_play_undo(b, &undo[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| while (i-- > 0) fn_play_undo(b, &undo[i]); |
2589637 |
2358760 |
4948397 |
|
| 523 |
2358760 |
▶ | return ok ? m->n : -1; |
Branch: 1 branch
| Branch | True | False | Total |
| return ok ? m->n : -1; |
2358760 |
0 |
2358760 |
|
| 524 |
|
| } |
| 525 |
|
| |
| 526 |
35677 |
| static unsigned long long fn_perft_moves(struct fn_board *b, int depth) |
| 527 |
|
| { |
| 528 |
35677 |
| const struct fn_abi *e = E(); |
| 529 |
|
| struct fn_step list[FN_STEPS_MAX]; |
| 530 |
|
| struct fn_undo u, stop; |
| 531 |
35677 |
| unsigned long long total = 0; |
| 532 |
|
| int n, i; |
| 533 |
35677 |
▶ | if (depth <= 0) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (depth <= 0) return 1; |
22834 |
12843 |
35677 |
|
| 534 |
12843 |
| n = fn_play_steps(b, list, FN_STEPS_MAX); |
| 535 |
36866 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
24023 |
12843 |
36866 |
|
| 536 |
24023 |
| fn_play_step(b, &list[i], &u); |
| 537 |
24023 |
▶ | if (e->chain_at(b) >= 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (e->chain_at(b) >= 0) { |
11636 |
12387 |
24023 |
|
| 538 |
|
| /* the mover may stop here, which ends this move ... */ |
| 539 |
11636 |
| fn_play_stop(b, &stop); |
| 540 |
11636 |
| total += fn_perft_moves(b, depth - 1); |
| 541 |
11636 |
| fn_play_undo(b, &stop); |
| 542 |
|
| /* ... or go on, which is still this move */ |
| 543 |
11636 |
| total += fn_perft_moves(b, depth); |
| 544 |
|
| } |
| 545 |
|
| else { |
| 546 |
12387 |
| total += fn_perft_moves(b, depth - 1); |
| 547 |
|
| } |
| 548 |
24023 |
| fn_play_undo(b, &u); |
| 549 |
|
| } |
| 550 |
12843 |
| return total; |
| 551 |
|
| } |
| 552 |
|
| |
| 553 |
|
| /* ---- installed into the table by fn_engine.c ---------------------------------- */ |
| 554 |
|
| |
| 555 |
|
| void fn_moves_install(struct fn_abi *t); |
| 556 |
|
| |
| 557 |
46 |
| void fn_moves_install(struct fn_abi *t) |
| 558 |
|
| { |
| 559 |
46 |
| t->gen_steps = fn_gen_steps; |
| 560 |
46 |
| t->steps_of = fn_steps_of; |
| 561 |
46 |
| t->can_capture = fn_can_capture; |
| 562 |
46 |
| t->step_why = fn_step_why; |
| 563 |
46 |
| t->step_build = fn_step_build; |
| 564 |
46 |
| t->do_step = fn_do_step; |
| 565 |
46 |
| t->undo_step = fn_undo_step; |
| 566 |
46 |
| t->perft_steps = fn_perft_steps; |
| 567 |
|
| |
| 568 |
46 |
| t->play_steps = fn_play_steps; |
| 569 |
46 |
| t->can_stop = fn_can_stop; |
| 570 |
46 |
| t->play_why = fn_play_why; |
| 571 |
46 |
| t->play_step = fn_play_step; |
| 572 |
46 |
| t->play_stop = fn_play_stop; |
| 573 |
46 |
| t->play_undo = fn_play_undo; |
| 574 |
46 |
| t->count_moves = fn_count_moves; |
| 575 |
46 |
| t->gen_moves = fn_gen_moves; |
| 576 |
46 |
| t->move_steps = fn_move_steps; |
| 577 |
46 |
| t->perft_moves = fn_perft_moves; |
| 578 |
46 |
| } |