sth_engine.c

filter: Statementstmt 95.6% 326 / 341 Branchbran 78.2% 330 / 422 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.9% 656 / 763 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« Sternhalma.xs ↑ Summary sth_moves.c »
1 /* sth_engine.c - the sternhalma board, and nothing that judges a move.
2 *
3 * PERL-FREE by design: see include/sth_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 star, the mailbox, the neighbours, the points, the
7 * ring, the hop classes, the layouts, put and lift, the seat to move, the
8 * zobrist key, and the position string in and out.
9 */
10  
11 #include <stdlib.h>
12 #include <string.h>
13 #include "sth_internal.h"
14  
15 /* ---- the mailbox and the tables: see sth_internal.h ------------------------- */
16  
17 const int STH_STEP[STH_DIRS] = { 1, -(STH_SIDE - 1), -STH_SIDE, -1, STH_SIDE - 1, STH_SIDE };
18  
19 int STH_SQ_OF[STH_HOLES];
20 int STH_HOLE_AT[STH_BOX];
21 int STH_HX[STH_HOLES];
22 int STH_HY[STH_HOLES];
23 static int ROW[STH_HOLES];
24 static int COL[STH_HOLES];
25 static int ROW_LEN[STH_ROWS];
26 static int ROW_START[STH_ROWS];
27 static int NEXT[STH_HOLES][STH_DIRS];
28 static int DEGREE[STH_HOLES];
29 static int POINT[STH_HOLES];
30 static int RING[STH_HOLES];
31 static int CLS[STH_HOLES];
32 int STH_POINT_LIST[STH_POINTS][STH_POINT_HOLES];
33 int STH_FRONT[STH_POINTS][STH_FRONT_HOLES];
34 static int PAIR[STH_PAIRS][2];
35 static int TABLES_READY = 0;
36 static int TABLES_OK = 0;
37  
38 /* ---- the layouts --------------------------------------------------------------
39 *
40 * Which point each seat starts in, by the seat count, in seat order. Seat order
41 * is turn order, and the points run clockwise, so the seats do too. Four seats
42 * are two opposite pairs that are neighbours; six fill the star; three take
43 * every other point and race into the three empty ones.
44 */
45  
46 const int STH_LAYOUT[STH_SEATS_MAX + 1][STH_SEATS_MAX] = {
47 { -1, -1, -1, -1, -1, -1 },
48 { 0, -1, -1, -1, -1, -1 },
49 { 0, 3, -1, -1, -1, -1 },
50 { 0, 2, 4, -1, -1, -1 },
51 { 0, 1, 3, 4, -1, -1 },
52 { -1, -1, -1, -1, -1, -1 },
53 { 0, 1, 2, 3, 4, 5 },
54 };
55  
56 152535 static int sth_layout_ok(int seats, int pieces)
57 {
58 152535 ▶ if (seats < 1 || seats > STH_SEATS_MAX || STH_LAYOUT[seats][0] < 0) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (seats < 1 || seats > STH_SEATS_MAX || STH_LAYOUT[seats][0] < 0) return 0; 152529 6 152535
if (seats < 1 || seats > STH_SEATS_MAX || STH_LAYOUT[seats][0] < 0) return 0; 152523 6 152529
if (seats < 1 || seats > STH_SEATS_MAX || STH_LAYOUT[seats][0] < 0) return 0; 7 152516 152523
59 152516 ▶ if (pieces == STH_PIECES) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (pieces == STH_PIECES) return 1; 147977 4539 152516
60 4539 ▶ if (pieces == STH_PIECES_MAX) return seats <= 3;
Branch: 1 branch
BranchTrueFalseTotal
if (pieces == STH_PIECES_MAX) return seats <= 3; 4514 25 4539
61 25 return 0;
62 }
63  
64 126 static int sth_layout_point(int seats, int seat)
65 {
66 126 ▶ if (seats < 1 || seats > STH_SEATS_MAX || STH_LAYOUT[seats][0] < 0) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (seats < 1 || seats > STH_SEATS_MAX || STH_LAYOUT[seats][0] < 0) return -1; 126 0 126
if (seats < 1 || seats > STH_SEATS_MAX || STH_LAYOUT[seats][0] < 0) return -1; 125 1 126
if (seats < 1 || seats > STH_SEATS_MAX || STH_LAYOUT[seats][0] < 0) return -1; 1 124 125
67 124 ▶ if (seat < 0 || seat >= seats) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (seat < 0 || seat >= seats) return -1; 123 1 124
if (seat < 0 || seat >= seats) return -1; 1 122 123
68 122 return STH_LAYOUT[seats][seat];
69 }
70  
71 /* ---- the geometry tables -------------------------------------------------------- */
72  
73 15606 static int on_star(int x, int y)
74 {
75 15606 int z = -x - y;
76 15606 ▶ if (x < -8 || x > 8 || y < -8 || y > 8 || z < -8 || z > 8) return 0;
Branch: 6 branches
BranchTrueFalseTotal
if (x < -8 || x > 8 || y < -8 || y > 8 || z < -8 || z > 8) return 0; 15606 0 15606
if (x < -8 || x > 8 || y < -8 || y > 8 || z < -8 || z > 8) return 0; 15606 0 15606
if (x < -8 || x > 8 || y < -8 || y > 8 || z < -8 || z > 8) return 0; 15606 0 15606
if (x < -8 || x > 8 || y < -8 || y > 8 || z < -8 || z > 8) return 0; 15606 0 15606
if (x < -8 || x > 8 || y < -8 || y > 8 || z < -8 || z > 8) return 0; 13662 1944 15606
if (x < -8 || x > 8 || y < -8 || y > 8 || z < -8 || z > 8) return 0; 1944 11718 13662
77 11718 ▶ if (x <= 4 && y <= 4 && z <= 4) return 1;
Branch: 3 branches
BranchTrueFalseTotal
if (x <= 4 && y <= 4 && z <= 4) return 1; 9450 2268 11718
if (x <= 4 && y <= 4 && z <= 4) return 1; 7182 2268 9450
if (x <= 4 && y <= 4 && z <= 4) return 1; 4914 2268 7182
78 6804 ▶ if (x >= -4 && y >= -4 && z >= -4) return 1;
Branch: 3 branches
BranchTrueFalseTotal
if (x >= -4 && y >= -4 && z >= -4) return 1; 5076 1728 6804
if (x >= -4 && y >= -4 && z >= -4) return 1; 3348 1728 5076
if (x >= -4 && y >= -4 && z >= -4) return 1; 1620 1728 3348
79 5184 return 0;
80 }
81  
82 6534 static int point_of_xy(int x, int y)
83 {
84 6534 int z = -x - y;
85 6534 ▶ if (y < -4) return 0;
Branch: 1 branch
BranchTrueFalseTotal
if (y < -4) return 0; 540 5994 6534
86 5994 ▶ if (x > 4) return 1;
Branch: 1 branch
BranchTrueFalseTotal
if (x > 4) return 1; 540 5454 5994
87 5454 ▶ if (z < -4) return 2;
Branch: 1 branch
BranchTrueFalseTotal
if (z < -4) return 2; 540 4914 5454
88 4914 ▶ if (y > 4) return 3;
Branch: 1 branch
BranchTrueFalseTotal
if (y > 4) return 3; 540 4374 4914
89 4374 ▶ if (x < -4) return 4;
Branch: 1 branch
BranchTrueFalseTotal
if (x < -4) return 4; 540 3834 4374
90 3834 ▶ if (z > 4) return 5;
Branch: 1 branch
BranchTrueFalseTotal
if (z > 4) return 5; 540 3294 3834
91 3294 return STH_CENTRE;
92 }
93  
94 /* The ring line in front of each point: the hexagon's edge that faces it. A
95 * corner of the hexagon is on two edges, so two neighbouring points share it,
96 * which is why fifteen pieces a seat cannot sit at neighbouring points. */
97 39204 static int in_front_of(int point, int x, int y)
98 {
99 39204 int z = -x - y;
100 39204 ▶ if (x < -4 || x > 4 || y < -4 || y > 4 || z < -4 || z > 4) return 0;
Branch: 6 branches
BranchTrueFalseTotal
if (x < -4 || x > 4 || y < -4 || y > 4 || z < -4 || z > 4) return 0; 35964 3240 39204
if (x < -4 || x > 4 || y < -4 || y > 4 || z < -4 || z > 4) return 0; 32724 3240 35964
if (x < -4 || x > 4 || y < -4 || y > 4 || z < -4 || z > 4) return 0; 29484 3240 32724
if (x < -4 || x > 4 || y < -4 || y > 4 || z < -4 || z > 4) return 0; 26244 3240 29484
if (x < -4 || x > 4 || y < -4 || y > 4 || z < -4 || z > 4) return 0; 23004 3240 26244
if (x < -4 || x > 4 || y < -4 || y > 4 || z < -4 || z > 4) return 0; 3240 19764 23004
101 19764 switch (point) {
102 3294 case 0: return y == -4;
103 3294 case 1: return x == 4;
104 3294 case 2: return z == -4;
105 3294 case 3: return y == 4;
106 3294 case 4: return x == -4;
107 3294 case 5: return z == 4;
108 }
109 0 return 0;
110 }
111  
112 8370 static int abs_int(int v) { return v < 0 ? -v : v; }
113  
114 2364528 void sth_tables_init(void)
115 {
116 2364528 int x, y, d, h, n = 0, pairs = 0, row;
117 int point_n[STH_POINTS], front_n[STH_POINTS];
118 int deg_n[STH_DIRS + 1], cls_n[STH_CLASSES];
119 2364528 int centre = 0, ring = 0, ok = 1;
120  
121 2364528 ▶ if (TABLES_READY) return;
Branch: 1 branch
BranchTrueFalseTotal
if (TABLES_READY) return; 2364474 54 2364528
122 54 memset(point_n, 0, sizeof point_n);
123 54 memset(front_n, 0, sizeof front_n);
124 54 memset(deg_n, 0, sizeof deg_n);
125 54 memset(cls_n, 0, sizeof cls_n);
126 23868 ▶ for (h = 0; h < STH_BOX; h++) STH_HOLE_AT[h] = -1;
Branch: 1 branch
BranchTrueFalseTotal
for (h = 0; h < STH_BOX; h++) STH_HOLE_AT[h] = -1; 23814 54 23868
127  
128 972 ▶ for (y = -8; y <= 8; y++) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = -8; y <= 8; y++) { 918 54 972
129 918 row = y + 8;
130 918 ROW_START[row] = n;
131 918 ROW_LEN[row] = 0;
132 16524 ▶ for (x = -8; x <= 8; x++) {
Branch: 1 branch
BranchTrueFalseTotal
for (x = -8; x <= 8; x++) { 15606 918 16524
133 int sq, p, f;
134 15606 ▶ if (!on_star(x, y)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!on_star(x, y)) continue; 9072 6534 15606
135 6534 ▶ if (n >= STH_HOLES) { ok = 0; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (n >= STH_HOLES) { ok = 0; continue; } 0 6534 6534
136 6534 sq = (y + STH_MARGIN) * STH_SIDE + (x + STH_MARGIN);
137 6534 STH_SQ_OF[n] = sq;
138 6534 STH_HOLE_AT[sq] = n;
139 6534 STH_HX[n] = x;
140 6534 STH_HY[n] = y;
141 6534 ROW[n] = row;
142 6534 COL[n] = ROW_LEN[row]++;
143 6534 p = point_of_xy(x, y);
144 6534 POINT[n] = p;
145 6534 ▶ if (p == STH_CENTRE) centre++;
Branch: 1 branch
BranchTrueFalseTotal
if (p == STH_CENTRE) centre++; 3294 3240 6534
146 3240 ▶ else if (point_n[p] < STH_POINT_HOLES) STH_POINT_LIST[p][point_n[p]++] = n;
Branch: 1 branch
BranchTrueFalseTotal
else if (point_n[p] < STH_POINT_HOLES) STH_POINT_LIST[p][point_n[p]++] = n; 3240 0 3240
147 0 else ok = 0;
148 6534 RING[n] = p == STH_CENTRE
149 6534 ▶ && (abs_int(x) == 4 || abs_int(y) == 4 || abs_int(x + y) == 4);
Branch: 4 branches
BranchTrueFalseTotal
&& (abs_int(x) == 4 || abs_int(y) == 4 || abs_int(x + y) == 4); 3294 3240 6534
&& (abs_int(x) == 4 || abs_int(y) == 4 || abs_int(x + y) == 4); 2754 540 3294
&& (abs_int(x) == 4 || abs_int(y) == 4 || abs_int(x + y) == 4); 2322 432 2754
&& (abs_int(x) == 4 || abs_int(y) == 4 || abs_int(x + y) == 4); 324 1998 2322
150 6534 ▶ if (RING[n]) ring++;
Branch: 1 branch
BranchTrueFalseTotal
if (RING[n]) ring++; 1296 5238 6534
151 45738 ▶ for (f = 0; f < STH_POINTS; f++) {
Branch: 1 branch
BranchTrueFalseTotal
for (f = 0; f < STH_POINTS; f++) { 39204 6534 45738
152 39204 ▶ if (!in_front_of(f, x, y)) continue;
Branch: 1 branch
BranchTrueFalseTotal
if (!in_front_of(f, x, y)) continue; 37584 1620 39204
153 1620 ▶ if (front_n[f] < STH_FRONT_HOLES) STH_FRONT[f][front_n[f]++] = n;
Branch: 1 branch
BranchTrueFalseTotal
if (front_n[f] < STH_FRONT_HOLES) STH_FRONT[f][front_n[f]++] = n; 1620 0 1620
154 0 else ok = 0;
155 }
156 6534 CLS[n] = ((x + 8) & 1) | (((y + 8) & 1) << 1);
157 6534 cls_n[CLS[n]]++;
158 6534 n++;
159 }
160 }
161 54 ▶ if (n != STH_HOLES) ok = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (n != STH_HOLES) ok = 0; 0 54 54
162  
163 6588 ▶ for (h = 0; h < n && h < STH_HOLES; h++) {
Branch: 2 branches
BranchTrueFalseTotal
for (h = 0; h < n && h < STH_HOLES; h++) { 6534 54 6588
for (h = 0; h < n && h < STH_HOLES; h++) { 6534 0 6534
164 6534 DEGREE[h] = 0;
165 45738 ▶ for (d = 0; d < STH_DIRS; d++) {
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < STH_DIRS; d++) { 39204 6534 45738
166 39204 int to = STH_HOLE_AT[STH_SQ_OF[h] + STH_STEP[d]];
167 39204 NEXT[h][d] = to;
168 39204 ▶ if (to >= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (to >= 0) { 33696 5508 39204
169 33696 DEGREE[h]++;
170 33696 ▶ if (to > h) {
Branch: 1 branch
BranchTrueFalseTotal
if (to > h) { 16848 16848 33696
171 16848 ▶ if (pairs < STH_PAIRS) {
Branch: 1 branch
BranchTrueFalseTotal
if (pairs < STH_PAIRS) { 16848 0 16848
172 16848 PAIR[pairs][0] = h;
173 16848 PAIR[pairs][1] = to;
174 }
175 16848 pairs++;
176 }
177 }
178 }
179 6534 deg_n[DEGREE[h]]++;
180 }
181  
182 54 ▶ if (centre != STH_CENTRE_HOLES || ring != STH_RING_HOLES || pairs != STH_PAIRS) ok = 0;
Branch: 3 branches
BranchTrueFalseTotal
if (centre != STH_CENTRE_HOLES || ring != STH_RING_HOLES || pairs != STH_PAIRS) ok = 0; 54 0 54
if (centre != STH_CENTRE_HOLES || ring != STH_RING_HOLES || pairs != STH_PAIRS) ok = 0; 54 0 54
if (centre != STH_CENTRE_HOLES || ring != STH_RING_HOLES || pairs != STH_PAIRS) ok = 0; 0 54 54
183 378 ▶ for (d = 0; d < STH_POINTS; d++)
Branch: 1 branch
BranchTrueFalseTotal
for (d = 0; d < STH_POINTS; d++) 324 54 378
184 324 ▶ if (point_n[d] != STH_POINT_HOLES || front_n[d] != STH_FRONT_HOLES) ok = 0;
Branch: 2 branches
BranchTrueFalseTotal
if (point_n[d] != STH_POINT_HOLES || front_n[d] != STH_FRONT_HOLES) ok = 0; 324 0 324
if (point_n[d] != STH_POINT_HOLES || front_n[d] != STH_FRONT_HOLES) ok = 0; 0 324 324
185 54 ▶ if (deg_n[6] != 73 || deg_n[5] != 6 || deg_n[4] != 36 || deg_n[2] != 6) ok = 0;
Branch: 4 branches
BranchTrueFalseTotal
if (deg_n[6] != 73 || deg_n[5] != 6 || deg_n[4] != 36 || deg_n[2] != 6) ok = 0; 54 0 54
if (deg_n[6] != 73 || deg_n[5] != 6 || deg_n[4] != 36 || deg_n[2] != 6) ok = 0; 54 0 54
if (deg_n[6] != 73 || deg_n[5] != 6 || deg_n[4] != 36 || deg_n[2] != 6) ok = 0; 54 0 54
if (deg_n[6] != 73 || deg_n[5] != 6 || deg_n[4] != 36 || deg_n[2] != 6) ok = 0; 0 54 54
186 54 ▶ if (cls_n[0] != 37 || cls_n[1] != 28 || cls_n[2] != 28 || cls_n[3] != 28) ok = 0;
Branch: 4 branches
BranchTrueFalseTotal
if (cls_n[0] != 37 || cls_n[1] != 28 || cls_n[2] != 28 || cls_n[3] != 28) ok = 0; 54 0 54
if (cls_n[0] != 37 || cls_n[1] != 28 || cls_n[2] != 28 || cls_n[3] != 28) ok = 0; 54 0 54
if (cls_n[0] != 37 || cls_n[1] != 28 || cls_n[2] != 28 || cls_n[3] != 28) ok = 0; 54 0 54
if (cls_n[0] != 37 || cls_n[1] != 28 || cls_n[2] != 28 || cls_n[3] != 28) ok = 0; 0 54 54
187 972 ▶ for (row = 0; row < STH_ROWS; row++)
Branch: 1 branch
BranchTrueFalseTotal
for (row = 0; row < STH_ROWS; row++) 918 54 972
188 918 ▶ if (ROW_LEN[row] > STH_ROW_MAX) ok = 0;
Branch: 1 branch
BranchTrueFalseTotal
if (ROW_LEN[row] > STH_ROW_MAX) ok = 0; 0 918 918
189  
190 54 TABLES_OK = ok;
191 54 TABLES_READY = 1;
192 }
193  
194 15320727 ▶static int is_hole(int hole) { return hole >= 0 && hole < STH_HOLES; }
Branch: 2 branches
BranchTrueFalseTotal
static int is_hole(int hole) { return hole >= 0 && hole < STH_HOLES; } 15320727 0 15320727
static int is_hole(int hole) { return hole >= 0 && hole < STH_HOLES; } 15320727 0 15320727
195  
196 1 static int sth_tables_ok(void) { sth_tables_init(); return TABLES_OK; }
197  
198 2777 static int sth_hole_of(int x, int y)
199 {
200 2777 sth_tables_init();
201 2777 ▶ if (x < -8 || x > 8 || y < -8 || y > 8) return -1;
Branch: 4 branches
BranchTrueFalseTotal
if (x < -8 || x > 8 || y < -8 || y > 8) return -1; 2733 44 2777
if (x < -8 || x > 8 || y < -8 || y > 8) return -1; 2689 44 2733
if (x < -8 || x > 8 || y < -8 || y > 8) return -1; 2653 36 2689
if (x < -8 || x > 8 || y < -8 || y > 8) return -1; 36 2617 2653
202 2617 return STH_HOLE_AT[(y + STH_MARGIN) * STH_SIDE + (x + STH_MARGIN)];
203 }
204  
205 13532 ▶static int sth_hole_x(int hole) { sth_tables_init(); return is_hole(hole) ? STH_HX[hole] : 0; }
Branch: 1 branch
BranchTrueFalseTotal
static int sth_hole_x(int hole) { sth_tables_init(); return is_hole(hole) ? STH_HX[hole] : 0; } 13532 0 13532
206 13532 ▶static int sth_hole_y(int hole) { sth_tables_init(); return is_hole(hole) ? STH_HY[hole] : 0; }
Branch: 1 branch
BranchTrueFalseTotal
static int sth_hole_y(int hole) { sth_tables_init(); return is_hole(hole) ? STH_HY[hole] : 0; } 13532 0 13532
207 4612 ▶static int sth_row_of(int hole) { sth_tables_init(); return is_hole(hole) ? ROW[hole] : -1; }
Branch: 1 branch
BranchTrueFalseTotal
static int sth_row_of(int hole) { sth_tables_init(); return is_hole(hole) ? ROW[hole] : -1; } 4612 0 4612
208 242 ▶static int sth_col_of(int hole) { sth_tables_init(); return is_hole(hole) ? COL[hole] : -1; }
Branch: 1 branch
BranchTrueFalseTotal
static int sth_col_of(int hole) { sth_tables_init(); return is_hole(hole) ? COL[hole] : -1; } 242 0 242
209 34 ▶static int sth_row_len(int row) { sth_tables_init(); return row >= 0 && row < STH_ROWS ? ROW_LEN[row] : 0; }
Branch: 2 branches
BranchTrueFalseTotal
static int sth_row_len(int row) { sth_tables_init(); return row >= 0 && row < STH_ROWS ? ROW_LEN[row] : 0; } 34 0 34
static int sth_row_len(int row) { sth_tables_init(); return row >= 0 && row < STH_ROWS ? ROW_LEN[row] : 0; } 34 0 34
210 123 ▶static int sth_row_start(int row){ sth_tables_init(); return row >= 0 && row < STH_ROWS ? ROW_START[row] : -1; }
Branch: 2 branches
BranchTrueFalseTotal
static int sth_row_start(int row){ sth_tables_init(); return row >= 0 && row < STH_ROWS ? ROW_START[row] : -1; } 123 0 123
static int sth_row_start(int row){ sth_tables_init(); return row >= 0 && row < STH_ROWS ? ROW_START[row] : -1; } 123 0 123
211  
212 1686298 static int sth_next(int hole, int dir)
213 {
214 1686298 sth_tables_init();
215 1686298 ▶ if (!is_hole(hole) || dir < 0 || dir >= STH_DIRS) return -1;
Branch: 3 branches
BranchTrueFalseTotal
if (!is_hole(hole) || dir < 0 || dir >= STH_DIRS) return -1; 1686298 0 1686298
if (!is_hole(hole) || dir < 0 || dir >= STH_DIRS) return -1; 1686298 0 1686298
if (!is_hole(hole) || dir < 0 || dir >= STH_DIRS) return -1; 0 1686298 1686298
216 1686298 return NEXT[hole][dir];
217 }
218  
219 763 ▶static int sth_degree(int hole) { sth_tables_init(); return is_hole(hole) ? DEGREE[hole] : 0; }
Branch: 1 branch
BranchTrueFalseTotal
static int sth_degree(int hole) { sth_tables_init(); return is_hole(hole) ? DEGREE[hole] : 0; } 763 0 763
220 631 ▶static int sth_dir_opposite(int dir) { return dir >= 0 && dir < STH_DIRS ? (dir + 3) % STH_DIRS : -1; }
Branch: 2 branches
BranchTrueFalseTotal
static int sth_dir_opposite(int dir) { return dir >= 0 && dir < STH_DIRS ? (dir + 3) % STH_DIRS : -1; } 631 0 631
static int sth_dir_opposite(int dir) { return dir >= 0 && dir < STH_DIRS ? (dir + 3) % STH_DIRS : -1; } 630 1 631
221 56357 ▶static int sth_point_of(int hole) { sth_tables_init(); return is_hole(hole) ? POINT[hole] : -1; }
Branch: 1 branch
BranchTrueFalseTotal
static int sth_point_of(int hole) { sth_tables_init(); return is_hole(hole) ? POINT[hole] : -1; } 56357 0 56357
222 617 ▶static int sth_on_ring(int hole) { sth_tables_init(); return is_hole(hole) ? RING[hole] : 0; }
Branch: 1 branch
BranchTrueFalseTotal
static int sth_on_ring(int hole) { sth_tables_init(); return is_hole(hole) ? RING[hole] : 0; } 617 0 617
223 437987 ▶static int sth_class_of(int hole) { sth_tables_init(); return is_hole(hole) ? CLS[hole] : -1; }
Branch: 1 branch
BranchTrueFalseTotal
static int sth_class_of(int hole) { sth_tables_init(); return is_hole(hole) ? CLS[hole] : -1; } 437987 0 437987
224  
225 174 static int copy_ints(const int *from, int n, int *out, int max)
226 {
227 int i;
228 2373 ▶ for (i = 0; i < n && i < max; i++) out[i] = from[i];
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < n && i < max; i++) out[i] = from[i]; 2199 174 2373
for (i = 0; i < n && i < max; i++) out[i] = from[i]; 2199 0 2199
229 174 return n;
230 }
231  
232 143 static int sth_point_holes(int point, int *out, int max)
233 {
234 143 sth_tables_init();
235 143 ▶ if (point < 0 || point >= STH_POINTS) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (point < 0 || point >= STH_POINTS) return 0; 143 0 143
if (point < 0 || point >= STH_POINTS) return 0; 1 142 143
236 142 return copy_ints(STH_POINT_LIST[point], STH_POINT_HOLES, out, max);
237 }
238  
239 31 static int sth_front(int point, int *out, int max)
240 {
241 31 sth_tables_init();
242 31 ▶ if (point < 0 || point >= STH_POINTS) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (point < 0 || point >= STH_POINTS) return 0; 31 0 31
if (point < 0 || point >= STH_POINTS) return 0; 0 31 31
243 31 return copy_ints(STH_FRONT[point], STH_FRONT_HOLES, out, max);
244 }
245  
246 1 static int sth_pairs(int *out, int max)
247 {
248 1 sth_tables_init();
249 1 return copy_ints(&PAIR[0][0], 2 * STH_PAIRS, out, max) / 2;
250 }
251  
252 /* ---- the zobrist table -----------------------------------------------------
253 *
254 * DERIVED, NOT COMMITTED. A fixed function of a fixed seed is the same in every
255 * process and on every machine, and cannot suffer the transcription error a
256 * hand-pasted array can. splitmix64 is published and has no weak seeds.
257 *
258 * CHANGING THIS GENERATOR, ITS SEED OR THE ORDER THE TABLE IS FILLED IN
259 * CHANGES EVERY KEY. t/02-position.t pins values so that is a red test.
260 */
261  
262 unsigned long long STH_ZOB[STH_SEATS_MAX][STH_HOLES];
263 unsigned long long STH_ZOB_MOVE[STH_SEATS_MAX];
264 static unsigned long long ZOB_SEATS[STH_SEATS_MAX + 1];
265 static int ZOB_READY = 0;
266  
267 39906 static unsigned long long splitmix64(unsigned long long *state)
268 {
269 unsigned long long z;
270 39906 *state += 0x9E3779B97F4A7C15ULL;
271 39906 z = *state;
272 39906 z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL;
273 39906 z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL;
274 39906 return z ^ (z >> 31);
275 }
276  
277 76257 static void zob_init(void)
278 {
279 76257 unsigned long long s = 0x737465726E68616CULL; /* "sternhal" */
280 int seat, h;
281 76257 ▶ if (ZOB_READY) return;
Branch: 1 branch
BranchTrueFalseTotal
if (ZOB_READY) return; 76203 54 76257
282 378 ▶ for (seat = 0; seat < STH_SEATS_MAX; seat++)
Branch: 1 branch
BranchTrueFalseTotal
for (seat = 0; seat < STH_SEATS_MAX; seat++) 324 54 378
283 39528 ▶ for (h = 0; h < STH_HOLES; h++)
Branch: 1 branch
BranchTrueFalseTotal
for (h = 0; h < STH_HOLES; h++) 39204 324 39528
284 39204 STH_ZOB[seat][h] = splitmix64(&s);
285 378 ▶ for (seat = 0; seat < STH_SEATS_MAX; seat++)
Branch: 1 branch
BranchTrueFalseTotal
for (seat = 0; seat < STH_SEATS_MAX; seat++) 324 54 378
286 324 STH_ZOB_MOVE[seat] = splitmix64(&s);
287 432 ▶ for (seat = 0; seat <= STH_SEATS_MAX; seat++)
Branch: 1 branch
BranchTrueFalseTotal
for (seat = 0; seat <= STH_SEATS_MAX; seat++) 378 54 432
288 378 ZOB_SEATS[seat] = splitmix64(&s);
289 54 ZOB_READY = 1;
290 }
291  
292 0 static unsigned long long sth_zobrist(int seat, int hole)
293 {
294 0 zob_init();
295 0 ▶ if (seat < 0 || seat >= STH_SEATS_MAX || !is_hole(hole)) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (seat < 0 || seat >= STH_SEATS_MAX || !is_hole(hole)) return 0; 0 0 0
if (seat < 0 || seat >= STH_SEATS_MAX || !is_hole(hole)) return 0; 0 0 0
if (seat < 0 || seat >= STH_SEATS_MAX || !is_hole(hole)) return 0; 0 0 0
296 0 return STH_ZOB[seat][hole];
297 }
298  
299 0 static unsigned long long sth_zobrist_move(int seat)
300 {
301 0 zob_init();
302 0 ▶ if (seat < 0 || seat >= STH_SEATS_MAX) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (seat < 0 || seat >= STH_SEATS_MAX) return 0; 0 0 0
if (seat < 0 || seat >= STH_SEATS_MAX) return 0; 0 0 0
303 0 return STH_ZOB_MOVE[seat];
304 }
305  
306 0 static unsigned long long sth_zobrist_seats(int seats)
307 {
308 0 zob_init();
309 0 ▶ if (seats < 0 || seats > STH_SEATS_MAX) return 0;
Branch: 2 branches
BranchTrueFalseTotal
if (seats < 0 || seats > STH_SEATS_MAX) return 0; 0 0 0
if (seats < 0 || seats > STH_SEATS_MAX) return 0; 0 0 0
310 0 return ZOB_SEATS[seats];
311 }
312  
313 /* Boards handed out and not yet dropped. It exists so a test can show that a
314 * destructor ran exactly once; it is a plain int and is not synchronised. */
315 static int STH_LIVE = 0;
316  
317 76203 static struct sth_board *board_alloc(int seats, int pieces)
318 {
319 struct sth_board *b;
320 int h;
321 76203 sth_tables_init();
322 76203 zob_init();
323 76203 ▶ if (!TABLES_OK || !sth_layout_ok(seats, pieces)) return NULL;
Branch: 2 branches
BranchTrueFalseTotal
if (!TABLES_OK || !sth_layout_ok(seats, pieces)) return NULL; 76203 0 76203
if (!TABLES_OK || !sth_layout_ok(seats, pieces)) return NULL; 0 76203 76203
324 76203 b = (struct sth_board *) malloc(sizeof(struct sth_board));
325 76203 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 76203 76203
326 76203 memset(b->cell, STH_OFF, sizeof b->cell);
327 9296766 ▶ for (h = 0; h < STH_HOLES; h++) b->cell[STH_SQ_OF[h]] = STH_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
for (h = 0; h < STH_HOLES; h++) b->cell[STH_SQ_OF[h]] = STH_EMPTY; 9220563 76203 9296766
328 76203 memset(b->count, 0, sizeof b->count);
329 76203 b->seats = seats;
330 76203 b->pieces = pieces;
331 76203 b->to_move = 0;
332 76203 sth_game_init(b);
333 76203 b->key = ZOB_SEATS[seats] ^ STH_ZOB_MOVE[0];
334 76203 STH_LIVE++;
335 76203 return b;
336 }
337  
338 10242080 static int sth_at(const struct sth_board *b, int hole)
339 {
340 10242080 ▶ if (!b || !is_hole(hole)) return STH_OFF;
Branch: 2 branches
BranchTrueFalseTotal
if (!b || !is_hole(hole)) return STH_OFF; 10242080 0 10242080
if (!b || !is_hole(hole)) return STH_OFF; 0 10242080 10242080
341 10242080 return b->cell[STH_SQ_OF[hole]];
342 }
343  
344 2864572 static void sth_put(struct sth_board *b, int hole, int seat)
345 {
346 int sq, old;
347 2864572 ▶ if (!b || !is_hole(hole) || seat < 0 || seat >= b->seats) return;
Branch: 4 branches
BranchTrueFalseTotal
if (!b || !is_hole(hole) || seat < 0 || seat >= b->seats) return; 2864572 0 2864572
if (!b || !is_hole(hole) || seat < 0 || seat >= b->seats) return; 2864572 0 2864572
if (!b || !is_hole(hole) || seat < 0 || seat >= b->seats) return; 2864572 0 2864572
if (!b || !is_hole(hole) || seat < 0 || seat >= b->seats) return; 1 2864571 2864572
348 2864571 sq = STH_SQ_OF[hole];
349 2864571 old = b->cell[sq];
350 2864571 ▶ if (old != STH_EMPTY) {
Branch: 1 branch
BranchTrueFalseTotal
if (old != STH_EMPTY) { 2 2864569 2864571
351 2 b->key ^= STH_ZOB[old][hole];
352 2 b->count[old]--;
353 }
354 2864571 b->cell[sq] = (unsigned char) seat;
355 2864571 b->key ^= STH_ZOB[seat][hole];
356 2864571 b->count[seat]++;
357 }
358  
359 135 static void sth_lift(struct sth_board *b, int hole)
360 {
361 int sq, old;
362 135 ▶ if (!b || !is_hole(hole)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!b || !is_hole(hole)) return; 135 0 135
if (!b || !is_hole(hole)) return; 0 135 135
363 135 sq = STH_SQ_OF[hole];
364 135 old = b->cell[sq];
365 135 ▶ if (old == STH_EMPTY) return;
Branch: 1 branch
BranchTrueFalseTotal
if (old == STH_EMPTY) return; 1 134 135
366 134 b->key ^= STH_ZOB[old][hole];
367 134 b->count[old]--;
368 134 b->cell[sq] = STH_EMPTY;
369 }
370  
371 5379 static struct sth_board *sth_board_empty(int seats, int pieces)
372 {
373 5379 return board_alloc(seats, pieces);
374 }
375  
376 1562 static struct sth_board *sth_board_new(int seats, int pieces)
377 {
378 1562 struct sth_board *b = board_alloc(seats, pieces);
379 int seat, i, p;
380 1562 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 1562 1562
381 7005 ▶ for (seat = 0; seat < seats; seat++) {
Branch: 1 branch
BranchTrueFalseTotal
for (seat = 0; seat < seats; seat++) { 5443 1562 7005
382 5443 p = STH_LAYOUT[seats][seat];
383 59873 ▶ for (i = 0; i < STH_POINT_HOLES; i++) sth_put(b, STH_POINT_LIST[p][i], seat);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < STH_POINT_HOLES; i++) sth_put(b, STH_POINT_LIST[p][i], seat); 54430 5443 59873
384 5443 ▶ if (pieces == STH_PIECES_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (pieces == STH_PIECES_MAX) 255 5188 5443
385 1530 ▶ for (i = 0; i < STH_FRONT_HOLES; i++) sth_put(b, STH_FRONT[p][i], seat);
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < STH_FRONT_HOLES; i++) sth_put(b, STH_FRONT[p][i], seat); 1275 255 1530
386 }
387 1562 return b;
388 }
389  
390 731190 static struct sth_board *sth_board_copy(const struct sth_board *b)
391 {
392 struct sth_board *n;
393 731190 ▶ if (!b) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return NULL; 0 731190 731190
394 731190 n = (struct sth_board *) malloc(sizeof(struct sth_board));
395 731190 ▶ if (!n) return NULL;
Branch: 1 branch
BranchTrueFalseTotal
if (!n) return NULL; 0 731190 731190
396 731190 *n = *b;
397 731190 STH_LIVE++;
398 731190 return n;
399 }
400  
401 807393 static void sth_board_drop(struct sth_board *b)
402 {
403 807393 ▶ if (!b) return;
Branch: 1 branch
BranchTrueFalseTotal
if (!b) return; 0 807393 807393
404 807393 free(b);
405 807393 STH_LIVE--;
406 }
407  
408 7 static int sth_live(void) { return STH_LIVE; }
409  
410 132290 ▶static int sth_to_move(const struct sth_board *b) { return b ? b->to_move : -1; }
Branch: 1 branch
BranchTrueFalseTotal
static int sth_to_move(const struct sth_board *b) { return b ? b->to_move : -1; } 132290 0 132290
411  
412 73244 static void sth_set_to_move(struct sth_board *b, int seat)
413 {
414 73244 ▶ if (!b || seat < 0 || seat >= b->seats) return;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || seat < 0 || seat >= b->seats) return; 73244 0 73244
if (!b || seat < 0 || seat >= b->seats) return; 73244 0 73244
if (!b || seat < 0 || seat >= b->seats) return; 1 73243 73244
415 73243 b->key ^= STH_ZOB_MOVE[b->to_move] ^ STH_ZOB_MOVE[seat];
416 73243 b->to_move = seat;
417 }
418  
419 1982799 ▶static int sth_seats(const struct sth_board *b) { return b ? b->seats : 0; }
Branch: 1 branch
BranchTrueFalseTotal
static int sth_seats(const struct sth_board *b) { return b ? b->seats : 0; } 1982799 0 1982799
420 992 ▶static int sth_pieces(const struct sth_board *b) { return b ? b->pieces : 0; }
Branch: 1 branch
BranchTrueFalseTotal
static int sth_pieces(const struct sth_board *b) { return b ? b->pieces : 0; } 992 0 992
421  
422 24 static int sth_count(const struct sth_board *b, int seat)
423 {
424 24 ▶ if (!b || seat < 0 || seat >= b->seats) return 0;
Branch: 3 branches
BranchTrueFalseTotal
if (!b || seat < 0 || seat >= b->seats) return 0; 24 0 24
if (!b || seat < 0 || seat >= b->seats) return 0; 24 0 24
if (!b || seat < 0 || seat >= b->seats) return 0; 0 24 24
425 24 return b->count[seat];
426 }
427  
428 911638 ▶static unsigned long long sth_key(const struct sth_board *b) { return b ? b->key : 0; }
Branch: 1 branch
BranchTrueFalseTotal
static unsigned long long sth_key(const struct sth_board *b) { return b ? b->key : 0; } 911638 0 911638
429  
430 /* ---- the position string -------------------------------------------------
431 *
432 * The seventeen rows from the top, '/' between them. In a row, a digit 0 to 5
433 * is that seat's piece and [n] is a run of n empty holes, 1 to 13. Then a
434 * space, the seat to move, a space, the seat count, a space, the piece count,
435 * and from version 5 the game after a ';' (sth_rules.c). The two-seat set-up is
436 *
437 * 0/00/000/0000/[13]/[12]/[11]/[10]/[9]/[10]/[11]/[12]/[13]/1111/111/11/1 0 2 10
438 */
439  
440 5927813 static int put_num(char *tmp, int n, int v)
441 {
442 5927813 ▶ if (v >= 10) tmp[n++] = (char) ('0' + v / 10);
Branch: 1 branch
BranchTrueFalseTotal
if (v >= 10) tmp[n++] = (char) ('0' + v / 10); 322975 5604838 5927813
443 5927813 tmp[n++] = (char) ('0' + v % 10);
444 5927813 return n;
445 }
446  
447 188307 static int sth_to_string(const struct sth_board *b, char *out, int max)
448 {
449 char tmp[STH_POS_MAX];
450 188307 int n = 0, row, i, run, c;
451 188307 ▶ if (!b || !out) return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (!b || !out) return -1; 188307 0 188307
if (!b || !out) return -1; 0 188307 188307
452 3389526 ▶ for (row = 0; row < STH_ROWS; row++) {
Branch: 1 branch
BranchTrueFalseTotal
for (row = 0; row < STH_ROWS; row++) { 3201219 188307 3389526
453 3201219 ▶ if (row > 0) tmp[n++] = '/';
Branch: 1 branch
BranchTrueFalseTotal
if (row > 0) tmp[n++] = '/'; 3012912 188307 3201219
454 3201219 run = 0;
455 25986366 ▶ for (i = 0; i < ROW_LEN[row]; i++) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < ROW_LEN[row]; i++) { 22785147 3201219 25986366
456 22785147 c = b->cell[STH_SQ_OF[ROW_START[row] + i]];
457 22785147 ▶ if (c == STH_EMPTY) { run++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (c == STH_EMPTY) { run++; continue; } 15689202 7095945 22785147
458 7095945 ▶ if (run) { tmp[n++] = '['; n = put_num(tmp, n, run); tmp[n++] = ']'; run = 0; }
Branch: 1 branch
BranchTrueFalseTotal
if (run) { tmp[n++] = '['; n = put_num(tmp, n, run); tmp[n++] = ']'; run = 0; } 3660104 3435841 7095945
459 7095945 tmp[n++] = (char) ('0' + c);
460 }
461 3201219 ▶ if (run) { tmp[n++] = '['; n = put_num(tmp, n, run); tmp[n++] = ']'; }
Branch: 1 branch
BranchTrueFalseTotal
if (run) { tmp[n++] = '['; n = put_num(tmp, n, run); tmp[n++] = ']'; } 2079402 1121817 3201219
462 }
463 188307 tmp[n++] = ' ';
464 188307 tmp[n++] = (char) ('0' + b->to_move);
465 188307 tmp[n++] = ' ';
466 188307 tmp[n++] = (char) ('0' + b->seats);
467 188307 tmp[n++] = ' ';
468 188307 n = put_num(tmp, n, b->pieces);
469 188307 n = sth_game_suffix_write(b, tmp, n);
470 188307 ▶ if (n + 1 > max) return -1;
Branch: 1 branch
BranchTrueFalseTotal
if (n + 1 > max) return -1; 0 188307 188307
471 188307 memcpy(out, tmp, (size_t) n);
472 188307 out[n] = '\0';
473 188307 return n;
474 }
475  
476 /* A decimal of one or two digits with no leading zero, or -1. */
477 6428620 static int read_num(const char *s, const char **end)
478 {
479 int v;
480 6428620 ▶ if (*s < '1' || *s > '9') return -1;
Branch: 2 branches
BranchTrueFalseTotal
if (*s < '1' || *s > '9') return -1; 6428522 98 6428620
if (*s < '1' || *s > '9') return -1; 138 6428384 6428522
481 6428384 v = *s++ - '0';
482 6428384 ▶ if (*s >= '0' && *s <= '9') v = v * 10 + (*s++ - '0');
Branch: 2 branches
BranchTrueFalseTotal
if (*s >= '0' && *s <= '9') v = v * 10 + (*s++ - '0'); 6428324 60 6428384
if (*s >= '0' && *s <= '9') v = v * 10 + (*s++ - '0'); 224156 6204168 6428324
483 6428384 *end = s;
484 6428384 return v;
485 }
486  
487 71212 static struct sth_board *sth_board_of_string(const char *str, int *err)
488 {
489 71212 int code = STH_OK;
490 int cells[STH_HOLES];
491 int width[STH_ROWS];
492 71212 int nrows = 1, filled = 0, seat_max = -1, to_move, seats, pieces, i, seat;
493 int count[STH_SEATS_MAX];
494 const char *s, *tail, *end, *game;
495 size_t len;
496 struct sth_board *b;
497  
498 71212 sth_tables_init();
499 71212 ▶ if (!str || !*str) { code = STH_POS_NULL; goto refused; }
Branch: 2 branches
BranchTrueFalseTotal
if (!str || !*str) { code = STH_POS_NULL; goto refused; } 71212 0 71212
if (!str || !*str) { code = STH_POS_NULL; goto refused; } 2 71210 71212
500 71210 len = strlen(str);
501 71210 ▶ if (len >= STH_POS_MAX) { code = STH_POS_LONG; goto refused; }
Branch: 1 branch
BranchTrueFalseTotal
if (len >= STH_POS_MAX) { code = STH_POS_LONG; goto refused; } 1 71209 71210
502  
503 71209 tail = strchr(str, ' ');
504 71209 ▶ if (!tail) tail = str + len;
Branch: 1 branch
BranchTrueFalseTotal
if (!tail) tail = str + len; 3 71206 71209
505  
506 5986350 ▶ for (s = str; s < tail; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = str; s < tail; s++) { 5916335 70015 5986350
507 5916335 ▶ if (*s == '/' || (*s >= '0' && *s <= '5')) continue;
Branch: 3 branches
BranchTrueFalseTotal
if (*s == '/' || (*s >= '0' && *s <= '5')) continue; 4787110 1129225 5916335
if (*s == '/' || (*s >= '0' && *s <= '5')) continue; 4787110 0 4787110
if (*s == '/' || (*s >= '0' && *s <= '5')) continue; 2659838 2127272 4787110
508 2127272 ▶ if (*s == '[') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '[') { 2126742 530 2127272
509 2126742 int v = read_num(s + 1, &end);
510 2126742 ▶ if (v < 1 || v > STH_ROW_MAX || end >= tail || *end != ']') {
Branch: 4 branches
BranchTrueFalseTotal
if (v < 1 || v > STH_ROW_MAX || end >= tail || *end != ']') { 2126526 216 2126742
if (v < 1 || v > STH_ROW_MAX || end >= tail || *end != ']') { 2126326 200 2126526
if (v < 1 || v > STH_ROW_MAX || end >= tail || *end != ']') { 2126305 21 2126326
if (v < 1 || v > STH_ROW_MAX || end >= tail || *end != ']') { 227 2126078 2126305
511 664 code = STH_POS_LETTER; goto refused;
512 }
513 2126078 s = end;
514 2126078 continue;
515 }
516 530 code = STH_POS_LETTER; goto refused;
517 }
518  
519 70015 memset(width, 0, sizeof width);
520 10240486 ▶ for (s = str; s < tail; s++)
Branch: 1 branch
BranchTrueFalseTotal
for (s = str; s < tail; s++) 10170471 70015 10240486
521 10170471 ▶ if (*s == '/') nrows++;
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '/') nrows++; 1119441 9051030 10170471
522 70015 ▶ if (nrows != STH_ROWS) { code = STH_POS_ROWS; goto refused; }
Branch: 1 branch
BranchTrueFalseTotal
if (nrows != STH_ROWS) { code = STH_POS_ROWS; goto refused; } 269 69746 70015
523  
524 69746 nrows = 1;
525 5933002 ▶ for (s = str; s < tail; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = str; s < tail; s++) { 5863256 69746 5933002
526 5863256 ▶ if (*s == '/') { nrows++; continue; }
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '/') { nrows++; continue; } 1115936 4747320 5863256
527 4747320 ▶ if (*s == '[') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '[') { 2118443 2628877 4747320
528 2118443 int v = read_num(s + 1, &end);
529 2118443 width[nrows - 1] += v;
530 2118443 s = end;
531 2118443 continue;
532 }
533 2628877 width[nrows - 1]++;
534 }
535 1252974 ▶ for (i = 0; i < STH_ROWS; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < STH_ROWS; i++) 1183515 69459 1252974
536 1183515 ▶ if (width[i] != ROW_LEN[i]) { code = STH_POS_WIDTH; goto refused; }
Branch: 1 branch
BranchTrueFalseTotal
if (width[i] != ROW_LEN[i]) { code = STH_POS_WIDTH; goto refused; } 287 1183228 1183515
537  
538 69459 s = tail;
539 69459 ▶ if (s[0] != ' ' || s[1] < '0' || s[1] > '5' || s[2] != ' ' || s[3] < '1' || s[3] > '6'
Branch: 6 branches
BranchTrueFalseTotal
if (s[0] != ' ' || s[1] < '0' || s[1] > '5' || s[2] != ' ' || s[3] < '1' || s[3] > '6' 69458 1 69459
if (s[0] != ' ' || s[1] < '0' || s[1] > '5' || s[2] != ' ' || s[3] < '1' || s[3] > '6' 69450 8 69458
if (s[0] != ' ' || s[1] < '0' || s[1] > '5' || s[2] != ' ' || s[3] < '1' || s[3] > '6' 69439 11 69450
if (s[0] != ' ' || s[1] < '0' || s[1] > '5' || s[2] != ' ' || s[3] < '1' || s[3] > '6' 69422 17 69439
if (s[0] != ' ' || s[1] < '0' || s[1] > '5' || s[2] != ' ' || s[3] < '1' || s[3] > '6' 69414 8 69422
if (s[0] != ' ' || s[1] < '0' || s[1] > '5' || s[2] != ' ' || s[3] < '1' || s[3] > '6' 69402 12 69414
540 69402 ▶ || s[4] != ' ') {
Branch: 1 branch
BranchTrueFalseTotal
|| s[4] != ' ') { 32 69370 69402
541 89 code = STH_POS_FIELDS; goto refused;
542 }
543 69370 to_move = s[1] - '0';
544 69370 seats = s[3] - '0';
545 69370 pieces = read_num(s + 5, &end);
546 69370 ▶ if (pieces < 0 || (*end != '\0' && *end != ';') || !sth_layout_ok(seats, pieces) || to_move >= seats) {
Branch: 5 branches
BranchTrueFalseTotal
if (pieces < 0 || (*end != '\0' && *end != ';') || !sth_layout_ok(seats, pieces) || to_move >= seats) { 69350 20 69370
if (pieces < 0 || (*end != '\0' && *end != ';') || !sth_layout_ok(seats, pieces) || to_move >= seats) { 25134 44216 69350
if (pieces < 0 || (*end != '\0' && *end != ';') || !sth_layout_ok(seats, pieces) || to_move >= seats) { 25115 19 25134
if (pieces < 0 || (*end != '\0' && *end != ';') || !sth_layout_ok(seats, pieces) || to_move >= seats) { 69320 11 69331
if (pieces < 0 || (*end != '\0' && *end != ';') || !sth_layout_ok(seats, pieces) || to_move >= seats) { 1 69319 69320
547 51 code = STH_POS_FIELDS; goto refused;
548 }
549 69319 ▶ game = *end == ';' ? end + 1 : NULL;
Branch: 1 branch
BranchTrueFalseTotal
game = *end == ';' ? end + 1 : NULL; 25113 44206 69319
550  
551 5905425 ▶ for (s = str; s < tail; s++) {
Branch: 1 branch
BranchTrueFalseTotal
for (s = str; s < tail; s++) { 5836106 69319 5905425
552 5836106 ▶ if (*s == '/') continue;
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '/') continue; 1109104 4727002 5836106
553 4727002 ▶ if (*s == '[') {
Branch: 1 branch
BranchTrueFalseTotal
if (*s == '[') { 2114065 2612937 4727002
554 2114065 int v = read_num(s + 1, &end);
555 7888727 ▶ for (i = 0; i < v; i++) cells[filled++] = STH_EMPTY;
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < v; i++) cells[filled++] = STH_EMPTY; 5774662 2114065 7888727
556 2114065 s = end;
557 2114065 continue;
558 }
559 2612937 seat = *s - '0';
560 2612937 ▶ if (seat > seat_max) seat_max = seat;
Branch: 1 branch
BranchTrueFalseTotal
if (seat > seat_max) seat_max = seat; 149448 2463489 2612937
561 2612937 cells[filled++] = seat;
562 }
563 69319 ▶ if (seat_max >= seats) { code = STH_POS_SEAT; goto refused; }
Branch: 1 branch
BranchTrueFalseTotal
if (seat_max >= seats) { code = STH_POS_SEAT; goto refused; } 15 69304 69319
564  
565 69304 memset(count, 0, sizeof count);
566 8455088 ▶ for (i = 0; i < STH_HOLES; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < STH_HOLES; i++) 8385784 69304 8455088
567 8385784 ▶ if (cells[i] != STH_EMPTY) count[cells[i]]++;
Branch: 1 branch
BranchTrueFalseTotal
if (cells[i] != STH_EMPTY) count[cells[i]]++; 2612477 5773307 8385784
568 328998 ▶ for (seat = 0; seat < seats; seat++)
Branch: 1 branch
BranchTrueFalseTotal
for (seat = 0; seat < seats; seat++) 259736 69262 328998
569 259736 ▶ if (count[seat] != pieces) { code = STH_POS_COUNT; goto refused; }
Branch: 1 branch
BranchTrueFalseTotal
if (count[seat] != pieces) { code = STH_POS_COUNT; goto refused; } 42 259694 259736
570  
571 69262 b = board_alloc(seats, pieces);
572 69262 ▶ if (!b) { code = STH_POS_FIELDS; goto refused; }
Branch: 1 branch
BranchTrueFalseTotal
if (!b) { code = STH_POS_FIELDS; goto refused; } 0 69262 69262
573 8449964 ▶ for (i = 0; i < STH_HOLES; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < STH_HOLES; i++) 8380702 69262 8449964
574 8380702 ▶ if (cells[i] != STH_EMPTY) sth_put(b, i, cells[i]);
Branch: 1 branch
BranchTrueFalseTotal
if (cells[i] != STH_EMPTY) sth_put(b, i, cells[i]); 2610705 5769997 8380702
575 69262 sth_set_to_move(b, to_move);
576 69262 ▶ if (game) {
Branch: 1 branch
BranchTrueFalseTotal
if (game) { 25113 44149 69262
577 25113 int g = sth_game_suffix_read(b, game);
578 25113 ▶ if (g != STH_OK) { sth_board_drop(b); code = g; goto refused; }
Branch: 1 branch
BranchTrueFalseTotal
if (g != STH_OK) { sth_board_drop(b); code = g; goto refused; } 17 25096 25113
579 }
580 69245 ▶ if (err) *err = STH_OK;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = STH_OK; 69245 0 69245
581 69245 return b;
582  
583 1967 refused:
584 1967 ▶ if (err) *err = code;
Branch: 1 branch
BranchTrueFalseTotal
if (err) *err = code; 1967 0 1967
585 1967 return NULL;
586 }
587  
588 /* ---- the table ---------------------------------------------------------------- */
589  
590 static const struct sth_abi STH_ABI = {
591 STH_ABI_VERSION,
592  
593 sth_board_new,
594 sth_board_empty,
595 sth_board_of_string,
596 sth_board_copy,
597 sth_board_drop,
598 sth_live,
599  
600 sth_at,
601 sth_put,
602 sth_lift,
603  
604 sth_to_move,
605 sth_set_to_move,
606 sth_seats,
607 sth_pieces,
608 sth_count,
609  
610 sth_key,
611 sth_zobrist,
612 sth_zobrist_move,
613 sth_zobrist_seats,
614  
615 sth_to_string,
616  
617 sth_tables_ok,
618 sth_layout_ok,
619 sth_layout_point,
620 sth_hole_of,
621 sth_hole_x,
622 sth_hole_y,
623 sth_row_of,
624 sth_col_of,
625 sth_row_len,
626 sth_row_start,
627 sth_next,
628 sth_degree,
629 sth_dir_opposite,
630 sth_point_of,
631 sth_on_ring,
632 sth_class_of,
633 sth_point_holes,
634 sth_front,
635 sth_pairs,
636  
637 sth_steps,
638 sth_hops,
639 sth_do_move,
640 sth_undo_move,
641  
642 sth_reach,
643 sth_path,
644 sth_legal,
645  
646 sth_home_complete,
647 sth_finished,
648 sth_place_of,
649 sth_distance_left,
650 sth_can_pass,
651 sth_play,
652 sth_pass,
653 sth_resign,
654 sth_unplay,
655 sth_game_over,
656 sth_game_how,
657 sth_ply,
658 sth_cap,
659 sth_set_cap,
660  
661 sth_weights_default,
662 sth_evaluate,
663 sth_search,
664 sth_random_forward,
665 sth_own_key,
666 };
667  
668 54 const struct sth_abi *sth_abi_table(void)
669 {
670 54 sth_tables_init();
671 54 zob_init();
672 54 return &STH_ABI;
673 }
« Sternhalma.xs ↑ Summary sth_moves.c »