shg_engine.c

filter: Statementstmt 99.0% 316 / 319 Branchbran 81.2% 377 / 464 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 88.5% 693 / 783 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« shg_mate.c ↑ Summary shg_judge.c »
1 /* shg_engine.c - the shogi board, and nothing that judges a move.
2 *
3 * PERL-FREE by design: see include/shg_abi.h. This file compiles into the XS,
4 * into a plain program, or to wasm, and knows nothing about any of them.
5 *
6 * THIS FILE'S SCOPE. The mailbox, the square names, the step, the zone, the
7 * kinds and their promotion, the set, put and lift, the hands, the side to
8 * move, the move number, the zobrist key, and the SFEN in and out.
9 */
10  
11 #include <stdlib.h>
12 #include <string.h>
13 #include "shg_internal.h"
14  
15 /* ---- the mailbox and the tables: see shg_internal.h ------------------------- */
16  
17 int SHG_SQ_OF[SHG_SQUARES];
18 int SHG_SQ_AT[SHG_BOX];
19 static int FILE_OF[SHG_SQUARES];
20 static int RANK_OF[SHG_SQUARES];
21 static int ZONE[SHG_SIDES][SHG_SQUARES];
22 static int LAST[SHG_SIDES][SHG_SQUARES];
23 static int TABLES_READY = 0;
24 static int TABLES_OK = 0;
25  
26 /* ---- the kinds and the set ------------------------------------------------------
27 *
28 * Indexed by kind. A promoted kind's letter is its base kind's, written after
29 * a '+'. SET is how many of each base kind a whole set has: nine pawns, two
30 * lances, two knights, two silvers, two golds, one bishop, one rook and one
31 * king a side.
32 */
33  
34 static const char LETTER[SHG_KINDS] = { 0, 'P', 'L', 'N', 'S', 'G', 'B', 'R', 'K',
35 'P', 'L', 'N', 'S', 'B', 'R' };
36 static const int PROMOTED[SHG_KINDS] = { 0, 9, 10, 11, 12, 0, 13, 14, 0, 0, 0, 0, 0, 0, 0 };
37 static const int UNPROMOTED[SHG_KINDS] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 6, 7 };
38 static const int SET[SHG_KINDS] = { 0, 18, 4, 4, 4, 4, 2, 2, 2, 0, 0, 0, 0, 0, 0 };
39  
40 int (*SHG_ZONE)[SHG_SQUARES] = ZONE;
41 int (*SHG_LAST)[SHG_SQUARES] = LAST;
42 const int *SHG_PROMOTED_OF = PROMOTED;
43 const int *SHG_UNPROMOTED_OF = UNPROMOTED;
44 const int *SHG_SET_OF = SET;
45  
46 /* The hands are written in this order, which U gives: rook, bishop, gold,
47 * silver, knight, lance, pawn. */
48 static const int HAND_ORDER[SHG_HAND_KINDS - 1] = { SHG_ROOK, SHG_BISHOP, SHG_GOLD,
49 SHG_SILVER, SHG_KNIGHT, SHG_LANCE, SHG_PAWN };
50  
51 /* The start of an even game, as SFEN reads it, rank a first. */
52 static const char *START[SHG_RANKS] = {
53 "lnsgkgsnl", ".r.....b.", "ppppppppp", ".........", ".........",
54 ".........", "PPPPPPPPP", ".B.....R.", "LNSGKGSNL",
55 };
56  
57 160866 static int kind_of_letter(int c)
58 {
59 int k;
60 160866 ▶ if (c >= 'a' && c <= 'z') c -= 'a' - 'A';
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 'a' && c <= 'z') c -= 'a' - 'A'; 59417 101449 160866
if (c >= 'a' && c <= 'z') c -= 'a' - 'A'; 59417 0 59417
61 703044 ▶ for (k = SHG_PAWN; k <= SHG_KING; k++)
Branch: 1 branch
BranchTrueFalseTotal
for (k = SHG_PAWN; k <= SHG_KING; k++) 667124 35920 703044
62 667124 ▶ if (LETTER[k] == c) return k;
Branch: 1 branch
BranchTrueFalseTotal
if (LETTER[k] == c) return k; 124946 542178 667124
63 35920 return 0;
64 }
65  
66 3127032 void shg_tables_init(void)
67 {
68 3127032 int sq, box, side, df, dr, k, n = 0, ok = 1;
69 3127032 int zone_n[SHG_SIDES] = { 0, 0 };
70 3127032 int set_total = 0;
71  
72 3127032 ▶ if (TABLES_READY) return;
Branch: 1 branch
BranchTrueFalseTotal
if (TABLES_READY) return; 3126985 47 3127032
73 6768 ▶ for (box = 0; box < SHG_BOX; box++) SHG_SQ_AT[box] = -1;
Branch: 1 branch
BranchTrueFalseTotal
for (box = 0; box < SHG_BOX; box++) SHG_SQ_AT[box] = -1; 6721 47 6768
74  
75 3854 ▶ for (sq = 0; sq < SHG_SQUARES; sq++) {
Branch: 1 branch
BranchTrueFalseTotal
for (sq = 0; sq < SHG_SQUARES; sq++) { 3807 47 3854
76 3807 int row = sq / SHG_FILES, col = sq % SHG_FILES;
77 3807 box = (row + SHG_MARGIN_ROWS) * SHG_WIDTH + (col + SHG_MARGIN_COLS);
78 3807 SHG_SQ_OF[sq] = box;
79 3807 SHG_SQ_AT[box] = sq;
80 3807 FILE_OF[sq] = SHG_FILES - col;
81 3807 RANK_OF[sq] = row + 1;
82 3807 ZONE[SHG_BLACK][sq] = RANK_OF[sq] <= SHG_ZONE_RANKS;
83 3807 ZONE[SHG_WHITE][sq] = RANK_OF[sq] > SHG_RANKS - SHG_ZONE_RANKS;
84 3807 LAST[SHG_BLACK][sq] = RANK_OF[sq] - 1;
85 3807 LAST[SHG_WHITE][sq] = SHG_RANKS - RANK_OF[sq];
86 11421 ▶ for (side = 0; side < SHG_SIDES; side++) zone_n[side] += ZONE[side][sq];
Branch: 1 branch
BranchTrueFalseTotal
for (side = 0; side < SHG_SIDES; side++) zone_n[side] += ZONE[side][sq]; 7614 3807 11421
87 3807 n++;
88 }
89 47 ▶ if (n != SHG_SQUARES) ok = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n != SHG_SQUARES) ok = 0; 0 47 47
90 47 ▶ if (zone_n[SHG_BLACK] != 27 || zone_n[SHG_WHITE] != 27) ok = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (zone_n[SHG_BLACK] != 27 || zone_n[SHG_WHITE] != 27) ok = 0; 47 0 47
if (zone_n[SHG_BLACK] != 27 || zone_n[SHG_WHITE] != 27) ok = 0; 0 47 47
91  
92 /* Every step the mailbox serves, from every square, against the arithmetic
93 * on files and ranks. A margin too narrow, or an offset with the wrong
94 * sign, cannot pass this. */
95 3854 ▶ for (sq = 0; sq < SHG_SQUARES; sq++)
Branch: 1 branch
BranchTrueFalseTotal
for (sq = 0; sq < SHG_SQUARES; sq++) 3807 47 3854
96 15228 ▶ for (df = -1; df <= 1; df++)
Branch: 1 branch
BranchTrueFalseTotal
for (df = -1; df <= 1; df++) 11421 3807 15228
97 68526 ▶ for (dr = -2; dr <= 2; dr++) {
Branch: 1 branch
BranchTrueFalseTotal
for (dr = -2; dr <= 2; dr++) { 57105 11421 68526
98 57105 int f = FILE_OF[sq] + df, r = RANK_OF[sq] + dr;
99 54990 ▶ int want = (f >= 1 && f <= SHG_FILES && r >= 1 && r <= SHG_RANKS)
Branch: 3 branches
BranchTrueFalseTotal
int want = (f >= 1 && f <= SHG_FILES && r >= 1 && r <= SHG_RANKS) 52875 2115 54990
int want = (f >= 1 && f <= SHG_FILES && r >= 1 && r <= SHG_RANKS) 49350 3525 52875
int want = (f >= 1 && f <= SHG_FILES && r >= 1 && r <= SHG_RANKS) 45825 3525 49350
100 112095 ▶ ? (r - 1) * SHG_FILES + (SHG_FILES - f) : -1;
Branch: 1 branch
BranchTrueFalseTotal
? (r - 1) * SHG_FILES + (SHG_FILES - f) : -1; 54990 2115 57105
101 57105 int at = SHG_SQ_OF[sq] + dr * SHG_WIDTH - df;
102 57105 ▶ if (at < 0 || at >= SHG_BOX || SHG_SQ_AT[at] != want) ok = 0;
Branch: 3 branches
BranchTrueFalseTotal
if (at < 0 || at >= SHG_BOX || SHG_SQ_AT[at] != want) ok = 0; 57105 0 57105
if (at < 0 || at >= SHG_BOX || SHG_SQ_AT[at] != want) ok = 0; 57105 0 57105
if (at < 0 || at >= SHG_BOX || SHG_SQ_AT[at] != want) ok = 0; 0 57105 57105
103 }
104  
105 705 ▶ for (k = SHG_PAWN; k <= SHG_DRAGON; k++) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = SHG_PAWN; k <= SHG_DRAGON; k++) { 658 47 705
106 658 set_total += SET[k];
107 658 ▶ if (UNPROMOTED[k] < SHG_PAWN || UNPROMOTED[k] > SHG_KING) ok = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (UNPROMOTED[k] < SHG_PAWN || UNPROMOTED[k] > SHG_KING) ok = 0; 658 0 658
if (UNPROMOTED[k] < SHG_PAWN || UNPROMOTED[k] > SHG_KING) ok = 0; 0 658 658
108 658 ▶ if (PROMOTED[k] && UNPROMOTED[PROMOTED[k]] != k) ok = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (PROMOTED[k] && UNPROMOTED[PROMOTED[k]] != k) ok = 0; 282 376 658
if (PROMOTED[k] && UNPROMOTED[PROMOTED[k]] != k) ok = 0; 0 282 282
109 }
110 47 ▶ if (set_total != SHG_SET_PIECES || SET[SHG_PAWN] != SHG_SET_PAWNS) ok = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (set_total != SHG_SET_PIECES || SET[SHG_PAWN] != SHG_SET_PAWNS) ok = 0; 47 0 47
if (set_total != SHG_SET_PIECES || SET[SHG_PAWN] != SHG_SET_PAWNS) ok = 0; 0 47 47
111  
112 47 ▶ if (!shg_move_tables_init()) ok = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (!shg_move_tables_init()) ok = 0; 0 47 47
113  
114 47 TABLES_OK = ok;
115 47 TABLES_READY = 1;
116 }
117  
118 1 static int shg_tables_ok(void) { shg_tables_init(); return TABLES_OK; }
119  
120 325615 static int shg_sq_of(int file, int rank)
121 {
122 325615 ▶ if (file < 1 || file > SHG_FILES || rank < 1 || rank > SHG_RANKS) return -1;
Branch: 4 branches
BranchTrueFalseTotal
if (file < 1 || file > SHG_FILES || rank < 1 || rank > SHG_RANKS) return -1; 325614 1 325615
if (file < 1 || file > SHG_FILES || rank < 1 || rank > SHG_RANKS) return -1; 325613 1 325614
if (file < 1 || file > SHG_FILES || rank < 1 || rank > SHG_RANKS) return -1; 325612 1 325613
if (file < 1 || file > SHG_FILES || rank < 1 || rank > SHG_RANKS) return -1; 1 325611 325612
123 325611 return (rank - 1) * SHG_FILES + (SHG_FILES - file);
124 }
125  
126 1559130 ▶static int shg_file_of(int sq) { shg_tables_init(); return SHG_IS_SQ(sq) ? FILE_OF[sq] : 0; }
Branch: 2 branches
BranchTrueFalseTotal
static int shg_file_of(int sq) { shg_tables_init(); return SHG_IS_SQ(sq) ? FILE_OF[sq] : 0; } 1559130 0 1559130
static int shg_file_of(int sq) { shg_tables_init(); return SHG_IS_SQ(sq) ? FILE_OF[sq] : 0; } 1559130 0 1559130
127 1559211 ▶static int shg_rank_of(int sq) { shg_tables_init(); return SHG_IS_SQ(sq) ? RANK_OF[sq] : 0; }
Branch: 2 branches
BranchTrueFalseTotal
static int shg_rank_of(int sq) { shg_tables_init(); return SHG_IS_SQ(sq) ? RANK_OF[sq] : 0; } 1559211 0 1559211
static int shg_rank_of(int sq) { shg_tables_init(); return SHG_IS_SQ(sq) ? RANK_OF[sq] : 0; } 1559211 0 1559211
128  
129 1225 static int shg_step(int sq, int dfile, int drank)
130 {
131 1225 shg_tables_init();
132 1225 ▶ if (!SHG_IS_SQ(sq) || dfile < -1 || dfile > 1 || drank < -2 || drank > 2) return -1;
Branch: 6 branches
BranchTrueFalseTotal
if (!SHG_IS_SQ(sq) || dfile < -1 || dfile > 1 || drank < -2 || drank > 2) return -1; 1225 0 1225
if (!SHG_IS_SQ(sq) || dfile < -1 || dfile > 1 || drank < -2 || drank > 2) return -1; 1225 0 1225
if (!SHG_IS_SQ(sq) || dfile < -1 || dfile > 1 || drank < -2 || drank > 2) return -1; 1224 1 1225
if (!SHG_IS_SQ(sq) || dfile < -1 || dfile > 1 || drank < -2 || drank > 2) return -1; 1222 2 1224
if (!SHG_IS_SQ(sq) || dfile < -1 || dfile > 1 || drank < -2 || drank > 2) return -1; 1221 1 1222
if (!SHG_IS_SQ(sq) || dfile < -1 || dfile > 1 || drank < -2 || drank > 2) return -1; 1 1220 1221
133 1220 return SHG_SQ_AT[SHG_SQ_OF[sq] + drank * SHG_WIDTH - dfile];
134 }
135  
136 324 static int shg_in_zone(int side, int sq)
137 {
138 324 shg_tables_init();
139 324 ▶ return SHG_IS_SIDE(side) && SHG_IS_SQ(sq) ? ZONE[side][sq] : 0;
Branch: 4 branches
BranchTrueFalseTotal
return SHG_IS_SIDE(side) && SHG_IS_SQ(sq) ? ZONE[side][sq] : 0; 162 162 324
return SHG_IS_SIDE(side) && SHG_IS_SQ(sq) ? ZONE[side][sq] : 0; 162 0 162
return SHG_IS_SIDE(side) && SHG_IS_SQ(sq) ? ZONE[side][sq] : 0; 324 0 324
return SHG_IS_SIDE(side) && SHG_IS_SQ(sq) ? ZONE[side][sq] : 0; 324 0 324
140 }
141  
142 164 static int shg_last_distance(int side, int sq)
143 {
144 164 shg_tables_init();
145 164 ▶ return SHG_IS_SIDE(side) && SHG_IS_SQ(sq) ? LAST[side][sq] : -1;
Branch: 4 branches
BranchTrueFalseTotal
return SHG_IS_SIDE(side) && SHG_IS_SQ(sq) ? LAST[side][sq] : -1; 82 82 164
return SHG_IS_SIDE(side) && SHG_IS_SQ(sq) ? LAST[side][sq] : -1; 82 0 82
return SHG_IS_SIDE(side) && SHG_IS_SQ(sq) ? LAST[side][sq] : -1; 164 0 164
return SHG_IS_SIDE(side) && SHG_IS_SQ(sq) ? LAST[side][sq] : -1; 164 0 164
146 }
147  
148 2029 ▶static int shg_promoted(int kind) { return SHG_IS_KIND(kind) ? PROMOTED[kind] : 0; }
Branch: 2 branches
BranchTrueFalseTotal
static int shg_promoted(int kind) { return SHG_IS_KIND(kind) ? PROMOTED[kind] : 0; } 2028 1 2029
static int shg_promoted(int kind) { return SHG_IS_KIND(kind) ? PROMOTED[kind] : 0; } 2027 1 2028
149 615 ▶static int shg_unpromoted(int kind) { return SHG_IS_KIND(kind) ? UNPROMOTED[kind] : 0; }
Branch: 2 branches
BranchTrueFalseTotal
static int shg_unpromoted(int kind) { return SHG_IS_KIND(kind) ? UNPROMOTED[kind] : 0; } 614 1 615
static int shg_unpromoted(int kind) { return SHG_IS_KIND(kind) ? UNPROMOTED[kind] : 0; } 613 1 614
150 30 ▶static int shg_set_count(int kind) { return SHG_IS_KIND(kind) ? SET[kind] : 0; }
Branch: 2 branches
BranchTrueFalseTotal
static int shg_set_count(int kind) { return SHG_IS_KIND(kind) ? SET[kind] : 0; } 30 0 30
static int shg_set_count(int kind) { return SHG_IS_KIND(kind) ? SET[kind] : 0; } 30 0 30
151  
152 /* ---- the zobrist table -----------------------------------------------------
153 *
154 * DERIVED, NOT COMMITTED. A fixed function of a fixed seed is the same in every
155 * process and on every machine, and cannot suffer the transcription error a
156 * hand-pasted array can. splitmix64 is published and has no weak seeds.
157 *
158 * A HAND IS HASHED BY ITS COUNT, one number per (side, kind, count), so adding
159 * a piece to a hand XORs out the old count's number and XORs in the new one's.
160 * A count of 0 is 0, so an empty hand adds nothing.
161 *
162 * CHANGING THIS GENERATOR, ITS SEED OR THE ORDER THE TABLE IS FILLED IN
163 * CHANGES EVERY KEY. t/02-position.t pins values so that is a red test.
164 */
165  
166 unsigned long long SHG_ZOB[SHG_SIDES][SHG_KINDS][SHG_SQUARES];
167 unsigned long long SHG_ZOB_HAND[SHG_SIDES][SHG_HAND_KINDS][SHG_SET_PAWNS + 1];
168 unsigned long long SHG_ZOB_MOVE;
169 static int ZOB_READY = 0;
170  
171 118487 static unsigned long long splitmix64(unsigned long long *state)
172 {
173 unsigned long long z;
174 118487 *state += 0x9E3779B97F4A7C15ULL;
175 118487 z = *state;
176 118487 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL;
177 118487 z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL;
178 118487 return z ^ (z >> 31);
179 }
180  
181 7420 static void zob_init(void)
182 {
183 7420 unsigned long long s = 0x73686F6769303031ULL; /* "shogi001" */
184 int side, kind, sq, n;
185 7420 ▶ if (ZOB_READY) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ZOB_READY) return; 7373 47 7420
186 141 ▶ for (side = 0; side < SHG_SIDES; side++)
Branch: 1 branch
BranchTrueFalseTotal
for (side = 0; side < SHG_SIDES; side++) 94 47 141
187 1504 ▶ for (kind = 0; kind < SHG_KINDS; kind++)
Branch: 1 branch
BranchTrueFalseTotal
for (kind = 0; kind < SHG_KINDS; kind++) 1410 94 1504
188 115620 ▶ for (sq = 0; sq < SHG_SQUARES; sq++)
Branch: 1 branch
BranchTrueFalseTotal
for (sq = 0; sq < SHG_SQUARES; sq++) 114210 1410 115620
189 114210 ▶ SHG_ZOB[side][kind][sq] = kind ? splitmix64(&s) : 0;
Branch: 1 branch
BranchTrueFalseTotal
SHG_ZOB[side][kind][sq] = kind ? splitmix64(&s) : 0; 106596 7614 114210
190 141 ▶ for (side = 0; side < SHG_SIDES; side++)
Branch: 1 branch
BranchTrueFalseTotal
for (side = 0; side < SHG_SIDES; side++) 94 47 141
191 846 ▶ for (kind = 0; kind < SHG_HAND_KINDS; kind++)
Branch: 1 branch
BranchTrueFalseTotal
for (kind = 0; kind < SHG_HAND_KINDS; kind++) 752 94 846
192 15040 ▶ for (n = 0; n <= SHG_SET_PAWNS; n++)
Branch: 1 branch
BranchTrueFalseTotal
for (n = 0; n <= SHG_SET_PAWNS; n++) 14288 752 15040
193 14288 ▶ SHG_ZOB_HAND[side][kind][n] = kind && n ? splitmix64(&s) : 0;
Branch: 2 branches
BranchTrueFalseTotal
SHG_ZOB_HAND[side][kind][n] = kind && n ? splitmix64(&s) : 0; 12502 1786 14288
SHG_ZOB_HAND[side][kind][n] = kind && n ? splitmix64(&s) : 0; 11844 658 12502
194 47 SHG_ZOB_MOVE = splitmix64(&s);
195 47 ZOB_READY = 1;
196 }
197  
198 2419 static unsigned long long shg_zobrist(int side, int kind, int sq)
199 {
200 2419 zob_init();
201 2419 ▶ if (!SHG_IS_SIDE(side) || !SHG_IS_KIND(kind) || !SHG_IS_SQ(sq)) return 0;
Branch: 6 branches
BranchTrueFalseTotal
if (!SHG_IS_SIDE(side) || !SHG_IS_KIND(kind) || !SHG_IS_SQ(sq)) return 0; 1189 1230 2419
if (!SHG_IS_SIDE(side) || !SHG_IS_KIND(kind) || !SHG_IS_SQ(sq)) return 0; 1188 1 1189
if (!SHG_IS_SIDE(side) || !SHG_IS_KIND(kind) || !SHG_IS_SQ(sq)) return 0; 2418 0 2418
if (!SHG_IS_SIDE(side) || !SHG_IS_KIND(kind) || !SHG_IS_SQ(sq)) return 0; 2418 0 2418
if (!SHG_IS_SIDE(side) || !SHG_IS_KIND(kind) || !SHG_IS_SQ(sq)) return 0; 2418 0 2418
if (!SHG_IS_SIDE(side) || !SHG_IS_KIND(kind) || !SHG_IS_SQ(sq)) return 0; 0 2418 2418
202 2418 return SHG_ZOB[side][kind][sq];
203 }
204  
205 205 static unsigned long long shg_zobrist_hand(int side, int kind, int count)
206 {
207 205 zob_init();
208 205 ▶ if (!SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return 0;
Branch: 6 branches
BranchTrueFalseTotal
if (!SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return 0; 101 104 205
if (!SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return 0; 101 0 101
if (!SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return 0; 205 0 205
if (!SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return 0; 205 0 205
if (!SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return 0; 205 0 205
if (!SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return 0; 1 204 205
209 204 return SHG_ZOB_HAND[side][kind][count];
210 }
211  
212 2 static unsigned long long shg_zobrist_move(void) { zob_init(); return SHG_ZOB_MOVE; }
213  
214 /* ---- a board --------------------------------------------------------------- */
215  
216 /* Boards handed out and not yet dropped. It exists so a test can show that a
217 * destructor ran exactly once; it is a plain int and is not synchronised. */
218 static int SHG_LIVE = 0;
219  
220 4747 static struct shg_board *board_alloc(void)
221 {
222 struct shg_board *b;
223 int sq;
224 4747 shg_tables_init();
225 4747 zob_init();
226 4747 ▶ if (!TABLES_OK) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!TABLES_OK) return NULL; 0 4747 4747
227 4747 b = (struct shg_board *) malloc(sizeof(struct shg_board));
228 4747 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 4747 4747
229 4747 memset(b->cell, SHG_OFF, sizeof b->cell);
230 389254 ▶ for (sq = 0; sq < SHG_SQUARES; sq++) b->cell[SHG_SQ_OF[sq]] = SHG_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
for (sq = 0; sq < SHG_SQUARES; sq++) b->cell[SHG_SQ_OF[sq]] = SHG_EMPTY; 384507 4747 389254
231 4747 memset(b->hand, 0, sizeof b->hand);
232 4747 b->to_move = SHG_BLACK;
233 4747 b->move_number = 1;
234 4747 b->king[SHG_BLACK] = -1;
235 4747 b->king[SHG_WHITE] = -1;
236 4747 b->key = 0;
237 4747 SHG_LIVE++;
238 4747 return b;
239 }
240  
241 21828364 static int is_piece(int piece)
242 {
243 21828364 ▶ return piece >= 0 && (piece & ~0x1F) == 0 && SHG_IS_KIND(SHG_KIND_OF(piece));
Branch: 4 branches
BranchTrueFalseTotal
return piece >= 0 && (piece & ~0x1F) == 0 && SHG_IS_KIND(SHG_KIND_OF(piece)); 21828364 0 21828364
return piece >= 0 && (piece & ~0x1F) == 0 && SHG_IS_KIND(SHG_KIND_OF(piece)); 21828364 0 21828364
return piece >= 0 && (piece & ~0x1F) == 0 && SHG_IS_KIND(SHG_KIND_OF(piece)); 21828364 0 21828364
return piece >= 0 && (piece & ~0x1F) == 0 && SHG_IS_KIND(SHG_KIND_OF(piece)); 21828364 0 21828364
244 }
245  
246 79222 static int shg_at(const struct shg_board *b, int sq)
247 {
248 79222 ▶ if (!b || !SHG_IS_SQ(sq)) return SHG_OFF;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || !SHG_IS_SQ(sq)) return SHG_OFF; 79222 0 79222
if (!b || !SHG_IS_SQ(sq)) return SHG_OFF; 79222 0 79222
if (!b || !SHG_IS_SQ(sq)) return SHG_OFF; 0 79222 79222
249 79222 return b->cell[SHG_SQ_OF[sq]];
250 }
251  
252 43274578 void shg_lift(struct shg_board *b, int sq)
253 {
254 int box, old, side, kind;
255 43274578 ▶ if (!b || !SHG_IS_SQ(sq)) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || !SHG_IS_SQ(sq)) return; 43274578 0 43274578
if (!b || !SHG_IS_SQ(sq)) return; 43274578 0 43274578
if (!b || !SHG_IS_SQ(sq)) return; 0 43274578 43274578
256 43274578 box = SHG_SQ_OF[sq];
257 43274578 old = b->cell[box];
258 43274578 ▶ if (old == SHG_EMPTY) return;
Branch: 1 branch
BranchTrueFalseTotal
if (old == SHG_EMPTY) return; 21828362 21446216 43274578
259 21446216 side = SHG_SIDE_OF(old);
260 21446216 kind = SHG_KIND_OF(old);
261 21446216 b->key ^= SHG_ZOB[side][kind][sq];
262 21446216 ▶ if (kind == SHG_KING && b->king[side] == sq) b->king[side] = -1;
Branch: 2 branches
BranchTrueFalseTotal
if (kind == SHG_KING && b->king[side] == sq) b->king[side] = -1; 2431462 19014754 21446216
if (kind == SHG_KING && b->king[side] == sq) b->king[side] = -1; 2431462 0 2431462
263 21446216 b->cell[box] = SHG_EMPTY;
264 }
265  
266 21828364 void shg_put(struct shg_board *b, int sq, int piece)
267 {
268 int side, kind;
269 21828364 ▶ if (!b || !SHG_IS_SQ(sq) || !is_piece(piece)) return;
Branch: 4 branches
BranchTrueFalseTotal
if (!b || !SHG_IS_SQ(sq) || !is_piece(piece)) return; 21828364 0 21828364
if (!b || !SHG_IS_SQ(sq) || !is_piece(piece)) return; 21828364 0 21828364
if (!b || !SHG_IS_SQ(sq) || !is_piece(piece)) return; 21828364 0 21828364
if (!b || !SHG_IS_SQ(sq) || !is_piece(piece)) return; 0 21828364 21828364
270 21828364 shg_lift(b, sq);
271 21828364 side = SHG_SIDE_OF(piece);
272 21828364 kind = SHG_KIND_OF(piece);
273 21828364 b->cell[SHG_SQ_OF[sq]] = (unsigned char) piece;
274 21828364 b->key ^= SHG_ZOB[side][kind][sq];
275 21828364 ▶ if (kind == SHG_KING) b->king[side] = sq;
Branch: 1 branch
BranchTrueFalseTotal
if (kind == SHG_KING) b->king[side] = sq; 2435221 19393143 21828364
276 }
277  
278 14470 static int shg_hand(const struct shg_board *b, int side, int kind)
279 {
280 14470 ▶ if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind)) return 0;
Branch: 5 branches
BranchTrueFalseTotal
if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind)) return 0; 14470 0 14470
if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind)) return 0; 7232 7238 14470
if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind)) return 0; 7232 0 7232
if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind)) return 0; 14470 0 14470
if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind)) return 0; 0 14470 14470
281 14470 return b->hand[side][kind];
282 }
283  
284 5195066 void shg_set_hand(struct shg_board *b, int side, int kind, int count)
285 {
286 5195066 ▶ if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return;
Branch: 7 branches
BranchTrueFalseTotal
if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return; 5195066 0 5195066
if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return; 1796674 3398392 5195066
if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return; 1796674 0 1796674
if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return; 5195066 0 5195066
if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return; 5195066 0 5195066
if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return; 5195066 0 5195066
if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return; 2 5195064 5195066
287 5195064 b->key ^= SHG_ZOB_HAND[side][kind][b->hand[side][kind]] ^ SHG_ZOB_HAND[side][kind][count];
288 5195064 b->hand[side][kind] = (unsigned char) count;
289 }
290  
291 2344 static struct shg_board *shg_board_empty(void) { return board_alloc(); }
292  
293 271 static struct shg_board *shg_board_new(void)
294 {
295 271 struct shg_board *b = board_alloc();
296 271 int row, col, c, placed = 0;
297 271 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 271 271
298 2710 ▶ for (row = 0; row < SHG_RANKS; row++)
Branch: 1 branch
BranchTrueFalseTotal
for (row = 0; row < SHG_RANKS; row++) 2439 271 2710
299 24390 ▶ for (col = 0; col < SHG_FILES; col++) {
Branch: 1 branch
BranchTrueFalseTotal
for (col = 0; col < SHG_FILES; col++) { 21951 2439 24390
300 21951 c = START[row][col];
301 21951 ▶ if (c == '.') continue;
Branch: 1 branch
BranchTrueFalseTotal
if (c == '.') continue; 11111 10840 21951
302 10840 shg_put(b, row * SHG_FILES + col,
303 10840 ▶ SHG_PIECE(c >= 'a' ? SHG_WHITE : SHG_BLACK, kind_of_letter(c)));
Branch: 1 branch
BranchTrueFalseTotal
SHG_PIECE(c >= 'a' ? SHG_WHITE : SHG_BLACK, kind_of_letter(c))); 5420 5420 10840
304 10840 placed++;
305 }
306 /* The constructor counts what it built. */
307 271 ▶ if (placed != SHG_SET_PIECES || b->king[SHG_BLACK] < 0 || b->king[SHG_WHITE] < 0) {
Branch: 3 branches
BranchTrueFalseTotal
if (placed != SHG_SET_PIECES || b->king[SHG_BLACK] < 0 || b->king[SHG_WHITE] < 0) { 271 0 271
if (placed != SHG_SET_PIECES || b->king[SHG_BLACK] < 0 || b->king[SHG_WHITE] < 0) { 271 0 271
if (placed != SHG_SET_PIECES || b->king[SHG_BLACK] < 0 || b->king[SHG_WHITE] < 0) { 0 271 271
308 0 free(b);
309 0 SHG_LIVE--;
310 0 return NULL;
311 }
312 271 return b;
313 }
314  
315 1169 static struct shg_board *shg_board_copy(const struct shg_board *b)
316 {
317 struct shg_board *n;
318 1169 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 1169 1169
319 1169 n = (struct shg_board *) malloc(sizeof(struct shg_board));
320 1169 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 1169 1169
321 1169 *n = *b;
322 1169 SHG_LIVE++;
323 1169 return n;
324 }
325  
326 5916 static void shg_board_drop(struct shg_board *b)
327 {
328 5916 ▶ if (!b) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return; 0 5916 5916
329 5916 free(b);
330 5916 SHG_LIVE--;
331 }
332  
333 4 static int shg_live(void) { return SHG_LIVE; }
334  
335 34514 ▶static int shg_to_move(const struct shg_board *b) { return b ? b->to_move : -1; }
Branch: 1 branch
BranchTrueFalseTotal
static int shg_to_move(const struct shg_board *b) { return b ? b->to_move : -1; } 34514 0 34514
336  
337 20841448 void shg_set_to_move(struct shg_board *b, int side)
338 {
339 20841448 ▶ if (!b || !SHG_IS_SIDE(side) || side == b->to_move) return;
Branch: 4 branches
BranchTrueFalseTotal
if (!b || !SHG_IS_SIDE(side) || side == b->to_move) return; 20841448 0 20841448
if (!b || !SHG_IS_SIDE(side) || side == b->to_move) return; 10489459 10351989 20841448
if (!b || !SHG_IS_SIDE(side) || side == b->to_move) return; 10489459 0 10489459
if (!b || !SHG_IS_SIDE(side) || side == b->to_move) return; 2094 20839354 20841448
340 20839354 b->key ^= SHG_ZOB_MOVE;
341 20839354 b->to_move = side;
342 }
343  
344 30616 ▶static int shg_move_number(const struct shg_board *b) { return b ? b->move_number : 0; }
Branch: 1 branch
BranchTrueFalseTotal
static int shg_move_number(const struct shg_board *b) { return b ? b->move_number : 0; } 30616 0 30616
345  
346 3 static void shg_set_move_number(struct shg_board *b, int n)
347 {
348 3 ▶ if (!b || n < 1 || n > SHG_MOVE_MAX) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || n < 1 || n > SHG_MOVE_MAX) return; 3 0 3
if (!b || n < 1 || n > SHG_MOVE_MAX) return; 2 1 3
if (!b || n < 1 || n > SHG_MOVE_MAX) return; 0 2 2
349 2 b->move_number = n;
350 }
351  
352 9 static int shg_king_sq(const struct shg_board *b, int side)
353 {
354 9 ▶ if (!b || !SHG_IS_SIDE(side)) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || !SHG_IS_SIDE(side)) return -1; 9 0 9
if (!b || !SHG_IS_SIDE(side)) return -1; 2 7 9
if (!b || !SHG_IS_SIDE(side)) return -1; 0 2 2
355 9 return b->king[side];
356 }
357  
358 38198 ▶static unsigned long long shg_key(const struct shg_board *b) { return b ? b->key : 0; }
Branch: 1 branch
BranchTrueFalseTotal
static unsigned long long shg_key(const struct shg_board *b) { return b ? b->key : 0; } 38198 0 38198
359  
360 /* ---- the SFEN ---------------------------------------------------------------
361 *
362 * U, section 3: the board rank a first and each rank from file 9; uppercase
363 * for Black and lowercase for White; '+' before a promoted piece; a digit for
364 * a run of empty squares. Then the side to move, b or w; then the hands,
365 * counted, rook to pawn, Black's first, or '-'; then the move number, which U
366 * calls optional and this reader takes as 1 when it is missing. The start is
367 *
368 * lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b - 1
369 */
370  
371 10622 static int put_int(char *tmp, int n, int v)
372 {
373 char rev[12];
374 10622 int i = 0;
375 13833 ▶ do { rev[i++] = (char) ('0' + v % 10); v /= 10; } while (v && i < 11);
Branch: 2 branches
BranchTrueFalseTotal
do { rev[i++] = (char) ('0' + v % 10); v /= 10; } while (v && i < 11); 3211 10622 13833
do { rev[i++] = (char) ('0' + v % 10); v /= 10; } while (v && i < 11); 3211 0 3211
376 24455 ▶ while (i) tmp[n++] = rev[--i];
Branch: 1 branch
BranchTrueFalseTotal
while (i) tmp[n++] = rev[--i]; 13833 10622 24455
377 10622 return n;
378 }
379  
380 2705 static int shg_to_sfen(const struct shg_board *b, char *out, int max)
381 {
382 char tmp[SHG_SFEN_MAX];
383 2705 int n = 0, row, col, run, c, side, i, any = 0;
384 2705 ▶ if (!b || !out) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!b || !out) return -1; 2705 0 2705
if (!b || !out) return -1; 0 2705 2705
385 27050 ▶ for (row = 0; row < SHG_RANKS; row++) {
Branch: 1 branch
BranchTrueFalseTotal
for (row = 0; row < SHG_RANKS; row++) { 24345 2705 27050
386 24345 ▶ if (row > 0) tmp[n++] = '/';
Branch: 1 branch
BranchTrueFalseTotal
if (row > 0) tmp[n++] = '/'; 21640 2705 24345
387 24345 run = 0;
388 243450 ▶ for (col = 0; col < SHG_FILES; col++) {
Branch: 1 branch
BranchTrueFalseTotal
for (col = 0; col < SHG_FILES; col++) { 219105 24345 243450
389 int kind;
390 char letter;
391 219105 c = b->cell[SHG_SQ_OF[row * SHG_FILES + col]];
392 219105 ▶ if (c == SHG_EMPTY) { run++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (c == SHG_EMPTY) { run++; continue; } 159300 59805 219105
393 59805 ▶ if (run) { tmp[n++] = (char) ('0' + run); run = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (run) { tmp[n++] = (char) ('0' + run); run = 0; } 25370 34435 59805
394 59805 kind = SHG_KIND_OF(c);
395 59805 ▶ if (kind > SHG_KING) tmp[n++] = '+';
Branch: 1 branch
BranchTrueFalseTotal
if (kind > SHG_KING) tmp[n++] = '+'; 2756 57049 59805
396 59805 letter = LETTER[kind];
397 59805 ▶ tmp[n++] = SHG_SIDE_OF(c) == SHG_WHITE ? (char) (letter + ('a' - 'A')) : letter;
Branch: 1 branch
BranchTrueFalseTotal
tmp[n++] = SHG_SIDE_OF(c) == SHG_WHITE ? (char) (letter + ('a' - 'A')) : letter; 29443 30362 59805
398 }
399 24345 ▶ if (run) tmp[n++] = (char) ('0' + run);
Branch: 1 branch
BranchTrueFalseTotal
if (run) tmp[n++] = (char) ('0' + run); 17391 6954 24345
400 }
401 2705 tmp[n++] = ' ';
402 2705 ▶ tmp[n++] = b->to_move == SHG_WHITE ? 'w' : 'b';
Branch: 1 branch
BranchTrueFalseTotal
tmp[n++] = b->to_move == SHG_WHITE ? 'w' : 'b'; 1197 1508 2705
403 2705 tmp[n++] = ' ';
404 8115 ▶ for (side = 0; side < SHG_SIDES; side++)
Branch: 1 branch
BranchTrueFalseTotal
for (side = 0; side < SHG_SIDES; side++) 5410 2705 8115
405 43280 ▶ for (i = 0; i < SHG_HAND_KINDS - 1; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < SHG_HAND_KINDS - 1; i++) { 37870 5410 43280
406 37870 int kind = HAND_ORDER[i], count = b->hand[side][kind];
407 37870 ▶ if (!count) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!count) continue; 28584 9286 37870
408 9286 ▶ if (count > 1) n = put_int(tmp, n, count);
Branch: 1 branch
BranchTrueFalseTotal
if (count > 1) n = put_int(tmp, n, count); 7917 1369 9286
409 9286 ▶ tmp[n++] = side == SHG_WHITE ? (char) (LETTER[kind] + ('a' - 'A')) : LETTER[kind];
Branch: 1 branch
BranchTrueFalseTotal
tmp[n++] = side == SHG_WHITE ? (char) (LETTER[kind] + ('a' - 'A')) : LETTER[kind]; 1023 8263 9286
410 9286 any = 1;
411 }
412 2705 ▶ if (!any) tmp[n++] = '-';
Branch: 1 branch
BranchTrueFalseTotal
if (!any) tmp[n++] = '-'; 510 2195 2705
413 2705 tmp[n++] = ' ';
414 2705 n = put_int(tmp, n, b->move_number);
415 2705 ▶ if (n + 1 > max) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (n + 1 > max) return -1; 0 2705 2705
416 2705 memcpy(out, tmp, (size_t) n);
417 2705 out[n] = '\0';
418 2705 return n;
419 }
420  
421 234257 ▶static int is_digit(int c) { return c >= '0' && c <= '9'; }
Branch: 2 branches
BranchTrueFalseTotal
static int is_digit(int c) { return c >= '0' && c <= '9'; } 217197 17060 234257
static int is_digit(int c) { return c >= '0' && c <= '9'; } 84232 132965 217197
422  
423 2183 static struct shg_board *shg_board_of_sfen(const char *str, int *err)
424 {
425 2183 int code = SHG_OK;
426 char field[4][SHG_SFEN_MAX];
427 2183 int nfields = 0, flen = 0, ranks = 1, i, side, kind, move = 1;
428 int cells[SHG_SQUARES];
429 int hands[SHG_SIDES][SHG_HAND_KINDS];
430 int seen[SHG_SIDES][SHG_HAND_KINDS];
431 2183 int kings[SHG_SIDES] = { 0, 0 };
432 int total[SHG_KINDS];
433 const char *s;
434 size_t len;
435 struct shg_board *b;
436  
437 2183 shg_tables_init();
438 2183 ▶ if (!str || !*str) { code = SHG_POS_NULL; goto refused; }
Branch: 2 branches
BranchTrueFalseTotal
if (!str || !*str) { code = SHG_POS_NULL; goto refused; } 2183 0 2183
if (!str || !*str) { code = SHG_POS_NULL; goto refused; } 2 2181 2183
439 2181 len = strlen(str);
440 2181 ▶ if (len >= SHG_SFEN_MAX) { code = SHG_POS_LONG; goto refused; }
Branch: 1 branch
BranchTrueFalseTotal
if (len >= SHG_SFEN_MAX) { code = SHG_POS_LONG; goto refused; } 1 2180 2181
441  
442 /* ---- the fields: single spaces between, none before or after ---- */
443 2180 ▶ if (str[0] == ' ' || str[len - 1] == ' ') { code = SHG_POS_FIELDS; goto refused; }
Branch: 2 branches
BranchTrueFalseTotal
if (str[0] == ' ' || str[len - 1] == ' ') { code = SHG_POS_FIELDS; goto refused; } 2179 1 2180
if (str[0] == ' ' || str[len - 1] == ' ') { code = SHG_POS_FIELDS; goto refused; } 1 2178 2179
444 134582 ▶ for (s = str; *s; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = str; *s; s++) { 132407 2175 134582
445 132407 ▶ if (*s == ' ') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == ' ') { 6527 125880 132407
446 6527 ▶ if (s[1] == ' ' || nfields >= 3) { code = SHG_POS_FIELDS; goto refused; }
Branch: 2 branches
BranchTrueFalseTotal
if (s[1] == ' ' || nfields >= 3) { code = SHG_POS_FIELDS; goto refused; } 6526 1 6527
if (s[1] == ' ' || nfields >= 3) { code = SHG_POS_FIELDS; goto refused; } 2 6524 6526
447 6524 field[nfields][flen] = '\0';
448 6524 nfields++;
449 6524 flen = 0;
450 6524 continue;
451 }
452 125880 field[nfields][flen++] = *s;
453 }
454 2175 field[nfields][flen] = '\0';
455 2175 nfields++;
456 2175 ▶ if (nfields != 3 && nfields != 4) { code = SHG_POS_FIELDS; goto refused; }
Branch: 2 branches
BranchTrueFalseTotal
if (nfields != 3 && nfields != 4) { code = SHG_POS_FIELDS; goto refused; } 2174 1 2175
if (nfields != 3 && nfields != 4) { code = SHG_POS_FIELDS; goto refused; } 2 2172 2174
457  
458 /* ---- the board ---- */
459 111019 ▶ for (s = field[0]; *s; s++)
Branch: 1 branch
BranchTrueFalseTotal
for (s = field[0]; *s; s++) 108846 2173 111019
460 108846 ▶ if (*s == '/') ranks++;
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '/') ranks++; 17370 91476 108846
461 2173 ▶ if (ranks != SHG_RANKS) { code = SHG_POS_RANKS; goto refused; }
Branch: 1 branch
BranchTrueFalseTotal
if (ranks != SHG_RANKS) { code = SHG_POS_RANKS; goto refused; } 4 2169 2173
462  
463 110926 ▶ for (s = field[0]; *s; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = field[0]; *s; s++) { 108761 2165 110926
464 108761 ▶ if (*s == '/' || *s == '+' || kind_of_letter(*s)) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (*s == '/' || *s == '+' || kind_of_letter(*s)) continue; 91425 17336 108761
if (*s == '/' || *s == '+' || kind_of_letter(*s)) continue; 89095 2330 91425
if (*s == '/' || *s == '+' || kind_of_letter(*s)) continue; 53181 35914 89095
465 35914 ▶ if (*s >= '1' && *s <= '9' && !is_digit(s[1])) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (*s >= '1' && *s <= '9' && !is_digit(s[1])) continue; 35912 2 35914
if (*s >= '1' && *s <= '9' && !is_digit(s[1])) continue; 35911 1 35912
if (*s >= '1' && *s <= '9' && !is_digit(s[1])) continue; 35910 1 35911
466 4 code = SHG_POS_LETTER; goto refused;
467 }
468  
469 {
470 2165 int width = 0;
471 110872 for (s = field[0]; ; s++) {
472 110872 ▶ if (*s == '/' || !*s) {
Branch: 2 branches
BranchTrueFalseTotal
if (*s == '/' || !*s) { 93558 17314 110872
if (*s == '/' || !*s) { 2163 91395 93558
473 19477 ▶ if (width != SHG_FILES) { code = SHG_POS_WIDTH; goto refused; }
Branch: 1 branch
BranchTrueFalseTotal
if (width != SHG_FILES) { code = SHG_POS_WIDTH; goto refused; } 3 19474 19477
474 19474 ▶ if (!*s) break;
Branch: 1 branch
BranchTrueFalseTotal
if (!*s) break; 2162 17312 19474
475 17312 width = 0;
476 17312 continue;
477 }
478 91395 ▶ if (is_digit(*s)) width += *s - '0';
Branch: 1 branch
BranchTrueFalseTotal
if (is_digit(*s)) width += *s - '0'; 35884 55511 91395
479 55511 ▶ else if (*s != '+') width++;
Branch: 1 branch
BranchTrueFalseTotal
else if (*s != '+') width++; 53181 2330 55511
480 }
481 }
482  
483 110791 ▶ for (s = field[0]; *s; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = field[0]; *s; s++) { 108633 2158 110791
484 108633 ▶ if (*s != '+') continue;
Branch: 1 branch
BranchTrueFalseTotal
if (*s != '+') continue; 106304 2329 108633
485 2329 kind = kind_of_letter(s[1]);
486 2329 ▶ if (!kind || !PROMOTED[kind]) { code = SHG_POS_PROMOTE; goto refused; }
Branch: 2 branches
BranchTrueFalseTotal
if (!kind || !PROMOTED[kind]) { code = SHG_POS_PROMOTE; goto refused; } 2327 2 2329
if (!kind || !PROMOTED[kind]) { code = SHG_POS_PROMOTE; goto refused; } 2 2325 2327
487 }
488  
489 /* ---- the side to move ---- */
490 2158 ▶ if ((field[1][0] != 'b' && field[1][0] != 'w') || field[1][1]) {
Branch: 3 branches
BranchTrueFalseTotal
if ((field[1][0] != 'b' && field[1][0] != 'w') || field[1][1]) { 1210 948 2158
if ((field[1][0] != 'b' && field[1][0] != 'w') || field[1][1]) { 1208 2 1210
if ((field[1][0] != 'b' && field[1][0] != 'w') || field[1][1]) { 1 2155 2156
491 3 code = SHG_POS_SIDE; goto refused;
492 }
493  
494 /* ---- the hands ---- */
495 2155 memset(hands, 0, sizeof hands);
496 2155 memset(seen, 0, sizeof seen);
497 2155 ▶ if (strcmp(field[2], "-") != 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (strcmp(field[2], "-") != 0) { 1575 580 2155
498 6992 ▶ for (s = field[2]; *s; ) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = field[2]; *s; ) { 5428 1564 6992
499 5428 int count = 1;
500 5428 ▶ if (is_digit(*s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_digit(*s)) { 4105 1323 5428
501 4105 ▶ if (*s == '0') { code = SHG_POS_HAND; goto refused; }
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '0') { code = SHG_POS_HAND; goto refused; } 2 4103 4105
502 4103 count = 0;
503 8752 ▶ while (is_digit(*s) && count <= SHG_SET_PAWNS) count = count * 10 + (*s++ - '0');
Branch: 2 branches
BranchTrueFalseTotal
while (is_digit(*s) && count <= SHG_SET_PAWNS) count = count * 10 + (*s++ - '0'); 4649 4103 8752
while (is_digit(*s) && count <= SHG_SET_PAWNS) count = count * 10 + (*s++ - '0'); 4649 0 4649
504 4103 ▶ if (count < 2) { code = SHG_POS_HAND; goto refused; }
Branch: 1 branch
BranchTrueFalseTotal
if (count < 2) { code = SHG_POS_HAND; goto refused; } 1 4102 4103
505 }
506 5425 kind = kind_of_letter(*s);
507 5425 ▶ if (!SHG_IS_HAND(kind)) { code = SHG_POS_HAND; goto refused; }
Branch: 2 branches
BranchTrueFalseTotal
if (!SHG_IS_HAND(kind)) { code = SHG_POS_HAND; goto refused; } 5421 4 5425
if (!SHG_IS_HAND(kind)) { code = SHG_POS_HAND; goto refused; } 1 5420 5421
508 5420 side = *s >= 'a' ? SHG_WHITE : SHG_BLACK;
509 5420 ▶ if (seen[side][kind] || count > SET[kind]) { code = SHG_POS_HAND; goto refused; }
Branch: 2 branches
BranchTrueFalseTotal
if (seen[side][kind] || count > SET[kind]) { code = SHG_POS_HAND; goto refused; } 5419 1 5420
if (seen[side][kind] || count > SET[kind]) { code = SHG_POS_HAND; goto refused; } 2 5417 5419
510 5417 seen[side][kind] = 1;
511 5417 hands[side][kind] = count;
512 5417 s++;
513 }
514 }
515  
516 /* ---- the move number ---- */
517 2144 ▶ if (nfields == 4) {
Branch: 1 branch
BranchTrueFalseTotal
if (nfields == 4) { 2143 1 2144
518 2143 s = field[3];
519 2143 ▶ if (*s < '1' || *s > '9' || strlen(s) > 6) { code = SHG_POS_MOVE; goto refused; }
Branch: 3 branches
BranchTrueFalseTotal
if (*s < '1' || *s > '9' || strlen(s) > 6) { code = SHG_POS_MOVE; goto refused; } 2140 3 2143
if (*s < '1' || *s > '9' || strlen(s) > 6) { code = SHG_POS_MOVE; goto refused; } 2140 0 2140
if (*s < '1' || *s > '9' || strlen(s) > 6) { code = SHG_POS_MOVE; goto refused; } 1 2139 2140
520 2139 move = 0;
521 6076 ▶ for (; *s; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (; *s; s++) { 3938 2138 6076
522 3938 ▶ if (!is_digit(*s)) { code = SHG_POS_MOVE; goto refused; }
Branch: 1 branch
BranchTrueFalseTotal
if (!is_digit(*s)) { code = SHG_POS_MOVE; goto refused; } 1 3937 3938
523 3937 move = move * 10 + (*s - '0');
524 }
525 }
526  
527 /* ---- what the board holds ---- */
528 {
529 2139 int sq = 0, promote = 0;
530 110409 ▶ for (s = field[0]; *s; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = field[0]; *s; s++) { 108270 2139 110409
531 108270 ▶ if (*s == '/') continue;
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '/') continue; 17112 91158 108270
532 91158 ▶ if (*s == '+') { promote = 1; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '+') { promote = 1; continue; } 2325 88833 91158
533 88833 ▶ if (is_digit(*s)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_digit(*s)) { 35656 53177 88833
534 155738 ▶ for (i = 0; i < *s - '0'; i++) cells[sq++] = SHG_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < *s - '0'; i++) cells[sq++] = SHG_EMPTY; 120082 35656 155738
535 35656 continue;
536 }
537 53177 kind = kind_of_letter(*s);
538 53177 ▶ if (promote) kind = PROMOTED[kind];
Branch: 1 branch
BranchTrueFalseTotal
if (promote) kind = PROMOTED[kind]; 2325 50852 53177
539 53177 promote = 0;
540 53177 side = *s >= 'a' ? SHG_WHITE : SHG_BLACK;
541 53177 ▶ if (kind == SHG_KING) kings[side]++;
Branch: 1 branch
BranchTrueFalseTotal
if (kind == SHG_KING) kings[side]++; 3058 50119 53177
542 53177 cells[sq++] = SHG_PIECE(side, kind);
543 }
544 }
545 2139 ▶ if (kings[SHG_BLACK] > 1 || kings[SHG_WHITE] > 1) { code = SHG_POS_KINGS; goto refused; }
Branch: 2 branches
BranchTrueFalseTotal
if (kings[SHG_BLACK] > 1 || kings[SHG_WHITE] > 1) { code = SHG_POS_KINGS; goto refused; } 2138 1 2139
if (kings[SHG_BLACK] > 1 || kings[SHG_WHITE] > 1) { code = SHG_POS_KINGS; goto refused; } 2 2136 2138
546  
547 2136 memset(total, 0, sizeof total);
548 175152 ▶ for (i = 0; i < SHG_SQUARES; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < SHG_SQUARES; i++) 173016 2136 175152
549 173016 ▶ if (cells[i] != SHG_EMPTY) total[UNPROMOTED[SHG_KIND_OF(cells[i])]]++;
Branch: 1 branch
BranchTrueFalseTotal
if (cells[i] != SHG_EMPTY) total[UNPROMOTED[SHG_KIND_OF(cells[i])]]++; 53170 119846 173016
550 6408 ▶ for (side = 0; side < SHG_SIDES; side++)
Branch: 1 branch
BranchTrueFalseTotal
for (side = 0; side < SHG_SIDES; side++) 4272 2136 6408
551 34176 ▶ for (kind = SHG_PAWN; kind <= SHG_ROOK; kind++) total[kind] += hands[side][kind];
Branch: 1 branch
BranchTrueFalseTotal
for (kind = SHG_PAWN; kind <= SHG_ROOK; kind++) total[kind] += hands[side][kind]; 29904 4272 34176
552 19204 ▶ for (kind = SHG_PAWN; kind <= SHG_KING; kind++)
Branch: 1 branch
BranchTrueFalseTotal
for (kind = SHG_PAWN; kind <= SHG_KING; kind++) 17072 2132 19204
553 17072 ▶ if (total[kind] > SET[kind]) { code = SHG_POS_COUNT; goto refused; }
Branch: 1 branch
BranchTrueFalseTotal
if (total[kind] > SET[kind]) { code = SHG_POS_COUNT; goto refused; } 4 17068 17072
554  
555 2132 b = board_alloc();
556 2132 ▶ if (!b) { code = SHG_POS_NULL; goto refused; }
Branch: 1 branch
BranchTrueFalseTotal
if (!b) { code = SHG_POS_NULL; goto refused; } 0 2132 2132
557 174824 ▶ for (i = 0; i < SHG_SQUARES; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < SHG_SQUARES; i++) 172692 2132 174824
558 172692 ▶ if (cells[i] != SHG_EMPTY) shg_put(b, i, cells[i]);
Branch: 1 branch
BranchTrueFalseTotal
if (cells[i] != SHG_EMPTY) shg_put(b, i, cells[i]); 53008 119684 172692
559 6396 ▶ for (side = 0; side < SHG_SIDES; side++)
Branch: 1 branch
BranchTrueFalseTotal
for (side = 0; side < SHG_SIDES; side++) 4264 2132 6396
560 34112 ▶ for (kind = SHG_PAWN; kind <= SHG_ROOK; kind++) shg_set_hand(b, side, kind, hands[side][kind]);
Branch: 1 branch
BranchTrueFalseTotal
for (kind = SHG_PAWN; kind <= SHG_ROOK; kind++) shg_set_hand(b, side, kind, hands[side][kind]); 29848 4264 34112
561 2132 shg_set_to_move(b, field[1][0] == 'w' ? SHG_WHITE : SHG_BLACK);
562 2132 b->move_number = move;
563 2132 ▶ if (err) *err = SHG_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = SHG_OK; 2132 0 2132
564 2132 return b;
565  
566 51 refused:
567 51 ▶ if (err) *err = code;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = code; 51 0 51
568 51 return NULL;
569 }
570  
571 /* ---- the table ---------------------------------------------------------------- */
572  
573 static const struct shg_abi SHG_ABI = {
574 SHG_ABI_VERSION,
575  
576 shg_board_new,
577 shg_board_empty,
578 shg_board_of_sfen,
579 shg_board_copy,
580 shg_board_drop,
581 shg_live,
582  
583 shg_at,
584 shg_put,
585 shg_lift,
586  
587 shg_hand,
588 shg_set_hand,
589  
590 shg_to_move,
591 shg_set_to_move,
592 shg_move_number,
593 shg_set_move_number,
594 shg_king_sq,
595  
596 shg_key,
597 shg_zobrist,
598 shg_zobrist_hand,
599 shg_zobrist_move,
600  
601 shg_to_sfen,
602  
603 shg_tables_ok,
604 shg_sq_of,
605 shg_file_of,
606 shg_rank_of,
607 shg_step,
608 shg_in_zone,
609 shg_last_distance,
610 shg_promoted,
611 shg_unpromoted,
612 shg_set_count,
613  
614 shg_attacked,
615 shg_in_check,
616 shg_pseudo_moves,
617 shg_board_moves,
618 shg_do_move,
619 shg_undo_move,
620  
621 shg_drops,
622 shg_legal,
623 shg_has_legal_move,
624 shg_drop_fault,
625 shg_perft,
626  
627 shg_status,
628 shg_move_fault,
629  
630 shg_pack,
631 shg_impasse_points,
632 shg_impasse_pieces,
633 shg_declare_fault,
634 shg_repetition,
635  
636 shg_evaluate,
637 shg_search,
638 };
639  
640 47 const struct shg_abi *shg_abi_table(void)
641 {
642 47 shg_tables_init();
643 47 zob_init();
644 47 return &SHG_ABI;
645 }
« shg_mate.c ↑ Summary shg_judge.c »