-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathposition.cpp
More file actions
686 lines (571 loc) · 15.2 KB
/
position.cpp
File metadata and controls
686 lines (571 loc) · 15.2 KB
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
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
//
// Created by austi on 12/9/2017.
//
#include "position.h"
namespace Zobrist
{
int64 holes[4][53];
int64 color[TEAM_COUNT];
int64 doubleCount[3];
}
void position::init()
{
prng rng(1070372);
for (int t = 0; t < TEAM_COUNT; t++)
{
for (int h = 0; h < 53; h++)
{
Zobrist::holes[t][h] = rng.rand64();
}
Zobrist::color[t] = rng.rand64();
}
for (int d = 0; d < 3; d++)
{
Zobrist::doubleCount[d] = rng.rand64();
}
}
position::position()
{
teamsMode = false;
disposed = false;
current = nullptr;
reset();
}
position::position(int64 t[TEAM_COUNT], int toMove, int doublesCnt, int removed)
{
teamsMode = false;
disposed = false;
current = new pstate();
current->key = 0;
current->removed = removed;
current->toMove = toMove;
current->doublesCnt = doublesCnt;
current->prev = nullptr;
for (int i = 0; i < TEAM_COUNT; i++)
{
teams[i] = t[i];
int64 m = t[i];
while (m)
{
int next = magic_popMsb(m);
current->key ^= Zobrist::holes[i][next];
}
}
current->key ^= Zobrist::color[toMove];
current->key ^= Zobrist::doubleCount[doublesCnt];
}
position::position(const position& pos)
{
teamsMode = pos.isTeamsMode();
disposed = false;
for (int i = 0; i < TEAM_COUNT; i++)
{
teams[i] = pos.teams[i];
}
copyState(pos.current);
}
position::~position()
{
dispose();
}
void position::dispose()
{
if (!disposed) {
deleteStates();
disposed = true;
}
}
void position::copyState(pstate *state)
{
assert(!disposed);
pstate *head;
pstate *curfrom = state;
while (curfrom != nullptr)
{
pstate *curcopy = new pstate();
curcopy->key = curfrom->key;
curcopy->removed = curfrom->removed;
curcopy->toMove = curfrom->toMove;
curcopy->doublesCnt = curfrom->doublesCnt;
assert(curcopy->isValid());
if (curfrom == state)
{
current = curcopy;
head = curcopy;
}
else
{
head->prev = curcopy;
head = curcopy;
}
curfrom = curfrom->prev;
}
}
void position::deleteStates()
{
pstate *cur = current;
while (cur != nullptr)
{
pstate* prev = cur->prev;
assert(cur->isValid());
delete cur;
cur = prev;
}
current = nullptr;
assert(current == nullptr);
}
bool position::isTeamsMode() const
{
return teamsMode;
}
int position::getToMove() const
{
return current->toMove;
}
int position::getDoublesCnt() const
{
return current->doublesCnt;
}
bool position::isValid() const
{
if (current == nullptr)
{
return false;
}
for (int i = 0; i < TEAM_COUNT; i++)
{
int64 mc = magic_marbleCount(teams[i]);
int64 invalidbits = teams[i] >> 53;
if (mc > MARBLE_COUNT || invalidbits)
{
return false;
}
}
return true;
}
std::string position::toString() const
{
std::string quads[4] = { "y", "r", "g", "b" };
std::string names[4] = { "yellow", "red", "green", "blue" };
std::string str = std::string();
for (int t = 0; t < TEAM_COUNT; t++)
{
int linepos = 0;
str.append("<");
str.append(names[t]);
str.append(">");
linepos += 2;
linepos += (int)names[t].length();
int nleft = 12 - (linepos % 12);
for (int m = 0; m < nleft; m++)
{
str.append(" ");
}
linepos += nleft;
int64 unsafe_left = magic_getRot48(teams[t], t, YELLOW);
while (unsafe_left)
{
int next = magic_popMsb(unsafe_left);
int index = next;
int quad = (index) / 12;
int quadIndex = (index % 12) + 1;
str.append(quads[quad]);
str.append(std::to_string(quadIndex));
str.append(" ");
}
int64 safe = magic_getSafe(teams[t]);
while (safe)
{
int next_safe = magic_popMsb(safe);
str.append("h");
str.append(quads[t]);
int index = next_safe;
int quadIndex = (index % 12) + 1;
str.append(std::to_string(quadIndex));
str.append(" ");
}
str.append("\n");
}
str.append("{ ");
for (int z = 0; z < TEAM_COUNT; z++)
{
str.append(std::to_string(teams[z]));
if (z != TEAM_COUNT - 1)
{
str.append(", ");
}
}
str.append("}\r\n");
return str;
}
void position::setTeamsMode(bool teamsMode)
{
this->teamsMode = teamsMode;
}
void position::set(std::string sq, int team)
{
set(team, notation::fromStr(sq, team));
}
void position::set(int team, int hole)
{
magic_set(teams[team], hole);
}
int position::removeLeading(int team)
{
assert(magic_isValid(teams[team]));
int64 pmag = teams[team];
int64 unsafe = magic_getUnsafe(pmag);
if (!unsafe)
{
return -1;
}
int leading = magic_getMsb(unsafe);
magic_remove(teams[team], leading);
assert(magic_isValid(teams[team]));
return leading;
}
void position::reset()
{
for (int i = 0; i < TEAM_COUNT; i++)
{
teams[i] = 0;
}
deleteStates();
current = new pstate();
current->key = 0;
current->removed = -1;
current->toMove = YELLOW;
current->doublesCnt = 0;
current->prev = nullptr;
}
void position::next(roll r)
{
assert(!disposed);
pstate* next = new pstate();
next->removed = -1;
next->doublesCnt = current->doublesCnt;
if (r.isDoubles() && current->doublesCnt + 1 < 3)
{
next->toMove = current->toMove;
next->doublesCnt++;
}
else
{
if (current->doublesCnt + 1 >= 3)
{
int r = removeLeading(current->toMove);
next->removed = r;
}
next->toMove = NEXT_TEAM(current->toMove);
next->doublesCnt = 0;
}
assert(current->isValid());
next->prev = current;
current = next;
assert(current->isValid());
//Remove old hash
current->key ^= Zobrist::doubleCount[current->prev->doublesCnt];
current->key ^= Zobrist::color[current->prev->toMove];
//Add new hash
current->key ^= Zobrist::color[current->toMove];
current->key ^= Zobrist::doubleCount[current->doublesCnt];
}
void position::doMove(int32 &m)
{
assert(!disposed);
assert(isValid());
int us = pseudomove_getTeam(m);
assert(us >= 0 && us < TEAM_COUNT);
int from = pseudomove_getFrom(m);
int to = pseudomove_getTo(m);
assert(from >= -1 && from <= 52 && to >=0 && to <= 53);
assert(magic_get(teams[us], to) == 0);
if (!pseudomove_isTakeout(m))
{
assert(magic_get(teams[us], from) == 1);
current->key ^= Zobrist::holes[us][from];
magic_remove(teams[us], from);
}
current->key ^= Zobrist::holes[us][to];
magic_set(teams[us], to);
assert(magic_get(teams[us], to) == 1);
assert(magic_isValid(teams[us]));
//Update other magics
for (int t = 0; t < TEAM_COUNT; t++)
{
if (t != us)
{
int64 rot = magic_getRot48(teams[t], t, us);
//Get taken marbles
int64 captured = rot & teams[us];
if (captured)
{
//Derotate
int64 derot = magic_getRot48(captured, us, t);
assert(magic_marbleCount(derot) == 1);
//Store captured
int pos = magic_getLsb(derot);
pseudomove_setCapturedTeam(m, t);
current->key ^= Zobrist::holes[t][pos];
//Remove taken marbles
teams[t] = teams[t] ^ derot;
//Only one marble can be captured per move.
assert(magic_isValid(teams[t]));
break;
}
}
}
assert(isValid());
}
void position::doMove(int64 &m, roll r)
{
assert(m != NONE);
assert(move_isValid(m));
int32 first = move_getFirst(m);
doMove(first);
if (move_isTwoStep(m))
{
int32 second = move_getSecond(m);
doMove(second);
m = move_make(first, second);
}
else
{
m = move_make(first);
}
next(r);
}
void position::undoMove(int64 &m)
{
assert(!disposed);
if (move_isTwoStep(m))
{
int32 second = move_getSecond(m);
undoMove(second);
}
int32 first = move_getFirst(m);
undoMove(first);
//Restore any removed leading marbles due to doubles roll
if (current->removed != -1)
{
int prevTeam = PREV_TEAM(current->toMove);
magic_set(teams[prevTeam], current->removed);
assert(magic_isValid(teams[prevTeam]));
}
//Copy previous before deleting current
pstate* prev = current->prev;
delete current;
//Restore previous state
current = prev;
}
void position::undoMove(int32& move)
{
int us = pseudomove_getTeam(move);
int from = pseudomove_getFrom(move);
int to = pseudomove_getTo(move);
assert(magic_get(teams[us], to) == 1);
magic_qremove(teams[us], to);
assert(magic_isValid(teams[us]));
assert(magic_get(teams[us], to) == 0);
if (!pseudomove_isTakeout(move))
{
magic_set(teams[us], from);
}
//Restore other team's marble
int t = pseudomove_getCapturedTeam(move);
if (t != -1)
{
int64 rot = ((int64)1 << to);
int64 derot = magic_getRot48(rot, us, t);
teams[t] = teams[t] ^ derot;
//Remove capture
pseudomove_removeCapture(move);
}
assert(magic_isValid(teams[us]));
}
int64 position::get(int team) const
{
assert(team >= 0 && team < TEAM_COUNT);
return teams[team];
}
int64 position::marbleCount(int team) const
{
assert(team >= 0 && team < TEAM_COUNT);
return magic_marbleCount(teams[team]);
}
int position::getWinner() const
{
for (int t = 0; t < TEAM_COUNT; t++)
{
if (magic_isWon(teams[t]))
{
return t;
}
}
return -1;
}
bool position::isLegalPseudo(int32 m) const
{
if (!pseudomove_isValid(m))
{
return false;
}
int f = pseudomove_getFrom(m);
int t = pseudomove_getTo(m);
int us = pseudomove_getTeam(m);
//Useless condition?
if (!pseudomove_isTakeout(m) && magic_get(teams[us], f) != 1)
{
return false;
}
int64 range = magic_getRange(teams[us], f + 1, t);
return magic_marbleCount(range) == 0;
}
bool position::isLegal(int64 &m) const
{
if (!move_isTwoStep(m))
{
return isLegalPseudo(move_getFirst(m));
}
else
{
int32 first = move_getFirst(m);
int32 second = move_getSecond(m);
if (!pseudomove_isValid(first)|| !pseudomove_isValid(second))
{
return false;
}
int us = pseudomove_getTeam(first);
int firstTo = pseudomove_getTo(first);
int firstFrom = pseudomove_getFrom(first);
int secondTo = pseudomove_getTo(second);
int secondFrom = pseudomove_getFrom(second);
int64 firstpath = magic_getRange(teams[us], firstFrom + 1, firstTo);
int64 secondpath = magic_getRange(teams[us], secondFrom + 1, secondTo);
if (!firstpath && !secondpath)
{
return true;
}
else if (firstpath)
{
return false;
}
int64 cnt = magic_marbleCount(secondpath);
int hit = secondFrom + magic_getLsb(secondpath) + 1;
return cnt == 1 && firstFrom == hit && secondTo < firstTo;
}
}
void position::genTakeOutMoves(movelist &moves, int team, int value) const
{
int32 takeoutmove = pseudomove_make(team, -1, 0);
int64 fulltakeout = move_make(takeoutmove, pseudomove_make(team, 0, value));
if (isLegal(fulltakeout))
{
moves.add(fulltakeout);
}
int64 marbles = teams[team];
while (marbles)
{
int curmarble = magic_popMsb(marbles);
int32 othermove = pseudomove_make(team, curmarble, curmarble + value);
int64 combined1 = move_make(takeoutmove, othermove);
int64 combined2 = move_make(othermove, takeoutmove);
if (isLegal(combined1))
{
moves.add(combined1);
}
else if (isLegal(combined2))
{
moves.add(combined2);
}
}
}
movelist position::getLegalMoves(const roll& roll, int team) const
{
movelist legal = movelist();
if (roll.isDoubles() && current->doublesCnt + 1 >= 3)
{
return legal; //No legal moves if roll doubles three times
}
//Generating a move
//Can you take out?
// Yes-Use the value of that die to take out, then use the rest to move any of the other marbles
//Permutations of using values from each die to move marbles
//foreach die value
// If is 1 or 6 takeout, then permutate through all marbles
int die1 = roll.getDie1();
int die2 = roll.getDie2();
//If can takeout
if (magic_marbleCount(teams[team]) < MARBLE_COUNT)
{
//Step 1. Generate take out move combinations for die1
if (die1 == 1 || die1 == 6)
{
genTakeOutMoves(legal, team, die2);
}
//Step 2. Generate take out move combinations for die2
if (die2 != die1 && (die2 == 1 || die2 == 6))
{
genTakeOutMoves(legal, team, die1);
}
}
//Step 3. Generate permutations for other marbles
int64 marbles = teams[team];
int64 split[MARBLE_COUNT] = { 0 };
int i = 0;
while (marbles)
{
int cur = magic_popMsb(marbles);
int64 curallforward = move_make(pseudomove_make(team, cur, cur + die1 + die2));
if (isLegal(curallforward))
{
legal.add(curallforward);
}
int32 cur1 = pseudomove_make(team, cur, cur + die1);
int32 cur2 = pseudomove_make(team, cur, cur + die2);
//Loop through all other marbles and split moves
int j = 0;
int64 othermarbles = teams[team];
while (othermarbles)
{
int other = magic_popMsb(othermarbles);
//Cant split with same marble
//also don't want to gen duplicate moves for marbles we've already split with
if (other == cur || magic_get(split[i], j))
{
j++;
continue;
}
//Record that we've generated split moves for the j and i marbles
magic_set(split[j], i);
int64 splitother1 = move_make(cur1, pseudomove_make(team, other, other + die2));
if (isLegal(splitother1))
{
legal.add(splitother1);
}
int64 splitother2 = move_make(cur2, pseudomove_make(team, other, other + die1));
if (die2 != die1 && isLegal(splitother2))
{
legal.add(splitother2);
}
j++;
}
i++;
}
return legal;
}
int64 position::getHash() const
{
return current->key;
}
bool position::equals(const position &pos) const
{
for (int t = 0;t < TEAM_COUNT; t++)
{
if (pos.teams[t] != teams[t])
{
return false;
}
}
return getToMove() == pos.getToMove() && getDoublesCnt() == pos.getDoublesCnt();
}