| 1 |
|
| /* shg_moves.c - how the pieces move, and making and unmaking a move. |
| 2 |
|
| * |
| 3 |
|
| * PERL-FREE by design: see include/shg_abi.h. |
| 4 |
|
| * |
| 5 |
|
| * THIS FILE'S SCOPE. The step and slide tables for each kind and side, the |
| 6 |
|
| * pseudo-legal board moves with the promotion choice, the filter that keeps |
| 7 |
|
| * the mover's king safe, and do_move and undo_move. NO DROP: that is version |
| 8 |
|
| * 3. Whether a square is attacked is shg_mate.c's. |
| 9 |
|
| */ |
| 10 |
|
| |
| 11 |
|
| #include <string.h> |
| 12 |
|
| #include "shg_internal.h" |
| 13 |
|
| |
| 14 |
|
| /* ---- the eight directions ------------------------------------------------- |
| 15 |
|
| * |
| 16 |
|
| * As (dfile, drank) for Black, drank -1 being forward, towards rank a. A |
| 17 |
|
| * mailbox offset is drank * SHG_WIDTH - dfile: a file up is a column left. |
| 18 |
|
| */ |
| 19 |
|
| |
| 20 |
|
| static const int DF[SHG_DIRS] = { 0, 1, -1, 1, -1, 0, 1, -1 }; |
| 21 |
|
| static const int DR[SHG_DIRS] = { -1, -1, -1, 0, 0, 1, 1, 1 }; |
| 22 |
|
| |
| 23 |
|
| #define D_F 0 /* forward */ |
| 24 |
|
| #define D_FL 1 /* forward and a file up */ |
| 25 |
|
| #define D_FR 2 /* forward and a file down */ |
| 26 |
|
| #define D_L 3 /* a file up */ |
| 27 |
|
| #define D_R 4 /* a file down */ |
| 28 |
|
| #define D_B 5 /* back */ |
| 29 |
|
| #define D_BL 6 /* back and a file up */ |
| 30 |
|
| #define D_BR 7 /* back and a file down */ |
| 31 |
|
| |
| 32 |
|
| int SHG_DIR_OFF[SHG_DIRS]; |
| 33 |
|
| int SHG_NSTEPS[SHG_SIDES][SHG_KINDS]; |
| 34 |
|
| int SHG_STEP_OFF[SHG_SIDES][SHG_KINDS][SHG_DIRS]; |
| 35 |
|
| int SHG_NSLIDES[SHG_SIDES][SHG_KINDS]; |
| 36 |
|
| int SHG_SLIDE_OFF[SHG_SIDES][SHG_KINDS][4]; |
| 37 |
|
| unsigned SHG_SLIDE_MASK[SHG_SIDES][SHG_KINDS]; |
| 38 |
|
| |
| 39 |
|
| /* ---- each kind, from W's "Movement" section, as Black sees it -------------- |
| 40 |
|
| * |
| 41 |
|
| * A list of directions ends at -1. The knight's jump is not a direction and |
| 42 |
|
| * is added on its own: two ranks forward and one file either way. |
| 43 |
|
| */ |
| 44 |
|
| |
| 45 |
|
| static const int PAWN_STEPS[] = { D_F, -1 }; |
| 46 |
|
| static const int SILVER_STEPS[] = { D_F, D_FL, D_FR, D_BL, D_BR, -1 }; |
| 47 |
|
| static const int GOLD_STEPS[] = { D_F, D_FL, D_FR, D_L, D_R, D_B, -1 }; |
| 48 |
|
| static const int KING_STEPS[] = { D_F, D_FL, D_FR, D_L, D_R, D_B, D_BL, D_BR, -1 }; |
| 49 |
|
| static const int ORTH[] = { D_F, D_L, D_R, D_B, -1 }; |
| 50 |
|
| static const int DIAG[] = { D_FL, D_FR, D_BL, D_BR, -1 }; |
| 51 |
|
| static const int LANCE_SLIDES[] = { D_F, -1 }; |
| 52 |
|
| static const int NONE[] = { -1 }; |
| 53 |
|
| |
| 54 |
|
| static const int *STEPS_OF[SHG_KINDS]; |
| 55 |
|
| static const int *SLIDES_OF[SHG_KINDS]; |
| 56 |
|
| |
| 57 |
|
| /* What each kind's tables must hold, as (steps, slides): the knight's two |
| 58 |
|
| * jumps count as steps. Checked when the tables are built. */ |
| 59 |
|
| static const int WANT_STEPS[SHG_KINDS] = { 0, 1, 0, 2, 5, 6, 0, 0, 8, 6, 6, 6, 6, 4, 4 }; |
| 60 |
|
| static const int WANT_SLIDES[SHG_KINDS] = { 0, 0, 1, 0, 0, 0, 4, 4, 0, 0, 0, 0, 0, 4, 4 }; |
| 61 |
|
| |
| 62 |
3600 |
| static int offset_of(int side, int df, int dr) |
| 63 |
|
| { |
| 64 |
3600 |
▶ | if (side == SHG_WHITE) dr = -dr; |
Branch: 1 branch
| Branch | True | False | Total |
| if (side == SHG_WHITE) dr = -dr; |
1704 |
1896 |
3600 |
|
| 65 |
3600 |
| return dr * SHG_WIDTH - df; |
| 66 |
|
| } |
| 67 |
|
| |
| 68 |
816 |
| static int dir_of_offset(int off) |
| 69 |
|
| { |
| 70 |
|
| int d; |
| 71 |
3624 |
▶ | for (d = 0; d < SHG_DIRS; d++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (d = 0; d < SHG_DIRS; d++) |
3624 |
0 |
3624 |
|
| 72 |
3624 |
▶ | if (SHG_DIR_OFF[d] == off) return d; |
Branch: 1 branch
| Branch | True | False | Total |
| if (SHG_DIR_OFF[d] == off) return d; |
816 |
2808 |
3624 |
|
| 73 |
0 |
| return -1; |
| 74 |
|
| } |
| 75 |
|
| |
| 76 |
24 |
| int shg_move_tables_init(void) |
| 77 |
|
| { |
| 78 |
24 |
| int side, kind, i, ok = 1; |
| 79 |
|
| |
| 80 |
24 |
| STEPS_OF[SHG_PAWN] = PAWN_STEPS; SLIDES_OF[SHG_PAWN] = NONE; |
| 81 |
24 |
| STEPS_OF[SHG_LANCE] = NONE; SLIDES_OF[SHG_LANCE] = LANCE_SLIDES; |
| 82 |
24 |
| STEPS_OF[SHG_KNIGHT] = NONE; SLIDES_OF[SHG_KNIGHT] = NONE; |
| 83 |
24 |
| STEPS_OF[SHG_SILVER] = SILVER_STEPS; SLIDES_OF[SHG_SILVER] = NONE; |
| 84 |
24 |
| STEPS_OF[SHG_GOLD] = GOLD_STEPS; SLIDES_OF[SHG_GOLD] = NONE; |
| 85 |
24 |
| STEPS_OF[SHG_BISHOP] = NONE; SLIDES_OF[SHG_BISHOP] = DIAG; |
| 86 |
24 |
| STEPS_OF[SHG_ROOK] = NONE; SLIDES_OF[SHG_ROOK] = ORTH; |
| 87 |
24 |
| STEPS_OF[SHG_KING] = KING_STEPS; SLIDES_OF[SHG_KING] = NONE; |
| 88 |
24 |
| STEPS_OF[SHG_TOKIN] = GOLD_STEPS; SLIDES_OF[SHG_TOKIN] = NONE; |
| 89 |
24 |
| STEPS_OF[SHG_PRO_LANCE] = GOLD_STEPS; SLIDES_OF[SHG_PRO_LANCE] = NONE; |
| 90 |
24 |
| STEPS_OF[SHG_PRO_KNIGHT] = GOLD_STEPS; SLIDES_OF[SHG_PRO_KNIGHT] = NONE; |
| 91 |
24 |
| STEPS_OF[SHG_PRO_SILVER] = GOLD_STEPS; SLIDES_OF[SHG_PRO_SILVER] = NONE; |
| 92 |
24 |
| STEPS_OF[SHG_HORSE] = ORTH; SLIDES_OF[SHG_HORSE] = DIAG; |
| 93 |
24 |
| STEPS_OF[SHG_DRAGON] = DIAG; SLIDES_OF[SHG_DRAGON] = ORTH; |
| 94 |
|
| |
| 95 |
216 |
▶ | for (i = 0; i < SHG_DIRS; i++) SHG_DIR_OFF[i] = offset_of(SHG_BLACK, DF[i], DR[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < SHG_DIRS; i++) SHG_DIR_OFF[i] = offset_of(SHG_BLACK, DF[i], DR[i]); |
192 |
24 |
216 |
|
| 96 |
|
| |
| 97 |
72 |
▶ | for (side = 0; side < SHG_SIDES; side++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (side = 0; side < SHG_SIDES; side++) { |
48 |
24 |
72 |
|
| 98 |
720 |
▶ | for (kind = SHG_PAWN; kind <= SHG_DRAGON; kind++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (kind = SHG_PAWN; kind <= SHG_DRAGON; kind++) { |
672 |
48 |
720 |
|
| 99 |
|
| const int *d; |
| 100 |
672 |
| int n = 0, m = 0; |
| 101 |
672 |
| unsigned mask = 0; |
| 102 |
3168 |
▶ | for (d = STEPS_OF[kind]; *d >= 0; d++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (d = STEPS_OF[kind]; *d >= 0; d++) |
2496 |
672 |
3168 |
|
| 103 |
2496 |
| SHG_STEP_OFF[side][kind][n++] = offset_of(side, DF[*d], DR[*d]); |
| 104 |
672 |
▶ | if (kind == SHG_KNIGHT) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (kind == SHG_KNIGHT) { |
48 |
624 |
672 |
|
| 105 |
48 |
| SHG_STEP_OFF[side][kind][n++] = offset_of(side, 1, -2); |
| 106 |
48 |
| SHG_STEP_OFF[side][kind][n++] = offset_of(side, -1, -2); |
| 107 |
|
| } |
| 108 |
1488 |
▶ | for (d = SLIDES_OF[kind]; *d >= 0; d++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (d = SLIDES_OF[kind]; *d >= 0; d++) { |
816 |
672 |
1488 |
|
| 109 |
816 |
| int off = offset_of(side, DF[*d], DR[*d]); |
| 110 |
816 |
| SHG_SLIDE_OFF[side][kind][m++] = off; |
| 111 |
816 |
| mask |= 1u << dir_of_offset(off); |
| 112 |
|
| } |
| 113 |
672 |
| SHG_NSTEPS[side][kind] = n; |
| 114 |
672 |
| SHG_NSLIDES[side][kind] = m; |
| 115 |
672 |
| SHG_SLIDE_MASK[side][kind] = mask; |
| 116 |
672 |
▶ | if (n != WANT_STEPS[kind] || m != WANT_SLIDES[kind]) ok = 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (n != WANT_STEPS[kind] || m != WANT_SLIDES[kind]) ok = 0; |
672 |
0 |
672 |
| if (n != WANT_STEPS[kind] || m != WANT_SLIDES[kind]) ok = 0; |
0 |
672 |
672 |
|
| 117 |
|
| } |
| 118 |
|
| } |
| 119 |
24 |
| return ok; |
| 120 |
|
| } |
| 121 |
|
| |
| 122 |
|
| /* ---- the pseudo-legal board moves ------------------------------------------- |
| 123 |
|
| * |
| 124 |
|
| * W line 363: a move may promote when it starts or ends in the zone. W line |
| 125 |
|
| * 365: a pawn or lance onto the last rank, or a knight onto either of the |
| 126 |
|
| * last two, must. A promotion is listed before its plain twin. |
| 127 |
|
| */ |
| 128 |
|
| |
| 129 |
45652399 |
| static int emit(int *out, int n, int max, int side, int kind, int from, int to) |
| 130 |
|
| { |
| 131 |
45652399 |
▶ | int can = SHG_PROMOTED_OF[kind] && (SHG_ZONE[side][from] || SHG_ZONE[side][to]); |
Branch: 3 branches
| Branch | True | False | Total |
| int can = SHG_PROMOTED_OF[kind] && (SHG_ZONE[side][from] || SHG_ZONE[side][to]); |
23065948 |
22586451 |
45652399 |
| int can = SHG_PROMOTED_OF[kind] && (SHG_ZONE[side][from] || SHG_ZONE[side][to]); |
22497541 |
568407 |
23065948 |
| int can = SHG_PROMOTED_OF[kind] && (SHG_ZONE[side][from] || SHG_ZONE[side][to]); |
956553 |
21540988 |
22497541 |
|
| 132 |
45652399 |
| int last = SHG_LAST[side][to]; |
| 133 |
45652399 |
▶ | int forced = ((kind == SHG_PAWN || kind == SHG_LANCE) && last == 0) |
Branch: 2 branches
| Branch | True | False | Total |
| int forced = ((kind == SHG_PAWN || kind == SHG_LANCE) && last == 0) |
2016563 |
34174251 |
36190814 |
| int forced = ((kind == SHG_PAWN || kind == SHG_LANCE) && last == 0) |
11353959 |
124189 |
11478148 |
|
| 134 |
91304798 |
▶ | || (kind == SHG_KNIGHT && last <= 1); |
Branch: 3 branches
| Branch | True | False | Total |
| || (kind == SHG_KNIGHT && last <= 1); |
36190814 |
9461585 |
45652399 |
| || (kind == SHG_KNIGHT && last <= 1); |
1228374 |
44299836 |
45528210 |
| || (kind == SHG_KNIGHT && last <= 1); |
105206 |
1123168 |
1228374 |
|
| 135 |
45652399 |
▶ | if (can) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (can) { |
1524960 |
44127439 |
45652399 |
|
| 136 |
1524960 |
▶ | if (n < max) out[n] = SHG_MOVE(from, to, 1); |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < max) out[n] = SHG_MOVE(from, to, 1); |
1524960 |
0 |
1524960 |
|
| 137 |
1524960 |
| n++; |
| 138 |
|
| } |
| 139 |
45652399 |
▶ | if (!forced) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!forced) { |
45423004 |
229395 |
45652399 |
|
| 140 |
45423004 |
▶ | if (n < max) out[n] = SHG_MOVE(from, to, 0); |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < max) out[n] = SHG_MOVE(from, to, 0); |
45423004 |
0 |
45423004 |
|
| 141 |
45423004 |
| n++; |
| 142 |
|
| } |
| 143 |
45652399 |
| return n; |
| 144 |
|
| } |
| 145 |
|
| |
| 146 |
2085730 |
| int shg_pseudo_moves(const struct shg_board *b, int *out, int max) |
| 147 |
|
| { |
| 148 |
2085730 |
| int sq, n = 0, side, i; |
| 149 |
2085730 |
▶ | if (!b || (!out && max > 0)) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!b || (!out && max > 0)) return 0; |
2085730 |
0 |
2085730 |
| if (!b || (!out && max > 0)) return 0; |
0 |
2085730 |
2085730 |
| if (!b || (!out && max > 0)) return 0; |
0 |
0 |
0 |
|
| 150 |
2085730 |
| side = b->to_move; |
| 151 |
171029860 |
▶ | for (sq = 0; sq < SHG_SQUARES; sq++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (sq = 0; sq < SHG_SQUARES; sq++) { |
168944130 |
2085730 |
171029860 |
|
| 152 |
168944130 |
| int box = SHG_SQ_OF[sq], p = b->cell[box], kind, t, c; |
| 153 |
168944130 |
▶ | if (p == SHG_EMPTY || SHG_SIDE_OF(p) != side) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p == SHG_EMPTY || SHG_SIDE_OF(p) != side) continue; |
49586404 |
119357726 |
168944130 |
| if (p == SHG_EMPTY || SHG_SIDE_OF(p) != side) continue; |
25087131 |
24499273 |
49586404 |
|
| 154 |
24499273 |
| kind = SHG_KIND_OF(p); |
| 155 |
88892346 |
▶ | for (i = 0; i < SHG_NSTEPS[side][kind]; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < SHG_NSTEPS[side][kind]; i++) { |
64393073 |
24499273 |
88892346 |
|
| 156 |
64393073 |
| t = box + SHG_STEP_OFF[side][kind][i]; |
| 157 |
64393073 |
| c = b->cell[t]; |
| 158 |
64393073 |
▶ | if (c == SHG_OFF || (c != SHG_EMPTY && SHG_SIDE_OF(c) == side)) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == SHG_OFF || (c != SHG_EMPTY && SHG_SIDE_OF(c) == side)) continue; |
56412113 |
7980960 |
64393073 |
| if (c == SHG_OFF || (c != SHG_EMPTY && SHG_SIDE_OF(c) == side)) continue; |
21250496 |
35161617 |
56412113 |
| if (c == SHG_OFF || (c != SHG_EMPTY && SHG_SIDE_OF(c) == side)) continue; |
18625506 |
2624990 |
21250496 |
|
| 159 |
37786607 |
| n = emit(out, n, max, side, kind, sq, SHG_SQ_AT[t]); |
| 160 |
|
| } |
| 161 |
35460148 |
▶ | for (i = 0; i < SHG_NSLIDES[side][kind]; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < SHG_NSLIDES[side][kind]; i++) { |
10960875 |
24499273 |
35460148 |
|
| 162 |
10960875 |
| int off = SHG_SLIDE_OFF[side][kind][i]; |
| 163 |
17986833 |
| for (t = box + off; ; t += off) { |
| 164 |
17986833 |
| c = b->cell[t]; |
| 165 |
17986833 |
▶ | if (c == SHG_OFF || (c != SHG_EMPTY && SHG_SIDE_OF(c) == side)) break; |
Branch: 3 branches
| Branch | True | False | Total |
| if (c == SHG_OFF || (c != SHG_EMPTY && SHG_SIDE_OF(c) == side)) break; |
16278213 |
1708620 |
17986833 |
| if (c == SHG_OFF || (c != SHG_EMPTY && SHG_SIDE_OF(c) == side)) break; |
9252255 |
7025958 |
16278213 |
| if (c == SHG_OFF || (c != SHG_EMPTY && SHG_SIDE_OF(c) == side)) break; |
839834 |
8412421 |
9252255 |
|
| 166 |
7865792 |
| n = emit(out, n, max, side, kind, sq, SHG_SQ_AT[t]); |
| 167 |
7865792 |
▶ | if (c != SHG_EMPTY) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c != SHG_EMPTY) break; |
839834 |
7025958 |
7865792 |
|
| 168 |
|
| } |
| 169 |
|
| } |
| 170 |
|
| } |
| 171 |
2085730 |
| return n; |
| 172 |
|
| } |
| 173 |
|
| |
| 174 |
|
| /* ---- the legal board moves ---------------------------------------------------- |
| 175 |
|
| * |
| 176 |
|
| * Each pseudo-legal move is made on a copy and kept when the mover's king is |
| 177 |
|
| * not attacked after it. A move do_move refuses (taking a king, a full hand) |
| 178 |
|
| * is not kept either. |
| 179 |
|
| */ |
| 180 |
|
| |
| 181 |
45259 |
| int shg_board_moves(const struct shg_board *b, int *out, int max) |
| 182 |
|
| { |
| 183 |
|
| int all[SHG_MOVES_MAX]; |
| 184 |
45259 |
| int count, i, n = 0, side; |
| 185 |
|
| struct shg_board copy; |
| 186 |
|
| struct shg_undo u; |
| 187 |
45259 |
▶ | if (!b || (!out && max > 0)) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!b || (!out && max > 0)) return 0; |
45259 |
0 |
45259 |
| if (!b || (!out && max > 0)) return 0; |
0 |
45259 |
45259 |
| if (!b || (!out && max > 0)) return 0; |
0 |
0 |
0 |
|
| 188 |
45259 |
| count = shg_pseudo_moves(b, all, SHG_MOVES_MAX); |
| 189 |
45259 |
▶ | if (count > SHG_MOVES_MAX) count = SHG_MOVES_MAX; |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > SHG_MOVES_MAX) count = SHG_MOVES_MAX; |
0 |
45259 |
45259 |
|
| 190 |
45259 |
| copy = *b; |
| 191 |
45259 |
| side = b->to_move; |
| 192 |
1453016 |
▶ | for (i = 0; i < count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < count; i++) { |
1407757 |
45259 |
1453016 |
|
| 193 |
|
| int safe; |
| 194 |
1407757 |
▶ | if (!shg_do_move(©, all[i], &u)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!shg_do_move(©, all[i], &u)) continue; |
2 |
1407755 |
1407757 |
|
| 195 |
2815510 |
| safe = copy.king[side] < 0 |
| 196 |
1407755 |
▶ | || !shg_attacked(©, copy.king[side], side == SHG_BLACK ? SHG_WHITE : SHG_BLACK); |
Branch: 2 branches
| Branch | True | False | Total |
| || !shg_attacked(©, copy.king[side], side == SHG_BLACK ? SHG_WHITE : SHG_BLACK); |
1406254 |
1501 |
1407755 |
| || !shg_attacked(©, copy.king[side], side == SHG_BLACK ? SHG_WHITE : SHG_BLACK); |
1378170 |
28084 |
1406254 |
|
| 197 |
1407755 |
| shg_undo_move(©, &u); |
| 198 |
1407755 |
▶ | if (!safe) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!safe) continue; |
28084 |
1379671 |
1407755 |
|
| 199 |
1379671 |
▶ | if (n < max) out[n] = all[i]; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < max) out[n] = all[i]; |
1379671 |
0 |
1379671 |
|
| 200 |
1379671 |
| n++; |
| 201 |
|
| } |
| 202 |
45259 |
| return n; |
| 203 |
|
| } |
| 204 |
|
| |
| 205 |
|
| /* ---- making and unmaking a move ------------------------------------------- */ |
| 206 |
|
| |
| 207 |
19118074 |
| static int is_board_move(int move) |
| 208 |
|
| { |
| 209 |
19118074 |
▶ | return move >= 0 && !SHG_MOVE_DROP(move) && (move >> 16) == 0 |
Branch: 2 branches
| Branch | True | False | Total |
| return move >= 0 && !SHG_MOVE_DROP(move) && (move >> 16) == 0 |
19118074 |
0 |
19118074 |
| return move >= 0 && !SHG_MOVE_DROP(move) && (move >> 16) == 0 |
19118074 |
0 |
19118074 |
|
| 210 |
19118074 |
▶ | && SHG_IS_SQ(SHG_MOVE_FROM(move)) && SHG_IS_SQ(SHG_MOVE_TO(move)) |
Branch: 2 branches
| Branch | True | False | Total |
| && SHG_IS_SQ(SHG_MOVE_FROM(move)) && SHG_IS_SQ(SHG_MOVE_TO(move)) |
19118074 |
0 |
19118074 |
| && SHG_IS_SQ(SHG_MOVE_FROM(move)) && SHG_IS_SQ(SHG_MOVE_TO(move)) |
19118074 |
0 |
19118074 |
|
| 211 |
38236148 |
▶ | && SHG_MOVE_FROM(move) != SHG_MOVE_TO(move); |
Branch: 2 branches
| Branch | True | False | Total |
| && SHG_MOVE_FROM(move) != SHG_MOVE_TO(move); |
19118074 |
0 |
19118074 |
| && SHG_MOVE_FROM(move) != SHG_MOVE_TO(move); |
19116870 |
1204 |
19118074 |
|
| 212 |
|
| } |
| 213 |
|
| |
| 214 |
|
| /* A drop: bit 15, a hand kind in bits 16-19, a square in bits 7-13, and |
| 215 |
|
| * nothing else. */ |
| 216 |
20952291 |
| static int is_drop(int move) |
| 217 |
|
| { |
| 218 |
20952291 |
▶ | return move >= 0 && SHG_MOVE_DROP(move) && (move >> 20) == 0 |
Branch: 2 branches
| Branch | True | False | Total |
| return move >= 0 && SHG_MOVE_DROP(move) && (move >> 20) == 0 |
1834217 |
19118074 |
20952291 |
| return move >= 0 && SHG_MOVE_DROP(move) && (move >> 20) == 0 |
1834217 |
0 |
1834217 |
|
| 219 |
1834217 |
▶ | && SHG_MOVE_FROM(move) == 0 && !SHG_MOVE_PROMOTE(move) |
Branch: 2 branches
| Branch | True | False | Total |
| && SHG_MOVE_FROM(move) == 0 && !SHG_MOVE_PROMOTE(move) |
1834217 |
0 |
1834217 |
| && SHG_MOVE_FROM(move) == 0 && !SHG_MOVE_PROMOTE(move) |
1834217 |
0 |
1834217 |
|
| 220 |
41904582 |
▶ | && SHG_IS_HAND(SHG_MOVE_KIND(move)) && SHG_IS_SQ(SHG_MOVE_TO(move)); |
Branch: 4 branches
| Branch | True | False | Total |
| && SHG_IS_HAND(SHG_MOVE_KIND(move)) && SHG_IS_SQ(SHG_MOVE_TO(move)); |
20952291 |
0 |
20952291 |
| && SHG_IS_HAND(SHG_MOVE_KIND(move)) && SHG_IS_SQ(SHG_MOVE_TO(move)); |
1834217 |
0 |
1834217 |
| && SHG_IS_HAND(SHG_MOVE_KIND(move)) && SHG_IS_SQ(SHG_MOVE_TO(move)); |
1834217 |
0 |
1834217 |
| && SHG_IS_HAND(SHG_MOVE_KIND(move)) && SHG_IS_SQ(SHG_MOVE_TO(move)); |
1834217 |
0 |
1834217 |
|
| 221 |
|
| } |
| 222 |
|
| |
| 223 |
1068901 |
| static int do_drop(struct shg_board *b, int move, struct shg_undo *undo) |
| 224 |
|
| { |
| 225 |
1068901 |
| int side = b->to_move, kind = SHG_MOVE_KIND(move), to = SHG_MOVE_TO(move); |
| 226 |
1068901 |
▶ | if (b->hand[side][kind] < 1 || b->cell[SHG_SQ_OF[to]] != SHG_EMPTY) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (b->hand[side][kind] < 1 || b->cell[SHG_SQ_OF[to]] != SHG_EMPTY) return 0; |
1068899 |
2 |
1068901 |
| if (b->hand[side][kind] < 1 || b->cell[SHG_SQ_OF[to]] != SHG_EMPTY) return 0; |
1 |
1068898 |
1068899 |
|
| 227 |
1068898 |
| undo->move = move; |
| 228 |
1068898 |
| undo->moved = SHG_PIECE(side, kind); |
| 229 |
1068898 |
| undo->captured = SHG_EMPTY; |
| 230 |
1068898 |
| undo->move_number = b->move_number; |
| 231 |
1068898 |
| shg_set_hand(b, side, kind, b->hand[side][kind] - 1); |
| 232 |
1068898 |
| shg_put(b, to, SHG_PIECE(side, kind)); |
| 233 |
1068898 |
| shg_set_to_move(b, side == SHG_BLACK ? SHG_WHITE : SHG_BLACK); |
| 234 |
1068898 |
▶ | if (b->move_number < SHG_MOVE_MAX) b->move_number++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->move_number < SHG_MOVE_MAX) b->move_number++; |
1068898 |
0 |
1068898 |
|
| 235 |
1068898 |
| return 1; |
| 236 |
|
| } |
| 237 |
|
| |
| 238 |
746581 |
| static int undo_drop(struct shg_board *b, const struct shg_undo *undo) |
| 239 |
|
| { |
| 240 |
746581 |
| int move = undo->move, p = undo->moved, kind = SHG_MOVE_KIND(move), to = SHG_MOVE_TO(move), side; |
| 241 |
746581 |
▶ | if (p <= 0 || (p & ~0x1F) || SHG_KIND_OF(p) != kind) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (p <= 0 || (p & ~0x1F) || SHG_KIND_OF(p) != kind) return 0; |
746581 |
0 |
746581 |
| if (p <= 0 || (p & ~0x1F) || SHG_KIND_OF(p) != kind) return 0; |
746581 |
0 |
746581 |
| if (p <= 0 || (p & ~0x1F) || SHG_KIND_OF(p) != kind) return 0; |
0 |
746581 |
746581 |
|
| 242 |
746581 |
| side = SHG_SIDE_OF(p); |
| 243 |
746581 |
▶ | if (b->to_move == side || undo->captured != SHG_EMPTY) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (b->to_move == side || undo->captured != SHG_EMPTY) return 0; |
746581 |
0 |
746581 |
| if (b->to_move == side || undo->captured != SHG_EMPTY) return 0; |
0 |
746581 |
746581 |
|
| 244 |
746581 |
▶ | if (b->cell[SHG_SQ_OF[to]] != p) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->cell[SHG_SQ_OF[to]] != p) return 0; |
0 |
746581 |
746581 |
|
| 245 |
746581 |
▶ | if (b->hand[side][kind] + 1 > SHG_SET_OF[kind]) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->hand[side][kind] + 1 > SHG_SET_OF[kind]) return 0; |
0 |
746581 |
746581 |
|
| 246 |
746581 |
▶ | if (undo->move_number < 1 || undo->move_number > SHG_MOVE_MAX) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (undo->move_number < 1 || undo->move_number > SHG_MOVE_MAX) return 0; |
746581 |
0 |
746581 |
| if (undo->move_number < 1 || undo->move_number > SHG_MOVE_MAX) return 0; |
0 |
746581 |
746581 |
|
| 247 |
746581 |
| shg_set_to_move(b, side); |
| 248 |
746581 |
| b->move_number = undo->move_number; |
| 249 |
746581 |
| shg_lift(b, to); |
| 250 |
746581 |
| shg_set_hand(b, side, kind, b->hand[side][kind] + 1); |
| 251 |
746581 |
| return 1; |
| 252 |
|
| } |
| 253 |
|
| |
| 254 |
10593612 |
| int shg_do_move(struct shg_board *b, int move, struct shg_undo *undo) |
| 255 |
|
| { |
| 256 |
10593612 |
| int from, to, p, c, side, kind, base = 0, put; |
| 257 |
10593612 |
▶ | if (b && undo && is_drop(move)) return do_drop(b, move, undo); |
Branch: 3 branches
| Branch | True | False | Total |
| if (b && undo && is_drop(move)) return do_drop(b, move, undo); |
10593612 |
0 |
10593612 |
| if (b && undo && is_drop(move)) return do_drop(b, move, undo); |
10593612 |
0 |
10593612 |
| if (b && undo && is_drop(move)) return do_drop(b, move, undo); |
1068901 |
9524711 |
10593612 |
|
| 258 |
9524711 |
▶ | if (!b || !undo || !is_board_move(move)) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!b || !undo || !is_board_move(move)) return 0; |
9524711 |
0 |
9524711 |
| if (!b || !undo || !is_board_move(move)) return 0; |
9524711 |
0 |
9524711 |
| if (!b || !undo || !is_board_move(move)) return 0; |
1 |
9524710 |
9524711 |
|
| 259 |
9524710 |
| from = SHG_MOVE_FROM(move); |
| 260 |
9524710 |
| to = SHG_MOVE_TO(move); |
| 261 |
9524710 |
| side = b->to_move; |
| 262 |
9524710 |
| p = b->cell[SHG_SQ_OF[from]]; |
| 263 |
9524710 |
| c = b->cell[SHG_SQ_OF[to]]; |
| 264 |
9524710 |
▶ | if (p == SHG_EMPTY || SHG_SIDE_OF(p) != side) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p == SHG_EMPTY || SHG_SIDE_OF(p) != side) return 0; |
9524709 |
1 |
9524710 |
| if (p == SHG_EMPTY || SHG_SIDE_OF(p) != side) return 0; |
1 |
9524708 |
9524709 |
|
| 265 |
9524708 |
▶ | if (c != SHG_EMPTY && SHG_SIDE_OF(c) == side) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c != SHG_EMPTY && SHG_SIDE_OF(c) == side) return 0; |
1676440 |
7848268 |
9524708 |
| if (c != SHG_EMPTY && SHG_SIDE_OF(c) == side) return 0; |
1 |
1676439 |
1676440 |
|
| 266 |
9524707 |
| kind = SHG_KIND_OF(p); |
| 267 |
9524707 |
▶ | if (SHG_MOVE_PROMOTE(move) && !SHG_PROMOTED_OF[kind]) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (SHG_MOVE_PROMOTE(move) && !SHG_PROMOTED_OF[kind]) return 0; |
635523 |
8889184 |
9524707 |
| if (SHG_MOVE_PROMOTE(move) && !SHG_PROMOTED_OF[kind]) return 0; |
2 |
635521 |
635523 |
|
| 268 |
9524705 |
▶ | if (c != SHG_EMPTY) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c != SHG_EMPTY) { |
1676439 |
7848266 |
9524705 |
|
| 269 |
1676439 |
| base = SHG_UNPROMOTED_OF[SHG_KIND_OF(c)]; |
| 270 |
1676439 |
▶ | if (base == SHG_KING) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (base == SHG_KING) return 0; |
3 |
1676436 |
1676439 |
|
| 271 |
1676436 |
▶ | if (b->hand[side][base] + 1 > SHG_SET_OF[base]) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->hand[side][base] + 1 > SHG_SET_OF[base]) return 0; |
1 |
1676435 |
1676436 |
|
| 272 |
|
| } |
| 273 |
|
| |
| 274 |
9524701 |
| undo->move = move; |
| 275 |
9524701 |
| undo->moved = p; |
| 276 |
9524701 |
| undo->captured = c; |
| 277 |
9524701 |
| undo->move_number = b->move_number; |
| 278 |
|
| |
| 279 |
9524701 |
▶ | put = SHG_MOVE_PROMOTE(move) ? SHG_PIECE(side, SHG_PROMOTED_OF[kind]) : p; |
Branch: 1 branch
| Branch | True | False | Total |
| put = SHG_MOVE_PROMOTE(move) ? SHG_PIECE(side, SHG_PROMOTED_OF[kind]) : p; |
635520 |
8889181 |
9524701 |
|
| 280 |
9524701 |
| shg_lift(b, from); |
| 281 |
9524701 |
▶ | if (c != SHG_EMPTY) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c != SHG_EMPTY) { |
1676435 |
7848266 |
9524701 |
|
| 282 |
1676435 |
| shg_lift(b, to); |
| 283 |
1676435 |
| shg_set_hand(b, side, base, b->hand[side][base] + 1); |
| 284 |
|
| } |
| 285 |
9524701 |
| shg_put(b, to, put); |
| 286 |
9524701 |
| shg_set_to_move(b, side == SHG_BLACK ? SHG_WHITE : SHG_BLACK); |
| 287 |
9524701 |
▶ | if (b->move_number < SHG_MOVE_MAX) b->move_number++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->move_number < SHG_MOVE_MAX) b->move_number++; |
9524701 |
0 |
9524701 |
|
| 288 |
9524701 |
| return 1; |
| 289 |
|
| } |
| 290 |
|
| |
| 291 |
10229161 |
| int shg_undo_move(struct shg_board *b, const struct shg_undo *undo) |
| 292 |
|
| { |
| 293 |
10229161 |
| int move, from, to, p, c, side, kind, base = 0, shown; |
| 294 |
10229161 |
▶ | if (!b || !undo) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!b || !undo) return 0; |
10229161 |
0 |
10229161 |
| if (!b || !undo) return 0; |
0 |
10229161 |
10229161 |
|
| 295 |
10229161 |
| move = undo->move; |
| 296 |
10229161 |
| p = undo->moved; |
| 297 |
10229161 |
| c = undo->captured; |
| 298 |
10229161 |
▶ | if (is_drop(move)) return undo_drop(b, undo); |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_drop(move)) return undo_drop(b, undo); |
746581 |
9482580 |
10229161 |
|
| 299 |
9482580 |
▶ | if (!is_board_move(move)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!is_board_move(move)) return 0; |
1 |
9482579 |
9482580 |
|
| 300 |
9482579 |
▶ | if (p <= 0 || (p & ~0x1F) || !SHG_IS_KIND(SHG_KIND_OF(p))) return 0; |
Branch: 4 branches
| Branch | True | False | Total |
| if (p <= 0 || (p & ~0x1F) || !SHG_IS_KIND(SHG_KIND_OF(p))) return 0; |
9482579 |
0 |
9482579 |
| if (p <= 0 || (p & ~0x1F) || !SHG_IS_KIND(SHG_KIND_OF(p))) return 0; |
9482579 |
0 |
9482579 |
| if (p <= 0 || (p & ~0x1F) || !SHG_IS_KIND(SHG_KIND_OF(p))) return 0; |
9482579 |
0 |
9482579 |
| if (p <= 0 || (p & ~0x1F) || !SHG_IS_KIND(SHG_KIND_OF(p))) return 0; |
0 |
9482579 |
9482579 |
|
| 301 |
9482579 |
| from = SHG_MOVE_FROM(move); |
| 302 |
9482579 |
| to = SHG_MOVE_TO(move); |
| 303 |
9482579 |
| side = SHG_SIDE_OF(p); |
| 304 |
9482579 |
| kind = SHG_KIND_OF(p); |
| 305 |
9482579 |
▶ | if (b->to_move == side) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->to_move == side) return 0; |
1 |
9482578 |
9482579 |
|
| 306 |
9482578 |
▶ | if (SHG_MOVE_PROMOTE(move) && !SHG_PROMOTED_OF[kind]) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (SHG_MOVE_PROMOTE(move) && !SHG_PROMOTED_OF[kind]) return 0; |
633882 |
8848696 |
9482578 |
| if (SHG_MOVE_PROMOTE(move) && !SHG_PROMOTED_OF[kind]) return 0; |
0 |
633882 |
633882 |
|
| 307 |
9482578 |
▶ | shown = SHG_MOVE_PROMOTE(move) ? SHG_PIECE(side, SHG_PROMOTED_OF[kind]) : p; |
Branch: 1 branch
| Branch | True | False | Total |
| shown = SHG_MOVE_PROMOTE(move) ? SHG_PIECE(side, SHG_PROMOTED_OF[kind]) : p; |
633882 |
8848696 |
9482578 |
|
| 308 |
9482578 |
▶ | if (b->cell[SHG_SQ_OF[from]] != SHG_EMPTY || b->cell[SHG_SQ_OF[to]] != shown) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (b->cell[SHG_SQ_OF[from]] != SHG_EMPTY || b->cell[SHG_SQ_OF[to]] != shown) return 0; |
9482578 |
0 |
9482578 |
| if (b->cell[SHG_SQ_OF[from]] != SHG_EMPTY || b->cell[SHG_SQ_OF[to]] != shown) return 0; |
1 |
9482577 |
9482578 |
|
| 309 |
9482577 |
▶ | if (c != SHG_EMPTY) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c != SHG_EMPTY) { |
1670113 |
7812464 |
9482577 |
|
| 310 |
1670113 |
▶ | if (c < 0 || (c & ~0x1F) || !SHG_IS_KIND(SHG_KIND_OF(c)) || SHG_SIDE_OF(c) == side) return 0; |
Branch: 5 branches
| Branch | True | False | Total |
| if (c < 0 || (c & ~0x1F) || !SHG_IS_KIND(SHG_KIND_OF(c)) || SHG_SIDE_OF(c) == side) return 0; |
1670113 |
0 |
1670113 |
| if (c < 0 || (c & ~0x1F) || !SHG_IS_KIND(SHG_KIND_OF(c)) || SHG_SIDE_OF(c) == side) return 0; |
1670113 |
0 |
1670113 |
| if (c < 0 || (c & ~0x1F) || !SHG_IS_KIND(SHG_KIND_OF(c)) || SHG_SIDE_OF(c) == side) return 0; |
1670113 |
0 |
1670113 |
| if (c < 0 || (c & ~0x1F) || !SHG_IS_KIND(SHG_KIND_OF(c)) || SHG_SIDE_OF(c) == side) return 0; |
1670113 |
0 |
1670113 |
| if (c < 0 || (c & ~0x1F) || !SHG_IS_KIND(SHG_KIND_OF(c)) || SHG_SIDE_OF(c) == side) return 0; |
0 |
1670113 |
1670113 |
|
| 311 |
1670113 |
| base = SHG_UNPROMOTED_OF[SHG_KIND_OF(c)]; |
| 312 |
1670113 |
▶ | if (base == SHG_KING || b->hand[side][base] < 1) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (base == SHG_KING || b->hand[side][base] < 1) return 0; |
1670113 |
0 |
1670113 |
| if (base == SHG_KING || b->hand[side][base] < 1) return 0; |
0 |
1670113 |
1670113 |
|
| 313 |
|
| } |
| 314 |
9482577 |
▶ | if (undo->move_number < 1 || undo->move_number > SHG_MOVE_MAX) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (undo->move_number < 1 || undo->move_number > SHG_MOVE_MAX) return 0; |
9482577 |
0 |
9482577 |
| if (undo->move_number < 1 || undo->move_number > SHG_MOVE_MAX) return 0; |
0 |
9482577 |
9482577 |
|
| 315 |
|
| |
| 316 |
9482577 |
| shg_set_to_move(b, side); |
| 317 |
9482577 |
| b->move_number = undo->move_number; |
| 318 |
9482577 |
| shg_lift(b, to); |
| 319 |
9482577 |
▶ | if (c != SHG_EMPTY) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (c != SHG_EMPTY) { |
1670113 |
7812464 |
9482577 |
|
| 320 |
1670113 |
| shg_set_hand(b, side, base, b->hand[side][base] - 1); |
| 321 |
1670113 |
| shg_put(b, to, c); |
| 322 |
|
| } |
| 323 |
9482577 |
| shg_put(b, from, p); |
| 324 |
9482577 |
| return 1; |
| 325 |
|
| } |
| 326 |
|
| |
| 327 |
|
| /* ---- the drops, version 3 ---------------------------------------------------- |
| 328 |
|
| * |
| 329 |
|
| * W lines 499-507. A piece from hand onto any empty square, unpromoted, never |
| 330 |
|
| * capturing. Three restrictions: no piece where it could never move; no pawn |
| 331 |
|
| * onto a file with an unpromoted pawn of the same side; no pawn dropped to |
| 332 |
|
| * give mate. And, as for any move, not leaving the side's own king attacked. |
| 333 |
|
| */ |
| 334 |
|
| |
| 335 |
|
| /* 1 when the side to move has a legal board move; it stops at the first. */ |
| 336 |
352165 |
| static int any_board_move(const struct shg_board *b) |
| 337 |
|
| { |
| 338 |
|
| int all[SHG_MOVES_MAX]; |
| 339 |
352165 |
| int count, i, side = b->to_move; |
| 340 |
352165 |
| struct shg_board copy = *b; |
| 341 |
|
| struct shg_undo u; |
| 342 |
352165 |
| count = shg_pseudo_moves(b, all, SHG_MOVES_MAX); |
| 343 |
352165 |
▶ | if (count > SHG_MOVES_MAX) count = SHG_MOVES_MAX; |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > SHG_MOVES_MAX) count = SHG_MOVES_MAX; |
0 |
352165 |
352165 |
|
| 344 |
4870210 |
▶ | for (i = 0; i < count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < count; i++) { |
4865103 |
5107 |
4870210 |
|
| 345 |
|
| int safe; |
| 346 |
4865103 |
▶ | if (!shg_do_move(©, all[i], &u)) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!shg_do_move(©, all[i], &u)) continue; |
0 |
4865103 |
4865103 |
|
| 347 |
9730206 |
| safe = copy.king[side] < 0 |
| 348 |
4865103 |
▶ | || !shg_attacked(©, copy.king[side], side == SHG_BLACK ? SHG_WHITE : SHG_BLACK); |
Branch: 2 branches
| Branch | True | False | Total |
| || !shg_attacked(©, copy.king[side], side == SHG_BLACK ? SHG_WHITE : SHG_BLACK); |
4865102 |
1 |
4865103 |
| || !shg_attacked(©, copy.king[side], side == SHG_BLACK ? SHG_WHITE : SHG_BLACK); |
347057 |
4518045 |
4865102 |
|
| 349 |
4865103 |
| shg_undo_move(©, &u); |
| 350 |
4865103 |
▶ | if (safe) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (safe) return 1; |
347058 |
4518045 |
4865103 |
|
| 351 |
|
| } |
| 352 |
5107 |
| return 0; |
| 353 |
|
| } |
| 354 |
|
| |
| 355 |
484954 |
| static int pawn_on_file(const struct shg_board *b, int side, int sq) |
| 356 |
|
| { |
| 357 |
484954 |
| int col = sq % SHG_FILES, row; |
| 358 |
2823513 |
▶ | for (row = 0; row < SHG_RANKS; row++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (row = 0; row < SHG_RANKS; row++) |
2731451 |
92062 |
2823513 |
|
| 359 |
2731451 |
▶ | if (b->cell[SHG_SQ_OF[row * SHG_FILES + col]] == SHG_PIECE(side, SHG_PAWN)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->cell[SHG_SQ_OF[row * SHG_FILES + col]] == SHG_PIECE(side, SHG_PAWN)) return 1; |
392892 |
2338559 |
2731451 |
|
| 360 |
92062 |
| return 0; |
| 361 |
|
| } |
| 362 |
|
| |
| 363 |
1432788 |
| int shg_drop_fault(const struct shg_board *b, int kind, int sq) |
| 364 |
|
| { |
| 365 |
1432788 |
| int side, last, other, fault = SHG_DROP_OK; |
| 366 |
|
| struct shg_board copy; |
| 367 |
|
| struct shg_undo u; |
| 368 |
1432788 |
▶ | if (!b || !SHG_IS_HAND(kind) || !SHG_IS_SQ(sq)) return SHG_DROP_BAD; |
Branch: 5 branches
| Branch | True | False | Total |
| if (!b || !SHG_IS_HAND(kind) || !SHG_IS_SQ(sq)) return SHG_DROP_BAD; |
1432788 |
0 |
1432788 |
| if (!b || !SHG_IS_HAND(kind) || !SHG_IS_SQ(sq)) return SHG_DROP_BAD; |
1432788 |
0 |
1432788 |
| if (!b || !SHG_IS_HAND(kind) || !SHG_IS_SQ(sq)) return SHG_DROP_BAD; |
1432788 |
0 |
1432788 |
| if (!b || !SHG_IS_HAND(kind) || !SHG_IS_SQ(sq)) return SHG_DROP_BAD; |
1432788 |
0 |
1432788 |
| if (!b || !SHG_IS_HAND(kind) || !SHG_IS_SQ(sq)) return SHG_DROP_BAD; |
0 |
1432788 |
1432788 |
|
| 369 |
1432788 |
| side = b->to_move; |
| 370 |
1432788 |
| other = side == SHG_BLACK ? SHG_WHITE : SHG_BLACK; |
| 371 |
1432788 |
▶ | if (b->hand[side][kind] < 1) return SHG_DROP_NOT_HELD; |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->hand[side][kind] < 1) return SHG_DROP_NOT_HELD; |
17014 |
1415774 |
1432788 |
|
| 372 |
1415774 |
▶ | if (b->cell[SHG_SQ_OF[sq]] != SHG_EMPTY) return SHG_DROP_OCCUPIED; |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->cell[SHG_SQ_OF[sq]] != SHG_EMPTY) return SHG_DROP_OCCUPIED; |
624673 |
791101 |
1415774 |
|
| 373 |
791101 |
| last = SHG_LAST[side][sq]; |
| 374 |
791101 |
▶ | if (((kind == SHG_PAWN || kind == SHG_LANCE) && last == 0) || (kind == SHG_KNIGHT && last <= 1)) |
Branch: 5 branches
| Branch | True | False | Total |
| if (((kind == SHG_PAWN || kind == SHG_LANCE) && last == 0) || (kind == SHG_KNIGHT && last <= 1)) |
238297 |
552804 |
791101 |
| if (((kind == SHG_PAWN || kind == SHG_LANCE) && last == 0) || (kind == SHG_KNIGHT && last <= 1)) |
24816 |
213481 |
238297 |
| if (((kind == SHG_PAWN || kind == SHG_LANCE) && last == 0) || (kind == SHG_KNIGHT && last <= 1)) |
506652 |
70968 |
577620 |
| if (((kind == SHG_PAWN || kind == SHG_LANCE) && last == 0) || (kind == SHG_KNIGHT && last <= 1)) |
34509 |
685624 |
720133 |
| if (((kind == SHG_PAWN || kind == SHG_LANCE) && last == 0) || (kind == SHG_KNIGHT && last <= 1)) |
8455 |
26054 |
34509 |
|
| 375 |
79423 |
| return SHG_DROP_DEAD; |
| 376 |
711678 |
▶ | if (kind == SHG_PAWN && pawn_on_file(b, side, sq)) return SHG_DROP_TWO_PAWNS; |
Branch: 2 branches
| Branch | True | False | Total |
| if (kind == SHG_PAWN && pawn_on_file(b, side, sq)) return SHG_DROP_TWO_PAWNS; |
484954 |
226724 |
711678 |
| if (kind == SHG_PAWN && pawn_on_file(b, side, sq)) return SHG_DROP_TWO_PAWNS; |
392892 |
92062 |
484954 |
|
| 377 |
|
| |
| 378 |
318786 |
| copy = *b; |
| 379 |
318786 |
▶ | if (!shg_do_move(©, SHG_DROP(kind, sq), &u)) return SHG_DROP_NOT_HELD; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!shg_do_move(©, SHG_DROP(kind, sq), &u)) return SHG_DROP_NOT_HELD; |
0 |
318786 |
318786 |
|
| 380 |
318786 |
▶ | if (copy.king[side] >= 0 && shg_attacked(©, copy.king[side], other)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (copy.king[side] >= 0 && shg_attacked(©, copy.king[side], other)) |
317640 |
1146 |
318786 |
| if (copy.king[side] >= 0 && shg_attacked(©, copy.king[side], other)) |
165330 |
152310 |
317640 |
|
| 381 |
165330 |
| fault = SHG_DROP_IN_CHECK; |
| 382 |
|
| /* A dropped pawn checks only from the square in front of the king, so the |
| 383 |
|
| * check cannot be blocked and is never double: the only answers are board |
| 384 |
|
| * moves. No board move is mate. */ |
| 385 |
153456 |
▶ | else if (kind == SHG_PAWN && shg_in_check(©, other) && !any_board_move(©)) |
Branch: 3 branches
| Branch | True | False | Total |
| else if (kind == SHG_PAWN && shg_in_check(©, other) && !any_board_move(©)) |
57688 |
95768 |
153456 |
| else if (kind == SHG_PAWN && shg_in_check(©, other) && !any_board_move(©)) |
997 |
56691 |
57688 |
| else if (kind == SHG_PAWN && shg_in_check(©, other) && !any_board_move(©)) |
6 |
991 |
997 |
|
| 386 |
6 |
| fault = SHG_DROP_PAWN_MATE; |
| 387 |
318786 |
| return fault; |
| 388 |
|
| } |
| 389 |
|
| |
| 390 |
45168 |
| int shg_drops(const struct shg_board *b, int *out, int max) |
| 391 |
|
| { |
| 392 |
|
| static const int ORDER[SHG_HAND_KINDS - 1] = { SHG_ROOK, SHG_BISHOP, SHG_GOLD, |
| 393 |
|
| SHG_SILVER, SHG_KNIGHT, SHG_LANCE, SHG_PAWN }; |
| 394 |
45168 |
| int i, sq, n = 0; |
| 395 |
45168 |
▶ | if (!b || (!out && max > 0)) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!b || (!out && max > 0)) return 0; |
45168 |
0 |
45168 |
| if (!b || (!out && max > 0)) return 0; |
0 |
45168 |
45168 |
| if (!b || (!out && max > 0)) return 0; |
0 |
0 |
0 |
|
| 396 |
361344 |
▶ | for (i = 0; i < SHG_HAND_KINDS - 1; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < SHG_HAND_KINDS - 1; i++) { |
316176 |
45168 |
361344 |
|
| 397 |
316176 |
| int kind = ORDER[i]; |
| 398 |
316176 |
▶ | if (b->hand[b->to_move][kind] < 1) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->hand[b->to_move][kind] < 1) continue; |
305804 |
10372 |
316176 |
|
| 399 |
850504 |
▶ | for (sq = 0; sq < SHG_SQUARES; sq++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (sq = 0; sq < SHG_SQUARES; sq++) { |
840132 |
10372 |
850504 |
|
| 400 |
840132 |
▶ | if (shg_drop_fault(b, kind, sq) != SHG_DROP_OK) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (shg_drop_fault(b, kind, sq) != SHG_DROP_OK) continue; |
688414 |
151718 |
840132 |
|
| 401 |
151718 |
▶ | if (n < max) out[n] = SHG_DROP(kind, sq); |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < max) out[n] = SHG_DROP(kind, sq); |
151718 |
0 |
151718 |
|
| 402 |
151718 |
| n++; |
| 403 |
|
| } |
| 404 |
|
| } |
| 405 |
45168 |
| return n; |
| 406 |
|
| } |
| 407 |
|
| |
| 408 |
45159 |
| int shg_legal(const struct shg_board *b, int *out, int max) |
| 409 |
|
| { |
| 410 |
|
| int n, m, room; |
| 411 |
45159 |
▶ | if (!b || (!out && max > 0)) return 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!b || (!out && max > 0)) return 0; |
45159 |
0 |
45159 |
| if (!b || (!out && max > 0)) return 0; |
0 |
45159 |
45159 |
| if (!b || (!out && max > 0)) return 0; |
0 |
0 |
0 |
|
| 412 |
45159 |
| n = shg_board_moves(b, out, max); |
| 413 |
45159 |
▶ | room = n < max ? max - n : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| room = n < max ? max - n : 0; |
45159 |
0 |
45159 |
|
| 414 |
45159 |
▶ | m = shg_drops(b, room ? out + n : NULL, room); |
Branch: 1 branch
| Branch | True | False | Total |
| m = shg_drops(b, room ? out + n : NULL, room); |
45159 |
0 |
45159 |
|
| 415 |
45159 |
| return n + m; |
| 416 |
|
| } |
| 417 |
|
| |
| 418 |
346566 |
| int shg_has_legal_move(const struct shg_board *b) |
| 419 |
|
| { |
| 420 |
|
| int i, sq; |
| 421 |
346566 |
▶ | if (!b) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!b) return 0; |
0 |
346566 |
346566 |
|
| 422 |
346566 |
▶ | if (any_board_move(b)) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (any_board_move(b)) return 1; |
341483 |
5083 |
346566 |
|
| 423 |
40651 |
▶ | for (i = SHG_PAWN; i <= SHG_ROOK; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = SHG_PAWN; i <= SHG_ROOK; i++) { |
35571 |
5080 |
40651 |
|
| 424 |
35571 |
▶ | if (b->hand[b->to_move][i] < 1) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->hand[b->to_move][i] < 1) continue; |
28485 |
7086 |
35571 |
|
| 425 |
580972 |
▶ | for (sq = 0; sq < SHG_SQUARES; sq++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (sq = 0; sq < SHG_SQUARES; sq++) |
573889 |
7083 |
580972 |
|
| 426 |
573889 |
▶ | if (shg_drop_fault(b, i, sq) == SHG_DROP_OK) return 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (shg_drop_fault(b, i, sq) == SHG_DROP_OK) return 1; |
3 |
573886 |
573889 |
|
| 427 |
|
| } |
| 428 |
5080 |
| return 0; |
| 429 |
|
| } |
| 430 |
|
| |
| 431 |
|
| /* ---- why a move is refused, version 4 --------------------------------------- |
| 432 |
|
| * |
| 433 |
|
| * Built on the lists, so it cannot disagree with them: a board move is looked |
| 434 |
|
| * for in the pseudo-legal list, by its squares and then by its promotion, and |
| 435 |
|
| * then made on a copy to ask about the king. A drop is drop_fault's answer. |
| 436 |
|
| */ |
| 437 |
|
| |
| 438 |
129518 |
| int shg_move_fault(const struct shg_board *b, int move) |
| 439 |
|
| { |
| 440 |
|
| int all[SHG_MOVES_MAX]; |
| 441 |
129518 |
| int count, i, from, to, p, plain = 0, promote = 0, side, safe; |
| 442 |
|
| struct shg_board copy; |
| 443 |
|
| struct shg_undo u; |
| 444 |
|
| |
| 445 |
129518 |
▶ | if (!b) return SHG_FAULT_BAD; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!b) return SHG_FAULT_BAD; |
0 |
129518 |
129518 |
|
| 446 |
129518 |
▶ | if (is_drop(move)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_drop(move)) { |
18735 |
110783 |
129518 |
|
| 447 |
18735 |
| switch (shg_drop_fault(b, SHG_MOVE_KIND(move), SHG_MOVE_TO(move))) { |
| 448 |
1711 |
| case SHG_DROP_OK: return SHG_FAULT_OK; |
| 449 |
17013 |
| case SHG_DROP_NOT_HELD: return SHG_FAULT_NOT_HELD; |
| 450 |
2 |
| case SHG_DROP_OCCUPIED: return SHG_FAULT_OCCUPIED; |
| 451 |
3 |
| case SHG_DROP_DEAD: return SHG_FAULT_DEAD; |
| 452 |
3 |
| case SHG_DROP_TWO_PAWNS: return SHG_FAULT_TWO_PAWNS; |
| 453 |
1 |
| case SHG_DROP_IN_CHECK: return SHG_FAULT_IN_CHECK; |
| 454 |
2 |
| case SHG_DROP_PAWN_MATE: return SHG_FAULT_PAWN_MATE; |
| 455 |
0 |
| default: return SHG_FAULT_BAD; |
| 456 |
|
| } |
| 457 |
|
| } |
| 458 |
110783 |
▶ | if (!is_board_move(move)) return SHG_FAULT_BAD; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!is_board_move(move)) return SHG_FAULT_BAD; |
1202 |
109581 |
110783 |
|
| 459 |
|
| |
| 460 |
109581 |
| from = SHG_MOVE_FROM(move); |
| 461 |
109581 |
| to = SHG_MOVE_TO(move); |
| 462 |
109581 |
| side = b->to_move; |
| 463 |
109581 |
| p = b->cell[SHG_SQ_OF[from]]; |
| 464 |
109581 |
▶ | if (p == SHG_EMPTY || SHG_SIDE_OF(p) != side) return SHG_FAULT_NOT_YOURS; |
Branch: 2 branches
| Branch | True | False | Total |
| if (p == SHG_EMPTY || SHG_SIDE_OF(p) != side) return SHG_FAULT_NOT_YOURS; |
109572 |
9 |
109581 |
| if (p == SHG_EMPTY || SHG_SIDE_OF(p) != side) return SHG_FAULT_NOT_YOURS; |
3 |
109569 |
109572 |
|
| 465 |
|
| |
| 466 |
109569 |
| count = shg_pseudo_moves(b, all, SHG_MOVES_MAX); |
| 467 |
109569 |
▶ | if (count > SHG_MOVES_MAX) count = SHG_MOVES_MAX; |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > SHG_MOVES_MAX) count = SHG_MOVES_MAX; |
0 |
109569 |
109569 |
|
| 468 |
3560351 |
▶ | for (i = 0; i < count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < count; i++) { |
3450782 |
109569 |
3560351 |
|
| 469 |
3450782 |
▶ | if (SHG_MOVE_FROM(all[i]) != from || SHG_MOVE_TO(all[i]) != to) continue; |
Branch: 2 branches
| Branch | True | False | Total |
| if (SHG_MOVE_FROM(all[i]) != from || SHG_MOVE_TO(all[i]) != to) continue; |
195392 |
3255390 |
3450782 |
| if (SHG_MOVE_FROM(all[i]) != from || SHG_MOVE_TO(all[i]) != to) continue; |
179116 |
16276 |
195392 |
|
| 470 |
16276 |
▶ | if (SHG_MOVE_PROMOTE(all[i])) promote = 1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (SHG_MOVE_PROMOTE(all[i])) promote = 1; |
1025 |
15251 |
16276 |
|
| 471 |
15251 |
| else plain = 1; |
| 472 |
|
| } |
| 473 |
109569 |
▶ | if (!plain && !promote) return SHG_FAULT_CANNOT_REACH; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!plain && !promote) return SHG_FAULT_CANNOT_REACH; |
94318 |
15251 |
109569 |
| if (!plain && !promote) return SHG_FAULT_CANNOT_REACH; |
94229 |
89 |
94318 |
|
| 474 |
15340 |
▶ | if (SHG_MOVE_PROMOTE(move) && !promote) return SHG_FAULT_PROMOTE_NOT_ALLOWED; |
Branch: 2 branches
| Branch | True | False | Total |
| if (SHG_MOVE_PROMOTE(move) && !promote) return SHG_FAULT_PROMOTE_NOT_ALLOWED; |
1415 |
13925 |
15340 |
| if (SHG_MOVE_PROMOTE(move) && !promote) return SHG_FAULT_PROMOTE_NOT_ALLOWED; |
851 |
564 |
1415 |
|
| 475 |
14489 |
▶ | if (!SHG_MOVE_PROMOTE(move) && !plain) return SHG_FAULT_PROMOTE_REQUIRED; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SHG_MOVE_PROMOTE(move) && !plain) return SHG_FAULT_PROMOTE_REQUIRED; |
13925 |
564 |
14489 |
| if (!SHG_MOVE_PROMOTE(move) && !plain) return SHG_FAULT_PROMOTE_REQUIRED; |
3 |
13922 |
13925 |
|
| 476 |
|
| |
| 477 |
14486 |
| copy = *b; |
| 478 |
14486 |
▶ | if (!shg_do_move(©, move, &u)) return SHG_FAULT_BAD; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!shg_do_move(©, move, &u)) return SHG_FAULT_BAD; |
0 |
14486 |
14486 |
|
| 479 |
28972 |
| safe = copy.king[side] < 0 |
| 480 |
14486 |
▶ | || !shg_attacked(©, copy.king[side], side == SHG_BLACK ? SHG_WHITE : SHG_BLACK); |
Branch: 2 branches
| Branch | True | False | Total |
| || !shg_attacked(©, copy.king[side], side == SHG_BLACK ? SHG_WHITE : SHG_BLACK); |
14486 |
0 |
14486 |
| || !shg_attacked(©, copy.king[side], side == SHG_BLACK ? SHG_WHITE : SHG_BLACK); |
14483 |
3 |
14486 |
|
| 481 |
14486 |
▶ | return safe ? SHG_FAULT_OK : SHG_FAULT_IN_CHECK; |
Branch: 1 branch
| Branch | True | False | Total |
| return safe ? SHG_FAULT_OK : SHG_FAULT_IN_CHECK; |
14483 |
3 |
14486 |
|
| 482 |
|
| } |
| 483 |
|
| |
| 484 |
|
| /* For the search: whether the side to move has a legal board move. */ |
| 485 |
4602 |
▶ | int shg_any_board_move(const struct shg_board *b) { return b ? any_board_move(b) : 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| int shg_any_board_move(const struct shg_board *b) { return b ? any_board_move(b) : 0; } |
4602 |
0 |
4602 |
|