shg_mate.c

filter: Statementstmt 100% 56 / 56 Branchbran 80.2% 69 / 86 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.0% 125 / 142 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« shg_moves.c ↑ Summary shg_engine.c »
1 /* shg_mate.c - whether a square is attacked, and check.
2 *
3 * PERL-FREE by design: see include/shg_abi.h.
4 *
5 * THIS FILE'S SCOPE at version 2: attacked and in_check. Mate and the
6 * pawn-drop mate test arrive with version 3.
7 */
8  
9 #include "shg_internal.h"
10  
11 /* A piece of `by` attacks `sq` when one of its steps lands there, or when it
12 * is the first piece along a ray from `sq` and slides back along that ray.
13 *
14 * THE STEPS ARE ASKED BACKWARDS: for each kind and each of its steps, is that
15 * kind of `by` standing one step short of `sq`? That covers the knight, whose
16 * jump is a step, and every piece that touches `sq`. The rays then find the
17 * sliders, near or far. */
18 13185444 int shg_attacked(const struct shg_board *b, int sq, int by)
19 {
20 int box, kind, i, d;
21 13185444 ▶ if (!b || !SHG_IS_SQ(sq) || !SHG_IS_SIDE(by)) return 0;
Branch: 5 branches
BranchTrueFalseTotal
if (!b || !SHG_IS_SQ(sq) || !SHG_IS_SIDE(by)) return 0; 13185444 0 13185444
if (!b || !SHG_IS_SQ(sq) || !SHG_IS_SIDE(by)) return 0; 13185444 0 13185444
if (!b || !SHG_IS_SQ(sq) || !SHG_IS_SIDE(by)) return 0; 13185444 0 13185444
if (!b || !SHG_IS_SQ(sq) || !SHG_IS_SIDE(by)) return 0; 8260062 4925382 13185444
if (!b || !SHG_IS_SQ(sq) || !SHG_IS_SIDE(by)) return 0; 0 8260062 8260062
22 13185444 box = SHG_SQ_OF[sq];
23  
24 169357323 ▶ for (kind = SHG_PAWN; kind <= SHG_DRAGON; kind++) {
Branch: 1 branch
BranchTrueFalseTotal
for (kind = SHG_PAWN; kind <= SHG_DRAGON; kind++) { 159437270 9920053 169357323
25 159437270 int piece = SHG_PIECE(by, kind);
26 745300898 ▶ for (i = 0; i < SHG_NSTEPS[by][kind]; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < SHG_NSTEPS[by][kind]; i++) 589129019 156171879 745300898
27 589129019 ▶ if (b->cell[box - SHG_STEP_OFF[by][kind][i]] == piece) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (b->cell[box - SHG_STEP_OFF[by][kind][i]] == piece) return 1; 3265391 585863628 589129019
28 }
29  
30 79852318 ▶ for (d = 0; d < SHG_DIRS; d++) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < SHG_DIRS; d++) { 71797653 8054665 79852318
31 71797653 int off = SHG_DIR_OFF[d], t, c;
32 158921644 ▶ for (t = box + off; (c = b->cell[t]) == SHG_EMPTY; t += off)
Branch: 1 branch
BranchTrueFalseTotal
for (t = box + off; (c = b->cell[t]) == SHG_EMPTY; t += off) 87123991 71797653 158921644
33 ;
34 71797653 ▶ if (c == SHG_OFF || SHG_SIDE_OF(c) != by) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (c == SHG_OFF || SHG_SIDE_OF(c) != by) continue; 39872584 31925069 71797653
if (c == SHG_OFF || SHG_SIDE_OF(c) != by) continue; 32693631 7178953 39872584
35 /* The piece at t moves towards sq, the opposite of d: directions are
36 * listed so that d's opposite is the one with the offset negated. */
37 {
38 7178953 int back, k = SHG_KIND_OF(c);
39 37015915 ▶ for (back = 0; back < SHG_DIRS; back++)
Branch: 1 branch
BranchTrueFalseTotal
for (back = 0; back < SHG_DIRS; back++) 37015915 0 37015915
40 37015915 ▶ if (SHG_DIR_OFF[back] == -off) break;
Branch: 1 branch
BranchTrueFalseTotal
if (SHG_DIR_OFF[back] == -off) break; 7178953 29836962 37015915
41 7178953 ▶ if (back < SHG_DIRS && (SHG_SLIDE_MASK[by][k] >> back & 1u)) return 1;
Branch: 2 branches
BranchTrueFalseTotal
if (back < SHG_DIRS && (SHG_SLIDE_MASK[by][k] >> back & 1u)) return 1; 7178953 0 7178953
if (back < SHG_DIRS && (SHG_SLIDE_MASK[by][k] >> back & 1u)) return 1; 1865388 5313565 7178953
42 }
43 }
44 8054665 return 0;
45 }
46  
47 3205828 int shg_in_check(const struct shg_board *b, int side)
48 {
49 3205828 ▶ if (!b || !SHG_IS_SIDE(side) || b->king[side] < 0) return 0;
Branch: 4 branches
BranchTrueFalseTotal
if (!b || !SHG_IS_SIDE(side) || b->king[side] < 0) return 0; 3205828 0 3205828
if (!b || !SHG_IS_SIDE(side) || b->king[side] < 0) return 0; 1340576 1865252 3205828
if (!b || !SHG_IS_SIDE(side) || b->king[side] < 0) return 0; 1340576 0 1340576
if (!b || !SHG_IS_SIDE(side) || b->king[side] < 0) return 0; 1032 3204796 3205828
50 3204796 return shg_attacked(b, b->king[side], side == SHG_BLACK ? SHG_WHITE : SHG_BLACK);
51 }
52  
53 /* ---- perft, version 3 -----------------------------------------------------
54 *
55 * Every legal move to `depth` plies, counted at the last: D's table also
56 * counts, of those last moves, the captures, the drops, the promotions, the
57 * checks and the mates, and so does this when it is asked to.
58 */
59  
60 27475 static unsigned long long perft_from(struct shg_board *b, int depth, unsigned long long *counts)
61 {
62 int moves[SHG_MOVES_MAX];
63 int n, i;
64 27475 unsigned long long nodes = 0;
65 struct shg_undo u;
66  
67 27475 n = shg_legal(b, moves, SHG_MOVES_MAX);
68 27475 ▶ if (n > SHG_MOVES_MAX) n = SHG_MOVES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
if (n > SHG_MOVES_MAX) n = SHG_MOVES_MAX; 0 27475 27475
69 27475 ▶ if (depth == 1 && !counts) return (unsigned long long) n;
Branch: 2 branches
BranchTrueFalseTotal
if (depth == 1 && !counts) return (unsigned long long) n; 26509 966 27475
if (depth == 1 && !counts) return (unsigned long long) n; 0 26509 26509
70  
71 803023 ▶ for (i = 0; i < n; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) { 775548 27475 803023
72 775548 int m = moves[i];
73 775548 ▶ if (depth == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth == 1) { 748086 27462 775548
74 748086 int drop = SHG_MOVE_DROP(m);
75 748086 ▶ int capture = !drop && b->cell[SHG_SQ_OF[SHG_MOVE_TO(m)]] != SHG_EMPTY;
Branch: 2 branches
BranchTrueFalseTotal
int capture = !drop && b->cell[SHG_SQ_OF[SHG_MOVE_TO(m)]] != SHG_EMPTY; 747545 541 748086
int capture = !drop && b->cell[SHG_SQ_OF[SHG_MOVE_TO(m)]] != SHG_EMPTY; 1868 745677 747545
76 748086 counts[SHG_PERFT_NODES]++;
77 748086 ▶ if (capture) counts[SHG_PERFT_CAPTURES]++;
Branch: 1 branch
BranchTrueFalseTotal
if (capture) counts[SHG_PERFT_CAPTURES]++; 1868 746218 748086
78 748086 ▶ if (drop) counts[SHG_PERFT_DROPS]++;
Branch: 1 branch
BranchTrueFalseTotal
if (drop) counts[SHG_PERFT_DROPS]++; 541 747545 748086
79 748086 ▶ if (SHG_MOVE_PROMOTE(m)) counts[SHG_PERFT_PROMOTIONS]++;
Branch: 1 branch
BranchTrueFalseTotal
if (SHG_MOVE_PROMOTE(m)) counts[SHG_PERFT_PROMOTIONS]++; 926 747160 748086
80 748086 ▶ if (!shg_do_move(b, m, &u)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!shg_do_move(b, m, &u)) continue; 0 748086 748086
81 748086 ▶ if (shg_in_check(b, b->to_move)) {
Branch: 1 branch
BranchTrueFalseTotal
if (shg_in_check(b, b->to_move)) { 1212 746874 748086
82 1212 counts[SHG_PERFT_CHECKS]++;
83 1212 ▶ if (!shg_has_legal_move(b)) counts[SHG_PERFT_MATES]++;
Branch: 1 branch
BranchTrueFalseTotal
if (!shg_has_legal_move(b)) counts[SHG_PERFT_MATES]++; 8 1204 1212
84 }
85 748086 shg_undo_move(b, &u);
86 748086 nodes++;
87 748086 continue;
88 }
89 27462 ▶ if (!shg_do_move(b, m, &u)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!shg_do_move(b, m, &u)) continue; 0 27462 27462
90 27462 nodes += perft_from(b, depth - 1, counts);
91 27462 shg_undo_move(b, &u);
92 }
93 27475 return nodes;
94 }
95  
96 14 unsigned long long shg_perft(const struct shg_board *b, int depth, unsigned long long *counts)
97 {
98 struct shg_board copy;
99 int i;
100 98 ▶ if (counts) for (i = 0; i < SHG_PERFT_COUNTS; i++) counts[i] = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (counts) for (i = 0; i < SHG_PERFT_COUNTS; i++) counts[i] = 0; 14 0 14
if (counts) for (i = 0; i < SHG_PERFT_COUNTS; i++) counts[i] = 0; 84 14 98
101 14 ▶ if (!b || depth < 0) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (!b || depth < 0) return 0; 14 0 14
if (!b || depth < 0) return 0; 0 14 14
102 14 ▶ if (depth == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (depth == 0) { 1 13 14
103 1 ▶ if (counts) counts[SHG_PERFT_NODES] = 1;
Branch: 1 branch
BranchTrueFalseTotal
if (counts) counts[SHG_PERFT_NODES] = 1; 1 0 1
104 1 return 1;
105 }
106 13 copy = *b;
107 13 return perft_from(&copy, depth, counts);
108 }
109  
110 /* ---- how a position stands, version 4 ------------------------------------ */
111  
112 30819 int shg_status(const struct shg_board *b)
113 {
114 30819 ▶ if (!b) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return -1; 0 30819 30819
115 30819 ▶ if (shg_has_legal_move(b)) return SHG_ONGOING;
Branch: 1 branch
BranchTrueFalseTotal
if (shg_has_legal_move(b)) return SHG_ONGOING; 30782 37 30819
116 37 ▶ return shg_in_check(b, b->to_move) ? SHG_MATE : SHG_NO_MOVE;
Branch: 1 branch
BranchTrueFalseTotal
return shg_in_check(b, b->to_move) ? SHG_MATE : SHG_NO_MOVE; 34 3 37
117 }
« shg_moves.c ↑ Summary shg_engine.c »