Brandubh.xs

filter: Statementstmt 98.2% 219 / 223 Branchbran 74.1% 138 / 186 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.2% 357 / 409 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« blib/lib/Game/Brandubh/Engine.pm ↑ Summary bd_rules.c »
1 /* Brandubh.xs - the door to the C board.
2 *
3 * At the TOP of the distribution and not under lib/, because an XSMULTI build's
4 * export list and Strawberry's import library rule never agree on a name. The
5 * siblings that learned this the hard way say so in Go.xs and Xiangqi.xs.
6 *
7 * The XSUBs land in Game::Brandubh::Engine, not in Game::Brandubh, and are named
8 * with a leading underscore: they take a raw pointer as a UV and there is no
9 * typemap. The Perl that owns the lifetime is lib/Game/Brandubh/Engine.pm.
10 *
11 * `_put`, `_lift` and `_relocate` are the structure primitives from bd_abi.h
12 * and CHECK NOTHING.
13 */
14  
15 #define PERL_NO_GET_CONTEXT
16  
17 #include "EXTERN.h"
18 #include "perl.h"
19 #include "XSUB.h"
20  
21 #include "bd_abi.h"
22  
23 static const struct bd_abi *BD = NULL;
24  
25 #define BOARD(u) INT2PTR(struct bd_board *, (u))
26 #define GAME(u) INT2PTR(struct bd_game *, (u))
27  
28 /* A 64-bit value crosses as a 16-character hex string, never as a UV.
29 *
30 * On a perl with 32-bit IVs a UV cannot hold it and the top half would vanish
31 * silently, which for a zobrist key means a test that passes while comparing
32 * half a number. Hand-rolled rather than via my_snprintf. Copied from
33 * Xiangqi.xs, which copied it from Go.xs, which is where it was worked out.
34 */
35 490394 static SV *bd_hex64(pTHX_ unsigned long long v)
36 {
37 static const char digits[] = "0123456789abcdef";
38 char buf[16];
39 int i;
40 8336698 ▶ for (i = 15; i >= 0; i--) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 15; i >= 0; i--) { 7846304 490394 8336698
41 7846304 buf[i] = digits[v & 0xfULL];
42 7846304 v >>= 4;
43 }
44 490394 return newSVpvn(buf, 16);
45 }
46  
47 /* A count crosses as a decimal STRING, for the reason a key crosses as hex: a
48 * perft total outgrows a 32-bit IV within a few plies. Hand-rolled and integer
49 * only; there is no floating point anywhere in this distribution's C.
50 */
51 18132 static SV *bd_dec64(pTHX_ unsigned long long v)
52 {
53 char buf[24];
54 18132 int i = (int) sizeof(buf);
55 do {
56 73428 buf[--i] = (char) ('0' + (int) (v % 10ULL));
57 73428 v /= 10ULL;
58 73428 ▶ } while (v);
Branch: 1 branch
BranchTrueFalseTotal
} while (v); 55296 18132 73428
59 18132 return newSVpvn(buf + i, (STRLEN) (sizeof(buf) - i));
60 }
61  
62 /* The rule set crosses as a hash reference, or as undef for the default. Only
63 * the keys that differ need be given.
64 *
65 * AN UNKNOWN KEY IS A CROAK and not a shrug: `throne_pas => 0` silently
66 * playing the default game is the kind of mistake that agrees with you.
67 */
68 1757706 static void bd_variant_of_sv(pTHX_ SV *sv, struct bd_variant *v)
69 {
70 HV *hv;
71 HE *he;
72  
73 1757706 (BD->variant_default)(v);
74 1757706 ▶ if (!sv || !SvOK(sv)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) return; 1757706 0 1757706
if (!sv || !SvOK(sv)) return; 821000 936706 1757706
75 936706 ▶ if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) 936705 1 936706
if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) 0 936705 936705
76 1 croak("Game::Brandubh::Engine: a variant is a hash reference");
77 936705 hv = (HV *) SvRV(sv);
78  
79 936705 hv_iterinit(hv);
80 2190808 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 1254106 936702 2190808
81 STRLEN klen;
82 1254106 ▶ const char *key = HePV(he, klen);
Branch: 1 branch
BranchTrueFalseTotal
const char *key = HePV(he, klen); 0 1254106 1254106
83 1254106 SV *val = HeVAL(he);
84  
85 1254106 ▶ if (klen == 11 && memEQ(key, "throne_pass", 11))
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 11 && memEQ(key, "throne_pass", 11)) 616975 637131 1254106
if (klen == 11 && memEQ(key, "throne_pass", 11)) 603659 13316 616975
86 603659 v->throne_pass = SvTRUE(val) ? 1 : 0;
87 650447 ▶ else if (klen == 14 && memEQ(key, "throne_reentry", 14))
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 14 && memEQ(key, "throne_reentry", 14)) 603698 46749 650447
else if (klen == 14 && memEQ(key, "throne_reentry", 14)) 603698 0 603698
88 603698 v->throne_reentry = SvTRUE(val) ? 1 : 0;
89 46749 ▶ else if (klen == 19 && memEQ(key, "king_everywhere_two", 19))
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 19 && memEQ(key, "king_everywhere_two", 19)) 13318 33431 46749
else if (klen == 19 && memEQ(key, "king_everywhere_two", 19)) 13318 0 13318
90 13318 v->king_everywhere_two = SvTRUE(val) ? 1 : 0;
91 33431 ▶ else if (klen == 11 && memEQ(key, "king_strong", 11))
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 11 && memEQ(key, "king_strong", 11)) 13316 20115 33431
else if (klen == 11 && memEQ(key, "king_strong", 11)) 13316 0 13316
92 13316 v->king_strong = SvTRUE(val) ? 1 : 0;
93 33221 ▶ else if (klen == 6 && memEQ(key, "escape", 6)) {
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 6 && memEQ(key, "escape", 6)) { 16027 4088 20115
else if (klen == 6 && memEQ(key, "escape", 6)) { 13107 2920 16027
94 STRLEN vlen;
95 13107 const char *s = SvPV(val, vlen);
96 13107 ▶ if (vlen == 4 && memEQ(s, "edge", 4)) v->escape_edge = 1;
Branch: 2 branches
BranchTrueFalseTotal
if (vlen == 4 && memEQ(s, "edge", 4)) v->escape_edge = 1; 10547 2560 13107
if (vlen == 4 && memEQ(s, "edge", 4)) v->escape_edge = 1; 10547 0 10547
97 2560 ▶ else if (vlen == 6 && memEQ(s, "corner", 6)) v->escape_edge = 0;
Branch: 2 branches
BranchTrueFalseTotal
else if (vlen == 6 && memEQ(s, "corner", 6)) v->escape_edge = 0; 2559 1 2560
else if (vlen == 6 && memEQ(s, "corner", 6)) v->escape_edge = 0; 2559 0 2559
98 1 else croak("Game::Brandubh::Engine: escape is 'corner' or 'edge'");
99 }
100 7008 ▶ else if (klen == 6 && memEQ(key, "repeat", 6))
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 6 && memEQ(key, "repeat", 6)) 2920 4088 7008
else if (klen == 6 && memEQ(key, "repeat", 6)) 2920 0 2920
101 2920 v->repeat = (int) SvIV(val);
102 4088 ▶ else if (klen == 7 && memEQ(key, "ply_cap", 7))
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 7 && memEQ(key, "ply_cap", 7)) 4087 1 4088
else if (klen == 7 && memEQ(key, "ply_cap", 7)) 4086 1 4087
103 4086 v->ply_cap = (int) SvIV(val);
104 else
105 2 ▶ croak("Game::Brandubh::Engine: no variant field is called '%" SVf "'",
Branch: 2 branches
BranchTrueFalseTotal
croak("Game::Brandubh::Engine: no variant field is called '%" SVf "'", 2 0 2
croak("Game::Brandubh::Engine: no variant field is called '%" SVf "'", 0 2 2
106 SVfARG(HeSVKEY_force(he)));
107 }
108 }
109  
110 /* The weights of the evaluation cross as a hash reference, or as undef for the
111 * defaults, and an unknown key croaks, for the reason a variant's does. */
112 12695 static void bd_weights_of_sv(pTHX_ SV *sv, struct bd_weights *w)
113 {
114 HV *hv;
115 HE *he;
116  
117 12695 (BD->weights_default)(w);
118 12695 ▶ if (!sv || !SvOK(sv)) return;
Branch: 2 branches
BranchTrueFalseTotal
if (!sv || !SvOK(sv)) return; 12695 0 12695
if (!sv || !SvOK(sv)) return; 12678 17 12695
119 17 ▶ if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV)
Branch: 2 branches
BranchTrueFalseTotal
if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) 17 0 17
if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) 1 16 17
120 1 croak("Game::Brandubh::Rules: weights are a hash reference");
121 16 hv = (HV *) SvRV(sv);
122  
123 16 hv_iterinit(hv);
124 114 ▶ while ((he = hv_iternext(hv))) {
Branch: 1 branch
BranchTrueFalseTotal
while ((he = hv_iternext(hv))) { 100 14 114
125 STRLEN klen;
126 100 ▶ const char *key = HePV(he, klen);
Branch: 1 branch
BranchTrueFalseTotal
const char *key = HePV(he, klen); 0 100 100
127 100 int val = (int) SvIV(HeVAL(he));
128  
129 100 ▶ if (klen == 8 && memEQ(key, "attacker", 8)) w->attacker = val;
Branch: 2 branches
BranchTrueFalseTotal
if (klen == 8 && memEQ(key, "attacker", 8)) w->attacker = val; 56 44 100
if (klen == 8 && memEQ(key, "attacker", 8)) w->attacker = val; 14 42 56
130 86 ▶ else if (klen == 8 && memEQ(key, "defender", 8)) w->defender = val;
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 8 && memEQ(key, "defender", 8)) w->defender = val; 42 44 86
else if (klen == 8 && memEQ(key, "defender", 8)) w->defender = val; 14 28 42
131 72 ▶ else if (klen == 8 && memEQ(key, "lane_one", 8)) w->lane_one = val;
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 8 && memEQ(key, "lane_one", 8)) w->lane_one = val; 28 44 72
else if (klen == 8 && memEQ(key, "lane_one", 8)) w->lane_one = val; 14 14 28
132 58 ▶ else if (klen == 8 && memEQ(key, "lane_two", 8)) w->lane_two = val;
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 8 && memEQ(key, "lane_two", 8)) w->lane_two = val; 14 44 58
else if (klen == 8 && memEQ(key, "lane_two", 8)) w->lane_two = val; 14 0 14
133 44 ▶ else if (klen == 7 && memEQ(key, "freedom", 7)) w->freedom = val;
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 7 && memEQ(key, "freedom", 7)) w->freedom = val; 15 29 44
else if (klen == 7 && memEQ(key, "freedom", 7)) w->freedom = val; 14 1 15
134 30 ▶ else if (klen == 12 && memEQ(key, "corner_guard", 12)) w->corner_guard = val;
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 12 && memEQ(key, "corner_guard", 12)) w->corner_guard = val; 14 16 30
else if (klen == 12 && memEQ(key, "corner_guard", 12)) w->corner_guard = val; 14 0 14
135 16 ▶ else if (klen == 4 && memEQ(key, "ring", 4)) w->ring = val;
Branch: 2 branches
BranchTrueFalseTotal
else if (klen == 4 && memEQ(key, "ring", 4)) w->ring = val; 14 2 16
else if (klen == 4 && memEQ(key, "ring", 4)) w->ring = val; 14 0 14
136 else
137 2 ▶ croak("Game::Brandubh::Rules: no weight is called '%" SVf "'",
Branch: 2 branches
BranchTrueFalseTotal
croak("Game::Brandubh::Rules: no weight is called '%" SVf "'", 2 0 2
croak("Game::Brandubh::Rules: no weight is called '%" SVf "'", 0 2 2
138 SVfARG(HeSVKEY_force(he)));
139 }
140 }
141  
142 MODULE = Game::Brandubh PACKAGE = Game::Brandubh::Engine
143  
144 PROTOTYPES: DISABLE
145  
146 BOOT:
147 59 BD = bd_abi_table();
148  
149 UV
150 _abi_ptr()
151 CODE:
152 0 ▶ RETVAL = PTR2UV(BD);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV(BD); 0 0 0
153 OUTPUT:
154 RETVAL
155  
156 UV
157 _abi_version()
158 CODE:
159 1 ▶ RETVAL = (UV)BD->abi_version;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = (UV)BD->abi_version; 0 1 1
160 OUTPUT:
161 RETVAL
162  
163 UV
164 _new_board()
165 CODE:
166 22 ▶ RETVAL = PTR2UV((BD->board_new)());
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV((BD->board_new)()); 15 7 22
167 OUTPUT:
168 RETVAL
169  
170 UV
171 _new_empty()
172 CODE:
173 47742 ▶ RETVAL = PTR2UV((BD->board_empty)());
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV((BD->board_empty)()); 47732 10 47742
174 OUTPUT:
175 RETVAL
176  
177 UV
178 _copy_board(u)
179 UV u
180 CODE:
181 1486 ▶ RETVAL = PTR2UV((BD->board_copy)(BOARD(u)));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV((BD->board_copy)(BOARD(u))); 1483 3 1486
182 OUTPUT:
183 RETVAL
184  
185 void
186 _drop_board(u)
187 UV u
188 CODE:
189 53620 (BD->board_drop)(BOARD(u));
190  
191 int
192 _live()
193 CODE:
194 14 RETVAL = (BD->live)();
195 OUTPUT:
196 RETVAL
197  
198 # A string that will not parse returns undef and the code, rather than
199 # croaking: the reason is what a caller shows a person, and six of them are
200 # distinguishable.
201 void
202 _of_string(str)
203 const char *str
204 PREINIT:
205 struct bd_board *b;
206 5354 int err = 0;
207 PPCODE:
208 5354 b = (BD->board_of_string)(str, &err);
209 5354 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 5354 5354
210 5354 ▶ if (b) PUSHs(sv_2mortal(newSVuv(PTR2UV(b))));
Branch: 1 branch
BranchTrueFalseTotal
if (b) PUSHs(sv_2mortal(newSVuv(PTR2UV(b)))); 4370 984 5354
211 984 else PUSHs(&PL_sv_undef);
212 5354 PUSHs(sv_2mortal(newSViv(err)));
213  
214 SV *
215 _to_string(u)
216 UV u
217 PREINIT:
218 char buf[BD_POS_MAX];
219 int n;
220 CODE:
221 177325 n = (BD->to_string)(BOARD(u), buf, (int)sizeof(buf));
222 177325 ▶ if (n < 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) XSRETURN_UNDEF; 0 177325 177325
223 177325 RETVAL = newSVpvn(buf, (STRLEN)n);
224 OUTPUT:
225 RETVAL
226  
227 int
228 _at(u, sq)
229 UV u
230 int sq
231 CODE:
232 524960 RETVAL = (BD->at)(BOARD(u), sq);
233 OUTPUT:
234 RETVAL
235  
236 void
237 _put(u, sq, piece)
238 UV u
239 int sq
240 int piece
241 CODE:
242 775114 (BD->put)(BOARD(u), sq, piece);
243  
244 void
245 _lift(u, sq)
246 UV u
247 int sq
248 CODE:
249 2561 (BD->lift)(BOARD(u), sq);
250  
251 int
252 _side(u)
253 UV u
254 CODE:
255 4018 RETVAL = (BD->side)(BOARD(u));
256 OUTPUT:
257 RETVAL
258  
259 void
260 _set_side(u, side)
261 UV u
262 int side
263 CODE:
264 43631 (BD->set_side)(BOARD(u), side);
265  
266 int
267 _count(u, piece)
268 UV u
269 int piece
270 CODE:
271 12 RETVAL = (BD->count)(BOARD(u), piece);
272 OUTPUT:
273 RETVAL
274  
275 int
276 _king_square(u)
277 UV u
278 CODE:
279 5 RETVAL = (BD->king_square)(BOARD(u));
280 OUTPUT:
281 RETVAL
282  
283 SV *
284 _key_hex(u)
285 UV u
286 CODE:
287 326509 RETVAL = bd_hex64(aTHX_ (BD->key)(BOARD(u)));
288 OUTPUT:
289 RETVAL
290  
291 SV *
292 _key_full_hex(u)
293 UV u
294 CODE:
295 156028 RETVAL = bd_hex64(aTHX_ (BD->key_full)(BOARD(u)));
296 OUTPUT:
297 RETVAL
298  
299 SV *
300 _zobrist_hex(piece, sq)
301 int piece
302 int sq
303 CODE:
304 5 RETVAL = bd_hex64(aTHX_ (BD->zobrist)(piece, sq));
305 OUTPUT:
306 RETVAL
307  
308 SV *
309 _zobrist_side_hex()
310 CODE:
311 1 RETVAL = bd_hex64(aTHX_ (BD->zobrist_side)());
312 OUTPUT:
313 RETVAL
314  
315 int
316 _stride()
317 CODE:
318 1 RETVAL = (BD->stride)();
319 OUTPUT:
320 RETVAL
321  
322 int
323 _square_of(file, rank)
324 int file
325 int rank
326 CODE:
327 4502787 RETVAL = (BD->square_of)(file, rank);
328 OUTPUT:
329 RETVAL
330  
331 int
332 _file_of(sq)
333 int sq
334 CODE:
335 3015524 RETVAL = (BD->file_of)(sq);
336 OUTPUT:
337 RETVAL
338  
339 int
340 _rank_of(sq)
341 int sq
342 CODE:
343 3015524 RETVAL = (BD->rank_of)(sq);
344 OUTPUT:
345 RETVAL
346  
347 int
348 _on_board(sq)
349 int sq
350 CODE:
351 243 RETVAL = (BD->on_board)(sq);
352 OUTPUT:
353 RETVAL
354  
355 int
356 _is_throne(sq)
357 int sq
358 CODE:
359 132 RETVAL = (BD->is_throne)(sq);
360 OUTPUT:
361 RETVAL
362  
363 int
364 _is_corner(sq)
365 int sq
366 CODE:
367 130 RETVAL = (BD->is_corner)(sq);
368 OUTPUT:
369 RETVAL
370  
371 int
372 _beside_throne(sq)
373 int sq
374 CODE:
375 130 RETVAL = (BD->beside_throne)(sq);
376 OUTPUT:
377 RETVAL
378  
379 int
380 _side_of(piece)
381 int piece
382 CODE:
383 5 RETVAL = (BD->side_of)(piece);
384 OUTPUT:
385 RETVAL
386  
387 # ---- version 2: where a piece may go ------------------------------------------
388  
389 int
390 _moves_max()
391 CODE:
392 1 ▶ RETVAL = BD_MOVES_MAX;
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = BD_MOVES_MAX; 0 1 1
393 OUTPUT:
394 RETVAL
395  
396 int
397 _move_make(from, to)
398 int from
399 int to
400 CODE:
401 1476139 RETVAL = (BD->move_make)(from, to);
402 OUTPUT:
403 RETVAL
404  
405 int
406 _move_from(mv)
407 int mv
408 CODE:
409 3016745 RETVAL = (BD->move_from)(mv);
410 OUTPUT:
411 RETVAL
412  
413 int
414 _move_to(mv)
415 int mv
416 CODE:
417 1400566 RETVAL = (BD->move_to)(mv);
418 OUTPUT:
419 RETVAL
420  
421 # The whole list in list context. gen_moves reports how many moves there ARE
422 # and writes no more than the buffer holds, so a count above BD_MOVES_MAX would
423 # mean the bound in bd_abi.h is wrong: that is a croak and not a truncation.
424 void
425 _gen_moves(u, variant = &PL_sv_undef)
426 UV u
427 SV *variant
428 PREINIT:
429 struct bd_variant v;
430 int list[BD_MOVES_MAX];
431 int n, i;
432 PPCODE:
433 33795 bd_variant_of_sv(aTHX_ variant, &v);
434 33792 n = (BD->gen_moves)(BOARD(u), &v, list, BD_MOVES_MAX);
435 33792 ▶ if (n > BD_MOVES_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (n > BD_MOVES_MAX) 0 33792 33792
436 0 croak("Game::Brandubh::Engine: %d moves, more than the %d the board can have", n, BD_MOVES_MAX);
437 33792 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 33792 0 33792
EXTEND(SP, n); 0 33792 33792
438 1418955 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 1385163 33792 1418955
439 1385163 PUSHs(sv_2mortal(newSViv(list[i])));
440  
441 int
442 _is_legal(u, mv, variant = &PL_sv_undef)
443 UV u
444 int mv
445 SV *variant
446 PREINIT:
447 struct bd_variant v;
448 CODE:
449 96094 bd_variant_of_sv(aTHX_ variant, &v);
450 96094 RETVAL = (BD->is_legal)(BOARD(u), &v, mv);
451 OUTPUT:
452 RETVAL
453  
454 int
455 _why_not(u, from, to, variant = &PL_sv_undef)
456 UV u
457 int from
458 int to
459 SV *variant
460 PREINIT:
461 struct bd_variant v;
462 CODE:
463 1200525 bd_variant_of_sv(aTHX_ variant, &v);
464 1200525 RETVAL = (BD->why_not)(BOARD(u), &v, from, to);
465 OUTPUT:
466 RETVAL
467  
468 void
469 _relocate(u, mv)
470 UV u
471 int mv
472 CODE:
473 871 (BD->relocate)(BOARD(u), mv);
474  
475 SV *
476 _perft_slides(u, depth, variant = &PL_sv_undef)
477 UV u
478 int depth
479 SV *variant
480 PREINIT:
481 struct bd_variant v;
482 CODE:
483 5138 bd_variant_of_sv(aTHX_ variant, &v);
484 5138 RETVAL = bd_dec64(aTHX_ (BD->perft_slides)(BOARD(u), &v, depth));
485 OUTPUT:
486 RETVAL
487  
488 # ---- version 3: what a move captures ------------------------------------------
489  
490 int
491 _hostile_to(u, sq, side)
492 UV u
493 int sq
494 int side
495 CODE:
496 86 RETVAL = (BD->hostile_to)(BOARD(u), sq, side);
497 OUTPUT:
498 RETVAL
499  
500 void
501 _captures_at(u, to, variant = &PL_sv_undef)
502 UV u
503 int to
504 SV *variant
505 PREINIT:
506 struct bd_variant v;
507 int out[BD_CAPTURES_MAX];
508 int n, i;
509 PPCODE:
510 2711 bd_variant_of_sv(aTHX_ variant, &v);
511 2711 n = (BD->captures_at)(BOARD(u), &v, to, out);
512 2711 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 2711 0 2711
EXTEND(SP, n); 0 2711 2711
513 5626 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 2915 2711 5626
514 2915 PUSHs(sv_2mortal(newSViv(out[i])));
515  
516 # The undo crosses as a STRING OF RAW BYTES and not as a pointer, so nothing is
517 # allocated that a caller could forget to release, and a board can be undone
518 # after the scalar holding the token has been copied about.
519 void
520 _do_move(u, mv, variant = &PL_sv_undef)
521 UV u
522 int mv
523 SV *variant
524 PREINIT:
525 struct bd_variant v;
526 struct bd_undo undo;
527 int flags;
528 PPCODE:
529 154495 bd_variant_of_sv(aTHX_ variant, &v);
530 154495 flags = (BD->do_move)(BOARD(u), &v, mv, &undo);
531 154495 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 154495 154495
532 154495 PUSHs(sv_2mortal(newSViv(flags)));
533 154495 PUSHs(sv_2mortal(newSVpvn((const char *)&undo, sizeof(undo))));
534  
535 void
536 _undo_move(u, token)
537 UV u
538 SV *token
539 PREINIT:
540 STRLEN len;
541 const char *bytes;
542 struct bd_undo undo;
543 CODE:
544 154031 bytes = SvPV(token, len);
545 154031 ▶ if (len != sizeof(undo))
Branch: 1 branch
BranchTrueFalseTotal
if (len != sizeof(undo)) 1 154030 154031
546 1 croak("Game::Brandubh::Engine: that is not an undo token");
547 154030 Copy(bytes, &undo, 1, struct bd_undo);
548 154030 (BD->undo_move)(BOARD(u), &undo);
549  
550 # (flags, square, square, ...)
551 void
552 _preview(u, mv, variant = &PL_sv_undef)
553 UV u
554 int mv
555 SV *variant
556 PREINIT:
557 struct bd_variant v;
558 int out[BD_CAPTURES_MAX];
559 251211 ▶ int n, i, flags = 0;
Branch: 1 branch
BranchTrueFalseTotal
int n, i, flags = 0; 0 251211 251211
560 PPCODE:
561 251211 bd_variant_of_sv(aTHX_ variant, &v);
562 251211 n = (BD->preview)(BOARD(u), &v, mv, out, &flags);
563 251211 ▶ EXTEND(SP, n + 1);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n + 1); 251211 0 251211
EXTEND(SP, n + 1); 0 251211 251211
564 251211 PUSHs(sv_2mortal(newSViv(flags)));
565 284451 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 33240 251211 284451
566 33240 PUSHs(sv_2mortal(newSViv(out[i])));
567  
568 SV *
569 _perft(u, depth, variant = &PL_sv_undef)
570 UV u
571 int depth
572 SV *variant
573 PREINIT:
574 struct bd_variant v;
575 CODE:
576 4818 bd_variant_of_sv(aTHX_ variant, &v);
577 4818 RETVAL = bd_dec64(aTHX_ (BD->perft)(BOARD(u), &v, depth));
578 OUTPUT:
579 RETVAL
580  
581 # ---- version 4: a game ----------------------------------------------------------
582 #
583 # A SECOND PACKAGE. A game is a different thing from a board: it owns one,
584 # remembers how it got there, and is the only thing here that refuses a move.
585 # The Perl that owns the lifetime is lib/Game/Brandubh/Rules.pm.
586  
587 MODULE = Game::Brandubh PACKAGE = Game::Brandubh::Rules
588  
589 PROTOTYPES: DISABLE
590  
591 # (pointer, error). With no position the game starts from the set-up. A
592 # position that will not parse returns undef and the code.
593 void
594 _new_game(position, variant = &PL_sv_undef)
595 SV *position
596 SV *variant
597 PREINIT:
598 struct bd_variant v;
599 8919 struct bd_board *b = NULL;
600 struct bd_game *g;
601 8919 ▶ int err = 0;
Branch: 1 branch
BranchTrueFalseTotal
int err = 0; 0 8919 8919
602 PPCODE:
603 8919 bd_variant_of_sv(aTHX_ variant, &v);
604 8918 ▶ if (SvOK(position)) {
Branch: 1 branch
BranchTrueFalseTotal
if (SvOK(position)) { 7876 1042 8918
605 15752 b = (BD->board_of_string)(SvPV_nolen(position), &err);
606 7876 ▶ if (!b) {
Branch: 1 branch
BranchTrueFalseTotal
if (!b) { 1 7875 7876
607 1 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 1 1
608 1 PUSHs(&PL_sv_undef);
609 1 PUSHs(sv_2mortal(newSViv(err)));
610 1 XSRETURN(2);
611 }
612 }
613 8917 g = (BD->game_new)(b, &v);
614 8917 ▶ if (b) (BD->board_drop)(b);
Branch: 1 branch
BranchTrueFalseTotal
if (b) (BD->board_drop)(b); 7875 1042 8917
615 8917 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 8917 8917
616 8917 ▶ if (g) PUSHs(sv_2mortal(newSVuv(PTR2UV(g))));
Branch: 1 branch
BranchTrueFalseTotal
if (g) PUSHs(sv_2mortal(newSVuv(PTR2UV(g)))); 8917 0 8917
617 0 else PUSHs(&PL_sv_undef);
618 8917 PUSHs(sv_2mortal(newSViv(0)));
619  
620 UV
621 _copy_game(u)
622 UV u
623 CODE:
624 3 ▶ RETVAL = PTR2UV((BD->game_copy)(GAME(u)));
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = PTR2UV((BD->game_copy)(GAME(u))); 0 3 3
625 OUTPUT:
626 RETVAL
627  
628 void
629 _drop_game(u)
630 UV u
631 CODE:
632 8920 (BD->game_drop)(GAME(u));
633  
634 int
635 _games_live()
636 CODE:
637 7 RETVAL = (BD->games_live)();
638 OUTPUT:
639 RETVAL
640  
641 SV *
642 _position(u)
643 UV u
644 PREINIT:
645 char buf[BD_POS_MAX];
646 int n;
647 CODE:
648 21216 n = (BD->to_string)((BD->game_board)(GAME(u)), buf, (int)sizeof(buf));
649 21216 ▶ if (n < 0) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (n < 0) XSRETURN_UNDEF; 0 21216 21216
650 21216 RETVAL = newSVpvn(buf, (STRLEN)n);
651 OUTPUT:
652 RETVAL
653  
654 SV *
655 _key_hex(u)
656 UV u
657 CODE:
658 7842 RETVAL = bd_hex64(aTHX_ (BD->key)((BD->game_board)(GAME(u))));
659 OUTPUT:
660 RETVAL
661  
662 SV *
663 _key_at_hex(u, ply)
664 UV u
665 int ply
666 CODE:
667 14 ▶ if (ply < 0 || ply > (BD->game_ply)(GAME(u))) XSRETURN_UNDEF;
Branch: 2 branches
BranchTrueFalseTotal
if (ply < 0 || ply > (BD->game_ply)(GAME(u))) XSRETURN_UNDEF; 14 0 14
if (ply < 0 || ply > (BD->game_ply)(GAME(u))) XSRETURN_UNDEF; 5 9 14
668 9 RETVAL = bd_hex64(aTHX_ (BD->game_key_at)(GAME(u), ply));
669 OUTPUT:
670 RETVAL
671  
672 int
673 _side(u)
674 UV u
675 CODE:
676 8779 RETVAL = (BD->side)((BD->game_board)(GAME(u)));
677 OUTPUT:
678 RETVAL
679  
680 int
681 _at(u, sq)
682 UV u
683 int sq
684 CODE:
685 2319061 RETVAL = (BD->at)((BD->game_board)(GAME(u)), sq);
686 OUTPUT:
687 RETVAL
688  
689 int
690 _count(u, piece)
691 UV u
692 int piece
693 CODE:
694 31241 RETVAL = (BD->count)((BD->game_board)(GAME(u)), piece);
695 OUTPUT:
696 RETVAL
697  
698 int
699 _ply(u)
700 UV u
701 CODE:
702 28111 RETVAL = (BD->game_ply)(GAME(u));
703 OUTPUT:
704 RETVAL
705  
706 int
707 _cap(u)
708 UV u
709 CODE:
710 6 RETVAL = (BD->game_cap)(GAME(u));
711 OUTPUT:
712 RETVAL
713  
714 int
715 _outcome(u)
716 UV u
717 CODE:
718 388316 RETVAL = (BD->game_outcome)(GAME(u));
719 OUTPUT:
720 RETVAL
721  
722 int
723 _winner(u)
724 UV u
725 CODE:
726 4188 RETVAL = (BD->game_winner)(GAME(u));
727 OUTPUT:
728 RETVAL
729  
730 int
731 _winner_of(outcome)
732 int outcome
733 CODE:
734 7 RETVAL = (BD->winner_of)(outcome);
735 OUTPUT:
736 RETVAL
737  
738 int
739 _repeats(u)
740 UV u
741 CODE:
742 6074 RETVAL = (BD->repeats)(GAME(u));
743 OUTPUT:
744 RETVAL
745  
746 void
747 _moves(u)
748 UV u
749 PREINIT:
750 int list[BD_MOVES_MAX];
751 int n, i;
752 PPCODE:
753 100965 n = (BD->game_moves)(GAME(u), list, BD_MOVES_MAX);
754 100965 ▶ if (n > BD_MOVES_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (n > BD_MOVES_MAX) 0 100965 100965
755 0 croak("Game::Brandubh::Rules: %d moves, more than the %d the board can have", n, BD_MOVES_MAX);
756 100965 ▶ EXTEND(SP, n);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n); 100965 0 100965
EXTEND(SP, n); 0 100965 100965
757 3369264 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 3268299 100965 3369264
758 3268299 PUSHs(sv_2mortal(newSViv(list[i])));
759  
760 # (answer, flags)
761 void
762 _play(u, mv)
763 UV u
764 int mv
765 PREINIT:
766 249052 int flags = 0, answer;
767 PPCODE:
768 249052 answer = (BD->game_do)(GAME(u), mv, &flags);
769 249052 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 249052 249052
770 249052 PUSHs(sv_2mortal(newSViv(answer)));
771 249052 PUSHs(sv_2mortal(newSViv(flags)));
772  
773 int
774 _undo(u)
775 UV u
776 CODE:
777 23641 RETVAL = (BD->game_undo)(GAME(u));
778 OUTPUT:
779 RETVAL
780  
781 # (flags, square, square, ...) under the game's own rule set
782 void
783 _preview(u, mv)
784 UV u
785 int mv
786 PREINIT:
787 struct bd_variant v;
788 int out[BD_CAPTURES_MAX];
789 1616571 int n, i, flags = 0;
790 PPCODE:
791 1616571 (BD->game_variant)(GAME(u), &v);
792 1616571 n = (BD->preview)((BD->game_board)(GAME(u)), &v, mv, out, &flags);
793 1616571 ▶ EXTEND(SP, n + 1);
Branch: 2 branches
BranchTrueFalseTotal
EXTEND(SP, n + 1); 1616571 0 1616571
EXTEND(SP, n + 1); 0 1616571 1616571
794 1616571 PUSHs(sv_2mortal(newSViv(flags)));
795 1682352 ▶ for (i = 0; i < n; i++)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < n; i++) 65781 1616571 1682352
796 65781 PUSHs(sv_2mortal(newSViv(out[i])));
797  
798 int
799 _why_not(u, from, to)
800 UV u
801 int from
802 int to
803 PREINIT:
804 struct bd_variant v;
805 CODE:
806 179634 (BD->game_variant)(GAME(u), &v);
807 179634 RETVAL = (BD->why_not)((BD->game_board)(GAME(u)), &v, from, to);
808 OUTPUT:
809 RETVAL
810  
811 # the rule set the game is playing, AS CLAMPED, so a caller can see what a cap
812 # of 0 or a repeat of 1 became
813 SV *
814 _variant(u)
815 UV u
816 PREINIT:
817 struct bd_variant v;
818 HV *hv;
819 CODE:
820 7 (BD->game_variant)(GAME(u), &v);
821 7 hv = newHV();
822 7 (void)hv_stores(hv, "throne_pass", newSViv(v.throne_pass));
823 7 (void)hv_stores(hv, "throne_reentry", newSViv(v.throne_reentry));
824 7 (void)hv_stores(hv, "king_everywhere_two", newSViv(v.king_everywhere_two));
825 7 (void)hv_stores(hv, "king_strong", newSViv(v.king_strong));
826 7 ▶ (void)hv_stores(hv, "escape", newSVpv(v.escape_edge ? "edge" : "corner", 0));
Branch: 1 branch
BranchTrueFalseTotal
(void)hv_stores(hv, "escape", newSVpv(v.escape_edge ? "edge" : "corner", 0)); 2 5 7
827 7 (void)hv_stores(hv, "repeat", newSViv(v.repeat));
828 7 (void)hv_stores(hv, "ply_cap", newSViv(v.ply_cap));
829 7 RETVAL = newRV_noinc((SV *)hv);
830 OUTPUT:
831 RETVAL
832  
833 # ---- version 5: choosing a move ---------------------------------------------------
834  
835 # (move, nodes, depth, score, stopped). The move is 0 when the game is over.
836 # The node count is a decimal string, as every count that can outgrow 32 bits
837 # is. The budget and the seed are UVs: a budget fits 32 bits with room, and the
838 # caller keeps a seed inside them.
839 void
840 _search(u, budget, seed = 1, max_depth = 0, weights = &PL_sv_undef)
841 UV u
842 UV budget
843 UV seed
844 int max_depth
845 SV *weights
846 PREINIT:
847 struct bd_weights w;
848 struct bd_search_info info;
849 int mv;
850 PPCODE:
851 8178 bd_weights_of_sv(aTHX_ weights, &w);
852 8176 mv = (BD->search)(GAME(u), &w, (unsigned long long)budget, (unsigned long long)seed, max_depth, &info);
853 8176 ▶ EXTEND(SP, 5);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 5); 0 8176 8176
854 8176 PUSHs(sv_2mortal(newSViv(mv)));
855 8176 PUSHs(sv_2mortal(bd_dec64(aTHX_ info.nodes)));
856 8176 PUSHs(sv_2mortal(newSViv(info.depth)));
857 8176 PUSHs(sv_2mortal(newSViv(info.score)));
858 8176 PUSHs(sv_2mortal(newSViv(info.stopped)));
859  
860 int
861 _evaluate(u, weights = &PL_sv_undef)
862 UV u
863 SV *weights
864 PREINIT:
865 struct bd_weights w;
866 struct bd_variant v;
867 CODE:
868 4517 bd_weights_of_sv(aTHX_ weights, &w);
869 4516 (BD->game_variant)(GAME(u), &v);
870 4516 RETVAL = (BD->evaluate)((BD->game_board)(GAME(u)), &v, &w);
871 OUTPUT:
872 RETVAL
873  
874 # the default weights, as a hash
875 SV *
876 _weights()
877 PREINIT:
878 struct bd_weights w;
879 HV *hv;
880 CODE:
881 1 (BD->weights_default)(&w);
882 1 hv = newHV();
883 1 (void)hv_stores(hv, "attacker", newSViv(w.attacker));
884 1 (void)hv_stores(hv, "defender", newSViv(w.defender));
885 1 (void)hv_stores(hv, "lane_one", newSViv(w.lane_one));
886 1 (void)hv_stores(hv, "lane_two", newSViv(w.lane_two));
887 1 (void)hv_stores(hv, "freedom", newSViv(w.freedom));
888 1 (void)hv_stores(hv, "corner_guard", newSViv(w.corner_guard));
889 1 (void)hv_stores(hv, "ring", newSViv(w.ring));
890 1 RETVAL = newRV_noinc((SV *)hv);
891 OUTPUT:
892 RETVAL
« blib/lib/Game/Brandubh/Engine.pm ↑ Summary bd_rules.c »