-
Notifications
You must be signed in to change notification settings - Fork 0
/
pos.cpp
434 lines (305 loc) · 10.4 KB
/
pos.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
// includes
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <string>
#include "bb_base.hpp"
#include "bit.hpp"
#include "common.hpp"
#include "gen.hpp"
#include "libmy.hpp"
#include "move.hpp"
#include "pos.hpp"
#include "score.hpp"
#include "util.hpp"
#include "var.hpp"
// functions
Pos::Pos(Side turn, Bit wm, Bit bm, Bit wk, Bit bk)
: Pos(wm | bm, wk | bk, wm | wk, bm | bk, wm | bm | wk | bk, turn)
{
assert(bit::count(wm | bm | wk | bk) == bit::count(wm) + bit::count(bm) + bit::count(wk) + bit::count(bk)); // all disjoint?
assert(bit::is_incl(wm, bit::WM_Squares));
assert(bit::is_incl(bm, bit::BM_Squares));
}
Pos::Pos(Bit man, Bit king, Bit white, Bit black, Bit all, Side turn) {
assert((man ^ king) == all);
assert((white ^ black) == all);
assert(bit::is_incl(man & white, bit::WM_Squares));
assert(bit::is_incl(man & black, bit::BM_Squares));
Bit side[Side_Size] { white, black }; // for debug
assert(side[side_opp(turn)] != 0);
if (var::Variant == var::BT) assert((side[turn] & king) == 0);
m_piece = { man, king };
m_side = { white, black };
m_all = all;
m_turn = turn;
for (int sd = 0; sd < Side_Size; sd++) {
m_wolf[sd] = -1;
m_count[sd] = 0;
}
}
Pos Pos::succ(Move mv) const {
Square from = move::from(mv, *this);
Square to = move::to(mv, *this);
Bit caps = move::captured(mv, *this);
Side atk = m_turn;
Side def = side_opp(atk);
assert(is_side(from, atk));
assert(from == to || is_empty(to));
assert(bit::is_incl(caps, side(def)));
auto piece = m_piece;
auto side = m_side;
Bit all = this->all();
Bit delta = bit::bit(from) ^ bit::bit(to);
side[atk] ^= delta;
all ^= delta;
if (is_piece(from, King)) { // king move
piece[King] ^= delta;
} else if (square_is_promotion(to, atk)) { // promotion
bit::clear(piece[Man], from);
bit::set(piece[King], to);
} else { // man move
piece[Man] ^= delta;
}
piece[Man] &= ~caps;
piece[King] &= ~caps;
side[def] &= ~caps;
all &= ~caps;
Pos pos(piece[Man], piece[King], side[White], side[Black], all, def);
if (var::Variant == var::Frisian) {
if (m_count[def] != 0 && pos.is_piece(square_make(m_wolf[def]), King) && pos.man(def) != 0) {
pos.m_wolf[def] = m_wolf[def];
pos.m_count[def] = m_count[def];
}
if (pos.man(atk) != 0 && !move::is_conversion(mv, *this)) { // quiet king move
if (from == m_wolf[atk]) pos.m_count[atk] = m_count[atk]; // same king
pos.m_wolf[atk] = to;
pos.m_count[atk] += 1;
assert(pos.m_count[atk] <= 3);
}
}
return pos;
}
void Pos::set_wolf(Side side, Square square, int count) {
assert(count <= 3);
m_wolf[side] = square;
m_count[side] = count;
}
bool operator==(const Pos & p0, const Pos & p1) { // for repetition detection
if (p0.m_all != p1.m_all) return false;
for (int pc = 0; pc < Piece_Size; pc++) {
if (p0.m_piece[pc] != p1.m_piece[pc]) return false;
}
for (int sd = 0; sd < Side_Size; sd++) {
if (p0.m_side[sd] != p1.m_side[sd]) return false;
}
if (p0.m_turn != p1.m_turn) return false;
if (var::Variant == var::Frisian) {
for (int sd = 0; sd < Side_Size; sd++) {
if (p0.m_count[sd] != p1.m_count[sd]) return false;
if (p0.m_count[sd] != 0 && p0.m_wolf[sd] != p1.m_wolf[sd]) return false;
}
}
return true;
}
Node::Node(const Pos & pos) : Node{pos, 0, nullptr} {}
Node::Node(const Pos & pos, int ply, const Node * parent) {
m_pos = pos;
m_ply = ply;
m_parent = parent;
}
Node Node::succ(Move mv) const {
Pos new_pos = m_pos.succ(mv);
if (move::is_conversion(mv, m_pos)) {
return Node{new_pos};
} else {
return Node{new_pos, m_ply + 1, this};
}
}
bool Node::is_end() const {
return pos::is_end(m_pos) || is_draw(3);
}
bool Node::is_draw(int rep) const {
if (m_ply < 4) return false;
int n = 1;
const Node * node = this;
for (int i = 0; i < m_ply / 2; i++) {
node = node->m_parent;
assert(node != nullptr);
node = node->m_parent;
assert(node != nullptr);
if (node->m_pos == m_pos) {
n += 1;
if (n == rep) return true;
}
}
return false;
}
namespace pos { // ###
// types
using fun_t = int (Piece pc, Side sd, Square sq);
// constants
const int Table_Bit {18};
const int Table_Size {1 << Table_Bit};
const int Table_Mask {Table_Size - 1};
// variables
Pos Start;
static int Tempo_Ranks_123[Table_Size];
static int Tempo_Ranks_456[Table_Size];
static int Tempo_Ranks_789[Table_Size];
static int Tempo_Ranks_012[Table_Size];
static int Tempo_Ranks_345[Table_Size];
static int Tempo_Ranks_678[Table_Size];
static int Skew_Ranks_123[Table_Size];
static int Skew_Ranks_456[Table_Size];
static int Skew_Ranks_789[Table_Size];
static int Skew_Ranks_012[Table_Size];
static int Skew_Ranks_345[Table_Size];
static int Skew_Ranks_678[Table_Size];
// prototypes
static int table_val (fun_t f, Piece pc, Side sd, int index, int offset);
static int tempo (Piece pc, Side sd, Square sq);
static int skew (Piece pc, Side sd, Square sq);
// functions
void init() {
// starting position
Start = Pos(White, Bit(0x7DF3EF8000000000), Bit(0x0000000000FBE7DF), Bit(0), Bit(0));
// men tables
for (int index = 0; index < Table_Size; index++) {
Tempo_Ranks_123[index] = table_val(tempo, Man, White, index, 6);
Tempo_Ranks_456[index] = table_val(tempo, Man, White, index, 26);
Tempo_Ranks_789[index] = table_val(tempo, Man, White, index, 45);
Tempo_Ranks_012[index] = table_val(tempo, Man, Black, index, 0);
Tempo_Ranks_345[index] = table_val(tempo, Man, Black, index, 19);
Tempo_Ranks_678[index] = table_val(tempo, Man, Black, index, 39);
Skew_Ranks_123[index] = table_val(skew, Man, White, index, 6);
Skew_Ranks_456[index] = table_val(skew, Man, White, index, 26);
Skew_Ranks_789[index] = table_val(skew, Man, White, index, 45);
Skew_Ranks_012[index] = table_val(skew, Man, Black, index, 0);
Skew_Ranks_345[index] = table_val(skew, Man, Black, index, 19);
Skew_Ranks_678[index] = table_val(skew, Man, Black, index, 39);
}
}
bool is_end(const Pos & pos) {
return !can_move(pos, pos.turn())
|| (var::Variant == var::BT && has_king(pos, side_opp(pos.turn())));
}
bool is_wipe(const Pos & pos) { // fast subset of "is_end", for BT variant
return pos.side(pos.turn()) == 0
|| (var::Variant == var::BT && has_king(pos, side_opp(pos.turn())));
}
int result(const Pos & pos, Side sd) { // for losing variant
assert(is_end(pos));
int res = (var::Variant == var::Losing) ? +1 : -1; // for turn
res = score::side(res, pos.turn()); // for white
return score::side(res, sd); // for sd
}
Piece_Side piece_side(const Pos & pos, Square sq) {
int ps = (bit::bit(pos.empty(), sq) << 2)
| (bit::bit(pos.king(), sq) << 1)
| (bit::bit(pos.black(), sq) << 0);
return Piece_Side(ps); // can be Empty
}
int tempo(const Pos & pos) {
int tempo = 0;
tempo += Tempo_Ranks_123[(pos.wm() >> 6) & Table_Mask];
tempo += Tempo_Ranks_456[(pos.wm() >> 26) & Table_Mask];
tempo += Tempo_Ranks_789[(pos.wm() >> 45) & Table_Mask];
tempo += Tempo_Ranks_012[(pos.bm() >> 0) & Table_Mask];
tempo += Tempo_Ranks_345[(pos.bm() >> 19) & Table_Mask];
tempo += Tempo_Ranks_678[(pos.bm() >> 39) & Table_Mask];
return tempo;
}
int skew(const Pos & pos, Side sd) {
int skew = 0;
if (sd == White) {
skew += Skew_Ranks_123[(pos.wm() >> 6) & Table_Mask];
skew += Skew_Ranks_456[(pos.wm() >> 26) & Table_Mask];
skew += Skew_Ranks_789[(pos.wm() >> 45) & Table_Mask];
} else {
skew += Skew_Ranks_012[(pos.bm() >> 0) & Table_Mask];
skew += Skew_Ranks_345[(pos.bm() >> 19) & Table_Mask];
skew += Skew_Ranks_678[(pos.bm() >> 39) & Table_Mask];
}
return skew;
}
static int tempo(Piece pc, Side sd, Square sq) { // pc for debug
assert(pc == Man);
return (Rank_Size - 1) - square_rank(sq, sd);
}
static int skew(Piece pc, Side /* sd */, Square sq) { // pc for debug
assert(pc == Man);
return square_file(sq) * 2 - (File_Size - 1);
}
static int table_val(fun_t f, Piece pc, Side sd, int index, int offset) {
assert(index >= 0 && index < Table_Size);
assert(square_is_ok(offset));
int val = 0;
for (Square sq : bit::Squares & (uint64(index) << offset)) {
val += f(pc, sd, sq);
}
return val;
}
void disp(const Pos & pos) {
// pieces
for (int rk = 0; rk < Rank_Size; rk++) {
for (int fl = 0; fl < File_Size; fl++) {
if (square_is_light(fl, rk)) {
std::cout << " ";
} else {
Square sq = square_make(fl, rk);
switch (piece_side(pos, sq)) {
case White_Man : std::cout << "O "; break;
case Black_Man : std::cout << "* "; break;
case White_King : std::cout << "@ "; break;
case Black_King : std::cout << "# "; break;
case Empty : std::cout << "- "; break;
}
}
}
std::cout << " ";
for (int fl = 0; fl < File_Size; fl++) {
if (square_is_light(fl, rk)) {
std::cout << " ";
} else {
Square sq = square_make(fl, rk);
std::cout << std::right << std::setfill('0') << std::setw(2) << square_to_std(sq);
}
}
std::cout << '\n';
}
std::cout << '\n';
// turn/result
Side atk = pos.turn();
Side def = side_opp(atk);
if (is_end(pos)) {
std::cout << side_to_string(var::Variant == var::Losing ? atk : def) << " wins #";
} else {
std::cout << side_to_string(atk) << " to play";
if (bb::pos_is_load(pos)) std::cout << ", bitbase " << bb::value_to_string(bb::probe(pos));
}
std::cout << '\n';
std::cout << std::endl;
}
void wolf_from_hub(const std::string & s, Pos & pos) {
Scanner_Number scan(s);
std::string token;
Side side;
while (!scan.eos()) {
token = scan.get_token();
if (token == "W") {
side = White;
} else if (token == "B") {
side = Black;
} else {
continue;
}
token = scan.get_token();
Square square = square_from_string(token);
token = scan.get_token();
token = scan.get_token();
int count = std::stoi(token);
pos.set_wolf(side, square, count);
}
}
} // namespace pos