shg_judge.c

filter: Statementstmt 100% 63 / 63 Branchbran 85.0% 85 / 100 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 90.8% 148 / 163 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« shg_engine.c ↑ Summary shg_search.c »
1 /* shg_judge.c - what needs a game's history: a position packed into bytes,
2 * the fourfold repetition and the perpetual check, and the impasse
3 * declaration.
4 *
5 * PERL-FREE by design: see include/shg_abi.h. The history is the caller's;
6 * this file reads it and keeps nothing.
7 */
8  
9 #include <string.h>
10 #include "shg_internal.h"
11  
12 /* ---- a packed position ----------------------------------------------------- */
13  
14 63309 int shg_pack(const struct shg_board *b, unsigned char *out)
15 {
16 63309 int sq, side, kind, n = 0;
17 63309 ▶ if (!b || !out) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!b || !out) return -1; 63309 0 63309
if (!b || !out) return -1; 0 63309 63309
18 5191338 ▶ for (sq = 0; sq < SHG_SQUARES; sq++) out[n++] = b->cell[SHG_SQ_OF[sq]];
Branch: 1 branch
BranchTrueFalseTotal
for (sq = 0; sq < SHG_SQUARES; sq++) out[n++] = b->cell[SHG_SQ_OF[sq]]; 5128029 63309 5191338
19 189927 ▶ for (side = 0; side < SHG_SIDES; side++)
Branch: 1 branch
BranchTrueFalseTotal
for (side = 0; side < SHG_SIDES; side++) 126618 63309 189927
20 1012944 ▶ for (kind = SHG_PAWN; kind <= SHG_ROOK; kind++) out[n++] = b->hand[side][kind];
Branch: 1 branch
BranchTrueFalseTotal
for (kind = SHG_PAWN; kind <= SHG_ROOK; kind++) out[n++] = b->hand[side][kind]; 886326 126618 1012944
21 63309 out[n++] = (unsigned char) b->to_move;
22 63309 return n;
23 }
24  
25 /* ---- the repetition judge ------------------------------------------------------
26 *
27 * W line 613. The last position is judged: when it has stood SHG_REP_TIMES
28 * times, counting itself, the game is over. Between the first of those times
29 * and the last, a side whose every move gave check has kept checking, and
30 * loses. Which side made the move into position j is the side to move in
31 * position j - 1, the last byte of its packing.
32 */
33  
34 29550 int shg_repetition(const unsigned long long *keys, const unsigned char *packed,
35 const unsigned char *gave_check, int n, int *loser)
36 {
37 29550 int last, j, seen = 0, first = -1, side;
38 29550 int moved[SHG_SIDES] = { 0, 0 }, all_check[SHG_SIDES] = { 1, 1 }, kept = -1, keepers = 0;
39 const unsigned char *now;
40  
41 29550 ▶ if (loser) *loser = -1;
Branch: 1 branch
BranchTrueFalseTotal
if (loser) *loser = -1; 29550 0 29550
42 29550 ▶ if (!keys || !packed || !gave_check || n < 1) return SHG_REP_NONE;
Branch: 4 branches
BranchTrueFalseTotal
if (!keys || !packed || !gave_check || n < 1) return SHG_REP_NONE; 29550 0 29550
if (!keys || !packed || !gave_check || n < 1) return SHG_REP_NONE; 29550 0 29550
if (!keys || !packed || !gave_check || n < 1) return SHG_REP_NONE; 29550 0 29550
if (!keys || !packed || !gave_check || n < 1) return SHG_REP_NONE; 0 29550 29550
43 29550 last = n - 1;
44 29550 now = packed + (size_t) last * SHG_PACKED;
45 2281815 ▶ for (j = last; j >= 0; j--) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = last; j >= 0; j--) { 2252276 29539 2281815
46 2252276 ▶ if (keys[j] != keys[last]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (keys[j] != keys[last]) continue; 2222421 29855 2252276
47 29855 ▶ if (memcmp(packed + (size_t) j * SHG_PACKED, now, SHG_PACKED) != 0) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (memcmp(packed + (size_t) j * SHG_PACKED, now, SHG_PACKED) != 0) continue; 5 29850 29855
48 29850 ▶ if (++seen == SHG_REP_TIMES) { first = j; break; }
Branch: 1 branch
BranchTrueFalseTotal
if (++seen == SHG_REP_TIMES) { first = j; break; } 11 29839 29850
49 }
50 29550 ▶ if (first < 0) return SHG_REP_NONE;
Branch: 1 branch
BranchTrueFalseTotal
if (first < 0) return SHG_REP_NONE; 29539 11 29550
51  
52 137 ▶ for (j = first + 1; j <= last; j++) {
Branch: 1 branch
BranchTrueFalseTotal
for (j = first + 1; j <= last; j++) { 126 11 137
53 126 int mover = packed[(size_t) (j - 1) * SHG_PACKED + SHG_PACKED - 1];
54 126 ▶ if (!SHG_IS_SIDE(mover)) continue;
Branch: 2 branches
BranchTrueFalseTotal
if (!SHG_IS_SIDE(mover)) continue; 63 63 126
if (!SHG_IS_SIDE(mover)) continue; 0 63 63
55 126 moved[mover] = 1;
56 126 ▶ if (!gave_check[j]) all_check[mover] = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!gave_check[j]) all_check[mover] = 0; 92 34 126
57 }
58 33 ▶ for (side = 0; side < SHG_SIDES; side++)
Branch: 1 branch
BranchTrueFalseTotal
for (side = 0; side < SHG_SIDES; side++) 22 11 33
59 22 ▶ if (moved[side] && all_check[side]) { kept = side; keepers++; }
Branch: 2 branches
BranchTrueFalseTotal
if (moved[side] && all_check[side]) { kept = side; keepers++; } 22 0 22
if (moved[side] && all_check[side]) { kept = side; keepers++; } 5 17 22
60 11 ▶ if (keepers == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (keepers == 1) { 5 6 11
61 5 ▶ if (loser) *loser = kept;
Branch: 1 branch
BranchTrueFalseTotal
if (loser) *loser = kept; 5 0 5
62 5 return SHG_REP_PERPETUAL;
63 }
64 6 return SHG_REP_DRAW;
65 }
66  
67 /* ---- the impasse declaration ------------------------------------------------------
68 *
69 * Y lines 101-106 and 125. The camp is the declarer's promotion zone, the
70 * three ranks the opponent started on.
71 */
72  
73 465 static int worth(int kind)
74 {
75 465 int base = SHG_UNPROMOTED_OF[kind];
76 465 ▶ return base == SHG_ROOK || base == SHG_BISHOP ? 5 : 1;
Branch: 2 branches
BranchTrueFalseTotal
return base == SHG_ROOK || base == SHG_BISHOP ? 5 : 1; 383 82 465
return base == SHG_ROOK || base == SHG_BISHOP ? 5 : 1; 81 302 383
77 }
78  
79 28 int shg_impasse_points(const struct shg_board *b, int side)
80 {
81 28 int sq, kind, points = 0;
82 28 ▶ if (!b || !SHG_IS_SIDE(side)) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || !SHG_IS_SIDE(side)) return -1; 28 0 28
if (!b || !SHG_IS_SIDE(side)) return -1; 7 21 28
if (!b || !SHG_IS_SIDE(side)) return -1; 0 7 7
83 2296 ▶ for (sq = 0; sq < SHG_SQUARES; sq++) {
Branch: 1 branch
BranchTrueFalseTotal
for (sq = 0; sq < SHG_SQUARES; sq++) { 2268 28 2296
84 2268 int p = b->cell[SHG_SQ_OF[sq]];
85 2268 ▶ if (p == SHG_EMPTY || SHG_SIDE_OF(p) != side || SHG_KIND_OF(p) == SHG_KING) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (p == SHG_EMPTY || SHG_SIDE_OF(p) != side || SHG_KIND_OF(p) == SHG_KING) continue; 336 1932 2268
if (p == SHG_EMPTY || SHG_SIDE_OF(p) != side || SHG_KIND_OF(p) == SHG_KING) continue; 298 38 336
if (p == SHG_EMPTY || SHG_SIDE_OF(p) != side || SHG_KIND_OF(p) == SHG_KING) continue; 28 270 298
86 270 ▶ if (!SHG_ZONE[side][sq]) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!SHG_ZONE[side][sq]) continue; 1 269 270
87 269 points += worth(SHG_KIND_OF(p));
88 }
89 224 ▶ for (kind = SHG_PAWN; kind <= SHG_ROOK; kind++) points += b->hand[side][kind] * worth(kind);
Branch: 1 branch
BranchTrueFalseTotal
for (kind = SHG_PAWN; kind <= SHG_ROOK; kind++) points += b->hand[side][kind] * worth(kind); 196 28 224
90 28 return points;
91 }
92  
93 47 int shg_impasse_pieces(const struct shg_board *b, int side)
94 {
95 47 int sq, pieces = 0;
96 47 ▶ if (!b || !SHG_IS_SIDE(side)) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || !SHG_IS_SIDE(side)) return -1; 47 0 47
if (!b || !SHG_IS_SIDE(side)) return -1; 14 33 47
if (!b || !SHG_IS_SIDE(side)) return -1; 0 14 14
97 3854 ▶ for (sq = 0; sq < SHG_SQUARES; sq++) {
Branch: 1 branch
BranchTrueFalseTotal
for (sq = 0; sq < SHG_SQUARES; sq++) { 3807 47 3854
98 3807 int p = b->cell[SHG_SQ_OF[sq]];
99 3807 ▶ if (p == SHG_EMPTY || SHG_SIDE_OF(p) != side || SHG_KIND_OF(p) == SHG_KING) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (p == SHG_EMPTY || SHG_SIDE_OF(p) != side || SHG_KIND_OF(p) == SHG_KING) continue; 935 2872 3807
if (p == SHG_EMPTY || SHG_SIDE_OF(p) != side || SHG_KIND_OF(p) == SHG_KING) continue; 595 340 935
if (p == SHG_EMPTY || SHG_SIDE_OF(p) != side || SHG_KIND_OF(p) == SHG_KING) continue; 47 548 595
100 548 ▶ if (SHG_ZONE[side][sq]) pieces++;
Branch: 1 branch
BranchTrueFalseTotal
if (SHG_ZONE[side][sq]) pieces++; 517 31 548
101 }
102 47 return pieces;
103 }
104  
105 47 int shg_declare_fault(const struct shg_board *b)
106 {
107 int side, king;
108 47 ▶ if (!b) return SHG_DECLARE_NO_KING;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return SHG_DECLARE_NO_KING; 0 47 47
109 47 side = b->to_move;
110 47 king = b->king[side];
111 47 ▶ if (king < 0 || !SHG_ZONE[side][king]) return SHG_DECLARE_NO_KING;
Branch: 2 branches
BranchTrueFalseTotal
if (king < 0 || !SHG_ZONE[side][king]) return SHG_DECLARE_NO_KING; 46 1 47
if (king < 0 || !SHG_ZONE[side][king]) return SHG_DECLARE_NO_KING; 22 24 46
112 24 ▶ if (shg_impasse_pieces(b, side) < SHG_DECLARE_PIECES) return SHG_DECLARE_FEW_PIECES;
Branch: 1 branch
BranchTrueFalseTotal
if (shg_impasse_pieces(b, side) < SHG_DECLARE_PIECES) return SHG_DECLARE_FEW_PIECES; 2 22 24
113 22 ▶ if (shg_in_check(b, side)) return SHG_DECLARE_IN_CHECK;
Branch: 1 branch
BranchTrueFalseTotal
if (shg_in_check(b, side)) return SHG_DECLARE_IN_CHECK; 2 20 22
114 40 ▶ if (shg_impasse_points(b, side)
Branch: 1 branch
BranchTrueFalseTotal
if (shg_impasse_points(b, side) 3 17 20
115 20 ▶ < (side == SHG_BLACK ? SHG_DECLARE_POINTS_BLACK : SHG_DECLARE_POINTS_WHITE))
Branch: 1 branch
BranchTrueFalseTotal
< (side == SHG_BLACK ? SHG_DECLARE_POINTS_BLACK : SHG_DECLARE_POINTS_WHITE)) 16 4 20
116 3 return SHG_DECLARE_FEW_POINTS;
117 17 return SHG_DECLARE_OK;
118 }
« shg_engine.c ↑ Summary shg_search.c »