| 1 |
|
| /* bd_engine.c - the brandubh board, and nothing that judges a move. |
| 2 |
|
| * |
| 3 |
|
| * PERL-FREE by design: see include/bd_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 board, the padded index, put and lift, the side to |
| 7 |
|
| * move, the zobrist key, and the position string in and out. Where a piece |
| 8 |
|
| * may go is bd_moves.c, which appends to the table. |
| 9 |
|
| */ |
| 10 |
|
| |
| 11 |
|
| #include <stdlib.h> |
| 12 |
|
| #include <string.h> |
| 13 |
|
| #include "bd_abi.h" |
| 14 |
|
| |
| 15 |
|
| /* ---- the position --------------------------------------------------------- |
| 16 |
|
| * |
| 17 |
|
| * A flat 81-cell array with a sentinel ring, the side to move, and a key |
| 18 |
|
| * maintained incrementally. No pointers, so a copy is a struct assignment and |
| 19 |
|
| * two boards never share anything. |
| 20 |
|
| */ |
| 21 |
|
| |
| 22 |
|
| struct bd_board { |
| 23 |
|
| unsigned char cell[BD_CELLS]; |
| 24 |
|
| int side; |
| 25 |
|
| unsigned long long key; |
| 26 |
|
| }; |
| 27 |
|
| |
| 28 |
|
| /* Boards handed out and not yet dropped. It exists so a test can show that a |
| 29 |
|
| * destructor ran exactly once; it is a plain int and is not synchronised. */ |
| 30 |
|
| static int BD_LIVE = 0; |
| 31 |
|
| |
| 32 |
|
| /* ---- the zobrist table ----------------------------------------------------- |
| 33 |
|
| * |
| 34 |
|
| * DERIVED, NOT COMMITTED. Two workers in a pool must agree about whether a |
| 35 |
|
| * position has been seen, so the table must not come from rand() or from |
| 36 |
|
| * anything that varies per process. A fixed function of a fixed seed satisfies |
| 37 |
|
| * that exactly as a committed array does, and it cannot suffer the |
| 38 |
|
| * transcription error a hand-pasted array can. splitmix64 is published, has no |
| 39 |
|
| * weak seeds, and is what the sibling engine uses. |
| 40 |
|
| * |
| 41 |
|
| * CHANGING THIS GENERATOR OR ITS SEED CHANGES EVERY KEY. t/03-key.t pins |
| 42 |
|
| * values so that is caught by a red test. |
| 43 |
|
| */ |
| 44 |
|
| |
| 45 |
|
| static unsigned long long ZOB[BD_KING + 1][BD_CELLS]; |
| 46 |
|
| static unsigned long long ZOB_SIDE; |
| 47 |
|
| static int ZOB_READY = 0; |
| 48 |
|
| |
| 49 |
19175 |
| static unsigned long long splitmix64(unsigned long long *state) |
| 50 |
|
| { |
| 51 |
|
| unsigned long long z; |
| 52 |
19175 |
| *state += 0x9E3779B97F4A7C15ULL; |
| 53 |
19175 |
| z = *state; |
| 54 |
19175 |
| z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL; |
| 55 |
19175 |
| z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL; |
| 56 |
19175 |
| return z ^ (z >> 31); |
| 57 |
|
| } |
| 58 |
|
| |
| 59 |
713230985 |
| static void zob_init(void) |
| 60 |
|
| { |
| 61 |
713230985 |
| unsigned long long s = 0x6272616E64756268ULL; /* "brandubh" */ |
| 62 |
|
| int p, c; |
| 63 |
713230985 |
▶ | if (ZOB_READY) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ZOB_READY) return; |
713230926 |
59 |
713230985 |
|
| 64 |
295 |
▶ | for (p = 0; p <= BD_KING; p++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (p = 0; p <= BD_KING; p++) |
236 |
59 |
295 |
|
| 65 |
19352 |
▶ | for (c = 0; c < BD_CELLS; c++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (c = 0; c < BD_CELLS; c++) |
19116 |
236 |
19352 |
|
| 66 |
19116 |
| ZOB[p][c] = splitmix64(&s); |
| 67 |
59 |
| ZOB_SIDE = splitmix64(&s); |
| 68 |
59 |
| ZOB_READY = 1; |
| 69 |
|
| } |
| 70 |
|
| |
| 71 |
|
| /* ---- geometry -------------------------------------------------------------- */ |
| 72 |
|
| |
| 73 |
1 |
| static int bd_stride(void) { return BD_STRIDE; } |
| 74 |
|
| |
| 75 |
7657326960 |
| static int bd_square_of(int file, int rank) |
| 76 |
|
| { |
| 77 |
7657326960 |
▶ | if (file < 0 || file >= BD_SIZE || rank < 0 || rank >= BD_SIZE) return -1; |
Branch: 4 branches
| Branch | True | False | Total |
| if (file < 0 || file >= BD_SIZE || rank < 0 || rank >= BD_SIZE) return -1; |
7657326959 |
1 |
7657326960 |
| if (file < 0 || file >= BD_SIZE || rank < 0 || rank >= BD_SIZE) return -1; |
7657326958 |
1 |
7657326959 |
| if (file < 0 || file >= BD_SIZE || rank < 0 || rank >= BD_SIZE) return -1; |
7657326958 |
0 |
7657326958 |
| if (file < 0 || file >= BD_SIZE || rank < 0 || rank >= BD_SIZE) return -1; |
1 |
7657326957 |
7657326958 |
|
| 78 |
7657326957 |
| return (rank + 1) * BD_STRIDE + (file + 1); |
| 79 |
|
| } |
| 80 |
|
| |
| 81 |
15101581133 |
| static int bd_on_board(int sq) |
| 82 |
|
| { |
| 83 |
|
| int f, r; |
| 84 |
15101581133 |
▶ | if (sq < 0 || sq >= BD_CELLS) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (sq < 0 || sq >= BD_CELLS) return 0; |
15101581132 |
1 |
15101581133 |
| if (sq < 0 || sq >= BD_CELLS) return 0; |
0 |
15101581132 |
15101581132 |
|
| 85 |
15101581132 |
| f = sq % BD_STRIDE; |
| 86 |
15101581132 |
| r = sq / BD_STRIDE; |
| 87 |
15101581132 |
▶ | return f >= 1 && f <= BD_SIZE && r >= 1 && r <= BD_SIZE; |
Branch: 4 branches
| Branch | True | False | Total |
| return f >= 1 && f <= BD_SIZE && r >= 1 && r <= BD_SIZE; |
15100176820 |
1404312 |
15101581132 |
| return f >= 1 && f <= BD_SIZE && r >= 1 && r <= BD_SIZE; |
15098772510 |
1404310 |
15100176820 |
| return f >= 1 && f <= BD_SIZE && r >= 1 && r <= BD_SIZE; |
15097680272 |
1092238 |
15098772510 |
| return f >= 1 && f <= BD_SIZE && r >= 1 && r <= BD_SIZE; |
15096588034 |
1092238 |
15097680272 |
|
| 88 |
|
| } |
| 89 |
|
| |
| 90 |
6468644750 |
| static int bd_file_of(int sq) |
| 91 |
|
| { |
| 92 |
6468644750 |
▶ | if (!bd_on_board(sq)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bd_on_board(sq)) return -1; |
33 |
6468644717 |
6468644750 |
|
| 93 |
6468644717 |
| return (sq % BD_STRIDE) - 1; |
| 94 |
|
| } |
| 95 |
|
| |
| 96 |
6468644750 |
| static int bd_rank_of(int sq) |
| 97 |
|
| { |
| 98 |
6468644750 |
▶ | if (!bd_on_board(sq)) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bd_on_board(sq)) return -1; |
33 |
6468644717 |
6468644750 |
|
| 99 |
6468644717 |
| return (sq / BD_STRIDE) - 1; |
| 100 |
|
| } |
| 101 |
|
| |
| 102 |
|
| #define BD_THRONE ((BD_SIZE / 2 + 1) * BD_STRIDE + (BD_SIZE / 2 + 1)) |
| 103 |
|
| |
| 104 |
12549573338 |
| static int bd_is_throne(int sq) { return sq == BD_THRONE; } |
| 105 |
|
| |
| 106 |
6464626103 |
| static int bd_is_corner(int sq) |
| 107 |
|
| { |
| 108 |
6464626103 |
| int f = bd_file_of(sq), r = bd_rank_of(sq); |
| 109 |
6464626103 |
▶ | if (f < 0) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (f < 0) return 0; |
32 |
6464626071 |
6464626103 |
|
| 110 |
6464626071 |
▶ | return (f == 0 || f == BD_SIZE - 1) && (r == 0 || r == BD_SIZE - 1); |
Branch: 4 branches
| Branch | True | False | Total |
| return (f == 0 || f == BD_SIZE - 1) && (r == 0 || r == BD_SIZE - 1); |
5671807489 |
792818582 |
6464626071 |
| return (f == 0 || f == BD_SIZE - 1) && (r == 0 || r == BD_SIZE - 1); |
727949158 |
4943858331 |
5671807489 |
| return (f == 0 || f == BD_SIZE - 1) && (r == 0 || r == BD_SIZE - 1); |
1329682901 |
191084839 |
1520767740 |
| return (f == 0 || f == BD_SIZE - 1) && (r == 0 || r == BD_SIZE - 1); |
189941416 |
1139741485 |
1329682901 |
|
| 111 |
|
| } |
| 112 |
|
| |
| 113 |
|
| /* The four squares that share an edge with the throne. Not the diagonals: the |
| 114 |
|
| * king's capture there counts orthogonal neighbours and nothing else. */ |
| 115 |
2622254 |
| static int bd_beside_throne(int sq) |
| 116 |
|
| { |
| 117 |
2622254 |
▶ | if (!bd_on_board(sq)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bd_on_board(sq)) return 0; |
32 |
2622222 |
2622254 |
|
| 118 |
2552053 |
▶ | return sq == BD_THRONE - 1 || sq == BD_THRONE + 1 |
Branch: 1 branch
| Branch | True | False | Total |
| return sq == BD_THRONE - 1 || sq == BD_THRONE + 1 |
2479821 |
72232 |
2552053 |
|
| 119 |
5174275 |
▶ | || sq == BD_THRONE - BD_STRIDE || sq == BD_THRONE + BD_STRIDE; |
Branch: 3 branches
| Branch | True | False | Total |
| || sq == BD_THRONE - BD_STRIDE || sq == BD_THRONE + BD_STRIDE; |
2552053 |
70169 |
2622222 |
| || sq == BD_THRONE - BD_STRIDE || sq == BD_THRONE + BD_STRIDE; |
2251731 |
228090 |
2479821 |
| || sq == BD_THRONE - BD_STRIDE || sq == BD_THRONE + BD_STRIDE; |
58254 |
2193477 |
2251731 |
|
| 120 |
|
| } |
| 121 |
|
| |
| 122 |
|
| /* THE KING IS ON THE DEFENDERS' SIDE, and this is the one place that says so. */ |
| 123 |
2463902558 |
| static int bd_side_of(int piece) |
| 124 |
|
| { |
| 125 |
2463902558 |
▶ | if (piece == BD_ATTACKER) return BD_ATTACKERS; |
Branch: 1 branch
| Branch | True | False | Total |
| if (piece == BD_ATTACKER) return BD_ATTACKERS; |
1179375146 |
1284527412 |
2463902558 |
|
| 126 |
1284527412 |
▶ | if (piece == BD_DEFENDER || piece == BD_KING) return BD_DEFENDERS; |
Branch: 2 branches
| Branch | True | False | Total |
| if (piece == BD_DEFENDER || piece == BD_KING) return BD_DEFENDERS; |
383968726 |
900558686 |
1284527412 |
| if (piece == BD_DEFENDER || piece == BD_KING) return BD_DEFENDERS; |
383968724 |
2 |
383968726 |
|
| 127 |
2 |
| return -1; |
| 128 |
|
| } |
| 129 |
|
| |
| 130 |
|
| /* ---- the key ---------------------------------------------------------------- |
| 131 |
|
| * |
| 132 |
|
| * The key covers the pieces, their squares, and the side to move, and NOTHING |
| 133 |
|
| * else, so a position repeating is a position repeating. |
| 134 |
|
| */ |
| 135 |
|
| |
| 136 |
713099879 |
| static unsigned long long bd_zobrist(int piece, int sq) |
| 137 |
|
| { |
| 138 |
713099879 |
| zob_init(); |
| 139 |
713099879 |
▶ | if (!BD_IS_PIECE(piece)) return 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!BD_IS_PIECE(piece)) return 0; |
713099878 |
1 |
713099879 |
| if (!BD_IS_PIECE(piece)) return 0; |
1 |
713099877 |
713099878 |
|
| 140 |
713099877 |
▶ | if (!bd_on_board(sq)) return 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bd_on_board(sq)) return 0; |
1 |
713099876 |
713099877 |
|
| 141 |
713099876 |
| return ZOB[piece][sq]; |
| 142 |
|
| } |
| 143 |
|
| |
| 144 |
69016 |
| static unsigned long long bd_zobrist_side(void) |
| 145 |
|
| { |
| 146 |
69016 |
| zob_init(); |
| 147 |
69016 |
| return ZOB_SIDE; |
| 148 |
|
| } |
| 149 |
|
| |
| 150 |
|
| /* ---- the board ------------------------------------------------------------- */ |
| 151 |
|
| |
| 152 |
62031 |
| static void board_blank(struct bd_board *b) |
| 153 |
|
| { |
| 154 |
|
| int f, r; |
| 155 |
62031 |
| memset(b, 0, sizeof(*b)); |
| 156 |
62031 |
| memset(b->cell, BD_BORDER, sizeof(b->cell)); |
| 157 |
496248 |
▶ | for (r = 0; r < BD_SIZE; r++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < BD_SIZE; r++) |
434217 |
62031 |
496248 |
|
| 158 |
3473736 |
▶ | for (f = 0; f < BD_SIZE; f++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (f = 0; f < BD_SIZE; f++) |
3039519 |
434217 |
3473736 |
|
| 159 |
3039519 |
| b->cell[bd_square_of(f, r)] = BD_EMPTY; |
| 160 |
62031 |
| b->side = BD_ATTACKERS; |
| 161 |
62031 |
| b->key = 0; |
| 162 |
62031 |
| } |
| 163 |
|
| |
| 164 |
62031 |
| static struct bd_board *bd_board_empty(void) |
| 165 |
|
| { |
| 166 |
|
| struct bd_board *b; |
| 167 |
62031 |
| zob_init(); |
| 168 |
62031 |
| b = (struct bd_board *) malloc(sizeof(struct bd_board)); |
| 169 |
62031 |
▶ | if (!b) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!b) return NULL; |
0 |
62031 |
62031 |
|
| 170 |
62031 |
| board_blank(b); |
| 171 |
62031 |
| BD_LIVE++; |
| 172 |
62031 |
| return b; |
| 173 |
|
| } |
| 174 |
|
| |
| 175 |
|
| /* put and lift MAINTAIN THE KEY and judge nothing else. A piece value that is |
| 176 |
|
| * not one of the three, or BD_EMPTY, is ignored: the ring is not writable and |
| 177 |
|
| * neither is a square with a border on it. */ |
| 178 |
709326068 |
| static void bd_put(struct bd_board *b, int sq, int piece) |
| 179 |
|
| { |
| 180 |
|
| int old; |
| 181 |
709326068 |
▶ | if (!bd_on_board(sq)) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!bd_on_board(sq)) return; |
3 |
709326065 |
709326068 |
|
| 182 |
709326065 |
▶ | if (piece != BD_EMPTY && !BD_IS_PIECE(piece)) return; |
Branch: 3 branches
| Branch | True | False | Total |
| if (piece != BD_EMPTY && !BD_IS_PIECE(piece)) return; |
355108228 |
354217837 |
709326065 |
| if (piece != BD_EMPTY && !BD_IS_PIECE(piece)) return; |
355108228 |
0 |
355108228 |
| if (piece != BD_EMPTY && !BD_IS_PIECE(piece)) return; |
2 |
355108226 |
355108228 |
|
| 183 |
709326063 |
| old = b->cell[sq]; |
| 184 |
709326063 |
▶ | if (old != BD_EMPTY) b->key ^= bd_zobrist(old, sq); |
Branch: 1 branch
| Branch | True | False | Total |
| if (old != BD_EMPTY) b->key ^= bd_zobrist(old, sq); |
354219704 |
355106359 |
709326063 |
|
| 185 |
709326063 |
| b->cell[sq] = (unsigned char) piece; |
| 186 |
709326063 |
▶ | if (piece != BD_EMPTY) b->key ^= bd_zobrist(piece, sq); |
Branch: 1 branch
| Branch | True | False | Total |
| if (piece != BD_EMPTY) b->key ^= bd_zobrist(piece, sq); |
355108226 |
354217837 |
709326063 |
|
| 187 |
|
| } |
| 188 |
|
| |
| 189 |
354217837 |
| static void bd_lift(struct bd_board *b, int sq) |
| 190 |
|
| { |
| 191 |
354217837 |
| bd_put(b, sq, BD_EMPTY); |
| 192 |
354217837 |
| } |
| 193 |
|
| |
| 194 |
20762536256 |
| static int bd_at(const struct bd_board *b, int sq) |
| 195 |
|
| { |
| 196 |
20762536256 |
▶ | if (sq < 0 || sq >= BD_CELLS) return BD_BORDER; |
Branch: 2 branches
| Branch | True | False | Total |
| if (sq < 0 || sq >= BD_CELLS) return BD_BORDER; |
20762536254 |
2 |
20762536256 |
| if (sq < 0 || sq >= BD_CELLS) return BD_BORDER; |
3 |
20762536251 |
20762536254 |
|
| 197 |
20762536251 |
| return b->cell[sq]; |
| 198 |
|
| } |
| 199 |
|
| |
| 200 |
531979891 |
| static int bd_side(const struct bd_board *b) { return b->side; } |
| 201 |
|
| |
| 202 |
349503278 |
| static void bd_set_side(struct bd_board *b, int side) |
| 203 |
|
| { |
| 204 |
349503278 |
▶ | if (side != BD_ATTACKERS && side != BD_DEFENDERS) return; |
Branch: 2 branches
| Branch | True | False | Total |
| if (side != BD_ATTACKERS && side != BD_DEFENDERS) return; |
174886335 |
174616943 |
349503278 |
| if (side != BD_ATTACKERS && side != BD_DEFENDERS) return; |
1 |
174886334 |
174886335 |
|
| 205 |
349503277 |
▶ | if (b->side == side) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->side == side) return; |
31570 |
349471707 |
349503277 |
|
| 206 |
349471707 |
| b->side = side; |
| 207 |
349471707 |
| b->key ^= ZOB_SIDE; |
| 208 |
|
| } |
| 209 |
|
| |
| 210 |
53334330 |
| static int bd_count(const struct bd_board *b, int piece) |
| 211 |
|
| { |
| 212 |
53334330 |
| int sq, n = 0; |
| 213 |
4373415060 |
▶ | for (sq = 0; sq < BD_CELLS; sq++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (sq = 0; sq < BD_CELLS; sq++) |
4320080730 |
53334330 |
4373415060 |
|
| 214 |
4320080730 |
▶ | if (b->cell[sq] == piece && bd_on_board(sq)) n++; |
Branch: 2 branches
| Branch | True | False | Total |
| if (b->cell[sq] == piece && bd_on_board(sq)) n++; |
336159848 |
3983920882 |
4320080730 |
| if (b->cell[sq] == piece && bd_on_board(sq)) n++; |
336159848 |
0 |
336159848 |
|
| 215 |
53334330 |
| return n; |
| 216 |
|
| } |
| 217 |
|
| |
| 218 |
|
| /* -1 and not 0 for "no king": 0 is a cell of the ring, and a caller that walked |
| 219 |
|
| * from it would be walking the border. */ |
| 220 |
36142807 |
| static int bd_king_square(const struct bd_board *b) |
| 221 |
|
| { |
| 222 |
|
| int sq; |
| 223 |
1459857025 |
▶ | for (sq = 0; sq < BD_CELLS; sq++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (sq = 0; sq < BD_CELLS; sq++) |
1459857023 |
2 |
1459857025 |
|
| 224 |
1459857023 |
▶ | if (b->cell[sq] == BD_KING && bd_on_board(sq)) return sq; |
Branch: 2 branches
| Branch | True | False | Total |
| if (b->cell[sq] == BD_KING && bd_on_board(sq)) return sq; |
36142805 |
1423714218 |
1459857023 |
| if (b->cell[sq] == BD_KING && bd_on_board(sq)) return sq; |
36142805 |
0 |
36142805 |
|
| 225 |
2 |
| return -1; |
| 226 |
|
| } |
| 227 |
|
| |
| 228 |
106314482 |
| static unsigned long long bd_key(const struct bd_board *b) { return b->key; } |
| 229 |
|
| |
| 230 |
|
| /* The key from nothing, by walking the board. It must equal the maintained key |
| 231 |
|
| * after any sequence of put, lift and set_side, and a test holds it to that. */ |
| 232 |
156028 |
| static unsigned long long bd_key_full(const struct bd_board *b) |
| 233 |
|
| { |
| 234 |
156028 |
| unsigned long long k = 0; |
| 235 |
|
| int sq; |
| 236 |
12794296 |
▶ | for (sq = 0; sq < BD_CELLS; sq++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (sq = 0; sq < BD_CELLS; sq++) |
12638268 |
156028 |
12794296 |
|
| 237 |
12638268 |
▶ | if (bd_on_board(sq) && b->cell[sq] != BD_EMPTY) |
Branch: 2 branches
| Branch | True | False | Total |
| if (bd_on_board(sq) && b->cell[sq] != BD_EMPTY) |
7645372 |
4992896 |
12638268 |
| if (bd_on_board(sq) && b->cell[sq] != BD_EMPTY) |
3771944 |
3873428 |
7645372 |
|
| 238 |
3771944 |
| k ^= bd_zobrist(b->cell[sq], sq); |
| 239 |
156028 |
▶ | if (b->side == BD_DEFENDERS) k ^= bd_zobrist_side(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (b->side == BD_DEFENDERS) k ^= bd_zobrist_side(); |
69015 |
87013 |
156028 |
|
| 240 |
156028 |
| return k; |
| 241 |
|
| } |
| 242 |
|
| |
| 243 |
|
| /* The set-up: the king on the throne, a defender on each side of him, and the |
| 244 |
|
| * attackers two deep at the end of each arm of the cross. Attackers move first. |
| 245 |
|
| * |
| 246 |
|
| * 7 . . . A . . . |
| 247 |
|
| * 6 . . . A . . . |
| 248 |
|
| * 5 . . . D . . . |
| 249 |
|
| * 4 A A D K D A A |
| 250 |
|
| * 3 . . . D . . . |
| 251 |
|
| * 2 . . . A . . . |
| 252 |
|
| * 1 . . . A . . . |
| 253 |
|
| * a b c d e f g |
| 254 |
|
| */ |
| 255 |
1064 |
| static struct bd_board *bd_board_new(void) |
| 256 |
|
| { |
| 257 |
|
| static const int ARM[BD_SIZE] = { |
| 258 |
|
| BD_ATTACKER, BD_ATTACKER, BD_DEFENDER, BD_KING, |
| 259 |
|
| BD_DEFENDER, BD_ATTACKER, BD_ATTACKER |
| 260 |
|
| }; |
| 261 |
1064 |
| struct bd_board *b = bd_board_empty(); |
| 262 |
1064 |
| int mid = BD_SIZE / 2, i; |
| 263 |
1064 |
▶ | if (!b) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!b) return NULL; |
0 |
1064 |
1064 |
|
| 264 |
8512 |
▶ | for (i = 0; i < BD_SIZE; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < BD_SIZE; i++) { |
7448 |
1064 |
8512 |
|
| 265 |
7448 |
| bd_put(b, bd_square_of(i, mid), ARM[i]); |
| 266 |
7448 |
| bd_put(b, bd_square_of(mid, i), ARM[i]); |
| 267 |
|
| } |
| 268 |
1064 |
| return b; |
| 269 |
|
| } |
| 270 |
|
| |
| 271 |
1885322 |
| static struct bd_board *bd_board_copy(const struct bd_board *b) |
| 272 |
|
| { |
| 273 |
|
| struct bd_board *n; |
| 274 |
1885322 |
▶ | if (!b) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!b) return NULL; |
0 |
1885322 |
1885322 |
|
| 275 |
1885322 |
| n = (struct bd_board *) malloc(sizeof(struct bd_board)); |
| 276 |
1885322 |
▶ | if (!n) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) return NULL; |
0 |
1885322 |
1885322 |
|
| 277 |
1885322 |
| *n = *b; |
| 278 |
1885322 |
| BD_LIVE++; |
| 279 |
1885322 |
| return n; |
| 280 |
|
| } |
| 281 |
|
| |
| 282 |
1947353 |
| static void bd_board_drop(struct bd_board *b) |
| 283 |
|
| { |
| 284 |
1947353 |
▶ | if (!b) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!b) return; |
0 |
1947353 |
1947353 |
|
| 285 |
1947353 |
| BD_LIVE--; |
| 286 |
1947353 |
| free(b); |
| 287 |
|
| } |
| 288 |
|
| |
| 289 |
14 |
| static int bd_live(void) { return BD_LIVE; } |
| 290 |
|
| |
| 291 |
|
| /* ---- the position string ------------------------------------------------------ |
| 292 |
|
| * |
| 293 |
|
| * Seven rows, rank 7 first, a slash between rows, a digit for a run of empty |
| 294 |
|
| * squares, then one space and the side to move: |
| 295 |
|
| * |
| 296 |
|
| * 3a3/3a3/3d3/aadkdaa/3d3/3a3/3a3 a |
| 297 |
|
| * |
| 298 |
|
| * a is an attacker, d a defender, k the king; the side is a or d. |
| 299 |
|
| * |
| 300 |
|
| * THE SIDE IS REQUIRED. Defaulting it would turn a string with the field cut |
| 301 |
|
| * off into a position with a player to move, which is a different position |
| 302 |
|
| * from the one somebody meant. |
| 303 |
|
| */ |
| 304 |
|
| |
| 305 |
212716 |
| static int letter_piece(char c) |
| 306 |
|
| { |
| 307 |
212716 |
| switch (c) { |
| 308 |
106231 |
| case 'a': return BD_ATTACKER; |
| 309 |
81077 |
| case 'd': return BD_DEFENDER; |
| 310 |
25282 |
| case 'k': return BD_KING; |
| 311 |
126 |
| default: return -1; |
| 312 |
|
| } |
| 313 |
|
| } |
| 314 |
|
| |
| 315 |
4473754 |
| static char piece_letter(int piece) |
| 316 |
|
| { |
| 317 |
4473754 |
| switch (piece) { |
| 318 |
2072830 |
| case BD_ATTACKER: return 'a'; |
| 319 |
1967560 |
| case BD_DEFENDER: return 'd'; |
| 320 |
433364 |
| case BD_KING: return 'k'; |
| 321 |
0 |
| default: return '?'; |
| 322 |
|
| } |
| 323 |
|
| } |
| 324 |
|
| |
| 325 |
980 |
| static struct bd_board *refuse(struct bd_board *b, int *err, int code) |
| 326 |
|
| { |
| 327 |
980 |
▶ | if (err) *err = code; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) *err = code; |
980 |
0 |
980 |
|
| 328 |
980 |
| bd_board_drop(b); |
| 329 |
980 |
| return NULL; |
| 330 |
|
| } |
| 331 |
|
| |
| 332 |
13230 |
| static struct bd_board *bd_board_of_string(const char *str, int *err) |
| 333 |
|
| { |
| 334 |
|
| struct bd_board *b; |
| 335 |
13230 |
| int rank = BD_SIZE - 1, file = 0; |
| 336 |
13230 |
| const char *s = str; |
| 337 |
|
| size_t n; |
| 338 |
|
| |
| 339 |
13230 |
▶ | if (err) *err = BD_OK; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) *err = BD_OK; |
13230 |
0 |
13230 |
|
| 340 |
13230 |
▶ | if (!str) { if (err) *err = BD_POS_NULL; return NULL; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!str) { if (err) *err = BD_POS_NULL; return NULL; } |
0 |
13230 |
13230 |
| if (!str) { if (err) *err = BD_POS_NULL; return NULL; } |
0 |
0 |
0 |
|
| 341 |
13230 |
| n = strlen(str); |
| 342 |
13230 |
▶ | if (n == 0) { if (err) *err = BD_POS_NULL; return NULL; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (n == 0) { if (err) *err = BD_POS_NULL; return NULL; } |
4 |
13226 |
13230 |
| if (n == 0) { if (err) *err = BD_POS_NULL; return NULL; } |
4 |
0 |
4 |
|
| 343 |
13226 |
▶ | if (n >= BD_POS_MAX) { if (err) *err = BD_POS_LONG; return NULL; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (n >= BD_POS_MAX) { if (err) *err = BD_POS_LONG; return NULL; } |
1 |
13225 |
13226 |
| if (n >= BD_POS_MAX) { if (err) *err = BD_POS_LONG; return NULL; } |
1 |
0 |
1 |
|
| 344 |
|
| |
| 345 |
13225 |
| b = bd_board_empty(); |
| 346 |
13225 |
▶ | if (!b) { if (err) *err = BD_POS_NULL; return NULL; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!b) { if (err) *err = BD_POS_NULL; return NULL; } |
0 |
13225 |
13225 |
| if (!b) { if (err) *err = BD_POS_NULL; return NULL; } |
0 |
0 |
0 |
|
| 347 |
|
| |
| 348 |
468111 |
▶ | for (; *s && *s != ' '; s++) { |
Branch: 2 branches
| Branch | True | False | Total |
| for (; *s && *s != ' '; s++) { |
467901 |
210 |
468111 |
| for (; *s && *s != ' '; s++) { |
455390 |
12511 |
467901 |
|
| 349 |
455390 |
▶ | if (*s == '/') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s == '/') { |
77189 |
378201 |
455390 |
|
| 350 |
77189 |
▶ | if (file != BD_SIZE) return refuse(b, err, BD_POS_WIDTH); |
Branch: 1 branch
| Branch | True | False | Total |
| if (file != BD_SIZE) return refuse(b, err, BD_POS_WIDTH); |
186 |
77003 |
77189 |
|
| 351 |
77003 |
▶ | if (rank == 0) return refuse(b, err, BD_POS_ROWS); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rank == 0) return refuse(b, err, BD_POS_ROWS); |
2 |
77001 |
77003 |
|
| 352 |
77001 |
| rank--; file = 0; |
| 353 |
77001 |
| continue; |
| 354 |
|
| } |
| 355 |
378201 |
▶ | if (*s >= '1' && *s <= '7') { |
Branch: 2 branches
| Branch | True | False | Total |
| if (*s >= '1' && *s <= '7') { |
378140 |
61 |
378201 |
| if (*s >= '1' && *s <= '7') { |
165485 |
212655 |
378140 |
|
| 356 |
165485 |
| file += *s - '0'; |
| 357 |
165485 |
▶ | if (file > BD_SIZE) return refuse(b, err, BD_POS_WIDTH); |
Branch: 1 branch
| Branch | True | False | Total |
| if (file > BD_SIZE) return refuse(b, err, BD_POS_WIDTH); |
114 |
165371 |
165485 |
|
| 358 |
165371 |
| continue; |
| 359 |
|
| } |
| 360 |
|
| { |
| 361 |
212716 |
| int piece = letter_piece(*s); |
| 362 |
212716 |
▶ | if (piece < 0) return refuse(b, err, BD_POS_LETTER); |
Branch: 1 branch
| Branch | True | False | Total |
| if (piece < 0) return refuse(b, err, BD_POS_LETTER); |
126 |
212590 |
212716 |
|
| 363 |
212590 |
▶ | if (file >= BD_SIZE) return refuse(b, err, BD_POS_WIDTH); |
Branch: 1 branch
| Branch | True | False | Total |
| if (file >= BD_SIZE) return refuse(b, err, BD_POS_WIDTH); |
76 |
212514 |
212590 |
|
| 364 |
212514 |
| bd_put(b, bd_square_of(file, rank), piece); |
| 365 |
212514 |
| file++; |
| 366 |
|
| } |
| 367 |
|
| } |
| 368 |
12721 |
▶ | if (rank != 0) return refuse(b, err, BD_POS_ROWS); |
Branch: 1 branch
| Branch | True | False | Total |
| if (rank != 0) return refuse(b, err, BD_POS_ROWS); |
204 |
12517 |
12721 |
|
| 369 |
12517 |
▶ | if (file != BD_SIZE) return refuse(b, err, BD_POS_WIDTH); |
Branch: 1 branch
| Branch | True | False | Total |
| if (file != BD_SIZE) return refuse(b, err, BD_POS_WIDTH); |
53 |
12464 |
12517 |
|
| 370 |
|
| |
| 371 |
12464 |
▶ | if (*s != ' ') return refuse(b, err, BD_POS_SIDE); |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s != ' ') return refuse(b, err, BD_POS_SIDE); |
3 |
12461 |
12464 |
|
| 372 |
12461 |
| s++; |
| 373 |
12461 |
▶ | if (*s == 'a') bd_set_side(b, BD_ATTACKERS); |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s == 'a') bd_set_side(b, BD_ATTACKERS); |
10009 |
2452 |
12461 |
|
| 374 |
2452 |
▶ | else if (*s == 'd') bd_set_side(b, BD_DEFENDERS); |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*s == 'd') bd_set_side(b, BD_DEFENDERS); |
2434 |
18 |
2452 |
|
| 375 |
18 |
| else return refuse(b, err, BD_POS_SIDE); |
| 376 |
12443 |
| s++; |
| 377 |
12443 |
▶ | if (*s) return refuse(b, err, BD_POS_SIDE); |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s) return refuse(b, err, BD_POS_SIDE); |
198 |
12245 |
12443 |
|
| 378 |
|
| |
| 379 |
12245 |
| return b; |
| 380 |
|
| } |
| 381 |
|
| |
| 382 |
198541 |
| static int bd_to_string(const struct bd_board *b, char *buf, int len) |
| 383 |
|
| { |
| 384 |
198541 |
| int rank, file, i = 0, run; |
| 385 |
198541 |
▶ | if (!b || !buf || len < BD_POS_MAX) return -1; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!b || !buf || len < BD_POS_MAX) return -1; |
198541 |
0 |
198541 |
| if (!b || !buf || len < BD_POS_MAX) return -1; |
198541 |
0 |
198541 |
| if (!b || !buf || len < BD_POS_MAX) return -1; |
0 |
198541 |
198541 |
|
| 386 |
1588328 |
▶ | for (rank = BD_SIZE - 1; rank >= 0; rank--) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (rank = BD_SIZE - 1; rank >= 0; rank--) { |
1389787 |
198541 |
1588328 |
|
| 387 |
1389787 |
| run = 0; |
| 388 |
11118296 |
▶ | for (file = 0; file < BD_SIZE; file++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (file = 0; file < BD_SIZE; file++) { |
9728509 |
1389787 |
11118296 |
|
| 389 |
9728509 |
| int piece = b->cell[bd_square_of(file, rank)]; |
| 390 |
9728509 |
▶ | if (piece == BD_EMPTY) { run++; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (piece == BD_EMPTY) { run++; continue; } |
5254755 |
4473754 |
9728509 |
|
| 391 |
4473754 |
▶ | if (run) { buf[i++] = (char) ('0' + run); run = 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (run) { buf[i++] = (char) ('0' + run); run = 0; } |
1952432 |
2521322 |
4473754 |
|
| 392 |
4473754 |
| buf[i++] = piece_letter(piece); |
| 393 |
|
| } |
| 394 |
1389787 |
▶ | if (run) buf[i++] = (char) ('0' + run); |
Branch: 1 branch
| Branch | True | False | Total |
| if (run) buf[i++] = (char) ('0' + run); |
753106 |
636681 |
1389787 |
|
| 395 |
1389787 |
▶ | if (rank) buf[i++] = '/'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (rank) buf[i++] = '/'; |
1191246 |
198541 |
1389787 |
|
| 396 |
|
| } |
| 397 |
198541 |
| buf[i++] = ' '; |
| 398 |
198541 |
▶ | buf[i++] = (b->side == BD_DEFENDERS) ? 'd' : 'a'; |
Branch: 1 branch
| Branch | True | False | Total |
| buf[i++] = (b->side == BD_DEFENDERS) ? 'd' : 'a'; |
102628 |
95913 |
198541 |
|
| 399 |
198541 |
| buf[i] = '\0'; |
| 400 |
198541 |
| return i; |
| 401 |
|
| } |
| 402 |
|
| |
| 403 |
|
| /* ---- the table -------------------------------------------------------------- */ |
| 404 |
|
| |
| 405 |
|
| static struct bd_abi BD_TABLE; |
| 406 |
|
| static int BD_TABLE_READY = 0; |
| 407 |
|
| |
| 408 |
|
| /* bd_moves.c installs itself into the table here. The two files call into each |
| 409 |
|
| * other and it terminates: BD_TABLE_READY is set before the installer runs, and |
| 410 |
|
| * bd_moves.c only reaches bd_abi_table from inside a function the installer |
| 411 |
|
| * has already handed over. */ |
| 412 |
|
| void bd_moves_install(struct bd_abi *t); |
| 413 |
|
| void bd_rules_install(struct bd_abi *t); |
| 414 |
|
| void bd_search_install(struct bd_abi *t); |
| 415 |
|
| |
| 416 |
48854719184 |
| const struct bd_abi *bd_abi_table(void) |
| 417 |
|
| { |
| 418 |
48854719184 |
▶ | if (!BD_TABLE_READY) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (!BD_TABLE_READY) { |
59 |
48854719125 |
48854719184 |
|
| 419 |
59 |
| zob_init(); |
| 420 |
59 |
| BD_TABLE.abi_version = BD_ABI_VERSION; |
| 421 |
|
| |
| 422 |
59 |
| BD_TABLE.board_new = bd_board_new; |
| 423 |
59 |
| BD_TABLE.board_empty = bd_board_empty; |
| 424 |
59 |
| BD_TABLE.board_of_string = bd_board_of_string; |
| 425 |
59 |
| BD_TABLE.board_copy = bd_board_copy; |
| 426 |
59 |
| BD_TABLE.board_drop = bd_board_drop; |
| 427 |
59 |
| BD_TABLE.live = bd_live; |
| 428 |
|
| |
| 429 |
59 |
| BD_TABLE.at = bd_at; |
| 430 |
59 |
| BD_TABLE.put = bd_put; |
| 431 |
59 |
| BD_TABLE.lift = bd_lift; |
| 432 |
59 |
| BD_TABLE.side = bd_side; |
| 433 |
59 |
| BD_TABLE.set_side = bd_set_side; |
| 434 |
59 |
| BD_TABLE.count = bd_count; |
| 435 |
59 |
| BD_TABLE.king_square = bd_king_square; |
| 436 |
|
| |
| 437 |
59 |
| BD_TABLE.key = bd_key; |
| 438 |
59 |
| BD_TABLE.key_full = bd_key_full; |
| 439 |
59 |
| BD_TABLE.zobrist = bd_zobrist; |
| 440 |
59 |
| BD_TABLE.zobrist_side = bd_zobrist_side; |
| 441 |
59 |
| BD_TABLE.to_string = bd_to_string; |
| 442 |
|
| |
| 443 |
59 |
| BD_TABLE.stride = bd_stride; |
| 444 |
59 |
| BD_TABLE.square_of = bd_square_of; |
| 445 |
59 |
| BD_TABLE.file_of = bd_file_of; |
| 446 |
59 |
| BD_TABLE.rank_of = bd_rank_of; |
| 447 |
59 |
| BD_TABLE.on_board = bd_on_board; |
| 448 |
59 |
| BD_TABLE.is_throne = bd_is_throne; |
| 449 |
59 |
| BD_TABLE.is_corner = bd_is_corner; |
| 450 |
59 |
| BD_TABLE.beside_throne = bd_beside_throne; |
| 451 |
59 |
| BD_TABLE.side_of = bd_side_of; |
| 452 |
|
| |
| 453 |
59 |
| BD_TABLE_READY = 1; /* before the installer, or it recurses */ |
| 454 |
59 |
| bd_moves_install(&BD_TABLE); |
| 455 |
59 |
| bd_rules_install(&BD_TABLE); |
| 456 |
59 |
| bd_search_install(&BD_TABLE); |
| 457 |
|
| } |
| 458 |
48854719184 |
| return &BD_TABLE; |
| 459 |
|
| } |