| 1 |
|
| /* Senet.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::Senet::Engine, not in Game::Senet, and are named with |
| 8 |
|
| * 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/Senet/Engine.pm. |
| 10 |
|
| * |
| 11 |
|
| * `_put` and `_lift` are the structure primitives from sen_abi.h and CHECK |
| 12 |
|
| * 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 "sen_abi.h" |
| 22 |
|
| |
| 23 |
|
| static const struct sen_abi *SEN = NULL; |
| 24 |
|
| |
| 25 |
|
| #define BOARD(u) INT2PTR(struct sen_board *, (u)) |
| 26 |
|
| |
| 27 |
|
| /* The key crosses as a 16-character hex string, never as a UV. |
| 28 |
|
| * |
| 29 |
|
| * It is 61 bits. On a perl with 32-bit IVs a UV cannot hold it and the top |
| 30 |
|
| * would vanish silently, which for a key means a test that passes while |
| 31 |
|
| * comparing part of a number. Hand-rolled rather than via my_snprintf, as |
| 32 |
|
| * RoyalUr.xs and Brandubh.xs do it. |
| 33 |
|
| */ |
| 34 |
119370 |
| static SV *sen_hex64(pTHX_ unsigned long long v) |
| 35 |
|
| { |
| 36 |
|
| static const char digits[] = "0123456789abcdef"; |
| 37 |
|
| char buf[16]; |
| 38 |
|
| int i; |
| 39 |
2029290 |
▶ | for (i = 15; i >= 0; i--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 15; i >= 0; i--) { |
1909920 |
119370 |
2029290 |
|
| 40 |
1909920 |
| buf[i] = digits[v & 0xfULL]; |
| 41 |
1909920 |
| v >>= 4; |
| 42 |
|
| } |
| 43 |
119370 |
| return newSVpvn(buf, 16); |
| 44 |
|
| } |
| 45 |
|
| |
| 46 |
|
| /* A count crosses as a decimal STRING, for the reason a key crosses as hex: the |
| 47 |
|
| * leaves of a walk outgrow a 32-bit IV. Integer only. */ |
| 48 |
53436 |
| static SV *sen_dec64(pTHX_ unsigned long long v) |
| 49 |
|
| { |
| 50 |
|
| char buf[24]; |
| 51 |
53436 |
| int i = (int) sizeof(buf); |
| 52 |
|
| do { |
| 53 |
83319 |
| buf[--i] = (char) ('0' + (int) (v % 10ULL)); |
| 54 |
83319 |
| v /= 10ULL; |
| 55 |
83319 |
▶ | } while (v); |
Branch: 1 branch
| Branch | True | False | Total |
| } while (v); |
29883 |
53436 |
83319 |
|
| 56 |
53436 |
| return newSVpvn(buf + i, (STRLEN) (sizeof(buf) - i)); |
| 57 |
|
| } |
| 58 |
|
| |
| 59 |
56854853 |
| static int sen_word_is(const char *s, STRLEN len, const char *word) |
| 60 |
|
| { |
| 61 |
56854853 |
| STRLEN n = strlen(word); |
| 62 |
56854853 |
▶ | return len == n && memEQ(s, word, n); |
Branch: 2 branches
| Branch | True | False | Total |
| return len == n && memEQ(s, word, n); |
20447264 |
36407589 |
56854853 |
| return len == n && memEQ(s, word, n); |
16027956 |
4419308 |
20447264 |
|
| 63 |
|
| } |
| 64 |
|
| |
| 65 |
|
| /* The rule set crosses as undef for the default set, as the NAME of a set, or |
| 66 |
|
| * as a hash reference of what differs from a set: `exits` names the set whose |
| 67 |
|
| * chains and safe houses to start from, and every other key overrides one |
| 68 |
|
| * scalar field. |
| 69 |
|
| * |
| 70 |
|
| * AN UNKNOWN KEY IS A CROAK and not a shrug: `blockade3 => 0` silently playing |
| 71 |
|
| * the default set is the kind of mistake that agrees with you. So is a value a |
| 72 |
|
| * field may not hold. THE NAMES OF THE SETS ARE NOT IN THIS FILE: the engine |
| 73 |
|
| * answers them through rules_id. |
| 74 |
|
| */ |
| 75 |
2406295 |
| static void sen_rules_of_sv(pTHX_ SV *sv, struct sen_rules *r) |
| 76 |
|
| { |
| 77 |
|
| HV *hv; |
| 78 |
|
| HE *he; |
| 79 |
|
| SV **base; |
| 80 |
|
| |
| 81 |
2406295 |
| (SEN->rules_named)(SEN_RULES_KENDALL, r); |
| 82 |
2406295 |
▶ | if (!sv || !SvOK(sv)) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!sv || !SvOK(sv)) return; |
2406295 |
0 |
2406295 |
| if (!sv || !SvOK(sv)) return; |
1 |
2406294 |
2406295 |
|
| 83 |
|
| |
| 84 |
2406294 |
▶ | if (!SvROK(sv)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!SvROK(sv)) { |
619265 |
1787029 |
2406294 |
|
| 85 |
1238530 |
| int id = (SEN->rules_id)(SvPV_nolen(sv)); |
| 86 |
619265 |
▶ | if (id < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (id < 0) |
1 |
619264 |
619265 |
|
| 87 |
1 |
| croak("Game::Senet::Engine: no rule set is called '%" SVf "'", SVfARG(sv)); |
| 88 |
619264 |
| (SEN->rules_named)(id, r); |
| 89 |
619264 |
| return; |
| 90 |
|
| } |
| 91 |
1787029 |
▶ | if (SvTYPE(SvRV(sv)) != SVt_PVHV) |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvTYPE(SvRV(sv)) != SVt_PVHV) |
1 |
1787028 |
1787029 |
|
| 92 |
1 |
| croak("Game::Senet::Engine: a rule set is a name or a hash reference"); |
| 93 |
1787028 |
| hv = (HV *) SvRV(sv); |
| 94 |
|
| |
| 95 |
1787028 |
| base = hv_fetchs(hv, "exits", 0); |
| 96 |
1787028 |
▶ | if (base && SvOK(*base)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (base && SvOK(*base)) { |
1787008 |
20 |
1787028 |
| if (base && SvOK(*base)) { |
1787008 |
0 |
1787008 |
|
| 97 |
3574016 |
| int id = (SEN->rules_id)(SvPV_nolen(*base)); |
| 98 |
1787008 |
▶ | if (id < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (id < 0) |
1 |
1787007 |
1787008 |
|
| 99 |
1 |
| croak("Game::Senet::Engine: exits names a rule set, and no set is called '%" SVf "'", |
| 100 |
|
| SVfARG(*base)); |
| 101 |
1787007 |
| (SEN->rules_named)(id, r); |
| 102 |
|
| } |
| 103 |
|
| |
| 104 |
1787027 |
| hv_iterinit(hv); |
| 105 |
14243776 |
▶ | while ((he = hv_iternext(hv))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(hv))) { |
12456752 |
1787024 |
14243776 |
|
| 106 |
|
| STRLEN klen, vlen; |
| 107 |
12456752 |
▶ | const char *key = HePV(he, klen); |
Branch: 1 branch
| Branch | True | False | Total |
| const char *key = HePV(he, klen); |
0 |
12456752 |
12456752 |
|
| 108 |
12456752 |
| SV *val = HeVAL(he); |
| 109 |
|
| const char *s; |
| 110 |
|
| |
| 111 |
12456752 |
▶ | if (sen_word_is(key, klen, "exits")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (sen_word_is(key, klen, "exits")) |
1787007 |
10669745 |
12456752 |
|
| 112 |
1787007 |
| continue; |
| 113 |
10669745 |
▶ | if (sen_word_is(key, klen, "pieces")) |
Branch: 1 branch
| Branch | True | False | Total |
| if (sen_word_is(key, klen, "pieces")) |
1787001 |
8882744 |
10669745 |
|
| 114 |
1787001 |
| r->pieces = (int) SvIV(val); |
| 115 |
8882744 |
▶ | else if (sen_word_is(key, klen, "blockade")) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (sen_word_is(key, klen, "blockade")) |
1776557 |
7106187 |
8882744 |
|
| 116 |
1776557 |
| r->blockade = (int) SvIV(val); |
| 117 |
7106187 |
▶ | else if (sen_word_is(key, klen, "backward")) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (sen_word_is(key, klen, "backward")) |
1776552 |
5329635 |
7106187 |
|
| 118 |
1776552 |
| r->backward = (int) SvIV(val); |
| 119 |
5329635 |
▶ | else if (sen_word_is(key, klen, "throw_on_26")) |
Branch: 1 branch
| Branch | True | False | Total |
| else if (sen_word_is(key, klen, "throw_on_26")) |
1776541 |
3553094 |
5329635 |
|
| 120 |
1776541 |
| r->throw_on_26 = (int) SvIV(val); |
| 121 |
3553094 |
▶ | else if (sen_word_is(key, klen, "protect")) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (sen_word_is(key, klen, "protect")) { |
1776552 |
1776542 |
3553094 |
|
| 122 |
1776552 |
| s = SvPV(val, vlen); |
| 123 |
1776552 |
▶ | if (sen_word_is(s, vlen, "none")) r->protect = SEN_PROTECT_NONE; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sen_word_is(s, vlen, "none")) r->protect = SEN_PROTECT_NONE; |
387 |
1776165 |
1776552 |
|
| 124 |
1776165 |
▶ | else if (sen_word_is(s, vlen, "run")) r->protect = SEN_PROTECT_RUN; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (sen_word_is(s, vlen, "run")) r->protect = SEN_PROTECT_RUN; |
909953 |
866212 |
1776165 |
|
| 125 |
866212 |
▶ | else if (sen_word_is(s, vlen, "ends")) r->protect = SEN_PROTECT_ENDS; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (sen_word_is(s, vlen, "ends")) r->protect = SEN_PROTECT_ENDS; |
866211 |
1 |
866212 |
|
| 126 |
1 |
| else croak("Game::Senet::Engine: protect is 'none', 'run' or 'ends'"); |
| 127 |
|
| } |
| 128 |
1776542 |
▶ | else if (sen_word_is(key, klen, "reach26")) { |
Branch: 1 branch
| Branch | True | False | Total |
| else if (sen_word_is(key, klen, "reach26")) { |
1776541 |
1 |
1776542 |
|
| 129 |
1776541 |
| s = SvPV(val, vlen); |
| 130 |
1776541 |
▶ | if (sen_word_is(s, vlen, "exact")) r->reach26 = SEN_REACH_EXACT; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sen_word_is(s, vlen, "exact")) r->reach26 = SEN_REACH_EXACT; |
909971 |
866570 |
1776541 |
|
| 131 |
866570 |
▶ | else if (sen_word_is(s, vlen, "any")) r->reach26 = SEN_REACH_ANY; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (sen_word_is(s, vlen, "any")) r->reach26 = SEN_REACH_ANY; |
866569 |
1 |
866570 |
|
| 132 |
1 |
| else croak("Game::Senet::Engine: reach26 is 'exact' or 'any'"); |
| 133 |
|
| } |
| 134 |
|
| else |
| 135 |
1 |
▶ | croak("Game::Senet::Engine: no rule is called '%" SVf "'", |
Branch: 2 branches
| Branch | True | False | Total |
| croak("Game::Senet::Engine: no rule is called '%" SVf "'", |
1 |
0 |
1 |
| croak("Game::Senet::Engine: no rule is called '%" SVf "'", |
0 |
1 |
1 |
|
| 136 |
|
| SVfARG(HeSVKEY_force(he))); |
| 137 |
|
| } |
| 138 |
1787024 |
▶ | if (!(SEN->rules_ok)(r)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(SEN->rules_ok)(r)) |
5 |
1787019 |
1787024 |
|
| 139 |
5 |
| croak("Game::Senet::Engine: pieces is 1 to 7, blockade is 0, 2 or 3, and backward and throw_on_26 are 0 or 1"); |
| 140 |
|
| } |
| 141 |
|
| |
| 142 |
85998 |
| static void sen_push_rules(pTHX_ SV **sp, const struct sen_rules *r) |
| 143 |
|
| { |
| 144 |
|
| int h, roll, i; |
| 145 |
85998 |
| PUSHs(sv_2mortal(newSViv(r->pieces))); |
| 146 |
85998 |
| PUSHs(sv_2mortal(newSViv(r->blockade))); |
| 147 |
85998 |
| PUSHs(sv_2mortal(newSViv(r->protect))); |
| 148 |
85998 |
| PUSHs(sv_2mortal(newSViv(r->backward))); |
| 149 |
85998 |
| PUSHs(sv_2mortal(newSViv(r->reach26))); |
| 150 |
85998 |
| PUSHs(sv_2mortal(newSViv(r->throw_on_26))); |
| 151 |
85998 |
| PUSHs(sv_2mortal(newSVuv((UV) r->safe))); |
| 152 |
515988 |
▶ | for (h = 0; h < SEN_EXIT_HOUSES; h++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (h = 0; h < SEN_EXIT_HOUSES; h++) |
429990 |
85998 |
515988 |
|
| 153 |
2579940 |
▶ | for (roll = 0; roll < SEN_ROLL_MAX; roll++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (roll = 0; roll < SEN_ROLL_MAX; roll++) |
2149950 |
429990 |
2579940 |
|
| 154 |
10749750 |
▶ | for (i = 0; i < SEN_CHAIN_MAX; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < SEN_CHAIN_MAX; i++) |
8599800 |
2149950 |
10749750 |
|
| 155 |
8599800 |
| PUSHs(sv_2mortal(newSViv(r->exit[h][roll][i]))); |
| 156 |
85998 |
| } |
| 157 |
|
| |
| 158 |
|
| /* The weights of the evaluation cross as a hash reference, or as undef for the |
| 159 |
|
| * plain evaluation, and an unknown key croaks, for the reason a rule's does. */ |
| 160 |
111990 |
| static void sen_weights_of_sv(pTHX_ SV *sv, struct sen_weights *w) |
| 161 |
|
| { |
| 162 |
|
| HV *hv; |
| 163 |
|
| HE *he; |
| 164 |
|
| |
| 165 |
111990 |
| (SEN->weights_default)(w); |
| 166 |
111990 |
▶ | if (!sv || !SvOK(sv)) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!sv || !SvOK(sv)) return; |
111990 |
0 |
111990 |
| if (!sv || !SvOK(sv)) return; |
93876 |
18114 |
111990 |
|
| 167 |
18114 |
▶ | if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) |
18114 |
0 |
18114 |
| if (!SvROK(sv) || SvTYPE(SvRV(sv)) != SVt_PVHV) |
0 |
18114 |
18114 |
|
| 168 |
0 |
| croak("Game::Senet::Engine: weights are a hash reference"); |
| 169 |
18114 |
| hv = (HV *) SvRV(sv); |
| 170 |
|
| |
| 171 |
18114 |
| hv_iterinit(hv); |
| 172 |
36228 |
▶ | while ((he = hv_iternext(hv))) { |
Branch: 1 branch
| Branch | True | False | Total |
| while ((he = hv_iternext(hv))) { |
18114 |
18114 |
36228 |
|
| 173 |
|
| STRLEN klen; |
| 174 |
18114 |
▶ | const char *key = HePV(he, klen); |
Branch: 1 branch
| Branch | True | False | Total |
| const char *key = HePV(he, klen); |
0 |
18114 |
18114 |
|
| 175 |
18114 |
| int val = (int) SvIV(HeVAL(he)); |
| 176 |
|
| |
| 177 |
18114 |
▶ | if (sen_word_is(key, klen, "blockade")) w->blockade = val; |
Branch: 1 branch
| Branch | True | False | Total |
| if (sen_word_is(key, klen, "blockade")) w->blockade = val; |
18114 |
0 |
18114 |
|
| 178 |
0 |
▶ | else if (sen_word_is(key, klen, "ties")) w->ties = val ? SEN_TIES_FIRST : SEN_TIES_LAST; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (sen_word_is(key, klen, "ties")) w->ties = val ? SEN_TIES_FIRST : SEN_TIES_LAST; |
0 |
0 |
0 |
|
| 179 |
|
| else |
| 180 |
0 |
▶ | croak("Game::Senet::Engine: no weight is called '%" SVf "'", |
Branch: 2 branches
| Branch | True | False | Total |
| croak("Game::Senet::Engine: no weight is called '%" SVf "'", |
0 |
0 |
0 |
| croak("Game::Senet::Engine: no weight is called '%" SVf "'", |
0 |
0 |
0 |
|
| 181 |
|
| SVfARG(HeSVKEY_force(he))); |
| 182 |
|
| } |
| 183 |
|
| } |
| 184 |
|
| |
| 185 |
|
| #define SEN_RULES_WORDS (7 + SEN_EXIT_HOUSES * SEN_ROLL_MAX * SEN_CHAIN_MAX) |
| 186 |
|
| |
| 187 |
|
| MODULE = Game::Senet PACKAGE = Game::Senet::Engine |
| 188 |
|
| |
| 189 |
|
| PROTOTYPES: DISABLE |
| 190 |
|
| |
| 191 |
|
| BOOT: |
| 192 |
80 |
| SEN = sen_abi_table(); |
| 193 |
|
| |
| 194 |
|
| UV |
| 195 |
|
| _abi_ptr() |
| 196 |
|
| CODE: |
| 197 |
1 |
▶ | RETVAL = PTR2UV(SEN); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = PTR2UV(SEN); |
0 |
1 |
1 |
|
| 198 |
|
| OUTPUT: |
| 199 |
|
| RETVAL |
| 200 |
|
| |
| 201 |
|
| UV |
| 202 |
|
| _abi_version() |
| 203 |
|
| CODE: |
| 204 |
2 |
▶ | RETVAL = (UV)SEN->abi_version; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = (UV)SEN->abi_version; |
1 |
1 |
2 |
|
| 205 |
|
| OUTPUT: |
| 206 |
|
| RETVAL |
| 207 |
|
| |
| 208 |
|
| UV |
| 209 |
|
| _new_board(pieces) |
| 210 |
|
| int pieces |
| 211 |
|
| CODE: |
| 212 |
92896 |
▶ | RETVAL = PTR2UV((SEN->board_new)(pieces)); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = PTR2UV((SEN->board_new)(pieces)); |
92843 |
53 |
92896 |
|
| 213 |
|
| OUTPUT: |
| 214 |
|
| RETVAL |
| 215 |
|
| |
| 216 |
|
| UV |
| 217 |
|
| _copy_board(u) |
| 218 |
|
| UV u |
| 219 |
|
| CODE: |
| 220 |
582078 |
▶ | RETVAL = PTR2UV((SEN->board_copy)(BOARD(u))); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = PTR2UV((SEN->board_copy)(BOARD(u))); |
582035 |
43 |
582078 |
|
| 221 |
|
| OUTPUT: |
| 222 |
|
| RETVAL |
| 223 |
|
| |
| 224 |
|
| void |
| 225 |
|
| _drop_board(u) |
| 226 |
|
| UV u |
| 227 |
|
| CODE: |
| 228 |
0 |
| (SEN->board_drop)(BOARD(u)); |
| 229 |
|
| |
| 230 |
|
| # The board is let go by an object of its own, held by the board's object and |
| 231 |
|
| # freed with it. Its DESTROY is C and calls no perl. |
| 232 |
|
| SV * |
| 233 |
|
| _hold_board(u) |
| 234 |
|
| UV u |
| 235 |
|
| CODE: |
| 236 |
758473 |
| RETVAL = sv_setref_uv(newSV(0), "Game::Senet::Engine::Hold", u); |
| 237 |
|
| OUTPUT: |
| 238 |
|
| RETVAL |
| 239 |
|
| |
| 240 |
|
| int |
| 241 |
|
| _live() |
| 242 |
|
| CODE: |
| 243 |
5 |
| RETVAL = (SEN->live)(); |
| 244 |
|
| OUTPUT: |
| 245 |
|
| RETVAL |
| 246 |
|
| |
| 247 |
|
| # A string that will not parse returns undef and the code, rather than |
| 248 |
|
| # croaking: the reason is what a caller shows a person, and nine of them are |
| 249 |
|
| # distinguishable. |
| 250 |
|
| void |
| 251 |
|
| _of_string(str) |
| 252 |
|
| const char *str |
| 253 |
|
| PREINIT: |
| 254 |
|
| struct sen_board *b; |
| 255 |
83587 |
| int err = 0; |
| 256 |
|
| PPCODE: |
| 257 |
83587 |
| b = (SEN->board_of_string)(str, &err); |
| 258 |
83587 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
83587 |
83587 |
|
| 259 |
83587 |
▶ | if (b) PUSHs(sv_2mortal(newSVuv(PTR2UV(b)))); |
Branch: 1 branch
| Branch | True | False | Total |
| if (b) PUSHs(sv_2mortal(newSVuv(PTR2UV(b)))); |
83499 |
88 |
83587 |
|
| 260 |
88 |
| else PUSHs(&PL_sv_undef); |
| 261 |
83587 |
| PUSHs(sv_2mortal(newSViv(err))); |
| 262 |
|
| |
| 263 |
|
| SV * |
| 264 |
|
| _to_string(u) |
| 265 |
|
| UV u |
| 266 |
|
| PREINIT: |
| 267 |
|
| char buf[SEN_POS_MAX]; |
| 268 |
|
| int n; |
| 269 |
|
| CODE: |
| 270 |
116448 |
| n = (SEN->to_string)(BOARD(u), buf, (int)sizeof(buf)); |
| 271 |
116448 |
▶ | if (n < 0) XSRETURN_UNDEF; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 0) XSRETURN_UNDEF; |
0 |
116448 |
116448 |
|
| 272 |
116448 |
| RETVAL = newSVpvn(buf, (STRLEN)n); |
| 273 |
|
| OUTPUT: |
| 274 |
|
| RETVAL |
| 275 |
|
| |
| 276 |
|
| int |
| 277 |
|
| _at(u, house) |
| 278 |
|
| UV u |
| 279 |
|
| int house |
| 280 |
|
| CODE: |
| 281 |
703559 |
| RETVAL = (SEN->at)(BOARD(u), house); |
| 282 |
|
| OUTPUT: |
| 283 |
|
| RETVAL |
| 284 |
|
| |
| 285 |
|
| void |
| 286 |
|
| _put(u, house, piece) |
| 287 |
|
| UV u |
| 288 |
|
| int house |
| 289 |
|
| int piece |
| 290 |
|
| CODE: |
| 291 |
645891 |
| (SEN->put)(BOARD(u), house, piece); |
| 292 |
|
| |
| 293 |
|
| void |
| 294 |
|
| _lift(u, house) |
| 295 |
|
| UV u |
| 296 |
|
| int house |
| 297 |
|
| CODE: |
| 298 |
6 |
| (SEN->lift)(BOARD(u), house); |
| 299 |
|
| |
| 300 |
|
| int |
| 301 |
|
| _side(u) |
| 302 |
|
| UV u |
| 303 |
|
| CODE: |
| 304 |
4637760 |
| RETVAL = (SEN->side)(BOARD(u)); |
| 305 |
|
| OUTPUT: |
| 306 |
|
| RETVAL |
| 307 |
|
| |
| 308 |
|
| void |
| 309 |
|
| _set_side(u, side) |
| 310 |
|
| UV u |
| 311 |
|
| int side |
| 312 |
|
| CODE: |
| 313 |
355895 |
| (SEN->set_side)(BOARD(u), side); |
| 314 |
|
| |
| 315 |
|
| int |
| 316 |
|
| _pieces(u) |
| 317 |
|
| UV u |
| 318 |
|
| CODE: |
| 319 |
661 |
| RETVAL = (SEN->pieces)(BOARD(u)); |
| 320 |
|
| OUTPUT: |
| 321 |
|
| RETVAL |
| 322 |
|
| |
| 323 |
|
| int |
| 324 |
|
| _count(u, side) |
| 325 |
|
| UV u |
| 326 |
|
| int side |
| 327 |
|
| CODE: |
| 328 |
403522 |
| RETVAL = (SEN->count)(BOARD(u), side); |
| 329 |
|
| OUTPUT: |
| 330 |
|
| RETVAL |
| 331 |
|
| |
| 332 |
|
| int |
| 333 |
|
| _home(u, side) |
| 334 |
|
| UV u |
| 335 |
|
| int side |
| 336 |
|
| CODE: |
| 337 |
15573 |
| RETVAL = (SEN->home)(BOARD(u), side); |
| 338 |
|
| OUTPUT: |
| 339 |
|
| RETVAL |
| 340 |
|
| |
| 341 |
|
| int |
| 342 |
|
| _consistent(u) |
| 343 |
|
| UV u |
| 344 |
|
| CODE: |
| 345 |
3 |
| RETVAL = (SEN->consistent)(BOARD(u)); |
| 346 |
|
| OUTPUT: |
| 347 |
|
| RETVAL |
| 348 |
|
| |
| 349 |
|
| int |
| 350 |
|
| _ply(u) |
| 351 |
|
| UV u |
| 352 |
|
| CODE: |
| 353 |
879579 |
| RETVAL = (SEN->ply)(BOARD(u)); |
| 354 |
|
| OUTPUT: |
| 355 |
|
| RETVAL |
| 356 |
|
| |
| 357 |
|
| void |
| 358 |
|
| _set_ply(u, ply) |
| 359 |
|
| UV u |
| 360 |
|
| int ply |
| 361 |
|
| CODE: |
| 362 |
2007 |
| (SEN->set_ply)(BOARD(u), ply); |
| 363 |
|
| |
| 364 |
|
| SV * |
| 365 |
|
| _key_hex(u) |
| 366 |
|
| UV u |
| 367 |
|
| CODE: |
| 368 |
119370 |
| RETVAL = sen_hex64(aTHX_ (SEN->key)(BOARD(u))); |
| 369 |
|
| OUTPUT: |
| 370 |
|
| RETVAL |
| 371 |
|
| |
| 372 |
|
| int |
| 373 |
|
| _house_of(row, col) |
| 374 |
|
| int row |
| 375 |
|
| int col |
| 376 |
|
| CODE: |
| 377 |
36 |
| RETVAL = (SEN->house_of)(row, col); |
| 378 |
|
| OUTPUT: |
| 379 |
|
| RETVAL |
| 380 |
|
| |
| 381 |
|
| int |
| 382 |
|
| _row_of(house) |
| 383 |
|
| int house |
| 384 |
|
| CODE: |
| 385 |
35 |
| RETVAL = (SEN->row_of)(house); |
| 386 |
|
| OUTPUT: |
| 387 |
|
| RETVAL |
| 388 |
|
| |
| 389 |
|
| int |
| 390 |
|
| _col_of(house) |
| 391 |
|
| int house |
| 392 |
|
| CODE: |
| 393 |
39 |
| RETVAL = (SEN->col_of)(house); |
| 394 |
|
| OUTPUT: |
| 395 |
|
| RETVAL |
| 396 |
|
| |
| 397 |
|
| int |
| 398 |
|
| _is_marked(house) |
| 399 |
|
| int house |
| 400 |
|
| CODE: |
| 401 |
64 |
| RETVAL = (SEN->is_marked)(house); |
| 402 |
|
| OUTPUT: |
| 403 |
|
| RETVAL |
| 404 |
|
| |
| 405 |
|
| # The rule set a name or a hash stands for, flat: pieces, blockade, protect, |
| 406 |
|
| # backward, reach26, throw_on_26, the safe mask, then the 100 chain entries, |
| 407 |
|
| # house by house, roll by roll. |
| 408 |
|
| void |
| 409 |
|
| _rules(rules) |
| 410 |
|
| SV *rules |
| 411 |
|
| PREINIT: |
| 412 |
|
| struct sen_rules r; |
| 413 |
|
| PPCODE: |
| 414 |
86007 |
| sen_rules_of_sv(aTHX_ rules, &r); |
| 415 |
85996 |
▶ | EXTEND(SP, SEN_RULES_WORDS); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, SEN_RULES_WORDS); |
0 |
85996 |
85996 |
|
| 416 |
85996 |
| sen_push_rules(aTHX_ SP, &r); |
| 417 |
85996 |
| SP += SEN_RULES_WORDS; |
| 418 |
|
| |
| 419 |
|
| # The same flat list for a set by NUMBER, or nothing when there is no such set. |
| 420 |
|
| void |
| 421 |
|
| _rules_named(name) |
| 422 |
|
| int name |
| 423 |
|
| PREINIT: |
| 424 |
|
| struct sen_rules r; |
| 425 |
|
| PPCODE: |
| 426 |
4 |
▶ | if ((SEN->rules_named)(name, &r)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((SEN->rules_named)(name, &r)) { |
2 |
2 |
4 |
|
| 427 |
2 |
▶ | EXTEND(SP, SEN_RULES_WORDS); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, SEN_RULES_WORDS); |
0 |
2 |
2 |
|
| 428 |
2 |
| sen_push_rules(aTHX_ SP, &r); |
| 429 |
2 |
| SP += SEN_RULES_WORDS; |
| 430 |
|
| } |
| 431 |
|
| |
| 432 |
|
| int |
| 433 |
|
| _rules_id(name) |
| 434 |
|
| const char *name |
| 435 |
|
| CODE: |
| 436 |
4 |
| RETVAL = (SEN->rules_id)(name); |
| 437 |
|
| OUTPUT: |
| 438 |
|
| RETVAL |
| 439 |
|
| |
| 440 |
|
| # rules_ok on a struct written field by field, so that a test can hand it |
| 441 |
|
| # what no name and no hash would ever make. |
| 442 |
|
| int |
| 443 |
|
| _rules_ok_raw(pieces, blockade, protect, backward, reach26, throw_on_26, safe, chains, reserved) |
| 444 |
|
| int pieces |
| 445 |
|
| int blockade |
| 446 |
|
| int protect |
| 447 |
|
| int backward |
| 448 |
|
| int reach26 |
| 449 |
|
| int throw_on_26 |
| 450 |
|
| UV safe |
| 451 |
|
| SV *chains |
| 452 |
|
| int reserved |
| 453 |
|
| PREINIT: |
| 454 |
|
| struct sen_rules r; |
| 455 |
|
| AV *av; |
| 456 |
23 |
▶ | int h, roll, i, k = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| int h, roll, i, k = 0; |
23 |
0 |
23 |
|
| 457 |
|
| CODE: |
| 458 |
23 |
▶ | if (!SvROK(chains) || SvTYPE(SvRV(chains)) != SVt_PVAV) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvROK(chains) || SvTYPE(SvRV(chains)) != SVt_PVAV) |
23 |
0 |
23 |
| if (!SvROK(chains) || SvTYPE(SvRV(chains)) != SVt_PVAV) |
0 |
23 |
23 |
|
| 459 |
0 |
| croak("Game::Senet::Engine: the chains are an array reference"); |
| 460 |
23 |
| av = (AV *) SvRV(chains); |
| 461 |
23 |
▶ | if (av_len(av) + 1 != SEN_EXIT_HOUSES * SEN_ROLL_MAX * SEN_CHAIN_MAX) |
Branch: 1 branch
| Branch | True | False | Total |
| if (av_len(av) + 1 != SEN_EXIT_HOUSES * SEN_ROLL_MAX * SEN_CHAIN_MAX) |
0 |
23 |
23 |
|
| 462 |
0 |
| croak("Game::Senet::Engine: the chains are %d numbers", |
| 463 |
|
| SEN_EXIT_HOUSES * SEN_ROLL_MAX * SEN_CHAIN_MAX); |
| 464 |
23 |
| Zero(&r, 1, struct sen_rules); |
| 465 |
23 |
| r.pieces = pieces; |
| 466 |
23 |
| r.blockade = blockade; |
| 467 |
23 |
| r.protect = protect; |
| 468 |
23 |
| r.backward = backward; |
| 469 |
23 |
| r.reach26 = reach26; |
| 470 |
23 |
| r.throw_on_26 = throw_on_26; |
| 471 |
23 |
| r.safe = (unsigned long) safe; |
| 472 |
23 |
| r.reserved[0] = reserved; |
| 473 |
138 |
▶ | for (h = 0; h < SEN_EXIT_HOUSES; h++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (h = 0; h < SEN_EXIT_HOUSES; h++) |
115 |
23 |
138 |
|
| 474 |
690 |
▶ | for (roll = 0; roll < SEN_ROLL_MAX; roll++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (roll = 0; roll < SEN_ROLL_MAX; roll++) |
575 |
115 |
690 |
|
| 475 |
2875 |
▶ | for (i = 0; i < SEN_CHAIN_MAX; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < SEN_CHAIN_MAX; i++) { |
2300 |
575 |
2875 |
|
| 476 |
2300 |
| SV **e = av_fetch(av, k++, 0); |
| 477 |
2300 |
▶ | r.exit[h][roll][i] = (signed char) (e ? SvIV(*e) : 0); |
Branch: 1 branch
| Branch | True | False | Total |
| r.exit[h][roll][i] = (signed char) (e ? SvIV(*e) : 0); |
2300 |
0 |
2300 |
|
| 478 |
|
| } |
| 479 |
23 |
| RETVAL = (SEN->rules_ok)(&r); |
| 480 |
|
| OUTPUT: |
| 481 |
|
| RETVAL |
| 482 |
|
| |
| 483 |
|
| # ---- version 2: the chance of a roll ------------------------------------------- |
| 484 |
|
| |
| 485 |
|
| int |
| 486 |
|
| _chance_count() |
| 487 |
|
| CODE: |
| 488 |
11892 |
| RETVAL = (SEN->chance_count)(); |
| 489 |
|
| OUTPUT: |
| 490 |
|
| RETVAL |
| 491 |
|
| |
| 492 |
|
| # The roll, its weight and the denominator, or nothing when there is no such |
| 493 |
|
| # entry. |
| 494 |
|
| void |
| 495 |
|
| _chance(i) |
| 496 |
|
| int i |
| 497 |
|
| PREINIT: |
| 498 |
59462 |
| int roll = 0, weight = 0, denominator = 0; |
| 499 |
|
| PPCODE: |
| 500 |
59462 |
▶ | if ((SEN->chance)(i, &roll, &weight, &denominator)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((SEN->chance)(i, &roll, &weight, &denominator)) { |
59460 |
2 |
59462 |
|
| 501 |
59460 |
▶ | EXTEND(SP, 3); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 3); |
0 |
59460 |
59460 |
|
| 502 |
59460 |
| PUSHs(sv_2mortal(newSViv(roll))); |
| 503 |
59460 |
| PUSHs(sv_2mortal(newSViv(weight))); |
| 504 |
59460 |
| PUSHs(sv_2mortal(newSViv(denominator))); |
| 505 |
|
| } |
| 506 |
|
| |
| 507 |
|
| int |
| 508 |
|
| _extra(roll) |
| 509 |
|
| int roll |
| 510 |
|
| CODE: |
| 511 |
8 |
| RETVAL = (SEN->extra)(roll); |
| 512 |
|
| OUTPUT: |
| 513 |
|
| RETVAL |
| 514 |
|
| |
| 515 |
|
| # ---- version 3: what a roll allows --------------------------------------------- |
| 516 |
|
| |
| 517 |
|
| int |
| 518 |
|
| _moves_max() |
| 519 |
|
| CODE: |
| 520 |
0 |
▶ | RETVAL = SEN_MOVES_MAX; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = SEN_MOVES_MAX; |
0 |
0 |
0 |
|
| 521 |
|
| OUTPUT: |
| 522 |
|
| RETVAL |
| 523 |
|
| |
| 524 |
|
| # Each move as seven numbers in an array: from, to, roll, swap, back, off, |
| 525 |
|
| # walk. A roll |
| 526 |
|
| # that is not 1 to 5 croaks; it is not an empty list. |
| 527 |
|
| void |
| 528 |
|
| _moves(u, roll, rules, only) |
| 529 |
|
| UV u |
| 530 |
|
| int roll |
| 531 |
|
| SV *rules |
| 532 |
|
| int only |
| 533 |
|
| PREINIT: |
| 534 |
|
| struct sen_rules r; |
| 535 |
|
| struct sen_move m[SEN_MOVES_MAX]; |
| 536 |
|
| int n, i; |
| 537 |
|
| PPCODE: |
| 538 |
1227701 |
| sen_rules_of_sv(aTHX_ rules, &r); |
| 539 |
1227701 |
| n = (SEN->moves)(BOARD(u), &r, roll, only, m, SEN_MOVES_MAX); |
| 540 |
1227701 |
▶ | if (n < 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (n < 0) |
1 |
1227700 |
1227701 |
|
| 541 |
1 |
| croak("Game::Senet::Engine: a roll is a whole number of houses from 1 to %d", SEN_ROLL_MAX); |
| 542 |
1227700 |
▶ | if (n > SEN_MOVES_MAX) n = SEN_MOVES_MAX; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n > SEN_MOVES_MAX) n = SEN_MOVES_MAX; |
0 |
1227700 |
1227700 |
|
| 543 |
1227700 |
▶ | EXTEND(SP, n); |
Branch: 2 branches
| Branch | True | False | Total |
| EXTEND(SP, n); |
1227700 |
0 |
1227700 |
| EXTEND(SP, n); |
0 |
1227700 |
1227700 |
|
| 544 |
3689196 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
2461496 |
1227700 |
3689196 |
|
| 545 |
2461496 |
| AV *av = newAV(); |
| 546 |
2461496 |
| av_extend(av, 6); |
| 547 |
2461496 |
| av_push(av, newSViv(m[i].from)); |
| 548 |
2461496 |
| av_push(av, newSViv(m[i].to)); |
| 549 |
2461496 |
| av_push(av, newSViv(m[i].roll)); |
| 550 |
2461496 |
| av_push(av, newSViv(m[i].swap)); |
| 551 |
2461496 |
| av_push(av, newSViv(m[i].back)); |
| 552 |
2461496 |
| av_push(av, newSViv(m[i].off)); |
| 553 |
2461496 |
| av_push(av, newSViv(m[i].walk)); |
| 554 |
2461496 |
| PUSHs(sv_2mortal(newRV_noinc((SV *) av))); |
| 555 |
|
| } |
| 556 |
|
| |
| 557 |
|
| # FOR A TEST, and nothing else: `count` random positions from a seeded |
| 558 |
|
| # generator of its own, each roll 1 to 5, the lists under protect `ends` and |
| 559 |
|
| # under protect `run` compared, with `blockade` as given and the rest of |
| 560 |
|
| # skyruk. Returns how many (position, roll) pairs were compared and how many |
| 561 |
|
| # DIFFERED. In C because 200,000 positions from Perl is minutes. |
| 562 |
|
| void |
| 563 |
|
| _compare_ends_run(count, seed, blockade) |
| 564 |
|
| int count |
| 565 |
|
| UV seed |
| 566 |
|
| int blockade |
| 567 |
|
| PREINIT: |
| 568 |
|
| struct sen_rules ends, run; |
| 569 |
|
| struct sen_move a[SEN_MOVES_MAX], c[SEN_MOVES_MAX]; |
| 570 |
|
| unsigned long long s; |
| 571 |
3 |
| int i, roll, differ = 0, compared = 0; |
| 572 |
|
| PPCODE: |
| 573 |
3 |
| (SEN->rules_named)(SEN_RULES_SKYRUK, &ends); |
| 574 |
3 |
| ends.blockade = blockade; |
| 575 |
3 |
| ends.pieces = SEN_PIECES_MAX; |
| 576 |
3 |
| run = ends; |
| 577 |
3 |
| run.protect = SEN_PROTECT_RUN; |
| 578 |
3 |
▶ | if (!(SEN->rules_ok)(&ends)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (!(SEN->rules_ok)(&ends)) |
0 |
3 |
3 |
|
| 579 |
0 |
| croak("Game::Senet::Engine: blockade is 0, 2 or 3"); |
| 580 |
3 |
| s = (unsigned long long) seed * 6364136223846793005ULL + 1442695040888963407ULL; |
| 581 |
420003 |
▶ | for (i = 0; i < count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < count; i++) { |
420000 |
3 |
420003 |
|
| 582 |
420000 |
| struct sen_board *b = (SEN->board_new)(SEN_PIECES_MAX); |
| 583 |
|
| int side, placed; |
| 584 |
1260000 |
▶ | for (side = 0; side < 2; side++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (side = 0; side < 2; side++) { |
840000 |
420000 |
1260000 |
|
| 585 |
|
| int want; |
| 586 |
840000 |
| s = s * 6364136223846793005ULL + 1442695040888963407ULL; |
| 587 |
840000 |
| want = (int) ((s >> 33) % (SEN_PIECES_MAX + 1)); |
| 588 |
840000 |
| placed = 0; |
| 589 |
4253128 |
▶ | while (placed < want) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (placed < want) { |
3413128 |
840000 |
4253128 |
|
| 590 |
|
| int h; |
| 591 |
3413128 |
| s = s * 6364136223846793005ULL + 1442695040888963407ULL; |
| 592 |
3413128 |
| h = 1 + (int) ((s >> 33) % SEN_HOUSES); |
| 593 |
3413128 |
▶ | if ((SEN->at)(b, h) != SEN_EMPTY) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((SEN->at)(b, h) != SEN_EMPTY) continue; |
469275 |
2943853 |
3413128 |
|
| 594 |
2943853 |
| (SEN->put)(b, h, SEN_PIECE_OF(side)); |
| 595 |
2943853 |
| placed++; |
| 596 |
|
| } |
| 597 |
|
| } |
| 598 |
420000 |
| s = s * 6364136223846793005ULL + 1442695040888963407ULL; |
| 599 |
420000 |
| (SEN->set_side)(b, (int) ((s >> 33) & 1)); |
| 600 |
2520000 |
▶ | for (roll = 1; roll <= SEN_ROLL_MAX; roll++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (roll = 1; roll <= SEN_ROLL_MAX; roll++) { |
2100000 |
420000 |
2520000 |
|
| 601 |
2100000 |
| int na = (SEN->moves)(b, &ends, roll, 0, a, SEN_MOVES_MAX); |
| 602 |
2100000 |
| int nc = (SEN->moves)(b, &run, roll, 0, c, SEN_MOVES_MAX); |
| 603 |
2100000 |
| int k, same = (na == nc); |
| 604 |
7672513 |
▶ | for (k = 0; same && k < na; k++) |
Branch: 2 branches
| Branch | True | False | Total |
| for (k = 0; same && k < na; k++) |
7672070 |
443 |
7672513 |
| for (k = 0; same && k < na; k++) |
5572513 |
2099557 |
7672070 |
|
| 605 |
5572513 |
▶ | if (a[k].from != c[k].from || a[k].to != c[k].to || a[k].swap != c[k].swap |
Branch: 3 branches
| Branch | True | False | Total |
| if (a[k].from != c[k].from || a[k].to != c[k].to || a[k].swap != c[k].swap |
5572511 |
2 |
5572513 |
| if (a[k].from != c[k].from || a[k].to != c[k].to || a[k].swap != c[k].swap |
5572506 |
5 |
5572511 |
| if (a[k].from != c[k].from || a[k].to != c[k].to || a[k].swap != c[k].swap |
5572506 |
0 |
5572506 |
|
| 606 |
5572506 |
▶ | || a[k].back != c[k].back) |
Branch: 1 branch
| Branch | True | False | Total |
| || a[k].back != c[k].back) |
0 |
5572506 |
5572506 |
|
| 607 |
7 |
| same = 0; |
| 608 |
2100000 |
| compared++; |
| 609 |
2100000 |
▶ | if (!same) differ++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!same) differ++; |
443 |
2099557 |
2100000 |
|
| 610 |
|
| } |
| 611 |
420000 |
| (SEN->board_drop)(b); |
| 612 |
|
| } |
| 613 |
3 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
3 |
3 |
|
| 614 |
3 |
| PUSHs(sv_2mortal(newSViv(compared))); |
| 615 |
3 |
| PUSHs(sv_2mortal(newSViv(differ))); |
| 616 |
|
| |
| 617 |
|
| # ---- version 4: a move made ------------------------------------------------------ |
| 618 |
|
| |
| 619 |
|
| int |
| 620 |
|
| _ply_cap() |
| 621 |
|
| CODE: |
| 622 |
3 |
| RETVAL = (SEN->ply_cap)(); |
| 623 |
|
| OUTPUT: |
| 624 |
|
| RETVAL |
| 625 |
|
| |
| 626 |
|
| # What apply hands back is the undo, as the bytes of the struct, and the house |
| 627 |
|
| # the next throw is bound to, or 0. The undo is opaque: the only thing to do |
| 628 |
|
| # with one is hand it to _unapply. Nothing when the move was not made. |
| 629 |
|
| void |
| 630 |
|
| _apply(u, rules, from, to, roll, off) |
| 631 |
|
| UV u |
| 632 |
|
| SV *rules |
| 633 |
|
| int from |
| 634 |
|
| int to |
| 635 |
|
| int roll |
| 636 |
|
| int off |
| 637 |
|
| PREINIT: |
| 638 |
|
| struct sen_rules r; |
| 639 |
|
| struct sen_move m; |
| 640 |
|
| struct sen_undo undo; |
| 641 |
|
| PPCODE: |
| 642 |
943206 |
| sen_rules_of_sv(aTHX_ rules, &r); |
| 643 |
943206 |
| Zero(&m, 1, struct sen_move); |
| 644 |
943206 |
| m.from = from; |
| 645 |
943206 |
▶ | m.to = off ? -1 : to; |
Branch: 1 branch
| Branch | True | False | Total |
| m.to = off ? -1 : to; |
793380 |
149826 |
943206 |
|
| 646 |
943206 |
| m.roll = roll; |
| 647 |
943206 |
| m.off = off ? 1 : 0; |
| 648 |
943206 |
▶ | if ((SEN->apply)(BOARD(u), &r, &m, &undo)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((SEN->apply)(BOARD(u), &r, &m, &undo)) { |
943205 |
1 |
943206 |
|
| 649 |
943205 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
943205 |
943205 |
|
| 650 |
943205 |
| PUSHs(sv_2mortal(newSVpvn((const char *) &undo, sizeof(undo)))); |
| 651 |
943205 |
| PUSHs(sv_2mortal(newSViv(undo.bound))); |
| 652 |
|
| } |
| 653 |
|
| |
| 654 |
|
| SV * |
| 655 |
|
| _forfeit(u, roll) |
| 656 |
|
| UV u |
| 657 |
|
| int roll |
| 658 |
|
| PREINIT: |
| 659 |
|
| struct sen_undo undo; |
| 660 |
|
| CODE: |
| 661 |
235685 |
| (SEN->forfeit)(BOARD(u), roll, &undo); |
| 662 |
235685 |
| RETVAL = newSVpvn((const char *) &undo, sizeof(undo)); |
| 663 |
|
| OUTPUT: |
| 664 |
|
| RETVAL |
| 665 |
|
| |
| 666 |
|
| void |
| 667 |
|
| _unapply(u, token) |
| 668 |
|
| UV u |
| 669 |
|
| SV *token |
| 670 |
|
| PREINIT: |
| 671 |
|
| struct sen_undo undo; |
| 672 |
|
| STRLEN len; |
| 673 |
|
| const char *bytes; |
| 674 |
|
| CODE: |
| 675 |
71519 |
▶ | if (!SvOK(token) || SvROK(token)) |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SvOK(token) || SvROK(token)) |
71519 |
0 |
71519 |
| if (!SvOK(token) || SvROK(token)) |
0 |
71519 |
71519 |
|
| 676 |
0 |
| croak("Game::Senet::Engine: unapply takes what apply or forfeit returned"); |
| 677 |
71519 |
| bytes = SvPVbyte(token, len); |
| 678 |
71519 |
▶ | if (len != sizeof(undo)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (len != sizeof(undo)) |
0 |
71519 |
71519 |
|
| 679 |
0 |
| croak("Game::Senet::Engine: unapply takes what apply or forfeit returned"); |
| 680 |
71519 |
| Copy(bytes, &undo, 1, struct sen_undo); |
| 681 |
71519 |
| (SEN->unapply)(BOARD(u), &undo); |
| 682 |
|
| |
| 683 |
|
| int |
| 684 |
|
| _status(u) |
| 685 |
|
| UV u |
| 686 |
|
| CODE: |
| 687 |
7382630 |
| RETVAL = (SEN->status)(BOARD(u)); |
| 688 |
|
| OUTPUT: |
| 689 |
|
| RETVAL |
| 690 |
|
| |
| 691 |
|
| int |
| 692 |
|
| _how(u) |
| 693 |
|
| UV u |
| 694 |
|
| CODE: |
| 695 |
1413 |
| RETVAL = (SEN->how)(BOARD(u)); |
| 696 |
|
| OUTPUT: |
| 697 |
|
| RETVAL |
| 698 |
|
| |
| 699 |
|
| int |
| 700 |
|
| _winner(u) |
| 701 |
|
| UV u |
| 702 |
|
| CODE: |
| 703 |
62704 |
| RETVAL = (SEN->winner)(BOARD(u)); |
| 704 |
|
| OUTPUT: |
| 705 |
|
| RETVAL |
| 706 |
|
| |
| 707 |
|
| SV * |
| 708 |
|
| _walk(u, depth, rules) |
| 709 |
|
| UV u |
| 710 |
|
| int depth |
| 711 |
|
| SV *rules |
| 712 |
|
| PREINIT: |
| 713 |
|
| struct sen_rules r; |
| 714 |
|
| CODE: |
| 715 |
40 |
| sen_rules_of_sv(aTHX_ rules, &r); |
| 716 |
40 |
| RETVAL = sen_dec64(aTHX_ (SEN->walk)(BOARD(u), &r, depth)); |
| 717 |
|
| OUTPUT: |
| 718 |
|
| RETVAL |
| 719 |
|
| |
| 720 |
|
| # FOR A TEST, and nothing else: `count` random boards of `pieces` a side, light |
| 721 |
|
| # to move with ONE piece on 26 and every other piece of both sides placed at |
| 722 |
|
| # random on houses 1 to 15, and the kendall chain for a roll of 1, which is the |
| 723 |
|
| # walk. Returns how many boards the walk landed on and the lowest house it |
| 724 |
|
| # reached. In C because 200,000 boards from Perl is minutes. |
| 725 |
|
| void |
| 726 |
|
| _sample_walk(count, seed, pieces) |
| 727 |
|
| int count |
| 728 |
|
| UV seed |
| 729 |
|
| int pieces |
| 730 |
|
| PREINIT: |
| 731 |
|
| struct sen_rules r; |
| 732 |
|
| struct sen_move m[SEN_MOVES_MAX]; |
| 733 |
|
| unsigned long long s; |
| 734 |
2 |
| int i, landed = 0, lowest = SEN_HOUSES + 1; |
| 735 |
|
| PPCODE: |
| 736 |
2 |
▶ | if (pieces < 1 || pieces > SEN_PIECES_MAX) |
Branch: 2 branches
| Branch | True | False | Total |
| if (pieces < 1 || pieces > SEN_PIECES_MAX) |
2 |
0 |
2 |
| if (pieces < 1 || pieces > SEN_PIECES_MAX) |
0 |
2 |
2 |
|
| 737 |
0 |
| croak("Game::Senet::Engine: pieces is 1 to %d", SEN_PIECES_MAX); |
| 738 |
2 |
| (SEN->rules_named)(SEN_RULES_KENDALL, &r); |
| 739 |
2 |
| r.pieces = pieces; |
| 740 |
2 |
| s = (unsigned long long) seed * 6364136223846793005ULL + 1442695040888963407ULL; |
| 741 |
200002 |
▶ | for (i = 0; i < count; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < count; i++) { |
200000 |
2 |
200002 |
|
| 742 |
200000 |
| struct sen_board *b = (SEN->board_new)(pieces); |
| 743 |
200000 |
| int placed = 0, others = 2 * pieces - 1, n; |
| 744 |
200000 |
| (SEN->put)(b, 26, SEN_LIGHT); |
| 745 |
4224907 |
▶ | while (placed < others) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (placed < others) { |
4024907 |
200000 |
4224907 |
|
| 746 |
|
| int h; |
| 747 |
4024907 |
| s = s * 6364136223846793005ULL + 1442695040888963407ULL; |
| 748 |
4024907 |
| h = 1 + (int) ((s >> 33) % SEN_REBIRTH); |
| 749 |
4024907 |
▶ | if ((SEN->at)(b, h) != SEN_EMPTY) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if ((SEN->at)(b, h) != SEN_EMPTY) continue; |
1824907 |
2200000 |
4024907 |
|
| 750 |
2200000 |
▶ | (SEN->put)(b, h, placed < pieces - 1 ? SEN_LIGHT : SEN_DARK); |
Branch: 1 branch
| Branch | True | False | Total |
| (SEN->put)(b, h, placed < pieces - 1 ? SEN_LIGHT : SEN_DARK); |
1000000 |
1200000 |
2200000 |
|
| 751 |
2200000 |
| placed++; |
| 752 |
|
| } |
| 753 |
200000 |
| n = (SEN->moves)(b, &r, 1, 26, m, SEN_MOVES_MAX); |
| 754 |
200000 |
▶ | if (n == 1 && m[0].walk && m[0].to >= 1) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (n == 1 && m[0].walk && m[0].to >= 1) { |
200000 |
0 |
200000 |
| if (n == 1 && m[0].walk && m[0].to >= 1) { |
200000 |
0 |
200000 |
| if (n == 1 && m[0].walk && m[0].to >= 1) { |
200000 |
0 |
200000 |
|
| 755 |
200000 |
| landed++; |
| 756 |
200000 |
▶ | if (m[0].to < lowest) lowest = m[0].to; |
Branch: 1 branch
| Branch | True | False | Total |
| if (m[0].to < lowest) lowest = m[0].to; |
10 |
199990 |
200000 |
|
| 757 |
|
| } |
| 758 |
200000 |
| (SEN->board_drop)(b); |
| 759 |
|
| } |
| 760 |
2 |
▶ | EXTEND(SP, 2); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 2); |
0 |
2 |
2 |
|
| 761 |
2 |
| PUSHs(sv_2mortal(newSViv(landed))); |
| 762 |
2 |
| PUSHs(sv_2mortal(newSViv(lowest))); |
| 763 |
|
| |
| 764 |
|
| # ---- version 5: choosing a move ------------------------------------------------ |
| 765 |
|
| |
| 766 |
|
| int |
| 767 |
|
| _protected(u, rules, house) |
| 768 |
|
| UV u |
| 769 |
|
| SV *rules |
| 770 |
|
| int house |
| 771 |
|
| PREINIT: |
| 772 |
|
| struct sen_rules r; |
| 773 |
|
| CODE: |
| 774 |
2355 |
| sen_rules_of_sv(aTHX_ rules, &r); |
| 775 |
2355 |
| RETVAL = (SEN->protected_at)(BOARD(u), &r, house); |
| 776 |
|
| OUTPUT: |
| 777 |
|
| RETVAL |
| 778 |
|
| |
| 779 |
|
| int |
| 780 |
|
| _in_blockade(u, rules, house) |
| 781 |
|
| UV u |
| 782 |
|
| SV *rules |
| 783 |
|
| int house |
| 784 |
|
| PREINIT: |
| 785 |
|
| struct sen_rules r; |
| 786 |
|
| CODE: |
| 787 |
4262 |
| sen_rules_of_sv(aTHX_ rules, &r); |
| 788 |
4262 |
| RETVAL = (SEN->in_blockade)(BOARD(u), &r, house); |
| 789 |
|
| OUTPUT: |
| 790 |
|
| RETVAL |
| 791 |
|
| |
| 792 |
|
| int |
| 793 |
|
| _evaluate(u, rules, weights, side) |
| 794 |
|
| UV u |
| 795 |
|
| SV *rules |
| 796 |
|
| SV *weights |
| 797 |
|
| int side |
| 798 |
|
| PREINIT: |
| 799 |
|
| struct sen_rules r; |
| 800 |
|
| struct sen_weights w; |
| 801 |
|
| CODE: |
| 802 |
56188 |
| sen_rules_of_sv(aTHX_ rules, &r); |
| 803 |
56188 |
| sen_weights_of_sv(aTHX_ weights, &w); |
| 804 |
56188 |
| RETVAL = (SEN->evaluate)(BOARD(u), &r, &w, side); |
| 805 |
|
| OUTPUT: |
| 806 |
|
| RETVAL |
| 807 |
|
| |
| 808 |
|
| int |
| 809 |
|
| _greedy(u, roll, rules, only) |
| 810 |
|
| UV u |
| 811 |
|
| int roll |
| 812 |
|
| SV *rules |
| 813 |
|
| int only |
| 814 |
|
| PREINIT: |
| 815 |
|
| struct sen_rules r; |
| 816 |
|
| CODE: |
| 817 |
30734 |
| sen_rules_of_sv(aTHX_ rules, &r); |
| 818 |
30734 |
| RETVAL = (SEN->greedy)(BOARD(u), &r, roll, only); |
| 819 |
|
| OUTPUT: |
| 820 |
|
| RETVAL |
| 821 |
|
| |
| 822 |
|
| # The index chosen, then the nodes spent as a string of digits, the depth |
| 823 |
|
| # finished, the value, and whether the budget ran out. A budget is a whole |
| 824 |
|
| # number of nodes from 0 to 2,000,000,000: more than that is refused, and does |
| 825 |
|
| # not wrap round on a perl whose integers are 32 bits. |
| 826 |
|
| void |
| 827 |
|
| _search(u, roll, rules, weights, budget, max_depth, only) |
| 828 |
|
| UV u |
| 829 |
|
| int roll |
| 830 |
|
| SV *rules |
| 831 |
|
| SV *weights |
| 832 |
|
| IV budget |
| 833 |
|
| int max_depth |
| 834 |
|
| int only |
| 835 |
|
| PREINIT: |
| 836 |
|
| struct sen_rules r; |
| 837 |
|
| struct sen_weights w; |
| 838 |
|
| struct sen_search_info info; |
| 839 |
|
| int chosen; |
| 840 |
|
| PPCODE: |
| 841 |
53396 |
| sen_rules_of_sv(aTHX_ rules, &r); |
| 842 |
53396 |
| sen_weights_of_sv(aTHX_ weights, &w); |
| 843 |
53396 |
▶ | if (budget < 0 || budget > 2000000000) |
Branch: 2 branches
| Branch | True | False | Total |
| if (budget < 0 || budget > 2000000000) |
53396 |
0 |
53396 |
| if (budget < 0 || budget > 2000000000) |
0 |
53396 |
53396 |
|
| 844 |
0 |
| croak("Game::Senet::Engine: a budget is a number of nodes from 0 to 2000000000"); |
| 845 |
53396 |
| chosen = (SEN->search)(BOARD(u), &r, roll, only, &w, (unsigned long long) budget, max_depth, &info); |
| 846 |
53396 |
▶ | EXTEND(SP, 5); |
Branch: 1 branch
| Branch | True | False | Total |
| EXTEND(SP, 5); |
0 |
53396 |
53396 |
|
| 847 |
53396 |
| PUSHs(sv_2mortal(newSViv(chosen))); |
| 848 |
53396 |
| PUSHs(sv_2mortal(sen_dec64(aTHX_ info.nodes))); |
| 849 |
53396 |
| PUSHs(sv_2mortal(newSViv(info.depth))); |
| 850 |
53396 |
| PUSHs(sv_2mortal(newSViv(info.value))); |
| 851 |
53396 |
| PUSHs(sv_2mortal(newSViv(info.stopped))); |
| 852 |
|
| |
| 853 |
|
| int |
| 854 |
|
| _expect(u, rules, weights, only, depth, side) |
| 855 |
|
| UV u |
| 856 |
|
| SV *rules |
| 857 |
|
| SV *weights |
| 858 |
|
| int only |
| 859 |
|
| int depth |
| 860 |
|
| int side |
| 861 |
|
| PREINIT: |
| 862 |
|
| struct sen_rules r; |
| 863 |
|
| struct sen_weights w; |
| 864 |
|
| CODE: |
| 865 |
2406 |
| sen_rules_of_sv(aTHX_ rules, &r); |
| 866 |
2406 |
| sen_weights_of_sv(aTHX_ weights, &w); |
| 867 |
2406 |
| RETVAL = (SEN->expect)(BOARD(u), &r, &w, only, depth, side); |
| 868 |
|
| OUTPUT: |
| 869 |
|
| RETVAL |
| 870 |
|
| |
| 871 |
|
| MODULE = Game::Senet PACKAGE = Game::Senet::Engine::Hold |
| 872 |
|
| |
| 873 |
|
| PROTOTYPES: DISABLE |
| 874 |
|
| |
| 875 |
|
| # ---- letting go ----------------------------------------------------------------- |
| 876 |
|
| # |
| 877 |
|
| # WHY THIS IS NOT A DEMOLISH IN PERL. On perls 5.14 to 5.26 an eval that runs |
| 878 |
|
| # inside a destructor, while an exception is unwinding past a TEMPORARY of that |
| 879 |
|
| # class, leaves $@ empty: the caller's die arrives with no message. Object::Proto |
| 880 |
|
| # calls DEMOLISH under an eval, so any class with one has this. Game-Fanorona's |
| 881 |
|
| # packaging matrix found it; a DESTROY written in C runs no eval. |
| 882 |
|
| |
| 883 |
|
| void |
| 884 |
|
| DESTROY(self) |
| 885 |
|
| SV *self |
| 886 |
|
| PREINIT: |
| 887 |
|
| SV *inner; |
| 888 |
|
| UV u; |
| 889 |
|
| CODE: |
| 890 |
758473 |
▶ | if (SvROK(self)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (SvROK(self)) { |
758473 |
0 |
758473 |
|
| 891 |
758473 |
| inner = SvRV(self); |
| 892 |
758473 |
| u = SvUV(inner); |
| 893 |
758473 |
▶ | if (u) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (u) { |
758473 |
0 |
758473 |
|
| 894 |
758473 |
| sv_setuv(inner, 0); |
| 895 |
758473 |
| (SEN->board_drop)(BOARD(u)); |
| 896 |
|
| } |
| 897 |
|
| } |