-
Notifications
You must be signed in to change notification settings - Fork 0
/
movement.d
427 lines (402 loc) · 14.6 KB
/
movement.d
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
import position;
import tango.util.log.Trace;
void piece_mobility(Position pos, ulong pbit, ulong freezers,
ulong[] reachable, out ulong frozen)
in
{
assert (popcount(pbit) == 1);
assert (pbit & ~pos.bitBoards[Piece.EMPTY]);
assert (pbit & ~(pos.bitBoards[Piece.WRABBIT] |
pos.bitBoards[Piece.BRABBIT]));
}
body
{
reachable[0] = pbit;
if (pbit & pos.frozen)
{
frozen = pbit;
reachable[1] = pbit;
reachable[2] = pbit;
reachable[3] = pbit;
reachable[4] = pbit;
return;
}
Side side = (pos.placement[Side.WHITE] & pbit) ? Side.WHITE : Side.BLACK;
bitix pix = bitindex(pbit);
Piece piece = pos.pieces[pix];
int pieceoffset = 0;
int enemyoffset = -6;
int opieceoffset = 6;
if (side == Side.BLACK)
{
pieceoffset = 6;
enemyoffset = 6;
opieceoffset = 0;
}
ulong empties = pos.bitBoards[Piece.EMPTY];
ulong bad_traps = TRAPS & ~neighbors_of(pos.placement[side] & ~pbit);
ulong safe_empties = empties & ~bad_traps;
ulong trapped = neighbors_of(pbit) & pos.placement[side] & bad_traps;
ulong fr_neighbors = neighbors_of(freezers);
ulong freeze_sq = fr_neighbors & ~neighbors_of(pos.placement[side] & ~pbit & ~trapped);
ulong p_neighbors = neighbors_of(pbit);
reachable[1] = p_neighbors & safe_empties;
frozen |= reachable[1] & freeze_sq;
reachable[2] = neighbors_of(reachable[1] & ~frozen) & safe_empties & ~pbit;
frozen |= reachable[2] & freeze_sq;
reachable[3] = neighbors_of(reachable[2] & ~frozen) & safe_empties
& ~(pbit | reachable[1]);
frozen |= reachable[3] & freeze_sq;
reachable[4] = neighbors_of(reachable[3] & ~frozen) & safe_empties
& ~(pbit | reachable[1] | reachable[2]);
frozen |= reachable[4] & freeze_sq;
ulong fmove = p_neighbors & pos.placement[side];
if (popcount(fmove) > 1
|| (pbit & ~fr_neighbors & ~TRAPS))
{
fmove &= ~(pos.bitBoards[Piece.WRABBIT + pieceoffset]
& ~rabbit_steps(cast(Side)(side^1), safe_empties))
& neighbors_of(safe_empties);
} else {
fmove = 0;
}
while (fmove)
{
ulong fbit = fmove & -fmove;
fmove ^= fbit;
ulong f_neighbors = neighbors_of(fbit);
ulong filled = 0;
ulong se_neighbors = f_neighbors & empties
& ~(TRAPS & ~neighbors_of(pos.placement[side] & ~pbit & ~fbit));
ulong f_steps = se_neighbors;
bool is_r = false;
if (fbit & pos.bitBoards[Piece.WRABBIT + pieceoffset])
{
is_r = true;
f_steps &= rabbit_steps(side, fbit);
}
if (popcount(f_steps) > 1)
{
reachable[3] |= se_neighbors;
frozen |= reachable[3] & fr_neighbors
& ~neighbors_of(pos.placement[side] & ~(pbit | fbit));
reachable[4] |= neighbors_of(se_neighbors & ~frozen)
& empties
& ~(TRAPS & ~neighbors_of(pos.placement[side]
& ~pbit & ~fbit));
} else {
bitix fix = bitindex(fbit);
ulong emptied = (f_neighbors & TRAPS & pos.placement[side])
& ~(neighbors_of(pos.placement[side] & ~fbit));
if ((f_neighbors & pos.placement[side] & ~pbit & ~emptied)
|| ((fbit & ~TRAPS)
&& piece >= pos.strongest[side^1][fix] + enemyoffset))
{
filled = f_steps;
}
}
if (f_steps)
{
reachable[2] |= fbit;
reachable[4] |= f_neighbors & (pos.placement[side] | filled)
& ~((pos.bitBoards[Piece.WRABBIT + pieceoffset]
| (is_r ? filled : 0))
& ~rabbit_steps(cast(Side)(side^1),
safe_empties & ~filled))
& neighbors_of(safe_empties & ~filled);
frozen |= reachable[4] & fr_neighbors
& ~neighbors_of(pos.placement[side] & ~(pbit | fbit));
}
}
fmove = neighbors_of(reachable[1]) & pos.placement[side];
fmove &= ~(pos.bitBoards[Piece.WRABBIT + pieceoffset]
& ~rabbit_steps(cast(Side)(side^1), safe_empties))
& neighbors_of(safe_empties);
while (fmove)
{
ulong fbit = fmove & -fmove;
fmove ^= fbit;
ulong f_neighbors = neighbors_of(fbit);
ulong filled = 0;
ulong safe_fempties = f_neighbors & empties
& ~(TRAPS & ~neighbors_of(pos.placement[side] & ~pbit & ~fbit));
ulong f_steps = safe_fempties;
if (fbit & pos.bitBoards[Piece.WRABBIT + pieceoffset])
{
f_steps &= rabbit_steps(side, fbit);
}
if (!f_steps)
continue;
ulong first_steps = safe_fempties & p_neighbors;
while (first_steps)
{
ulong first_step = first_steps & -first_steps;
first_steps ^= first_step;
if (!(neighbors_of(first_step) & ~fbit & ~pbit
& pos.placement[side])
&& (neighbors_of(first_step) & freezers))
continue;
if (!(f_steps & ~first_step))
continue;
reachable[3] |= fbit;
frozen |= fbit & freeze_sq;
if (popcount(f_steps & ~first_step) > 1)
{
reachable[4] |= safe_fempties;
frozen |= safe_fempties & freeze_sq;
break;
}
}
}
ulong weaker = pos.placement[side^1] & ~freezers
& ~pos.bitBoards[piece - enemyoffset];
ulong pmove = neighbors_of(pbit) & weaker & neighbors_of(empties)
& ~bad_traps;
reachable[2] |= pmove;
frozen |= pmove & freeze_sq;
while (pmove)
{
ulong obit = pmove & -pmove;
pmove ^= obit;
ulong filled = 0;
ulong on_empties = neighbors_of(obit) & empties;
if (on_empties & (TRAPS & ~neighbors_of(pos.placement[side^1] & ~obit))
|| popcount(on_empties) > 1)
{
if (obit & ~frozen)
{
ulong onr = neighbors_of(obit) & safe_empties;
reachable[3] |= onr;
reachable[4] |= neighbors_of(onr & ~freeze_sq) & safe_empties;
frozen |= (reachable[3] | reachable[4]) & freeze_sq;
}
} else {
filled = on_empties;
}
if (obit & ~frozen)
{
reachable[4] |= neighbors_of(obit) & (weaker | filled)
& neighbors_of(empties) & ~bad_traps;
frozen |= reachable[4] & freeze_sq;
}
}
pmove = neighbors_of(reachable[1] & ~frozen) & weaker
& neighbors_of(empties) & ~bad_traps;
while (pmove)
{
ulong obit = pmove & -pmove;
pmove ^= obit;
ulong on_empties = neighbors_of(obit) & empties;
ulong pfrom = on_empties & reachable[1] & ~frozen;
if (pfrom & safe_empties)
{
auto pop_one = popcount(on_empties);
if (pop_one <= 1)
continue;
reachable[3] |= obit;
if (pop_one > 2 && !(obit & freeze_sq))
reachable[4] |= on_empties & safe_empties;
frozen |= (reachable[3] | reachable[4]) & freeze_sq;
}
}
reachable[1] |= pbit;
reachable[2] |= reachable[1];
reachable[3] |= reachable[2];
reachable[4] |= reachable[3];
}
debug (test_movement)
{
import tango.io.FilePath;
import tango.io.Stdout;
import tango.io.UnicodeFile;
import tango.math.random.Random;
import tango.text.Ascii;
void test_pos(Position pos, out uint true_moves,
out uint reported_moves, out uint false_blockade,
out uint true_blockade, out uint no_blockade)
{
pos.set_side(Side.WHITE);
ulong[Piece.max+1] true_movement;
PosStore moves = pos.get_moves();
foreach (Position result; moves)
{
for (int p = Piece.WRABBIT; p <= Piece.WELEPHANT; p++)
{
true_movement[p] |= result.bitBoards[p];
}
}
moves.free_items();
delete moves;
pos.set_side(Side.BLACK);
moves = pos.get_moves();
foreach (Position result; moves)
{
for (int p = Piece.BRABBIT; p <= Piece.BELEPHANT; p++)
{
true_movement[p] |= result.bitBoards[p];
}
}
for (int p = Piece.WRABBIT; p <= Piece.BELEPHANT; p++)
true_movement[p] |= pos.bitBoards[p];
ulong enemyoffset = 6;
ulong freezers;
ulong[Piece.max+1] reported_movement;
for (int p = Piece.BELEPHANT; p > Piece.WRABBIT; p--)
{
if (p == Piece.BRABBIT)
{
enemyoffset = -6;
freezers = 0UL;
continue;
}
ulong pbits = pos.bitBoards[p];
while (pbits)
{
ulong pbit = pbits & -pbits;
pbits ^= pbit;
ulong[5] move_sq;
ulong frozen;
piece_mobility(pos, pbit, freezers, move_sq, frozen);
reported_movement[p] |= move_sq[4];
}
freezers |= pos.bitBoards[p - enemyoffset];
}
true_moves = 0;
reported_moves = 0;
for (int p = Piece.WCAT; p <= Piece.max; p++)
{
if (p == Piece.BRABBIT)
continue;
if (reported_movement[p] & ~true_movement[p])
{
Stdout.format("Incorrect moves for {}",
".RCDHMErcdhme"[p]).newline;
Stdout(bits_to_str(reported_movement[p]
& ~true_movement[p])).newline;
throw new Exception("Moves reported that could not be made.");
}
int true_count = popcount(true_movement[p]);
int reported_count = popcount(reported_movement[p]);
real found_per = true_count > 0
? (cast(real)reported_count / true_count) * 100.0
: 100.0;
Stdout.format("For {} found {} of {} ({:.2}) move squares.",
".RCDHMErcdhme"[p],
reported_count, true_count, found_per).newline;
if (reported_count == 0 && true_count != 0)
{
Stdout.format("False complete blockade for {}",
".RCDHMErcdhme"[p]).newline;
throw new Exception("False full blockade found.");
}
true_moves += true_count;
reported_moves += reported_count;
if (reported_count < 4 && true_count >= 4)
{
false_blockade += 1;
}
else if (true_count < 4)
{
true_blockade += 1;
} else {
no_blockade += 1;
}
}
real total_per = (cast(real)reported_moves / true_moves) * 100.0;
Stdout.format("Overall for pos found {} of {} ({:.2}) move squares.",
reported_moves, true_moves, total_per).newline;
}
ulong random_bit(ulong bits)
{
int num = popcount(bits);
int bix = rand.uniformR!(int)(num);
ulong b;
for (int i=0; i <= bix; i++)
{
b = bits & -bits;
bits ^= b;
}
return b;
}
void random_pos(inout Position pos)
{
Piece[] white_pieces;
white_pieces = [Piece.WELEPHANT, Piece.WCAMEL, Piece.WHORSE,
Piece.WHORSE, Piece.WDOG, Piece.WDOG, Piece.WCAT, Piece.WCAT].dup;
Piece[] black_pieces;
black_pieces = [Piece.BELEPHANT, Piece.BCAMEL, Piece.BHORSE,
Piece.BHORSE, Piece.BDOG, Piece.BDOG, Piece.BCAT, Piece.BCAT].dup;
ulong[] restricted_sqr = [0UL, RANK_8, 0, 0, 0, 0, 0,
RANK_1, 0, 0, 0, 0, 0];
int[] num_piece = [0, 8, 2, 2, 2, 1, 1, 8, 2, 2, 2, 1, 1];
ulong empty = ALL_BITS_SET;
ulong sqb;
for (Piece pt=Piece.WRABBIT; pt <= Piece.BELEPHANT; pt++)
{
int pt_side = pt < Piece.BRABBIT ? Side.WHITE : Side.BLACK;
auto place_num = rand.uniformR!(int)(num_piece[pt]);
if ((pt_side == Side.WHITE && pt >= Piece.WHORSE)
|| (pt_side == Side.BLACK && pt >= Piece.BHORSE))
place_num = num_piece[pt];
for (int n=0; n < place_num; n++)
{
sqb = random_bit(empty & ~(TRAPS
& ~neighbors_of(pos.placement[pt_side]))
& ~restricted_sqr[pt]);
empty ^= sqb;
pos.place_piece(pt, sqb);
}
}
pos.set_steps_left(4);
}
int main(char[][] args)
{
if (args.length < 2)
{
FilePath exec = new FilePath(args[0]);
Stdout.format("usage: {} <boardfile> | -r",
exec.name).newline;
return 1;
}
if (compare("-r", args[1]))
{
char[] boardstr;
boardstr = UnicodeFile!(char)(args[1], Encoding.Unknown).read();
Position pos = position.parse_long_str(boardstr);
Stdout("wb"[pos.side]).newline;
Stdout(pos.to_long_str(true)).newline;
Stdout(pos.to_placing_move()).newline;
Stdout().newline;
uint pos_moves, pos_reported, fb, tb, nb;
test_pos(pos, pos_moves, pos_reported, fb, tb, nb);
return 0;
}
// Check randomly generated positions
ulong total_moves, total_reported, total_false, total_true, total_none;
Position pos = new Position();
uint pos_num;
while (true)
{
pos_num++;
random_pos(pos);
Stdout(pos_num);
Stdout("wb"[pos.side]).newline;
Stdout(pos.to_long_str(true)).newline;
Stdout(pos.to_placing_move()).newline;
uint pos_moves, pos_reported, false_blk, true_blk, no_blk;
test_pos(pos, pos_moves, pos_reported, false_blk, true_blk, no_blk);
total_moves += pos_moves;
total_reported += pos_reported;
total_false += false_blk;
total_true += true_blk;
total_none += no_blk;
Stdout.format("All positions found {} moves of {} total ({:.2}), blockades {} false {} true {} not a blockade.",
total_reported, total_moves,
(cast(real)total_reported/total_moves) * 100.0,
total_false, total_true, total_none).newline;
Stdout.newline;
pos.clear();
}
return 0;
}
}