-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpawnsonly.cpp
1459 lines (1270 loc) · 36.9 KB
/
pawnsonly.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
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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2016 Sami Liedes
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#include "MemTranspositionTable.hpp"
#include "binom.hpp"
#include <algorithm>
#include <array>
#include <atomic>
#include <cassert>
#include <cmath>
#include <condition_variable>
#include <cstdint>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <mutex>
#include <signal.h>
#include <sstream>
#include <thread>
#include <vector>
static constexpr bool DEBUG = true;
// static constexpr int N = 7;
// static constexpr int VERBOSE_DEPTH = 3;
// static constexpr int PARALLEL_DEPTH = 10;
// static constexpr int CUT_MIN_DEPTH = 0;
// static constexpr int PARALLEL_MIN_DEPTH = 0;
// static constexpr size_t TP_TABLE_SIZE = 671088637; // 2.5 gigabytes
static constexpr int N = 8;
static constexpr int VERBOSE_DEPTH = 8;
static constexpr int PARALLEL_DEPTH = 18;
static constexpr int CUT_MIN_DEPTH = 4;
static constexpr int PARALLEL_MIN_DEPTH = 3;
static constexpr size_t TP_TABLE_SIZE = 6710886419; // 25 gigabytes
static constexpr int NUM_THREADS = 8;
//#define SAVE_NODES_LIMIT 50
//static constexpr int SAVE_LEVELS = 1;
// # of 8-byte elements; try to choose a prime
//static constexpr size_t TP_TABLE_SIZE = 30146531; // 115 megabytes
//static constexpr size_t TP_TABLE_SIZE = 134217689; // .5 gigabytes
//static constexpr size_t TP_TABLE_SIZE = 268435399; // 1 gigabyte
//static constexpr size_t TP_TABLE_SIZE = 536870909; // 2 gigabytes
//static constexpr size_t TP_TABLE_SIZE = 671088637; // 2.5 gigabytes
//static constexpr size_t TP_TABLE_SIZE = 1073741827; // 4 gigabytes
//static constexpr size_t TP_TABLE_SIZE = 1342177283; // 5 gigabytes
//static constexpr size_t TP_TABLE_SIZE = 3221225533; // 12 gigabytes
//static constexpr size_t TP_TABLE_SIZE = 6710886419; // 25 gigabytes
using std::array;
using std::atomic;
using std::cerr;
using std::condition_variable;
using std::cout;
using std::endl;
using std::ifstream;
using std::lock_guard;
using std::mutex;
using std::ostream;
using std::string;
using std::stringstream;
using std::thread;
using std::unique_lock;
using std::vector;
#define RESULT_ABORTED (-100)
static mutex cout_mutex;
// number of internal ranks (i.e. those on which pawns can be
// without the game being over)
static constexpr int NUM_RANKS = N-2;
// number of internal squares
static constexpr int NUM_ISQ = N*NUM_RANKS;
// starting ranks
static constexpr int RANK_WHITE = 0;
static constexpr int RANK_BLACK = NUM_RANKS-1;
// a pawn must be in one of these ranks to even be candidate for en passant
static constexpr int EP_RANK_WHITE = RANK_WHITE+2;
static constexpr int EP_RANK_BLACK = RANK_BLACK-2;
// tighter limit? N*2 is not enough
static constexpr int MAX_LEGAL_MOVES = N*3;
// compactly represented position
typedef uint64_t pos_t;
class Timer {
time_t start;
public:
Timer() { start = time(NULL); }
unsigned long elapsed() const { return time(NULL)-start; }
} timer;
static ostream &operator<<(ostream &str, const Timer &t) {
return str << "[" << t.elapsed() << "]";
}
static const int flip_horiz_sq(int sq) {
int rank = sq/N, file = sq%N;
return rank*N + (N-1-file);
}
static const char *player_name(int player) {
if (player == 1)
return "White";
else if (player == -1)
return "Black";
else
abort();
}
static void assert_valid_sq(int x, int y) {
assert(x >= 0);
assert(x < N);
assert(y >= 0);
assert(y < N-2);
}
static const int SQ(int x, int y) {
assert_valid_sq(x, y);
return y*N+x;
}
static string sqname(int x, int y) {
assert_valid_sq(x, y);
stringstream sb;
assert(N < 26);
sb << (char)('a'+x);
sb << 2+y;
return sb.str();
}
static string sqname(int sq) {
assert(sq >= 0);
assert(sq < NUM_ISQ);
return sqname(sq%N, sq/N);
}
// Singleton
class Compact_tab {
private:
static Compact_tab *instance;
static constexpr int SIZE = (N+1)*(N+1);
array<uint64_t, SIZE> tab;
public:
Compact_tab();
uint64_t operator[](int n) const {
assert(n >= 0);
assert(n < SIZE);
return tab[n];
}
uint64_t base(int nwhite, int nblack) const {
assert(nwhite >= 0 && nwhite <= N);
assert(nblack >= 0 && nblack <= N);
assert(nwhite != 0 || nblack != 0);
return tab[nwhite*(N+1)+nblack-1];
}
int num_white(int idx) const { return (idx+1)/(N+1); }
int num_black(int idx) const { return (idx+1)%(N+1); }
// returns the index of the last element <= n
int find(uint64_t n) const {
return std::upper_bound(&tab[0], &tab[SIZE], n) - tab.begin() - 1;
}
} ranks_tab;
Compact_tab *Compact_tab::instance = nullptr;
Compact_tab::Compact_tab() {
instance = this;
init_binom();
int p = 0;
tab[p++] = 0;
for (int white=0; white<=N; white++)
for (int black=0; black<=N; black++) {
if (white == 0 && black == 0)
continue;
// cout << "[" << p-1 << "]: " << white << "+" << black << ": "
// << binom(NUM_ISQ, white) * binom(NUM_ISQ, black) * 2 << endl;
tab[p] = tab[p-1] + binom(NUM_ISQ, white) * binom(NUM_ISQ, black) * 2 * (N+1);
p++;
}
assert(p == SIZE);
assert(tab[p-1] >> 62 == 0);
}
class Pos {
array<int, NUM_ISQ> sq; // 1 = white, -1 = black, 0 = empty
int turn; // 1 = white, -1 = black
mutable int num_white = -1, num_black = -1; // calculated if/when needed
int canonized_player_flip; // -1 changed player in canonize, else 1
bool horiz_flipped;
int ep_file; // en passant; -1 = no ep
void force_count_pieces() const;
void count_pieces() const { if (num_white == -1) force_count_pieces(); }
void clear();
// check if a hypothetical pawn at a square is unstoppable
bool is_unstoppable(int sq) const;
public:
struct Move {
// 'replacing' = the contents of the square moved to
// (so this information is enough to undo the move).
int from, to, replacing, value;
int new_ep_file, old_ep_file, ep_square;
// Move(int from, int to, int replacing, int value, int ep_square=-1)
// : from(from), to(to), replacing(replacing), value(value), ep_square(ep_square)
// {}
Move() : from(-1), to(-1), replacing(-100), value(-9999999), new_ep_file(-1),
old_ep_file(-9999999), ep_square(-1) {}
// used to remove symmetric moves if Pos::is_horiz_symmetric() returns true
bool is_from_right_half() const { return from >= (N+1)/2; }
Move decanonize(bool black, bool flip_horiz) const {
Move m(*this);
int from = this->from, to = this->to, replacing = this->replacing;
int old_ep_f = old_ep_file, new_ep_f = new_ep_file, ep_sq = ep_square;
if (black) {
from = NUM_ISQ-1-from;
to = NUM_ISQ-1-to;
if (old_ep_f != -1)
old_ep_f = N-1-old_ep_f;
if (new_ep_f != -1)
new_ep_f = N-1-new_ep_f;
if (ep_sq != -1)
ep_sq = NUM_ISQ-1-ep_sq;
replacing = -replacing;
}
if (flip_horiz) {
from = flip_horiz_sq(from);
to = flip_horiz_sq(to);
if (old_ep_f != -1)
old_ep_f = N-1-old_ep_f;
if (new_ep_f != -1)
new_ep_f = N-1-new_ep_f;
if (ep_sq != -1)
ep_sq = flip_horiz_sq(ep_sq);
}
m.from = from;
m.to = to;
m.replacing = replacing;
m.old_ep_file = old_ep_f;
m.new_ep_file = new_ep_f;
m.ep_square = ep_sq;
return m;
}
string name() const {
int from_file = from%N, from_rank = from/N,
to_file = to%N, to_rank = to/N;
assert(from_file >= 0);
assert(from_file < N);
assert(from_rank >= 0);
assert(from_rank < NUM_RANKS);
stringstream ss;
ss << ('a'+from_file) << ('1'+from_rank);
if (replacing)
ss << 'x';
else if (ep_square != -1)
ss << "(ep)";
ss << ('a'+to_file) << ('1'+to_rank);
return ss.str();
}
};
Pos(); // initial position
Pos(pos_t);
pos_t pack() const;
void check_sanity();
ostream &print(ostream &str) const;
void random_position();
void random_position(int nwhites, int nblacks);
int get_turn() const { return turn*canonized_player_flip; }
void canonize();
void horiz_mirror_board();
bool is_horiz_symmetric() const;
int winner() const; // -1 if won by black, 1 if by white, 0 otherwise
int get_canonize_flip() const { return canonized_player_flip; }
bool get_horiz_flipped() const { return horiz_flipped; }
// Returns count.
int get_legal_moves(array<Move, MAX_LEGAL_MOVES> &moves) const;
void do_move(const Move &move);
void undo_move(const Move &move);
bool operator==(const Pos &) const;
};
// check if a hypothetical pawn at a square is unstoppable
bool Pos::is_unstoppable(int s) const {
int file = s%N;
if (file != 0 && file != N-1) {
if (turn == 1) {
for (int s2=s+N; s2<NUM_ISQ; s2+=N)
if (sq[s2-1] != 0 || sq[s2] != 0 || sq[s2+1] != 0)
return false;
} else
for (int s2=s-N; s2>=0; s2-=N)
if (sq[s2-1] != 0 || sq[s2] != 0 || sq[s2+1] != 0)
return false;
} else {
int othersq;
if (file == 0)
othersq = 1;
else
othersq = -1;
if (turn == 1) {
for (int s2=s+N; s2<NUM_ISQ; s2+=N)
if (sq[s2] != 0 || sq[s2+othersq] != 0)
return false;
} else
for (int s2=s-N; s2>=0; s2-=N)
if (sq[s2] != 0 || sq[s2+othersq] != 0)
return false;
}
return true;
}
bool Pos::is_horiz_symmetric() const {
int left = 0, right = N-1;
while (left < right) {
for (int i=0; i<NUM_ISQ; i+=N)
if (sq[left+i] != sq[right+i])
return false;
left++;
right--;
}
return true;
}
void Pos::horiz_mirror_board() {
int left = 0, right = N-1;
while (left < right) {
for (int i=0; i<NUM_ISQ; i+=N) {
int tmp = sq[left+i];
sq[left+i] = sq[right+i];
sq[right+i] = tmp;
}
left++;
right--;
}
if (ep_file != -1)
ep_file = N-1-ep_file;
horiz_flipped = !horiz_flipped;
//check_sanity();
}
void Pos::canonize() {
// Canonize so that white is always to move
if (turn == -1) {
turn = 1;
canonized_player_flip = -canonized_player_flip;
for (int from=0, to=NUM_ISQ-1; from < to; from++, to--) {
int tmp = sq[from];
sq[from] = -sq[to];
sq[to] = -tmp;
}
if (NUM_ISQ%2 == 1)
sq[NUM_ISQ/2] = -sq[NUM_ISQ/2];
int tmp = num_white;
num_white = num_black;
num_black = tmp;
if (ep_file != -1)
ep_file = N-1-ep_file;
//check_sanity();
}
// Now possibly mirror the board horizontally
bool horiz_done = false;
for (int y=0; y<NUM_RANKS && !horiz_done; y++) {
int left = SQ(0,y);
int right = left+N-1;
while (left < right) {
if (sq[left] < sq[right]) {
horiz_mirror_board();
horiz_done = true;
break;
} else if (sq[left] > sq[right]) {
horiz_done = true;
break;
} else {
left++;
right--;
}
}
}
}
ostream &operator<<(ostream &os, const Pos::Move &move) {
os << sqname(move.from);
if (move.replacing)
os << "x";
os << sqname(move.to);
return os;
}
void Pos::do_move(const Move &move) {
assert(num_white >= 0 && num_black >= 0);
assert(sq[move.from] == turn);
assert(sq[move.to] == move.replacing);
sq[move.from] = 0;
sq[move.to] = turn;
bool captured = false;
if (move.replacing) {
assert(move.replacing == -turn);
captured = true;
} else if (move.ep_square != -1) {
assert(sq[move.ep_square] == -turn);
sq[move.ep_square] = 0;
captured = true;
}
if (captured) {
if (turn == 1)
num_black--;
else
num_white--;
}
assert(ep_file == move.old_ep_file);
ep_file = move.new_ep_file;
turn = -turn;
}
void Pos::undo_move(const Move &move) {
turn = -turn;
assert(sq[move.to] == turn);
assert(sq[move.from] == 0);
sq[move.from] = turn;
sq[move.to] = move.replacing;
bool captured = false;
if (move.replacing) {
assert(move.replacing == -turn);
captured = true;
} else if (move.ep_square != -1) {
assert(sq[move.ep_square] == 0);
sq[move.ep_square] = -turn;
captured = true;
}
if (captured) {
if (turn == 1)
num_black++;
else
num_white++;
}
assert(ep_file == move.new_ep_file);
ep_file = move.old_ep_file;
}
int Pos::get_legal_moves(array<Pos::Move, MAX_LEGAL_MOVES> &moves) const {
array<int, N> positions;
int num_pawns = 0, num_moves = 0;
//print(cerr);
if (winner() != 0)
return 0;
// for (int i=0; i<MAX_LEGAL_MOVES; i++)
// moves[i].value = 999;
// try to return potentially more useful moves first
if (turn == -1) {
for (int i=0; i<NUM_ISQ; i++)
if (sq[i] == turn)
positions[num_pawns++] = i;
} else {
for (int i=NUM_ISQ-1; i>=0; i--)
if (sq[i] == turn)
positions[num_pawns++] = i;
}
assert(num_pawns <= N);
// evaluation function: sum of ranks of pawns squared +
// 100*(2+rank) for best unstoppable pawn
int best_unstoppable = -1, best_unstoppable_rank = -1;
for (int i=0; i<num_pawns; i++) {
int s = positions[i], file = s%N;
int file_centrality = std::min(file, N-1-file);
int front = s + turn*N; // sq in front of current
int rank = s/N;
if (turn == -1)
rank = RANK_BLACK-rank;
if (sq[front] == 0) {
// front square empty, add it
moves[num_moves].from = s;
moves[num_moves].to = front;
moves[num_moves].value = rank+file_centrality;
if (rank+1 > best_unstoppable_rank && is_unstoppable(moves[num_moves].to)) {
best_unstoppable = num_moves;
best_unstoppable_rank = rank+1;
}
moves[num_moves++].replacing = 0;
if (N >= 5 && rank == 0) {
// move ahead 2 squares?
int front2 = front + turn*N;
assert(front2 >= 0);
assert(front2 < NUM_ISQ);
if (sq[front2] == 0) {
moves[num_moves].from = s;
moves[num_moves].to = front2;
moves[num_moves].value = rank+2+file_centrality;
if (rank+2 > best_unstoppable_rank && is_unstoppable(moves[num_moves].to)) {
best_unstoppable = num_moves;
best_unstoppable_rank = rank+2;
}
// if there's something that can capture this, mark file as en passant
if ((file != 0 && sq[front2-1] == -turn) ||
(file != N-1 && sq[front2+1] == -turn))
moves[num_moves].new_ep_file = file;
moves[num_moves++].replacing = 0;
}
}
}
if (file != 0 && sq[front-1] == -turn) {
// may capture to left
moves[num_moves].from = s;
moves[num_moves].to = front-1;
moves[num_moves].value = rank+file_centrality;
moves[num_moves].value += (NUM_RANKS-rank)*(NUM_RANKS-rank) + 1;
if (rank+1 > best_unstoppable_rank && is_unstoppable(moves[num_moves].to)) {
best_unstoppable = num_moves;
best_unstoppable_rank = rank+1;
}
moves[num_moves++].replacing = -turn;
}
if (file != N-1 && sq[front+1] == -turn) {
// may capture to right
moves[num_moves].from = s;
moves[num_moves].to = front+1;
moves[num_moves].value = rank+file_centrality;
moves[num_moves].value += (NUM_RANKS-rank)*(NUM_RANKS-rank) + 1;
if (rank+1 > best_unstoppable_rank && is_unstoppable(moves[num_moves].to)) {
best_unstoppable = num_moves;
best_unstoppable_rank = rank+1;
}
moves[num_moves++].replacing = -turn;
}
if ((turn == 1 && rank == EP_RANK_BLACK) || (turn == -1 && rank == EP_RANK_WHITE)) {
if (file != 0 && ep_file == file-1) {
// may capture en passant to left
assert(sq[s-1] == -turn);
assert(sq[front-1] == 0);
moves[num_moves].from = s;
moves[num_moves].to = front-1;
moves[num_moves].value = rank+file_centrality;
moves[num_moves].value += (NUM_RANKS-rank+1)*(NUM_RANKS-rank+1)+1;
if (rank+1 > best_unstoppable_rank && is_unstoppable(moves[num_moves].to)) {
best_unstoppable = num_moves;
best_unstoppable_rank = rank+1;
}
assert(sq[moves[num_moves].to] == 0);
moves[num_moves].ep_square = s-1;
moves[num_moves++].replacing = 0;
}
if (file != N-1 && ep_file == file+1) {
// may capture en passant to right
assert(sq[s+1] == -turn);
assert(sq[front+1] == 0);
moves[num_moves].from = s;
moves[num_moves].to = front+1;
moves[num_moves].value = rank+file_centrality;
moves[num_moves].value += (NUM_RANKS-rank+1)*(NUM_RANKS-rank+1)+1;
if (rank+1 > best_unstoppable_rank && is_unstoppable(moves[num_moves].to)) {
best_unstoppable = num_moves;
best_unstoppable_rank = rank+1;
}
moves[num_moves].ep_square = s+1;
moves[num_moves++].replacing = 0;
}
}
}
assert(num_moves <= MAX_LEGAL_MOVES);
if (best_unstoppable != -1)
moves[best_unstoppable].value += 100*(2+best_unstoppable_rank);
for (Move &m : moves)
m.old_ep_file = ep_file;
for (int i=0; i<num_moves-1; i++)
for (int j=i+1; j<num_moves; j++) {
//assert(moves[i].value != 999);
//assert(moves[j].value != 999);
if (moves[j].value > moves[i].value) {
Move tmp = moves[i];
moves[i] = moves[j];
moves[j] = tmp;
}
}
return num_moves;
}
int Pos::winner() const {
int base;
assert(num_white >= 0 && num_black >= 0);
if (num_white == 0) {
assert(num_black > 0);
return -1; /* *canonized_player_flip;*/
} else if (num_black == 0)
return 1; /* canonized_player_flip; */
if (turn == 1)
base = SQ(0, NUM_RANKS-1);
else {
assert(turn == -1);
base = SQ(0, 0);
}
for (int i=0; i<N; i++)
if (sq[base+i] == turn)
return turn; /**canonized_player_flip;*/
return 0;
}
bool Pos::operator==(const Pos &a) const {
if (turn != a.turn)
return false;
for (int i=0; i<NUM_ISQ; i++)
if (sq[i] != a.sq[i])
return false;
if (ep_file != a.ep_file)
return false;
return true;
}
void Pos::force_count_pieces() const {
num_white = num_black = 0;
for (int i=0; i<NUM_ISQ; i++)
if (sq[i] == -1)
num_black++;
else if (sq[i] == 1)
num_white++;
else
assert(sq[i] == 0);
if (num_white > N) {
cerr << "White has " << num_white << " pawns (>N)!" << endl;
this->print(cerr);
abort();
}
else if (num_black > N) {
cerr << "Black has" << num_black << " pawns (>N)!" << endl;
this->print(cerr);
abort();
}
}
Pos::Pos()
: turn(1)
, canonized_player_flip(1)
, horiz_flipped(false)
, ep_file(-1)
{
for (int i=0; i<NUM_ISQ; i++)
sq[i] = 0;
for (int i=0; i<N; i++) {
sq[SQ(i, RANK_WHITE)] = 1;
sq[SQ(i, RANK_BLACK)] = -1;
}
count_pieces();
}
pos_t Pos::pack() const {
count_pieces();
uint64_t base = ranks_tab.base(num_white, num_black);
array<array<int, NUM_ISQ>, 3> squares; // black, empty, white
array<int, 3> num_squares{{0}};
for (int i=0; i<NUM_ISQ; i++) {
//assert(sq[i] >= -1 && sq[i] <= 1);
squares[sq[i]+1][num_squares[sq[i]+1]++] = i;
}
int num_white = num_squares[2], num_black = num_squares[0];
uint64_t whites_rank = rank_combination(squares[2].data(), num_white);
uint64_t blacks_rank = rank_combination(squares[0].data(), num_black);
uint64_t offset = whites_rank;
offset = offset * binom(NUM_ISQ, num_black) + blacks_rank;
offset = offset * 2 + (turn == -1);
offset = offset * (N+1) + ep_file + 1;
bool error = false;
if (DEBUG) {
if (num_white != N || num_black != N) {
uint64_t base_range = ranks_tab[ranks_tab.find(base)+1]-ranks_tab[ranks_tab.find(base)];
if (offset >= base_range) {
cerr << "pack error: offset >= base_range." << endl;
error = true;
}
}
}
if (ranks_tab.find(base + offset) != num_white*(N+1)+num_black-1) {
cerr << "pack error: wrong index" << endl;
error = true;
}
if (error) {
print(cerr);
cerr << "base = " << base << endl;
cerr << "offset = " << offset << endl;
cerr << "whites_rank = " << whites_rank << endl;
cerr << "blacks_rank = " << blacks_rank << endl;
cerr << "num_white*(N+1)+num_black = " << num_white*(N+1)+num_black << endl;
cerr << "ranks_tab.find(base) = " << ranks_tab.find(base) << endl;
cerr << "ranks_tab@base = " << ranks_tab[ranks_tab.find(base)] << endl;
cerr << "base_range = " << ranks_tab[ranks_tab.find(base)+1]-ranks_tab[ranks_tab.find(base)] << endl;
cerr << "ranks_tab.find(base + offset) = index " << ranks_tab.find(base+offset) << endl;
cerr << "ranks_tab.find(base + offset) = " << ranks_tab[ranks_tab.find(base+offset)] << endl;
abort();
}
return base + offset;
}
Pos::Pos(pos_t compact) {
clear();
uint64_t idx = ranks_tab.find(compact);
assert(ranks_tab[idx] <= compact);
uint64_t base = ranks_tab[idx];
uint64_t offset = compact-base;
num_black = (idx+1)%(N+1);
num_white = (idx+1)/(N+1);
ep_file = (offset % (N+1)) - 1;
offset /= N+1;
if (offset % 2)
turn = -1;
else
turn = 1;
offset /= 2;
uint64_t b = binom(NUM_ISQ, num_black);
uint64_t blacks_rank = offset%b;
uint64_t whites_rank = offset/b;
array<int, N> squares;
unrank_combination(squares.data(), num_white, whites_rank);
for (int i=0; i<num_white; i++) {
assert(squares[i] >= 0);
assert(squares[i] < NUM_ISQ);
sq[squares[i]] = 1;
}
unrank_combination(squares.data(), num_black, blacks_rank);
for (int i=0; i<num_black; i++) {
assert(squares[i] >= 0);
assert(squares[i] < NUM_ISQ);
sq[squares[i]] = -1;
}
}
// mainly check that no player has more than N pawns
void Pos::check_sanity() {
assert(turn == -1 || turn == 1);
assert(num_white >= 0 && num_black >= 0);
int s = 0;
for (int i=0; i<NUM_ISQ; i++) {
if (sq[i] == 1)
s += i/N;
else if (sq[i] == -1)
s += (NUM_RANKS-1-i/N);
}
}
ostream &Pos::print(ostream &str) const {
array<char, N*2+2> delim;
for (int i=0; i<N; i++) {
delim[i*2] = '+';
delim[i*2+1] = '-';
}
delim[N*2] = '+';
delim[N*2+1] = 0;
for (int y=N-1; y >= 0; y--) {
str << delim.data() << "\n";
str << "|";
for (int x=0; x < N; x++) {
if (y == 0 || y == N-1)
str << " ";
else if (y > 0 && y < N-1) {
int t = sq[SQ(x, y-1)];
assert(t == 0 || t == -1 || t == 1);
str << "o x"[t+1];
}
str << "|";
}
if (y == 0)
str << " " << player_name(turn) << " to move";
str << "\n";
}
str << delim.data() << "\n";
return str;
}
void Pos::clear() {
for (int i=0; i<NUM_ISQ; i++)
sq[i] = 0;
num_white = num_black = 0;
}
void Pos::random_position() {
int w = 0, b = 0;
do {
int r = rand();
w = r % N;
r /= N;
b = r%N;
} while (w == 0 && b == 0);
random_position(w, b);
}
void Pos::random_position(int nw, int nb) {
assert(nw >= 0 && nw <= N);
assert(nb >= 0 && nb <= N);
assert(nw != 0 || nb != 0);
clear();
num_white = nw;
num_black = nb;
while (nw) {
int x = rand()%NUM_ISQ;
if (sq[x] == 0) {
sq[x] = 1;
nw--;
}
}
while (nb) {
int x = rand()%NUM_ISQ;
if (sq[x] == 0) {
sq[x] = -1;
nb--;
}
}
if (rand() % 2)
turn = -1;
else
turn = 1;
// now check if any of the pawns not in turn could be just moved
// two spaces, and possibly mark one of them as en passant
const int prev_turn = -turn;
const bool prev_was_white = (turn == -1);
const int ep_rank = prev_was_white ? EP_RANK_WHITE : EP_RANK_BLACK;
const int first_ep_square = SQ(0, ep_rank);
ep_file = -1;
int ep_files[N], ep_count=0;
int ep_backward = turn*N;
for (int i=0; i<N; i++) {
if (sq[first_ep_square+i] == prev_turn) {
if (((i != 0 && sq[first_ep_square+i-1] == turn) ||
(i != N-1 && sq[first_ep_square+i+1] == turn)) &&
sq[first_ep_square+i+ep_backward] == 0 &&
sq[first_ep_square+i+2*ep_backward] == 0)
ep_files[ep_count++] = i; // something can take this
}
}
if (ep_count == 0)
return;
int ep = rand() % (ep_count + 1);
if (ep == ep_count)
ep_file = -1; // no en passant this time
else
ep_file = ep_files[ep];
}
void count_boards() {
uint64_t total = 0;
cout << "Possible " << N << "x" << N << " boards with a+b pawns:" << endl;
for (int a=1; a<=N; a++)
for (int b=1; b<=N; b++) {
uint64_t count = binom(NUM_ISQ, a) * binom(NUM_ISQ, b);
total += count;
cout << a << "+" << b << "\t"
<< log(count)/log(2) << "\t\t" << count << endl;
}
cout << "\ntotal\t" << log(total)/log(2) << "\t\t" << total << endl;
}
void test_pack_unpack() {
int i = 0;
while (true) {
Pos p;
p.random_position();
uint64_t packed = p.pack();
//p.print(cout);
Pos p2(packed);
// cout << "p:\n";
// p.print(cout);
// cout << "p2:\n";
// p.print(cout);
assert(p == p2);
//cout << "--------------------\n";
//cout << p.pack() << endl;
if (++i % 10000 == 0)
cout << i << endl;
}
}
void test_do_undo_move() {
pos_t position_number;
int count = 0;
int verbose = 0;
while (true) {
Pos p;
p.random_position();
Pos origpos(p);
array<Pos::Move, MAX_LEGAL_MOVES> moves;
position_number = p.pack();
// if (position_number == 20137260669466283LL)
// verbose=1;
if (verbose) {
cout << "Getting legal moves for position " << position_number << ":" << endl;
p.print(cout);
}
int num_moves = p.get_legal_moves(moves);
if (num_moves == 0)
continue;
if (verbose)
p.print(cout);
for (int i=0; i<num_moves; i++) {
if (verbose)
cout << "Testing do-undo " << moves[i] << "..." << endl;
p.do_move(moves[i]);
//cout << "After move: " << endl;
//p.print(cout);
p.undo_move(moves[i]);
if (!(p == origpos)) {
cout << "Do-undo-move altered position! Original ("
<< origpos.pack() << ")" << endl;
origpos.print(cout);
cout << "After do-undo move " << moves[i] << " ("
<< p.pack() << "):" << endl;