| 1 |
|
| /* shg_engine.c - the shogi board, and nothing that judges a move. |
| 2 |
|
| * |
| 3 |
|
| * PERL-FREE by design: see include/shg_abi.h. This file compiles into the XS, |
| 4 |
|
| * into a plain program, or to wasm, and knows nothing about any of them. |
| 5 |
|
| * |
| 6 |
|
| * THIS FILE'S SCOPE. The mailbox, the square names, the step, the zone, the |
| 7 |
|
| * kinds and their promotion, the set, put and lift, the hands, the side to |
| 8 |
|
| * move, the move number, the zobrist key, and the SFEN in and out. |
| 9 |
|
| */ |
| 10 |
|
| |
| 11 |
|
| #include <stdlib.h> |
| 12 |
|
| #include <string.h> |
| 13 |
|
| #include "shg_internal.h" |
| 14 |
|
| |
| 15 |
|
| /* ---- the mailbox and the tables: see shg_internal.h ------------------------- */ |
| 16 |
|
| |
| 17 |
|
| int SHG_SQ_OF[SHG_SQUARES]; |
| 18 |
|
| int SHG_SQ_AT[SHG_BOX]; |
| 19 |
|
| static int FILE_OF[SHG_SQUARES]; |
| 20 |
|
| static int RANK_OF[SHG_SQUARES]; |
| 21 |
|
| static int ZONE[SHG_SIDES][SHG_SQUARES]; |
| 22 |
|
| static int LAST[SHG_SIDES][SHG_SQUARES]; |
| 23 |
|
| static int TABLES_READY = 0; |
| 24 |
|
| static int TABLES_OK = 0; |
| 25 |
|
| |
| 26 |
|
| /* ---- the kinds and the set ------------------------------------------------------ |
| 27 |
|
| * |
| 28 |
|
| * Indexed by kind. A promoted kind's letter is its base kind's, written after |
| 29 |
|
| * a '+'. SET is how many of each base kind a whole set has: nine pawns, two |
| 30 |
|
| * lances, two knights, two silvers, two golds, one bishop, one rook and one |
| 31 |
|
| * king a side. |
| 32 |
|
| */ |
| 33 |
|
| |
| 34 |
|
| static const char LETTER[SHG_KINDS] = { 0, 'P', 'L', 'N', 'S', 'G', 'B', 'R', 'K', |
| 35 |
|
| 'P', 'L', 'N', 'S', 'B', 'R' }; |
| 36 |
|
| static const int PROMOTED[SHG_KINDS] = { 0, 9, 10, 11, 12, 0, 13, 14, 0, 0, 0, 0, 0, 0, 0 }; |
| 37 |
|
| static const int UNPROMOTED[SHG_KINDS] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 6, 7 }; |
| 38 |
|
| static const int SET[SHG_KINDS] = { 0, 18, 4, 4, 4, 4, 2, 2, 2, 0, 0, 0, 0, 0, 0 }; |
| 39 |
|
| |
| 40 |
|
| int (*SHG_ZONE)[SHG_SQUARES] = ZONE; |
| 41 |
|
| int (*SHG_LAST)[SHG_SQUARES] = LAST; |
| 42 |
|
| const int *SHG_PROMOTED_OF = PROMOTED; |
| 43 |
|
| const int *SHG_UNPROMOTED_OF = UNPROMOTED; |
| 44 |
|
| const int *SHG_SET_OF = SET; |
| 45 |
|
| |
| 46 |
|
| /* The hands are written in this order, which U gives: rook, bishop, gold, |
| 47 |
|
| * silver, knight, lance, pawn. */ |
| 48 |
|
| static const int HAND_ORDER[SHG_HAND_KINDS - 1] = { SHG_ROOK, SHG_BISHOP, SHG_GOLD, |
| 49 |
|
| SHG_SILVER, SHG_KNIGHT, SHG_LANCE, SHG_PAWN }; |
| 50 |
|
| |
| 51 |
|
| /* The start of an even game, as SFEN reads it, rank a first. */ |
| 52 |
|
| static const char *START[SHG_RANKS] = { |
| 53 |
|
| "lnsgkgsnl", ".r.....b.", "ppppppppp", ".........", ".........", |
| 54 |
|
| ".........", "PPPPPPPPP", ".B.....R.", "LNSGKGSNL", |
| 55 |
|
| }; |
| 56 |
|
| |
| 57 |
159086 |
| static int kind_of_letter(int c) |
| 58 |
|
| { |
| 59 |
|
| int k; |
| 60 |
159086 |
▶ | if (c >= 'a' && c <= 'z') c -= 'a' - 'A'; |
Branch: 2 branches
| Branch | True | False | Total |
| if (c >= 'a' && c <= 'z') c -= 'a' - 'A'; |
58617 |
100469 |
159086 |
| if (c >= 'a' && c <= 'z') c -= 'a' - 'A'; |
58617 |
0 |
58617 |
|
| 61 |
696784 |
▶ | for (k = SHG_PAWN; k <= SHG_KING; k++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = SHG_PAWN; k <= SHG_KING; k++) |
661044 |
35740 |
696784 |
|
| 62 |
661044 |
▶ | if (LETTER[k] == c) return k; |
Branch: 1 branch
| Branch | True | False | Total |
| if (LETTER[k] == c) return k; |
123346 |
537698 |
661044 |
|
| 63 |
35740 |
| return 0; |
| 64 |
|
| } |
| 65 |
|
| |
| 66 |
3123917 |
| void shg_tables_init(void) |
| 67 |
|
| { |
| 68 |
3123917 |
| int sq, box, side, df, dr, k, n = 0, ok = 1; |
| 69 |
3123917 |
| int zone_n[SHG_SIDES] = { 0, 0 }; |
| 70 |
3123917 |
| int set_total = 0; |
| 71 |
|
| |
| 72 |
3123917 |
▶ | if (TABLES_READY) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (TABLES_READY) return; |
3123893 |
24 |
3123917 |
|
| 73 |
3456 |
▶ | for (box = 0; box < SHG_BOX; box++) SHG_SQ_AT[box] = -1; |
Branch: 1 branch
| Branch | True | False | Total |
| for (box = 0; box < SHG_BOX; box++) SHG_SQ_AT[box] = -1; |
3432 |
24 |
3456 |
|
| 74 |
|
| |
| 75 |
1968 |
▶ | for (sq = 0; sq < SHG_SQUARES; sq++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (sq = 0; sq < SHG_SQUARES; sq++) { |
1944 |
24 |
1968 |
|
| 76 |
1944 |
| int row = sq / SHG_FILES, col = sq % SHG_FILES; |
| 77 |
1944 |
| box = (row + SHG_MARGIN_ROWS) * SHG_WIDTH + (col + SHG_MARGIN_COLS); |
| 78 |
1944 |
| SHG_SQ_OF[sq] = box; |
| 79 |
1944 |
| SHG_SQ_AT[box] = sq; |
| 80 |
1944 |
| FILE_OF[sq] = SHG_FILES - col; |
| 81 |
1944 |
| RANK_OF[sq] = row + 1; |
| 82 |
1944 |
| ZONE[SHG_BLACK][sq] = RANK_OF[sq] <= SHG_ZONE_RANKS; |
| 83 |
1944 |
| ZONE[SHG_WHITE][sq] = RANK_OF[sq] > SHG_RANKS - SHG_ZONE_RANKS; |
| 84 |
1944 |
| LAST[SHG_BLACK][sq] = RANK_OF[sq] - 1; |
| 85 |
1944 |
| LAST[SHG_WHITE][sq] = SHG_RANKS - RANK_OF[sq]; |
| 86 |
5832 |
▶ | for (side = 0; side < SHG_SIDES; side++) zone_n[side] += ZONE[side][sq]; |
Branch: 1 branch
| Branch | True | False | Total |
| for (side = 0; side < SHG_SIDES; side++) zone_n[side] += ZONE[side][sq]; |
3888 |
1944 |
5832 |
|
| 87 |
1944 |
| n++; |
| 88 |
|
| } |
| 89 |
24 |
▶ | if (n != SHG_SQUARES) ok = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n != SHG_SQUARES) ok = 0; |
0 |
24 |
24 |
|
| 90 |
24 |
▶ | if (zone_n[SHG_BLACK] != 27 || zone_n[SHG_WHITE] != 27) ok = 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (zone_n[SHG_BLACK] != 27 || zone_n[SHG_WHITE] != 27) ok = 0; |
24 |
0 |
24 |
| if (zone_n[SHG_BLACK] != 27 || zone_n[SHG_WHITE] != 27) ok = 0; |
0 |
24 |
24 |
|
| 91 |
|
| |
| 92 |
|
| /* Every step the mailbox serves, from every square, against the arithmetic |
| 93 |
|
| * on files and ranks. A margin too narrow, or an offset with the wrong |
| 94 |
|
| * sign, cannot pass this. */ |
| 95 |
1968 |
▶ | for (sq = 0; sq < SHG_SQUARES; sq++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (sq = 0; sq < SHG_SQUARES; sq++) |
1944 |
24 |
1968 |
|
| 96 |
7776 |
▶ | for (df = -1; df <= 1; df++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (df = -1; df <= 1; df++) |
5832 |
1944 |
7776 |
|
| 97 |
34992 |
▶ | for (dr = -2; dr <= 2; dr++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (dr = -2; dr <= 2; dr++) { |
29160 |
5832 |
34992 |
|
| 98 |
29160 |
| int f = FILE_OF[sq] + df, r = RANK_OF[sq] + dr; |
| 99 |
28080 |
▶ | int want = (f >= 1 && f <= SHG_FILES && r >= 1 && r <= SHG_RANKS) |
Branch: 3 branches
| Branch | True | False | Total |
| int want = (f >= 1 && f <= SHG_FILES && r >= 1 && r <= SHG_RANKS) |
27000 |
1080 |
28080 |
| int want = (f >= 1 && f <= SHG_FILES && r >= 1 && r <= SHG_RANKS) |
25200 |
1800 |
27000 |
| int want = (f >= 1 && f <= SHG_FILES && r >= 1 && r <= SHG_RANKS) |
23400 |
1800 |
25200 |
|
| 100 |
57240 |
▶ | ? (r - 1) * SHG_FILES + (SHG_FILES - f) : -1; |
Branch: 1 branch
| Branch | True | False | Total |
| ? (r - 1) * SHG_FILES + (SHG_FILES - f) : -1; |
28080 |
1080 |
29160 |
|
| 101 |
29160 |
| int at = SHG_SQ_OF[sq] + dr * SHG_WIDTH - df; |
| 102 |
29160 |
▶ | if (at < 0 || at >= SHG_BOX || SHG_SQ_AT[at] != want) ok = 0; |
Branch: 3 branches
| Branch | True | False | Total |
| if (at < 0 || at >= SHG_BOX || SHG_SQ_AT[at] != want) ok = 0; |
29160 |
0 |
29160 |
| if (at < 0 || at >= SHG_BOX || SHG_SQ_AT[at] != want) ok = 0; |
29160 |
0 |
29160 |
| if (at < 0 || at >= SHG_BOX || SHG_SQ_AT[at] != want) ok = 0; |
0 |
29160 |
29160 |
|
| 103 |
|
| } |
| 104 |
|
| |
| 105 |
360 |
▶ | for (k = SHG_PAWN; k <= SHG_DRAGON; k++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (k = SHG_PAWN; k <= SHG_DRAGON; k++) { |
336 |
24 |
360 |
|
| 106 |
336 |
| set_total += SET[k]; |
| 107 |
336 |
▶ | if (UNPROMOTED[k] < SHG_PAWN || UNPROMOTED[k] > SHG_KING) ok = 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (UNPROMOTED[k] < SHG_PAWN || UNPROMOTED[k] > SHG_KING) ok = 0; |
336 |
0 |
336 |
| if (UNPROMOTED[k] < SHG_PAWN || UNPROMOTED[k] > SHG_KING) ok = 0; |
0 |
336 |
336 |
|
| 108 |
336 |
▶ | if (PROMOTED[k] && UNPROMOTED[PROMOTED[k]] != k) ok = 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (PROMOTED[k] && UNPROMOTED[PROMOTED[k]] != k) ok = 0; |
144 |
192 |
336 |
| if (PROMOTED[k] && UNPROMOTED[PROMOTED[k]] != k) ok = 0; |
0 |
144 |
144 |
|
| 109 |
|
| } |
| 110 |
24 |
▶ | if (set_total != SHG_SET_PIECES || SET[SHG_PAWN] != SHG_SET_PAWNS) ok = 0; |
Branch: 2 branches
| Branch | True | False | Total |
| if (set_total != SHG_SET_PIECES || SET[SHG_PAWN] != SHG_SET_PAWNS) ok = 0; |
24 |
0 |
24 |
| if (set_total != SHG_SET_PIECES || SET[SHG_PAWN] != SHG_SET_PAWNS) ok = 0; |
0 |
24 |
24 |
|
| 111 |
|
| |
| 112 |
24 |
▶ | if (!shg_move_tables_init()) ok = 0; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!shg_move_tables_init()) ok = 0; |
0 |
24 |
24 |
|
| 113 |
|
| |
| 114 |
24 |
| TABLES_OK = ok; |
| 115 |
24 |
| TABLES_READY = 1; |
| 116 |
|
| } |
| 117 |
|
| |
| 118 |
1 |
| static int shg_tables_ok(void) { shg_tables_init(); return TABLES_OK; } |
| 119 |
|
| |
| 120 |
321971 |
| static int shg_sq_of(int file, int rank) |
| 121 |
|
| { |
| 122 |
321971 |
▶ | if (file < 1 || file > SHG_FILES || rank < 1 || rank > SHG_RANKS) return -1; |
Branch: 4 branches
| Branch | True | False | Total |
| if (file < 1 || file > SHG_FILES || rank < 1 || rank > SHG_RANKS) return -1; |
321970 |
1 |
321971 |
| if (file < 1 || file > SHG_FILES || rank < 1 || rank > SHG_RANKS) return -1; |
321969 |
1 |
321970 |
| if (file < 1 || file > SHG_FILES || rank < 1 || rank > SHG_RANKS) return -1; |
321968 |
1 |
321969 |
| if (file < 1 || file > SHG_FILES || rank < 1 || rank > SHG_RANKS) return -1; |
1 |
321967 |
321968 |
|
| 123 |
321967 |
| return (rank - 1) * SHG_FILES + (SHG_FILES - file); |
| 124 |
|
| } |
| 125 |
|
| |
| 126 |
1557604 |
▶ | static int shg_file_of(int sq) { shg_tables_init(); return SHG_IS_SQ(sq) ? FILE_OF[sq] : 0; } |
Branch: 2 branches
| Branch | True | False | Total |
| static int shg_file_of(int sq) { shg_tables_init(); return SHG_IS_SQ(sq) ? FILE_OF[sq] : 0; } |
1557604 |
0 |
1557604 |
| static int shg_file_of(int sq) { shg_tables_init(); return SHG_IS_SQ(sq) ? FILE_OF[sq] : 0; } |
1557604 |
0 |
1557604 |
|
| 127 |
1557685 |
▶ | static int shg_rank_of(int sq) { shg_tables_init(); return SHG_IS_SQ(sq) ? RANK_OF[sq] : 0; } |
Branch: 2 branches
| Branch | True | False | Total |
| static int shg_rank_of(int sq) { shg_tables_init(); return SHG_IS_SQ(sq) ? RANK_OF[sq] : 0; } |
1557685 |
0 |
1557685 |
| static int shg_rank_of(int sq) { shg_tables_init(); return SHG_IS_SQ(sq) ? RANK_OF[sq] : 0; } |
1557685 |
0 |
1557685 |
|
| 128 |
|
| |
| 129 |
1225 |
| static int shg_step(int sq, int dfile, int drank) |
| 130 |
|
| { |
| 131 |
1225 |
| shg_tables_init(); |
| 132 |
1225 |
▶ | if (!SHG_IS_SQ(sq) || dfile < -1 || dfile > 1 || drank < -2 || drank > 2) return -1; |
Branch: 6 branches
| Branch | True | False | Total |
| if (!SHG_IS_SQ(sq) || dfile < -1 || dfile > 1 || drank < -2 || drank > 2) return -1; |
1225 |
0 |
1225 |
| if (!SHG_IS_SQ(sq) || dfile < -1 || dfile > 1 || drank < -2 || drank > 2) return -1; |
1225 |
0 |
1225 |
| if (!SHG_IS_SQ(sq) || dfile < -1 || dfile > 1 || drank < -2 || drank > 2) return -1; |
1224 |
1 |
1225 |
| if (!SHG_IS_SQ(sq) || dfile < -1 || dfile > 1 || drank < -2 || drank > 2) return -1; |
1222 |
2 |
1224 |
| if (!SHG_IS_SQ(sq) || dfile < -1 || dfile > 1 || drank < -2 || drank > 2) return -1; |
1221 |
1 |
1222 |
| if (!SHG_IS_SQ(sq) || dfile < -1 || dfile > 1 || drank < -2 || drank > 2) return -1; |
1 |
1220 |
1221 |
|
| 133 |
1220 |
| return SHG_SQ_AT[SHG_SQ_OF[sq] + drank * SHG_WIDTH - dfile]; |
| 134 |
|
| } |
| 135 |
|
| |
| 136 |
324 |
| static int shg_in_zone(int side, int sq) |
| 137 |
|
| { |
| 138 |
324 |
| shg_tables_init(); |
| 139 |
324 |
▶ | return SHG_IS_SIDE(side) && SHG_IS_SQ(sq) ? ZONE[side][sq] : 0; |
Branch: 4 branches
| Branch | True | False | Total |
| return SHG_IS_SIDE(side) && SHG_IS_SQ(sq) ? ZONE[side][sq] : 0; |
162 |
162 |
324 |
| return SHG_IS_SIDE(side) && SHG_IS_SQ(sq) ? ZONE[side][sq] : 0; |
162 |
0 |
162 |
| return SHG_IS_SIDE(side) && SHG_IS_SQ(sq) ? ZONE[side][sq] : 0; |
324 |
0 |
324 |
| return SHG_IS_SIDE(side) && SHG_IS_SQ(sq) ? ZONE[side][sq] : 0; |
324 |
0 |
324 |
|
| 140 |
|
| } |
| 141 |
|
| |
| 142 |
164 |
| static int shg_last_distance(int side, int sq) |
| 143 |
|
| { |
| 144 |
164 |
| shg_tables_init(); |
| 145 |
164 |
▶ | return SHG_IS_SIDE(side) && SHG_IS_SQ(sq) ? LAST[side][sq] : -1; |
Branch: 4 branches
| Branch | True | False | Total |
| return SHG_IS_SIDE(side) && SHG_IS_SQ(sq) ? LAST[side][sq] : -1; |
82 |
82 |
164 |
| return SHG_IS_SIDE(side) && SHG_IS_SQ(sq) ? LAST[side][sq] : -1; |
82 |
0 |
82 |
| return SHG_IS_SIDE(side) && SHG_IS_SQ(sq) ? LAST[side][sq] : -1; |
164 |
0 |
164 |
| return SHG_IS_SIDE(side) && SHG_IS_SQ(sq) ? LAST[side][sq] : -1; |
164 |
0 |
164 |
|
| 146 |
|
| } |
| 147 |
|
| |
| 148 |
2029 |
▶ | static int shg_promoted(int kind) { return SHG_IS_KIND(kind) ? PROMOTED[kind] : 0; } |
Branch: 2 branches
| Branch | True | False | Total |
| static int shg_promoted(int kind) { return SHG_IS_KIND(kind) ? PROMOTED[kind] : 0; } |
2028 |
1 |
2029 |
| static int shg_promoted(int kind) { return SHG_IS_KIND(kind) ? PROMOTED[kind] : 0; } |
2027 |
1 |
2028 |
|
| 149 |
615 |
▶ | static int shg_unpromoted(int kind) { return SHG_IS_KIND(kind) ? UNPROMOTED[kind] : 0; } |
Branch: 2 branches
| Branch | True | False | Total |
| static int shg_unpromoted(int kind) { return SHG_IS_KIND(kind) ? UNPROMOTED[kind] : 0; } |
614 |
1 |
615 |
| static int shg_unpromoted(int kind) { return SHG_IS_KIND(kind) ? UNPROMOTED[kind] : 0; } |
613 |
1 |
614 |
|
| 150 |
30 |
▶ | static int shg_set_count(int kind) { return SHG_IS_KIND(kind) ? SET[kind] : 0; } |
Branch: 2 branches
| Branch | True | False | Total |
| static int shg_set_count(int kind) { return SHG_IS_KIND(kind) ? SET[kind] : 0; } |
30 |
0 |
30 |
| static int shg_set_count(int kind) { return SHG_IS_KIND(kind) ? SET[kind] : 0; } |
30 |
0 |
30 |
|
| 151 |
|
| |
| 152 |
|
| /* ---- the zobrist table ----------------------------------------------------- |
| 153 |
|
| * |
| 154 |
|
| * DERIVED, NOT COMMITTED. A fixed function of a fixed seed is the same in every |
| 155 |
|
| * process and on every machine, and cannot suffer the transcription error a |
| 156 |
|
| * hand-pasted array can. splitmix64 is published and has no weak seeds. |
| 157 |
|
| * |
| 158 |
|
| * A HAND IS HASHED BY ITS COUNT, one number per (side, kind, count), so adding |
| 159 |
|
| * a piece to a hand XORs out the old count's number and XORs in the new one's. |
| 160 |
|
| * A count of 0 is 0, so an empty hand adds nothing. |
| 161 |
|
| * |
| 162 |
|
| * CHANGING THIS GENERATOR, ITS SEED OR THE ORDER THE TABLE IS FILLED IN |
| 163 |
|
| * CHANGES EVERY KEY. t/02-position.t pins values so that is a red test. |
| 164 |
|
| */ |
| 165 |
|
| |
| 166 |
|
| unsigned long long SHG_ZOB[SHG_SIDES][SHG_KINDS][SHG_SQUARES]; |
| 167 |
|
| unsigned long long SHG_ZOB_HAND[SHG_SIDES][SHG_HAND_KINDS][SHG_SET_PAWNS + 1]; |
| 168 |
|
| unsigned long long SHG_ZOB_MOVE; |
| 169 |
|
| static int ZOB_READY = 0; |
| 170 |
|
| |
| 171 |
60504 |
| static unsigned long long splitmix64(unsigned long long *state) |
| 172 |
|
| { |
| 173 |
|
| unsigned long long z; |
| 174 |
60504 |
| *state += 0x9E3779B97F4A7C15ULL; |
| 175 |
60504 |
| z = *state; |
| 176 |
60504 |
| z = (z ^ (z >> 30)) * 0xBF58476D1CE4E5B9ULL; |
| 177 |
60504 |
| z = (z ^ (z >> 27)) * 0x94D049BB133111EBULL; |
| 178 |
60504 |
| return z ^ (z >> 31); |
| 179 |
|
| } |
| 180 |
|
| |
| 181 |
7377 |
| static void zob_init(void) |
| 182 |
|
| { |
| 183 |
7377 |
| unsigned long long s = 0x73686F6769303031ULL; /* "shogi001" */ |
| 184 |
|
| int side, kind, sq, n; |
| 185 |
7377 |
▶ | if (ZOB_READY) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (ZOB_READY) return; |
7353 |
24 |
7377 |
|
| 186 |
72 |
▶ | for (side = 0; side < SHG_SIDES; side++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (side = 0; side < SHG_SIDES; side++) |
48 |
24 |
72 |
|
| 187 |
768 |
▶ | for (kind = 0; kind < SHG_KINDS; kind++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (kind = 0; kind < SHG_KINDS; kind++) |
720 |
48 |
768 |
|
| 188 |
59040 |
▶ | for (sq = 0; sq < SHG_SQUARES; sq++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (sq = 0; sq < SHG_SQUARES; sq++) |
58320 |
720 |
59040 |
|
| 189 |
58320 |
▶ | SHG_ZOB[side][kind][sq] = kind ? splitmix64(&s) : 0; |
Branch: 1 branch
| Branch | True | False | Total |
| SHG_ZOB[side][kind][sq] = kind ? splitmix64(&s) : 0; |
54432 |
3888 |
58320 |
|
| 190 |
72 |
▶ | for (side = 0; side < SHG_SIDES; side++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (side = 0; side < SHG_SIDES; side++) |
48 |
24 |
72 |
|
| 191 |
432 |
▶ | for (kind = 0; kind < SHG_HAND_KINDS; kind++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (kind = 0; kind < SHG_HAND_KINDS; kind++) |
384 |
48 |
432 |
|
| 192 |
7680 |
▶ | for (n = 0; n <= SHG_SET_PAWNS; n++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (n = 0; n <= SHG_SET_PAWNS; n++) |
7296 |
384 |
7680 |
|
| 193 |
7296 |
▶ | SHG_ZOB_HAND[side][kind][n] = kind && n ? splitmix64(&s) : 0; |
Branch: 2 branches
| Branch | True | False | Total |
| SHG_ZOB_HAND[side][kind][n] = kind && n ? splitmix64(&s) : 0; |
6384 |
912 |
7296 |
| SHG_ZOB_HAND[side][kind][n] = kind && n ? splitmix64(&s) : 0; |
6048 |
336 |
6384 |
|
| 194 |
24 |
| SHG_ZOB_MOVE = splitmix64(&s); |
| 195 |
24 |
| ZOB_READY = 1; |
| 196 |
|
| } |
| 197 |
|
| |
| 198 |
2419 |
| static unsigned long long shg_zobrist(int side, int kind, int sq) |
| 199 |
|
| { |
| 200 |
2419 |
| zob_init(); |
| 201 |
2419 |
▶ | if (!SHG_IS_SIDE(side) || !SHG_IS_KIND(kind) || !SHG_IS_SQ(sq)) return 0; |
Branch: 6 branches
| Branch | True | False | Total |
| if (!SHG_IS_SIDE(side) || !SHG_IS_KIND(kind) || !SHG_IS_SQ(sq)) return 0; |
1189 |
1230 |
2419 |
| if (!SHG_IS_SIDE(side) || !SHG_IS_KIND(kind) || !SHG_IS_SQ(sq)) return 0; |
1188 |
1 |
1189 |
| if (!SHG_IS_SIDE(side) || !SHG_IS_KIND(kind) || !SHG_IS_SQ(sq)) return 0; |
2418 |
0 |
2418 |
| if (!SHG_IS_SIDE(side) || !SHG_IS_KIND(kind) || !SHG_IS_SQ(sq)) return 0; |
2418 |
0 |
2418 |
| if (!SHG_IS_SIDE(side) || !SHG_IS_KIND(kind) || !SHG_IS_SQ(sq)) return 0; |
2418 |
0 |
2418 |
| if (!SHG_IS_SIDE(side) || !SHG_IS_KIND(kind) || !SHG_IS_SQ(sq)) return 0; |
0 |
2418 |
2418 |
|
| 202 |
2418 |
| return SHG_ZOB[side][kind][sq]; |
| 203 |
|
| } |
| 204 |
|
| |
| 205 |
205 |
| static unsigned long long shg_zobrist_hand(int side, int kind, int count) |
| 206 |
|
| { |
| 207 |
205 |
| zob_init(); |
| 208 |
205 |
▶ | if (!SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return 0; |
Branch: 6 branches
| Branch | True | False | Total |
| if (!SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return 0; |
101 |
104 |
205 |
| if (!SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return 0; |
101 |
0 |
101 |
| if (!SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return 0; |
205 |
0 |
205 |
| if (!SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return 0; |
205 |
0 |
205 |
| if (!SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return 0; |
205 |
0 |
205 |
| if (!SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return 0; |
1 |
204 |
205 |
|
| 209 |
204 |
| return SHG_ZOB_HAND[side][kind][count]; |
| 210 |
|
| } |
| 211 |
|
| |
| 212 |
2 |
| static unsigned long long shg_zobrist_move(void) { zob_init(); return SHG_ZOB_MOVE; } |
| 213 |
|
| |
| 214 |
|
| /* ---- a board --------------------------------------------------------------- */ |
| 215 |
|
| |
| 216 |
|
| /* Boards handed out and not yet dropped. It exists so a test can show that a |
| 217 |
|
| * destructor ran exactly once; it is a plain int and is not synchronised. */ |
| 218 |
|
| static int SHG_LIVE = 0; |
| 219 |
|
| |
| 220 |
4727 |
| static struct shg_board *board_alloc(void) |
| 221 |
|
| { |
| 222 |
|
| struct shg_board *b; |
| 223 |
|
| int sq; |
| 224 |
4727 |
| shg_tables_init(); |
| 225 |
4727 |
| zob_init(); |
| 226 |
4727 |
▶ | if (!TABLES_OK) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!TABLES_OK) return NULL; |
0 |
4727 |
4727 |
|
| 227 |
4727 |
| b = (struct shg_board *) malloc(sizeof(struct shg_board)); |
| 228 |
4727 |
▶ | if (!b) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!b) return NULL; |
0 |
4727 |
4727 |
|
| 229 |
4727 |
| memset(b->cell, SHG_OFF, sizeof b->cell); |
| 230 |
387614 |
▶ | for (sq = 0; sq < SHG_SQUARES; sq++) b->cell[SHG_SQ_OF[sq]] = SHG_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| for (sq = 0; sq < SHG_SQUARES; sq++) b->cell[SHG_SQ_OF[sq]] = SHG_EMPTY; |
382887 |
4727 |
387614 |
|
| 231 |
4727 |
| memset(b->hand, 0, sizeof b->hand); |
| 232 |
4727 |
| b->to_move = SHG_BLACK; |
| 233 |
4727 |
| b->move_number = 1; |
| 234 |
4727 |
| b->king[SHG_BLACK] = -1; |
| 235 |
4727 |
| b->king[SHG_WHITE] = -1; |
| 236 |
4727 |
| b->key = 0; |
| 237 |
4727 |
| SHG_LIVE++; |
| 238 |
4727 |
| return b; |
| 239 |
|
| } |
| 240 |
|
| |
| 241 |
21811746 |
| static int is_piece(int piece) |
| 242 |
|
| { |
| 243 |
21811746 |
▶ | return piece >= 0 && (piece & ~0x1F) == 0 && SHG_IS_KIND(SHG_KIND_OF(piece)); |
Branch: 4 branches
| Branch | True | False | Total |
| return piece >= 0 && (piece & ~0x1F) == 0 && SHG_IS_KIND(SHG_KIND_OF(piece)); |
21811746 |
0 |
21811746 |
| return piece >= 0 && (piece & ~0x1F) == 0 && SHG_IS_KIND(SHG_KIND_OF(piece)); |
21811746 |
0 |
21811746 |
| return piece >= 0 && (piece & ~0x1F) == 0 && SHG_IS_KIND(SHG_KIND_OF(piece)); |
21811746 |
0 |
21811746 |
| return piece >= 0 && (piece & ~0x1F) == 0 && SHG_IS_KIND(SHG_KIND_OF(piece)); |
21811746 |
0 |
21811746 |
|
| 244 |
|
| } |
| 245 |
|
| |
| 246 |
74168 |
| static int shg_at(const struct shg_board *b, int sq) |
| 247 |
|
| { |
| 248 |
74168 |
▶ | if (!b || !SHG_IS_SQ(sq)) return SHG_OFF; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!b || !SHG_IS_SQ(sq)) return SHG_OFF; |
74168 |
0 |
74168 |
| if (!b || !SHG_IS_SQ(sq)) return SHG_OFF; |
74168 |
0 |
74168 |
| if (!b || !SHG_IS_SQ(sq)) return SHG_OFF; |
0 |
74168 |
74168 |
|
| 249 |
74168 |
| return b->cell[SHG_SQ_OF[sq]]; |
| 250 |
|
| } |
| 251 |
|
| |
| 252 |
43242142 |
| void shg_lift(struct shg_board *b, int sq) |
| 253 |
|
| { |
| 254 |
|
| int box, old, side, kind; |
| 255 |
43242142 |
▶ | if (!b || !SHG_IS_SQ(sq)) return; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!b || !SHG_IS_SQ(sq)) return; |
43242142 |
0 |
43242142 |
| if (!b || !SHG_IS_SQ(sq)) return; |
43242142 |
0 |
43242142 |
| if (!b || !SHG_IS_SQ(sq)) return; |
0 |
43242142 |
43242142 |
|
| 256 |
43242142 |
| box = SHG_SQ_OF[sq]; |
| 257 |
43242142 |
| old = b->cell[box]; |
| 258 |
43242142 |
▶ | if (old == SHG_EMPTY) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (old == SHG_EMPTY) return; |
21811744 |
21430398 |
43242142 |
|
| 259 |
21430398 |
| side = SHG_SIDE_OF(old); |
| 260 |
21430398 |
| kind = SHG_KIND_OF(old); |
| 261 |
21430398 |
| b->key ^= SHG_ZOB[side][kind][sq]; |
| 262 |
21430398 |
▶ | if (kind == SHG_KING && b->king[side] == sq) b->king[side] = -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (kind == SHG_KING && b->king[side] == sq) b->king[side] = -1; |
2430188 |
19000210 |
21430398 |
| if (kind == SHG_KING && b->king[side] == sq) b->king[side] = -1; |
2430188 |
0 |
2430188 |
|
| 263 |
21430398 |
| b->cell[box] = SHG_EMPTY; |
| 264 |
|
| } |
| 265 |
|
| |
| 266 |
21811746 |
| void shg_put(struct shg_board *b, int sq, int piece) |
| 267 |
|
| { |
| 268 |
|
| int side, kind; |
| 269 |
21811746 |
▶ | if (!b || !SHG_IS_SQ(sq) || !is_piece(piece)) return; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!b || !SHG_IS_SQ(sq) || !is_piece(piece)) return; |
21811746 |
0 |
21811746 |
| if (!b || !SHG_IS_SQ(sq) || !is_piece(piece)) return; |
21811746 |
0 |
21811746 |
| if (!b || !SHG_IS_SQ(sq) || !is_piece(piece)) return; |
21811746 |
0 |
21811746 |
| if (!b || !SHG_IS_SQ(sq) || !is_piece(piece)) return; |
0 |
21811746 |
21811746 |
|
| 270 |
21811746 |
| shg_lift(b, sq); |
| 271 |
21811746 |
| side = SHG_SIDE_OF(piece); |
| 272 |
21811746 |
| kind = SHG_KIND_OF(piece); |
| 273 |
21811746 |
| b->cell[SHG_SQ_OF[sq]] = (unsigned char) piece; |
| 274 |
21811746 |
| b->key ^= SHG_ZOB[side][kind][sq]; |
| 275 |
21811746 |
▶ | if (kind == SHG_KING) b->king[side] = sq; |
Branch: 1 branch
| Branch | True | False | Total |
| if (kind == SHG_KING) b->king[side] = sq; |
2433907 |
19377839 |
21811746 |
|
| 276 |
|
| } |
| 277 |
|
| |
| 278 |
12118 |
| static int shg_hand(const struct shg_board *b, int side, int kind) |
| 279 |
|
| { |
| 280 |
12118 |
▶ | if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind)) return 0; |
Branch: 5 branches
| Branch | True | False | Total |
| if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind)) return 0; |
12118 |
0 |
12118 |
| if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind)) return 0; |
6056 |
6062 |
12118 |
| if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind)) return 0; |
6056 |
0 |
6056 |
| if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind)) return 0; |
12118 |
0 |
12118 |
| if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind)) return 0; |
0 |
12118 |
12118 |
|
| 281 |
12118 |
| return b->hand[side][kind]; |
| 282 |
|
| } |
| 283 |
|
| |
| 284 |
5191610 |
| void shg_set_hand(struct shg_board *b, int side, int kind, int count) |
| 285 |
|
| { |
| 286 |
5191610 |
▶ | if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return; |
Branch: 7 branches
| Branch | True | False | Total |
| if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return; |
5191610 |
0 |
5191610 |
| if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return; |
1795186 |
3396424 |
5191610 |
| if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return; |
1795186 |
0 |
1795186 |
| if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return; |
5191610 |
0 |
5191610 |
| if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return; |
5191610 |
0 |
5191610 |
| if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return; |
5191610 |
0 |
5191610 |
| if (!b || !SHG_IS_SIDE(side) || !SHG_IS_HAND(kind) || count < 0 || count > SET[kind]) return; |
2 |
5191608 |
5191610 |
|
| 287 |
5191608 |
| b->key ^= SHG_ZOB_HAND[side][kind][b->hand[side][kind]] ^ SHG_ZOB_HAND[side][kind][count]; |
| 288 |
5191608 |
| b->hand[side][kind] = (unsigned char) count; |
| 289 |
|
| } |
| 290 |
|
| |
| 291 |
2344 |
| static struct shg_board *shg_board_empty(void) { return board_alloc(); } |
| 292 |
|
| |
| 293 |
271 |
| static struct shg_board *shg_board_new(void) |
| 294 |
|
| { |
| 295 |
271 |
| struct shg_board *b = board_alloc(); |
| 296 |
271 |
| int row, col, c, placed = 0; |
| 297 |
271 |
▶ | if (!b) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!b) return NULL; |
0 |
271 |
271 |
|
| 298 |
2710 |
▶ | for (row = 0; row < SHG_RANKS; row++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (row = 0; row < SHG_RANKS; row++) |
2439 |
271 |
2710 |
|
| 299 |
24390 |
▶ | for (col = 0; col < SHG_FILES; col++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (col = 0; col < SHG_FILES; col++) { |
21951 |
2439 |
24390 |
|
| 300 |
21951 |
| c = START[row][col]; |
| 301 |
21951 |
▶ | if (c == '.') continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == '.') continue; |
11111 |
10840 |
21951 |
|
| 302 |
10840 |
| shg_put(b, row * SHG_FILES + col, |
| 303 |
10840 |
▶ | SHG_PIECE(c >= 'a' ? SHG_WHITE : SHG_BLACK, kind_of_letter(c))); |
Branch: 1 branch
| Branch | True | False | Total |
| SHG_PIECE(c >= 'a' ? SHG_WHITE : SHG_BLACK, kind_of_letter(c))); |
5420 |
5420 |
10840 |
|
| 304 |
10840 |
| placed++; |
| 305 |
|
| } |
| 306 |
|
| /* The constructor counts what it built. */ |
| 307 |
271 |
▶ | if (placed != SHG_SET_PIECES || b->king[SHG_BLACK] < 0 || b->king[SHG_WHITE] < 0) { |
Branch: 3 branches
| Branch | True | False | Total |
| if (placed != SHG_SET_PIECES || b->king[SHG_BLACK] < 0 || b->king[SHG_WHITE] < 0) { |
271 |
0 |
271 |
| if (placed != SHG_SET_PIECES || b->king[SHG_BLACK] < 0 || b->king[SHG_WHITE] < 0) { |
271 |
0 |
271 |
| if (placed != SHG_SET_PIECES || b->king[SHG_BLACK] < 0 || b->king[SHG_WHITE] < 0) { |
0 |
271 |
271 |
|
| 308 |
0 |
| free(b); |
| 309 |
0 |
| SHG_LIVE--; |
| 310 |
0 |
| return NULL; |
| 311 |
|
| } |
| 312 |
271 |
| return b; |
| 313 |
|
| } |
| 314 |
|
| |
| 315 |
1107 |
| static struct shg_board *shg_board_copy(const struct shg_board *b) |
| 316 |
|
| { |
| 317 |
|
| struct shg_board *n; |
| 318 |
1107 |
▶ | if (!b) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!b) return NULL; |
0 |
1107 |
1107 |
|
| 319 |
1107 |
| n = (struct shg_board *) malloc(sizeof(struct shg_board)); |
| 320 |
1107 |
▶ | if (!n) return NULL; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!n) return NULL; |
0 |
1107 |
1107 |
|
| 321 |
1107 |
| *n = *b; |
| 322 |
1107 |
| SHG_LIVE++; |
| 323 |
1107 |
| return n; |
| 324 |
|
| } |
| 325 |
|
| |
| 326 |
5834 |
| static void shg_board_drop(struct shg_board *b) |
| 327 |
|
| { |
| 328 |
5834 |
▶ | if (!b) return; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!b) return; |
0 |
5834 |
5834 |
|
| 329 |
5834 |
| free(b); |
| 330 |
5834 |
| SHG_LIVE--; |
| 331 |
|
| } |
| 332 |
|
| |
| 333 |
4 |
| static int shg_live(void) { return SHG_LIVE; } |
| 334 |
|
| |
| 335 |
34304 |
▶ | static int shg_to_move(const struct shg_board *b) { return b ? b->to_move : -1; } |
Branch: 1 branch
| Branch | True | False | Total |
| static int shg_to_move(const struct shg_board *b) { return b ? b->to_move : -1; } |
34304 |
0 |
34304 |
|
| 336 |
|
| |
| 337 |
20827198 |
| void shg_set_to_move(struct shg_board *b, int side) |
| 338 |
|
| { |
| 339 |
20827198 |
▶ | if (!b || !SHG_IS_SIDE(side) || side == b->to_move) return; |
Branch: 4 branches
| Branch | True | False | Total |
| if (!b || !SHG_IS_SIDE(side) || side == b->to_move) return; |
20827198 |
0 |
20827198 |
| if (!b || !SHG_IS_SIDE(side) || side == b->to_move) return; |
10482344 |
10344854 |
20827198 |
| if (!b || !SHG_IS_SIDE(side) || side == b->to_move) return; |
10482344 |
0 |
10482344 |
| if (!b || !SHG_IS_SIDE(side) || side == b->to_move) return; |
2074 |
20825124 |
20827198 |
|
| 340 |
20825124 |
| b->key ^= SHG_ZOB_MOVE; |
| 341 |
20825124 |
| b->to_move = side; |
| 342 |
|
| } |
| 343 |
|
| |
| 344 |
30544 |
▶ | static int shg_move_number(const struct shg_board *b) { return b ? b->move_number : 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| static int shg_move_number(const struct shg_board *b) { return b ? b->move_number : 0; } |
30544 |
0 |
30544 |
|
| 345 |
|
| |
| 346 |
3 |
| static void shg_set_move_number(struct shg_board *b, int n) |
| 347 |
|
| { |
| 348 |
3 |
▶ | if (!b || n < 1 || n > SHG_MOVE_MAX) return; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!b || n < 1 || n > SHG_MOVE_MAX) return; |
3 |
0 |
3 |
| if (!b || n < 1 || n > SHG_MOVE_MAX) return; |
2 |
1 |
3 |
| if (!b || n < 1 || n > SHG_MOVE_MAX) return; |
0 |
2 |
2 |
|
| 349 |
2 |
| b->move_number = n; |
| 350 |
|
| } |
| 351 |
|
| |
| 352 |
9 |
| static int shg_king_sq(const struct shg_board *b, int side) |
| 353 |
|
| { |
| 354 |
9 |
▶ | if (!b || !SHG_IS_SIDE(side)) return -1; |
Branch: 3 branches
| Branch | True | False | Total |
| if (!b || !SHG_IS_SIDE(side)) return -1; |
9 |
0 |
9 |
| if (!b || !SHG_IS_SIDE(side)) return -1; |
2 |
7 |
9 |
| if (!b || !SHG_IS_SIDE(side)) return -1; |
0 |
2 |
2 |
|
| 355 |
9 |
| return b->king[side]; |
| 356 |
|
| } |
| 357 |
|
| |
| 358 |
38130 |
▶ | static unsigned long long shg_key(const struct shg_board *b) { return b ? b->key : 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| static unsigned long long shg_key(const struct shg_board *b) { return b ? b->key : 0; } |
38130 |
0 |
38130 |
|
| 359 |
|
| |
| 360 |
|
| /* ---- the SFEN --------------------------------------------------------------- |
| 361 |
|
| * |
| 362 |
|
| * U, section 3: the board rank a first and each rank from file 9; uppercase |
| 363 |
|
| * for Black and lowercase for White; '+' before a promoted piece; a digit for |
| 364 |
|
| * a run of empty squares. Then the side to move, b or w; then the hands, |
| 365 |
|
| * counted, rook to pawn, Black's first, or '-'; then the move number, which U |
| 366 |
|
| * calls optional and this reader takes as 1 when it is missing. The start is |
| 367 |
|
| * |
| 368 |
|
| * lnsgkgsnl/1r5b1/ppppppppp/9/9/9/PPPPPPPPP/1B5R1/LNSGKGSNL b - 1 |
| 369 |
|
| */ |
| 370 |
|
| |
| 371 |
10602 |
| static int put_int(char *tmp, int n, int v) |
| 372 |
|
| { |
| 373 |
|
| char rev[12]; |
| 374 |
10602 |
| int i = 0; |
| 375 |
13813 |
▶ | do { rev[i++] = (char) ('0' + v % 10); v /= 10; } while (v && i < 11); |
Branch: 2 branches
| Branch | True | False | Total |
| do { rev[i++] = (char) ('0' + v % 10); v /= 10; } while (v && i < 11); |
3211 |
10602 |
13813 |
| do { rev[i++] = (char) ('0' + v % 10); v /= 10; } while (v && i < 11); |
3211 |
0 |
3211 |
|
| 376 |
24415 |
▶ | while (i) tmp[n++] = rev[--i]; |
Branch: 1 branch
| Branch | True | False | Total |
| while (i) tmp[n++] = rev[--i]; |
13813 |
10602 |
24415 |
|
| 377 |
10602 |
| return n; |
| 378 |
|
| } |
| 379 |
|
| |
| 380 |
2685 |
| static int shg_to_sfen(const struct shg_board *b, char *out, int max) |
| 381 |
|
| { |
| 382 |
|
| char tmp[SHG_SFEN_MAX]; |
| 383 |
2685 |
| int n = 0, row, col, run, c, side, i, any = 0; |
| 384 |
2685 |
▶ | if (!b || !out) return -1; |
Branch: 2 branches
| Branch | True | False | Total |
| if (!b || !out) return -1; |
2685 |
0 |
2685 |
| if (!b || !out) return -1; |
0 |
2685 |
2685 |
|
| 385 |
26850 |
▶ | for (row = 0; row < SHG_RANKS; row++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (row = 0; row < SHG_RANKS; row++) { |
24165 |
2685 |
26850 |
|
| 386 |
24165 |
▶ | if (row > 0) tmp[n++] = '/'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (row > 0) tmp[n++] = '/'; |
21480 |
2685 |
24165 |
|
| 387 |
24165 |
| run = 0; |
| 388 |
241650 |
▶ | for (col = 0; col < SHG_FILES; col++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (col = 0; col < SHG_FILES; col++) { |
217485 |
24165 |
241650 |
|
| 389 |
|
| int kind; |
| 390 |
|
| char letter; |
| 391 |
217485 |
| c = b->cell[SHG_SQ_OF[row * SHG_FILES + col]]; |
| 392 |
217485 |
▶ | if (c == SHG_EMPTY) { run++; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (c == SHG_EMPTY) { run++; continue; } |
158480 |
59005 |
217485 |
|
| 393 |
59005 |
▶ | if (run) { tmp[n++] = (char) ('0' + run); run = 0; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (run) { tmp[n++] = (char) ('0' + run); run = 0; } |
25290 |
33715 |
59005 |
|
| 394 |
59005 |
| kind = SHG_KIND_OF(c); |
| 395 |
59005 |
▶ | if (kind > SHG_KING) tmp[n++] = '+'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (kind > SHG_KING) tmp[n++] = '+'; |
2756 |
56249 |
59005 |
|
| 396 |
59005 |
| letter = LETTER[kind]; |
| 397 |
59005 |
▶ | tmp[n++] = SHG_SIDE_OF(c) == SHG_WHITE ? (char) (letter + ('a' - 'A')) : letter; |
Branch: 1 branch
| Branch | True | False | Total |
| tmp[n++] = SHG_SIDE_OF(c) == SHG_WHITE ? (char) (letter + ('a' - 'A')) : letter; |
29043 |
29962 |
59005 |
|
| 398 |
|
| } |
| 399 |
24165 |
▶ | if (run) tmp[n++] = (char) ('0' + run); |
Branch: 1 branch
| Branch | True | False | Total |
| if (run) tmp[n++] = (char) ('0' + run); |
17291 |
6874 |
24165 |
|
| 400 |
|
| } |
| 401 |
2685 |
| tmp[n++] = ' '; |
| 402 |
2685 |
▶ | tmp[n++] = b->to_move == SHG_WHITE ? 'w' : 'b'; |
Branch: 1 branch
| Branch | True | False | Total |
| tmp[n++] = b->to_move == SHG_WHITE ? 'w' : 'b'; |
1197 |
1488 |
2685 |
|
| 403 |
2685 |
| tmp[n++] = ' '; |
| 404 |
8055 |
▶ | for (side = 0; side < SHG_SIDES; side++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (side = 0; side < SHG_SIDES; side++) |
5370 |
2685 |
8055 |
|
| 405 |
42960 |
▶ | for (i = 0; i < SHG_HAND_KINDS - 1; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < SHG_HAND_KINDS - 1; i++) { |
37590 |
5370 |
42960 |
|
| 406 |
37590 |
| int kind = HAND_ORDER[i], count = b->hand[side][kind]; |
| 407 |
37590 |
▶ | if (!count) continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!count) continue; |
28304 |
9286 |
37590 |
|
| 408 |
9286 |
▶ | if (count > 1) n = put_int(tmp, n, count); |
Branch: 1 branch
| Branch | True | False | Total |
| if (count > 1) n = put_int(tmp, n, count); |
7917 |
1369 |
9286 |
|
| 409 |
9286 |
▶ | tmp[n++] = side == SHG_WHITE ? (char) (LETTER[kind] + ('a' - 'A')) : LETTER[kind]; |
Branch: 1 branch
| Branch | True | False | Total |
| tmp[n++] = side == SHG_WHITE ? (char) (LETTER[kind] + ('a' - 'A')) : LETTER[kind]; |
1023 |
8263 |
9286 |
|
| 410 |
9286 |
| any = 1; |
| 411 |
|
| } |
| 412 |
2685 |
▶ | if (!any) tmp[n++] = '-'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!any) tmp[n++] = '-'; |
490 |
2195 |
2685 |
|
| 413 |
2685 |
| tmp[n++] = ' '; |
| 414 |
2685 |
| n = put_int(tmp, n, b->move_number); |
| 415 |
2685 |
▶ | if (n + 1 > max) return -1; |
Branch: 1 branch
| Branch | True | False | Total |
| if (n + 1 > max) return -1; |
0 |
2685 |
2685 |
|
| 416 |
2685 |
| memcpy(out, tmp, (size_t) n); |
| 417 |
2685 |
| out[n] = '\0'; |
| 418 |
2685 |
| return n; |
| 419 |
|
| } |
| 420 |
|
| |
| 421 |
232097 |
▶ | static int is_digit(int c) { return c >= '0' && c <= '9'; } |
Branch: 2 branches
| Branch | True | False | Total |
| static int is_digit(int c) { return c >= '0' && c <= '9'; } |
215137 |
16960 |
232097 |
| static int is_digit(int c) { return c >= '0' && c <= '9'; } |
83852 |
131285 |
215137 |
|
| 422 |
|
| |
| 423 |
2163 |
| static struct shg_board *shg_board_of_sfen(const char *str, int *err) |
| 424 |
|
| { |
| 425 |
2163 |
| int code = SHG_OK; |
| 426 |
|
| char field[4][SHG_SFEN_MAX]; |
| 427 |
2163 |
| int nfields = 0, flen = 0, ranks = 1, i, side, kind, move = 1; |
| 428 |
|
| int cells[SHG_SQUARES]; |
| 429 |
|
| int hands[SHG_SIDES][SHG_HAND_KINDS]; |
| 430 |
|
| int seen[SHG_SIDES][SHG_HAND_KINDS]; |
| 431 |
2163 |
| int kings[SHG_SIDES] = { 0, 0 }; |
| 432 |
|
| int total[SHG_KINDS]; |
| 433 |
|
| const char *s; |
| 434 |
|
| size_t len; |
| 435 |
|
| struct shg_board *b; |
| 436 |
|
| |
| 437 |
2163 |
| shg_tables_init(); |
| 438 |
2163 |
▶ | if (!str || !*str) { code = SHG_POS_NULL; goto refused; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!str || !*str) { code = SHG_POS_NULL; goto refused; } |
2163 |
0 |
2163 |
| if (!str || !*str) { code = SHG_POS_NULL; goto refused; } |
2 |
2161 |
2163 |
|
| 439 |
2161 |
| len = strlen(str); |
| 440 |
2161 |
▶ | if (len >= SHG_SFEN_MAX) { code = SHG_POS_LONG; goto refused; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (len >= SHG_SFEN_MAX) { code = SHG_POS_LONG; goto refused; } |
1 |
2160 |
2161 |
|
| 441 |
|
| |
| 442 |
|
| /* ---- the fields: single spaces between, none before or after ---- */ |
| 443 |
2160 |
▶ | if (str[0] == ' ' || str[len - 1] == ' ') { code = SHG_POS_FIELDS; goto refused; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (str[0] == ' ' || str[len - 1] == ' ') { code = SHG_POS_FIELDS; goto refused; } |
2159 |
1 |
2160 |
| if (str[0] == ' ' || str[len - 1] == ' ') { code = SHG_POS_FIELDS; goto refused; } |
1 |
2158 |
2159 |
|
| 444 |
133302 |
▶ | for (s = str; *s; s++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (s = str; *s; s++) { |
131147 |
2155 |
133302 |
|
| 445 |
131147 |
▶ | if (*s == ' ') { |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s == ' ') { |
6467 |
124680 |
131147 |
|
| 446 |
6467 |
▶ | if (s[1] == ' ' || nfields >= 3) { code = SHG_POS_FIELDS; goto refused; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (s[1] == ' ' || nfields >= 3) { code = SHG_POS_FIELDS; goto refused; } |
6466 |
1 |
6467 |
| if (s[1] == ' ' || nfields >= 3) { code = SHG_POS_FIELDS; goto refused; } |
2 |
6464 |
6466 |
|
| 447 |
6464 |
| field[nfields][flen] = '\0'; |
| 448 |
6464 |
| nfields++; |
| 449 |
6464 |
| flen = 0; |
| 450 |
6464 |
| continue; |
| 451 |
|
| } |
| 452 |
124680 |
| field[nfields][flen++] = *s; |
| 453 |
|
| } |
| 454 |
2155 |
| field[nfields][flen] = '\0'; |
| 455 |
2155 |
| nfields++; |
| 456 |
2155 |
▶ | if (nfields != 3 && nfields != 4) { code = SHG_POS_FIELDS; goto refused; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (nfields != 3 && nfields != 4) { code = SHG_POS_FIELDS; goto refused; } |
2154 |
1 |
2155 |
| if (nfields != 3 && nfields != 4) { code = SHG_POS_FIELDS; goto refused; } |
2 |
2152 |
2154 |
|
| 457 |
|
| |
| 458 |
|
| /* ---- the board ---- */ |
| 459 |
109859 |
▶ | for (s = field[0]; *s; s++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (s = field[0]; *s; s++) |
107706 |
2153 |
109859 |
|
| 460 |
107706 |
▶ | if (*s == '/') ranks++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s == '/') ranks++; |
17210 |
90496 |
107706 |
|
| 461 |
2153 |
▶ | if (ranks != SHG_RANKS) { code = SHG_POS_RANKS; goto refused; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (ranks != SHG_RANKS) { code = SHG_POS_RANKS; goto refused; } |
4 |
2149 |
2153 |
|
| 462 |
|
| |
| 463 |
109766 |
▶ | for (s = field[0]; *s; s++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (s = field[0]; *s; s++) { |
107621 |
2145 |
109766 |
|
| 464 |
107621 |
▶ | if (*s == '/' || *s == '+' || kind_of_letter(*s)) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (*s == '/' || *s == '+' || kind_of_letter(*s)) continue; |
90445 |
17176 |
107621 |
| if (*s == '/' || *s == '+' || kind_of_letter(*s)) continue; |
88115 |
2330 |
90445 |
| if (*s == '/' || *s == '+' || kind_of_letter(*s)) continue; |
52381 |
35734 |
88115 |
|
| 465 |
35734 |
▶ | if (*s >= '1' && *s <= '9' && !is_digit(s[1])) continue; |
Branch: 3 branches
| Branch | True | False | Total |
| if (*s >= '1' && *s <= '9' && !is_digit(s[1])) continue; |
35732 |
2 |
35734 |
| if (*s >= '1' && *s <= '9' && !is_digit(s[1])) continue; |
35731 |
1 |
35732 |
| if (*s >= '1' && *s <= '9' && !is_digit(s[1])) continue; |
35730 |
1 |
35731 |
|
| 466 |
4 |
| code = SHG_POS_LETTER; goto refused; |
| 467 |
|
| } |
| 468 |
|
| |
| 469 |
|
| { |
| 470 |
2145 |
| int width = 0; |
| 471 |
109712 |
| for (s = field[0]; ; s++) { |
| 472 |
109712 |
▶ | if (*s == '/' || !*s) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (*s == '/' || !*s) { |
92558 |
17154 |
109712 |
| if (*s == '/' || !*s) { |
2143 |
90415 |
92558 |
|
| 473 |
19297 |
▶ | if (width != SHG_FILES) { code = SHG_POS_WIDTH; goto refused; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (width != SHG_FILES) { code = SHG_POS_WIDTH; goto refused; } |
3 |
19294 |
19297 |
|
| 474 |
19294 |
▶ | if (!*s) break; |
Branch: 1 branch
| Branch | True | False | Total |
| if (!*s) break; |
2142 |
17152 |
19294 |
|
| 475 |
17152 |
| width = 0; |
| 476 |
17152 |
| continue; |
| 477 |
|
| } |
| 478 |
90415 |
▶ | if (is_digit(*s)) width += *s - '0'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_digit(*s)) width += *s - '0'; |
35704 |
54711 |
90415 |
|
| 479 |
54711 |
▶ | else if (*s != '+') width++; |
Branch: 1 branch
| Branch | True | False | Total |
| else if (*s != '+') width++; |
52381 |
2330 |
54711 |
|
| 480 |
|
| } |
| 481 |
|
| } |
| 482 |
|
| |
| 483 |
109631 |
▶ | for (s = field[0]; *s; s++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (s = field[0]; *s; s++) { |
107493 |
2138 |
109631 |
|
| 484 |
107493 |
▶ | if (*s != '+') continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s != '+') continue; |
105164 |
2329 |
107493 |
|
| 485 |
2329 |
| kind = kind_of_letter(s[1]); |
| 486 |
2329 |
▶ | if (!kind || !PROMOTED[kind]) { code = SHG_POS_PROMOTE; goto refused; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!kind || !PROMOTED[kind]) { code = SHG_POS_PROMOTE; goto refused; } |
2327 |
2 |
2329 |
| if (!kind || !PROMOTED[kind]) { code = SHG_POS_PROMOTE; goto refused; } |
2 |
2325 |
2327 |
|
| 487 |
|
| } |
| 488 |
|
| |
| 489 |
|
| /* ---- the side to move ---- */ |
| 490 |
2138 |
▶ | if ((field[1][0] != 'b' && field[1][0] != 'w') || field[1][1]) { |
Branch: 3 branches
| Branch | True | False | Total |
| if ((field[1][0] != 'b' && field[1][0] != 'w') || field[1][1]) { |
1210 |
928 |
2138 |
| if ((field[1][0] != 'b' && field[1][0] != 'w') || field[1][1]) { |
1208 |
2 |
1210 |
| if ((field[1][0] != 'b' && field[1][0] != 'w') || field[1][1]) { |
1 |
2135 |
2136 |
|
| 491 |
3 |
| code = SHG_POS_SIDE; goto refused; |
| 492 |
|
| } |
| 493 |
|
| |
| 494 |
|
| /* ---- the hands ---- */ |
| 495 |
2135 |
| memset(hands, 0, sizeof hands); |
| 496 |
2135 |
| memset(seen, 0, sizeof seen); |
| 497 |
2135 |
▶ | if (strcmp(field[2], "-") != 0) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (strcmp(field[2], "-") != 0) { |
1575 |
560 |
2135 |
|
| 498 |
6992 |
▶ | for (s = field[2]; *s; ) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (s = field[2]; *s; ) { |
5428 |
1564 |
6992 |
|
| 499 |
5428 |
| int count = 1; |
| 500 |
5428 |
▶ | if (is_digit(*s)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_digit(*s)) { |
4105 |
1323 |
5428 |
|
| 501 |
4105 |
▶ | if (*s == '0') { code = SHG_POS_HAND; goto refused; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s == '0') { code = SHG_POS_HAND; goto refused; } |
2 |
4103 |
4105 |
|
| 502 |
4103 |
| count = 0; |
| 503 |
8752 |
▶ | while (is_digit(*s) && count <= SHG_SET_PAWNS) count = count * 10 + (*s++ - '0'); |
Branch: 2 branches
| Branch | True | False | Total |
| while (is_digit(*s) && count <= SHG_SET_PAWNS) count = count * 10 + (*s++ - '0'); |
4649 |
4103 |
8752 |
| while (is_digit(*s) && count <= SHG_SET_PAWNS) count = count * 10 + (*s++ - '0'); |
4649 |
0 |
4649 |
|
| 504 |
4103 |
▶ | if (count < 2) { code = SHG_POS_HAND; goto refused; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (count < 2) { code = SHG_POS_HAND; goto refused; } |
1 |
4102 |
4103 |
|
| 505 |
|
| } |
| 506 |
5425 |
| kind = kind_of_letter(*s); |
| 507 |
5425 |
▶ | if (!SHG_IS_HAND(kind)) { code = SHG_POS_HAND; goto refused; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (!SHG_IS_HAND(kind)) { code = SHG_POS_HAND; goto refused; } |
5421 |
4 |
5425 |
| if (!SHG_IS_HAND(kind)) { code = SHG_POS_HAND; goto refused; } |
1 |
5420 |
5421 |
|
| 508 |
5420 |
| side = *s >= 'a' ? SHG_WHITE : SHG_BLACK; |
| 509 |
5420 |
▶ | if (seen[side][kind] || count > SET[kind]) { code = SHG_POS_HAND; goto refused; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (seen[side][kind] || count > SET[kind]) { code = SHG_POS_HAND; goto refused; } |
5419 |
1 |
5420 |
| if (seen[side][kind] || count > SET[kind]) { code = SHG_POS_HAND; goto refused; } |
2 |
5417 |
5419 |
|
| 510 |
5417 |
| seen[side][kind] = 1; |
| 511 |
5417 |
| hands[side][kind] = count; |
| 512 |
5417 |
| s++; |
| 513 |
|
| } |
| 514 |
|
| } |
| 515 |
|
| |
| 516 |
|
| /* ---- the move number ---- */ |
| 517 |
2124 |
▶ | if (nfields == 4) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nfields == 4) { |
2123 |
1 |
2124 |
|
| 518 |
2123 |
| s = field[3]; |
| 519 |
2123 |
▶ | if (*s < '1' || *s > '9' || strlen(s) > 6) { code = SHG_POS_MOVE; goto refused; } |
Branch: 3 branches
| Branch | True | False | Total |
| if (*s < '1' || *s > '9' || strlen(s) > 6) { code = SHG_POS_MOVE; goto refused; } |
2120 |
3 |
2123 |
| if (*s < '1' || *s > '9' || strlen(s) > 6) { code = SHG_POS_MOVE; goto refused; } |
2120 |
0 |
2120 |
| if (*s < '1' || *s > '9' || strlen(s) > 6) { code = SHG_POS_MOVE; goto refused; } |
1 |
2119 |
2120 |
|
| 520 |
2119 |
| move = 0; |
| 521 |
6036 |
▶ | for (; *s; s++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (; *s; s++) { |
3918 |
2118 |
6036 |
|
| 522 |
3918 |
▶ | if (!is_digit(*s)) { code = SHG_POS_MOVE; goto refused; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!is_digit(*s)) { code = SHG_POS_MOVE; goto refused; } |
1 |
3917 |
3918 |
|
| 523 |
3917 |
| move = move * 10 + (*s - '0'); |
| 524 |
|
| } |
| 525 |
|
| } |
| 526 |
|
| |
| 527 |
|
| /* ---- what the board holds ---- */ |
| 528 |
|
| { |
| 529 |
2119 |
| int sq = 0, promote = 0; |
| 530 |
109249 |
▶ | for (s = field[0]; *s; s++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (s = field[0]; *s; s++) { |
107130 |
2119 |
109249 |
|
| 531 |
107130 |
▶ | if (*s == '/') continue; |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s == '/') continue; |
16952 |
90178 |
107130 |
|
| 532 |
90178 |
▶ | if (*s == '+') { promote = 1; continue; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (*s == '+') { promote = 1; continue; } |
2325 |
87853 |
90178 |
|
| 533 |
87853 |
▶ | if (is_digit(*s)) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (is_digit(*s)) { |
35476 |
52377 |
87853 |
|
| 534 |
154738 |
▶ | for (i = 0; i < *s - '0'; i++) cells[sq++] = SHG_EMPTY; |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < *s - '0'; i++) cells[sq++] = SHG_EMPTY; |
119262 |
35476 |
154738 |
|
| 535 |
35476 |
| continue; |
| 536 |
|
| } |
| 537 |
52377 |
| kind = kind_of_letter(*s); |
| 538 |
52377 |
▶ | if (promote) kind = PROMOTED[kind]; |
Branch: 1 branch
| Branch | True | False | Total |
| if (promote) kind = PROMOTED[kind]; |
2325 |
50052 |
52377 |
|
| 539 |
52377 |
| promote = 0; |
| 540 |
52377 |
| side = *s >= 'a' ? SHG_WHITE : SHG_BLACK; |
| 541 |
52377 |
▶ | if (kind == SHG_KING) kings[side]++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (kind == SHG_KING) kings[side]++; |
3018 |
49359 |
52377 |
|
| 542 |
52377 |
| cells[sq++] = SHG_PIECE(side, kind); |
| 543 |
|
| } |
| 544 |
|
| } |
| 545 |
2119 |
▶ | if (kings[SHG_BLACK] > 1 || kings[SHG_WHITE] > 1) { code = SHG_POS_KINGS; goto refused; } |
Branch: 2 branches
| Branch | True | False | Total |
| if (kings[SHG_BLACK] > 1 || kings[SHG_WHITE] > 1) { code = SHG_POS_KINGS; goto refused; } |
2118 |
1 |
2119 |
| if (kings[SHG_BLACK] > 1 || kings[SHG_WHITE] > 1) { code = SHG_POS_KINGS; goto refused; } |
2 |
2116 |
2118 |
|
| 546 |
|
| |
| 547 |
2116 |
| memset(total, 0, sizeof total); |
| 548 |
173512 |
▶ | for (i = 0; i < SHG_SQUARES; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < SHG_SQUARES; i++) |
171396 |
2116 |
173512 |
|
| 549 |
171396 |
▶ | if (cells[i] != SHG_EMPTY) total[UNPROMOTED[SHG_KIND_OF(cells[i])]]++; |
Branch: 1 branch
| Branch | True | False | Total |
| if (cells[i] != SHG_EMPTY) total[UNPROMOTED[SHG_KIND_OF(cells[i])]]++; |
52370 |
119026 |
171396 |
|
| 550 |
6348 |
▶ | for (side = 0; side < SHG_SIDES; side++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (side = 0; side < SHG_SIDES; side++) |
4232 |
2116 |
6348 |
|
| 551 |
33856 |
▶ | for (kind = SHG_PAWN; kind <= SHG_ROOK; kind++) total[kind] += hands[side][kind]; |
Branch: 1 branch
| Branch | True | False | Total |
| for (kind = SHG_PAWN; kind <= SHG_ROOK; kind++) total[kind] += hands[side][kind]; |
29624 |
4232 |
33856 |
|
| 552 |
19024 |
▶ | for (kind = SHG_PAWN; kind <= SHG_KING; kind++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (kind = SHG_PAWN; kind <= SHG_KING; kind++) |
16912 |
2112 |
19024 |
|
| 553 |
16912 |
▶ | if (total[kind] > SET[kind]) { code = SHG_POS_COUNT; goto refused; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (total[kind] > SET[kind]) { code = SHG_POS_COUNT; goto refused; } |
4 |
16908 |
16912 |
|
| 554 |
|
| |
| 555 |
2112 |
| b = board_alloc(); |
| 556 |
2112 |
▶ | if (!b) { code = SHG_POS_NULL; goto refused; } |
Branch: 1 branch
| Branch | True | False | Total |
| if (!b) { code = SHG_POS_NULL; goto refused; } |
0 |
2112 |
2112 |
|
| 557 |
173184 |
▶ | for (i = 0; i < SHG_SQUARES; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < SHG_SQUARES; i++) |
171072 |
2112 |
173184 |
|
| 558 |
171072 |
▶ | if (cells[i] != SHG_EMPTY) shg_put(b, i, cells[i]); |
Branch: 1 branch
| Branch | True | False | Total |
| if (cells[i] != SHG_EMPTY) shg_put(b, i, cells[i]); |
52208 |
118864 |
171072 |
|
| 559 |
6336 |
▶ | for (side = 0; side < SHG_SIDES; side++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (side = 0; side < SHG_SIDES; side++) |
4224 |
2112 |
6336 |
|
| 560 |
33792 |
▶ | for (kind = SHG_PAWN; kind <= SHG_ROOK; kind++) shg_set_hand(b, side, kind, hands[side][kind]); |
Branch: 1 branch
| Branch | True | False | Total |
| for (kind = SHG_PAWN; kind <= SHG_ROOK; kind++) shg_set_hand(b, side, kind, hands[side][kind]); |
29568 |
4224 |
33792 |
|
| 561 |
2112 |
| shg_set_to_move(b, field[1][0] == 'w' ? SHG_WHITE : SHG_BLACK); |
| 562 |
2112 |
| b->move_number = move; |
| 563 |
2112 |
▶ | if (err) *err = SHG_OK; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) *err = SHG_OK; |
2112 |
0 |
2112 |
|
| 564 |
2112 |
| return b; |
| 565 |
|
| |
| 566 |
51 |
| refused: |
| 567 |
51 |
▶ | if (err) *err = code; |
Branch: 1 branch
| Branch | True | False | Total |
| if (err) *err = code; |
51 |
0 |
51 |
|
| 568 |
51 |
| return NULL; |
| 569 |
|
| } |
| 570 |
|
| |
| 571 |
|
| /* ---- the table ---------------------------------------------------------------- */ |
| 572 |
|
| |
| 573 |
|
| static const struct shg_abi SHG_ABI = { |
| 574 |
|
| SHG_ABI_VERSION, |
| 575 |
|
| |
| 576 |
|
| shg_board_new, |
| 577 |
|
| shg_board_empty, |
| 578 |
|
| shg_board_of_sfen, |
| 579 |
|
| shg_board_copy, |
| 580 |
|
| shg_board_drop, |
| 581 |
|
| shg_live, |
| 582 |
|
| |
| 583 |
|
| shg_at, |
| 584 |
|
| shg_put, |
| 585 |
|
| shg_lift, |
| 586 |
|
| |
| 587 |
|
| shg_hand, |
| 588 |
|
| shg_set_hand, |
| 589 |
|
| |
| 590 |
|
| shg_to_move, |
| 591 |
|
| shg_set_to_move, |
| 592 |
|
| shg_move_number, |
| 593 |
|
| shg_set_move_number, |
| 594 |
|
| shg_king_sq, |
| 595 |
|
| |
| 596 |
|
| shg_key, |
| 597 |
|
| shg_zobrist, |
| 598 |
|
| shg_zobrist_hand, |
| 599 |
|
| shg_zobrist_move, |
| 600 |
|
| |
| 601 |
|
| shg_to_sfen, |
| 602 |
|
| |
| 603 |
|
| shg_tables_ok, |
| 604 |
|
| shg_sq_of, |
| 605 |
|
| shg_file_of, |
| 606 |
|
| shg_rank_of, |
| 607 |
|
| shg_step, |
| 608 |
|
| shg_in_zone, |
| 609 |
|
| shg_last_distance, |
| 610 |
|
| shg_promoted, |
| 611 |
|
| shg_unpromoted, |
| 612 |
|
| shg_set_count, |
| 613 |
|
| |
| 614 |
|
| shg_attacked, |
| 615 |
|
| shg_in_check, |
| 616 |
|
| shg_pseudo_moves, |
| 617 |
|
| shg_board_moves, |
| 618 |
|
| shg_do_move, |
| 619 |
|
| shg_undo_move, |
| 620 |
|
| |
| 621 |
|
| shg_drops, |
| 622 |
|
| shg_legal, |
| 623 |
|
| shg_has_legal_move, |
| 624 |
|
| shg_drop_fault, |
| 625 |
|
| shg_perft, |
| 626 |
|
| |
| 627 |
|
| shg_status, |
| 628 |
|
| shg_move_fault, |
| 629 |
|
| |
| 630 |
|
| shg_pack, |
| 631 |
|
| shg_impasse_points, |
| 632 |
|
| shg_impasse_pieces, |
| 633 |
|
| shg_declare_fault, |
| 634 |
|
| shg_repetition, |
| 635 |
|
| |
| 636 |
|
| shg_evaluate, |
| 637 |
|
| shg_search, |
| 638 |
|
| }; |
| 639 |
|
| |
| 640 |
24 |
| const struct shg_abi *shg_abi_table(void) |
| 641 |
|
| { |
| 642 |
24 |
| shg_tables_init(); |
| 643 |
24 |
| zob_init(); |
| 644 |
24 |
| return &SHG_ABI; |
| 645 |
|
| } |