-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChess.cpp
4575 lines (3791 loc) · 114 KB
/
Chess.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
/**
** Chess.cpp
** A simple UCI-compatible chess engine
** http://chess.johnbyrd.org
**
** astyle options:
** --align-pointer=name --align-reference=name --max-code-length=80 --pad-paren-in --pad-header --remove-brackets --convert-tabs --mode=c
** http://creativecommons.org/licenses/by/3.0/
**/
/**
** \todo Distance to mate reporting is wrong
** \todo Take castling into account in computing hashes
** \todo Understand pawn structure
** \todo Better endgame logic for say KRK
**/
#include "BuildInfo.h"
#define WEB_URL "http://chess.johnbyrd.org"
/** Number of rows and columns on the board */
const unsigned int MAX_FILES = 8;
/** See the table in the PieceInitializer() constructor for more details on this */
const unsigned int NUM_PIECES = 13;
const unsigned int HIGHEST_FILE = MAX_FILES - 1;
const unsigned int MAX_SQUARES = MAX_FILES * MAX_FILES;
/** Amount of memory to dedicate to position hash table, must be a power of 2, currently max 2 GB */
const unsigned int HASH_TABLE_SIZE = 128 * 1024 * 1024;
/** Maximum command length for UCI commands. */
const unsigned int MAX_COMMAND_LENGTH = 64 * 256;
/** Default search depth */
const unsigned int DEFAULT_SEARCH_DEPTH = 6; //-V112
/** An estimate of a reasonable maximum of moves in any given position. Not
** a hard bound.
**/
const unsigned int DEFAULT_MOVES_SIZE = 2 << 6;
#include <time.h>
#include <string>
#include <iostream>
#include <vector>
#include <array>
#include <algorithm>
#include <sstream>
#include <unordered_map>
#include <thread>
#include <mutex>
#include <memory>
#include <chrono>
#include <climits>
#include <ratio>
#include <atomic>
#include <random>
/** A number which is larger than any board score and idempotent under double negation */
const int BIG_NUMBER = 1000000;
using namespace std;
void Die( const string &s );
typedef bool Color;
const Color BLACK = false;
const Color WHITE = true;
/** The base class for all objects in the program. */
class Object
{
};
enum PieceType
{
NONE = 0,
PAWN,
KNIGHT,
BISHOP,
ROOK,
QUEEN,
KING
};
const int NONE_VALUE = 0;
const int PAWN_VALUE = 100;
const int KNIGHT_VALUE = 325;
const int BISHOP_VALUE = 325;
const int ROOK_VALUE = 500;
const int QUEEN_VALUE = 975;
const int CHECKMATE_VALUE = 990000; // any abs. value greater than this is mate
const int KING_VALUE = 1000000;
const int DRAW_SCORE = 0;
class PieceInitializer;
class Board;
class Move;
class Moves;
class Square;
class Interface;
class Position;
/* Definitions specifically to speed along function creation in the Interface class. */
#define INTERFACE_FUNCTION_PARAMS const string &sParams
#define INTERFACE_FUNCTION_NO_PARAMS const string &
#define INTERFACE_FUNCTION_RETURN_TYPE void
#define INTERFACE_PROTOTYPE( FunctionName ) INTERFACE_FUNCTION_RETURN_TYPE FunctionName ( INTERFACE_FUNCTION_PARAMS )
#define INTERFACE_PROTOTYPE_NO_PARAMS( FunctionName ) INTERFACE_FUNCTION_RETURN_TYPE FunctionName ( INTERFACE_FUNCTION_NO_PARAMS )
#define INTERFACE_FUNCTION_TYPE( Variable ) INTERFACE_FUNCTION_RETURN_TYPE ( Interface::* Variable )( INTERFACE_FUNCTION_PARAMS )
#define INTERFACE_FUNCTION_TYPE_NO_PARAMS( Variable ) INTERFACE_FUNCTION_RETURN_TYPE ( Interface::* Variable )( INTERFACE_FUNCTION_NO_PARAMS )
#define INTERFACE_FUNCTION_ABSTRACT_TYPE (*( INTERFACE_FUNCTION_RETURN_TYPE )())
typedef INTERFACE_FUNCTION_RETURN_TYPE( Interface::*InterfaceFunctionType )(
INTERFACE_FUNCTION_PARAMS );
typedef std::array<int, MAX_SQUARES> PieceSquareRawTableType;
PieceSquareRawTableType psrtDefault =
{
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
};
/* "A knight on the rim is grim." */
PieceSquareRawTableType psrtKnight =
{
-40, -30, -30, -30, -30, -30, -30, -40,
-40, -20, 0, 0, 0, 0, -20, -40,
-30, 0, 5, 10, 10, 5, 0, -50,
-30, 10, 15, 30, 30, 15, 10, -30,
-30, 10, 15, 30, 30, 15, 10, -30,
-30, 0, 10, 15, 15, 10, 0, -50,
-40, -20, 0, 0, 0, 0, -20, -40,
-40, -30, -30, -30, -30, -30, -30, -40
};
PieceSquareRawTableType psrtWhitePawnEarly =
{
0, 0, 0, 0, 0, 0, 0, 0,
5, 5, 10, -20, -20, 10, 5, 5,
5, -5, -10, 0, 0, -10, -5, 5,
0, 0, 0, 20, 20, 0, 0, 0,
20, 20, 30, 40, 40, 30, 30, 20,
40, 50, 60, 80, 80, 60, 50, 40,
60, 70, 80, 100, 100, 80, 70, 60,
0, 0, 0, 0, 0, 0, 0, 0
};
/* Go for the touchdown!*/
PieceSquareRawTableType psrtWhitePawnLate =
{
0, 0, 0, 0, 0, 0, 0, 0,
5, 5, 10, -20, -20, 10, 5, 5,
5, -5, -5, 0, 0, -5, -5, 5,
0, 0, 20, 40, 40, 20, 0, 0,
30, 30, 30, 50, 50, 50, 50, 50,
70, 70, 70, 70, 70, 70, 70, 70,
100,100, 100, 100, 100, 100, 100, 100,
0, 0, 0, 0, 0, 0, 0, 0
};
PieceSquareRawTableType psrtBishop =
{
-20, -10, -10, -10, -10, -10, -10, -20,
-10, 0, 0, 0, 0, 0, 0, -10,
-10, 0, 5, 10, 10, 5, 0, -10,
-10, 5, 5, 10, 10, 5, 5, -10,
-10, 5, 5, 10, 10, 5, 5, -10,
-10, 0, 5, 10, 10, 5, 0, -10,
-10, 0, 0, 0, 0, 0, 0, -10,
-20, -10, -10, -10, -10, -10, -10, -20
};
PieceSquareRawTableType psrtRook =
{
0, 0, 0, 20, 20, 20, 0, 0
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
20, 20, 20, 20, 20, 20, 20, 20,
-5, 0, 0, 0, 0, 0, 0, 0
};
PieceSquareRawTableType psrtWhiteKingEarly =
{
0, 0, 70,0, 0, 0,70, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
};
PieceSquareRawTableType psrtWhiteKingLate =
{
-50, -40, -30, -30, -30, -30, -40, -50,
-30, -20, -10, 0, 0, -10, -20, -30,
-30, -10, 30, 40, 40, 30, -10, -30,
-30, -10, 40, 60, 60, 40, -10, -30,
-30, -10, 40, 60, 60, 40, -10, -30,
-30, -10, 30, 40, 40, 30, -10, -30,
-30, -30, 0, 0, 0, 0, -30, -30,
-50, -30, -30, -30, -30, -30, -30, -50
};
class PieceSquareTableBase : public Object
{
public:
PieceSquareTableBase()
{
for ( unsigned int i = 0; i < MAX_SQUARES; i++ )
m_SourceTable[ i ] = 0;
}
PieceSquareTableBase( const PieceSquareRawTableType &table )
{
m_SourceTable = table;
}
virtual void InvertColor()
{
PieceSquareRawTableType temp;
temp = m_SourceTable;
for ( unsigned int i = 0; i < MAX_FILES; i++ )
for ( unsigned int j = 0; j < MAX_FILES; j++ )
{
m_SourceTable[ i + j * MAX_FILES ] =
temp[ i + ( ( MAX_FILES - 1 ) - j ) * MAX_FILES];
}
}
virtual int Get( unsigned int index ) const
{
return m_SourceTable[ ( size_t ) index ];
}
PieceSquareRawTableType m_SourceTable;
};
const int MAX_PIECE_SQUARE_INTERPOLATIONS = 64;
class PieceSquareTableInterpolating : PieceSquareTableBase
{
public:
PieceSquareTableInterpolating() :
m_nCurrentTable( 0 ),
m_fPhase( 0.0f )
{
}
void Append( const PieceSquareRawTableType &table,
const float fInterpolationFactor = 0.0f )
{
/* for now, we're order sensitive on this. */
PieceSquareTableBase baseTable( table );
m_SourceTables.push_back( baseTable );
m_Phases.push_back( fInterpolationFactor );
}
PieceSquareTableInterpolating( const PieceSquareRawTableType &table,
const float fInterpolationFactor = 0.0f ) :
m_nCurrentTable( 0 ),
m_fPhase( 0.0f )
{
Append( table, fInterpolationFactor );
}
virtual void InvertColor()
{
for ( auto &table : m_InterpolatedTables )
table.InvertColor();
}
virtual int Get( unsigned int index, const float fPhase = 0.0f ) const
{
size_t nSize = m_InterpolatedTables.size();
if ( m_InterpolatedTables.size() == 1 )
return m_InterpolatedTables[0].Get( index );
float fTable = fPhase * ( float )m_InterpolatedTables.size();
int nTable = ( int )fTable;
if ( nTable >= ( int )nSize )
nTable = ( int )nSize - 1;
if ( nTable < 0 )
nTable = 0;
return m_InterpolatedTables[nTable].Get( index );
}
virtual void CalculateInterpolations()
{
/* Only implemented for up to two children. Could reimplement for more. */
switch ( m_SourceTables.size() )
{
case 1:
m_nCurrentTable = 0;
m_InterpolatedTables.push_back( m_SourceTables[0] );
break;
case 2:
for ( unsigned int i = 0; i < MAX_PIECE_SQUARE_INTERPOLATIONS; i++ )
{
m_nCurrentTable = 0;
PieceSquareRawTableType pstInterpolated;
for ( unsigned int sq = 0; sq < MAX_SQUARES; sq++ )
{
float fScale;
fScale = ( float )i / ( float )MAX_PIECE_SQUARE_INTERPOLATIONS;
pstInterpolated[sq] = ( int ) (
( 1.0f - fScale ) * m_SourceTables[0].Get( sq ) +
fScale * m_SourceTables[1].Get( sq ) );
}
PieceSquareTableBase tableBase( pstInterpolated );
m_InterpolatedTables.push_back( tableBase );
}
break;
default:
Die( "Unsupported number of interpolated table sources!" );
}
}
protected:
typedef vector< PieceSquareTableBase > TablesType;
typedef vector< float > PhaseType;
TablesType m_InterpolatedTables;
TablesType m_SourceTables;
PhaseType m_Phases;
unsigned int m_nCurrentTable;
float m_fPhase;
};
typedef PieceSquareTableInterpolating PieceSquareTable;
class PieceSquareTableInitializer : Object
{
public:
PieceSquareTableInitializer()
{
Initialize();
}
void Initialize();
};
/** A centisecond wall clock. */
class Clock : Object
{
public:
typedef chrono::system_clock NativeClockType;
typedef NativeClockType::duration NativeClockDurationType;
typedef NativeClockType::time_point NativeTimePointType;
typedef int64_t ChessTickType;
typedef chrono::duration<ChessTickType, milli> Duration;
Clock()
{
Reset();
}
void Reset()
{
m_Start = m_Clock.now();
}
ChessTickType Get() const
{
NativeTimePointType timeNow;
timeNow = m_Clock.now();
Duration dur;
dur = chrono::duration_cast<Duration> ( timeNow - m_Start );
return dur.count();
}
void Start()
{
Reset();
}
void Test()
{
for ( int t = 0; t < 100; t++ )
{
chrono::milliseconds delay( 500 );
this_thread::sleep_for( delay );
cout << "Duration is now: " << Get() << endl;
}
}
protected:
NativeClockType m_Clock;
NativeTimePointType m_Start;
bool m_bIsRunning;
};
class Piece : Object
{
friend class PieceInitializer;
public:
Piece()
{
m_Color = BLACK;
m_PieceType = NONE;
m_pOtherColor = NULL;
m_PieceSquareTable = psrtDefault;
}
Piece( Color color )
{
m_Color = color;
m_PieceType = NONE;
m_pOtherColor = NULL;
m_PieceSquareTable = psrtDefault;
}
virtual int PieceValue() const = 0;
/** A unique index value for each piece. */
virtual int Index() const
{
return m_nIndex;
};
virtual void SetIndex( int i )
{
m_nIndex = i;
};
virtual Moves GenerateMoves( const Square &source,
const Position &pos ) const = 0;
virtual bool IsDifferent( const Square &dest, const Board &board ) const;
virtual bool IsDifferentOrEmpty( const Square &dest,
const Board &board ) const;
void SetOtherColor( Piece &otherPiece )
{
m_pOtherColor = &otherPiece;
}
Piece *InvertColor()
{
return m_pOtherColor;
}
char Letter() const
{
// in Forsyth-Edwards notation, white pieces are uppercase
if ( m_Color == BLACK )
return m_Letter;
return ( char ) toupper( m_Letter );
}
Color GetColor() const
{
return m_Color;
}
void SetColor( Color val )
{
m_Color = val;
}
PieceType Type() const
{
return m_PieceType;
}
const PieceSquareTable &GetPieceSquareTable() const
{
return m_PieceSquareTable;
}
void SetPieceSquareTable( const PieceSquareTable &val )
{
m_PieceSquareTable = val;
}
protected:
char m_Letter;
Color m_Color;
Piece *m_pOtherColor;
PieceType m_PieceType;
int m_nIndex;
PieceSquareTable m_PieceSquareTable;
};
class NoPiece : public Piece
{
public:
NoPiece()
{
m_Letter = '.';
m_PieceType = NONE;
m_pOtherColor = this;
}
int PieceValue() const
{
return NONE_VALUE;
}
Moves GenerateMoves( const Square &source, const Position &pos ) const;
};
class Pawn : public Piece
{
public:
Pawn( Color color ) : Piece( color )
{
m_PieceType = PAWN;
m_Letter = 'p';
}
int PieceValue() const
{
return PAWN_VALUE;
}
Moves GenerateMoves( const Square &source, const Position &pos ) const;
void AddEnPassantMove( Move &m, const Square &dest, Moves &moves ) const;
virtual void AddAndPromote( Moves &moves, Move &m,
const bool bIsPromote ) const;
};
class Bishop : public Piece
{
public:
Bishop( Color color ) : Piece( color )
{
m_Letter = 'b';
m_PieceType = BISHOP;
}
int PieceValue() const
{
return BISHOP_VALUE;
}
Moves GenerateMoves( const Square &source, const Position &pos ) const;
};
class Knight : public Piece
{
public:
Knight( Color color ) : Piece( color )
{
m_Letter = 'n';
m_PieceType = KNIGHT;
}
int PieceValue() const
{
return KNIGHT_VALUE;
}
Moves GenerateMoves( const Square &source, const Position &pos ) const;
};
class Rook : public Piece
{
public:
Rook( Color color ) : Piece( color )
{
m_Letter = 'r';
m_PieceType = ROOK;
}
int PieceValue() const
{
return ROOK_VALUE;
}
Moves GenerateMoves( const Square &source, const Position &pos ) const;
};
class Queen : public Piece
{
public:
Queen( Color color ) : Piece( color )
{
m_Letter = 'q';
m_PieceType = QUEEN;
}
int PieceValue() const
{
return QUEEN_VALUE;
}
Moves GenerateMoves( const Square &source, const Position &pos ) const;
};
class King : public Piece
{
public:
King( Color color ) : Piece( color )
{
m_Letter = 'k';
m_PieceType = KING;
}
int PieceValue() const
{
return KING_VALUE;
}
Moves GenerateCastlingMoves( const Square &source, const Position &pos ) const;
Moves GenerateMoves( const Square &source, const Position &pos ) const;
private:
King();
};
/* PVS-Studio objects to this casting of bool to class type */
Pawn WhitePawn( WHITE ), BlackPawn( BLACK ); //-V601
Knight WhiteKnight( WHITE ), BlackKnight( BLACK ); //-V601
Bishop WhiteBishop( WHITE ), BlackBishop( BLACK ); //-V601
Rook WhiteRook( WHITE ), BlackRook( BLACK ); //-V601
Queen WhiteQueen( WHITE ), BlackQueen( BLACK ); //-V601
King WhiteKing( WHITE ), BlackKing( BLACK ); //-V601
void PieceSquareTableInitializer::Initialize()
{
PieceSquareTable pstWhitePawn, pstBlackPawn, pstWhiteKnight, pstBlackKnight;
PieceSquareTable pstWhiteBishop, pstBlackBishop, pstWhiteRook, pstBlackRook;
PieceSquareTable pstWhiteQueen, pstBlackQueen, pstWhiteKing, pstBlackKing;
pstWhitePawn = PieceSquareTable();
pstWhitePawn.Append( psrtWhitePawnEarly , 0.0f );
pstWhitePawn.Append( psrtWhitePawnLate, 1.0f );
pstWhitePawn.CalculateInterpolations();
pstBlackPawn = pstWhitePawn;
pstBlackPawn.InvertColor();
pstWhiteBishop = PieceSquareTable( psrtBishop );
pstWhiteBishop.CalculateInterpolations();
pstBlackBishop = pstWhiteBishop;
pstBlackBishop.InvertColor();
pstWhiteKnight = PieceSquareTable( psrtKnight );
pstWhiteKnight.CalculateInterpolations();
pstBlackKnight = pstWhiteKnight;
pstBlackKnight.InvertColor();
pstWhiteRook = PieceSquareTable( psrtRook );
pstWhiteRook.CalculateInterpolations();
pstBlackRook = pstWhiteRook;
pstBlackRook.InvertColor();
pstWhiteQueen = PieceSquareTable( psrtDefault );
pstWhiteQueen.CalculateInterpolations();
pstBlackQueen = pstWhiteQueen;
pstBlackQueen.InvertColor();
pstWhiteKing = PieceSquareTable();
pstWhiteKing.Append( psrtWhiteKingEarly, 0.0f );
pstWhiteKing.Append( psrtWhiteKingLate, 1.0f );
pstWhiteKing.CalculateInterpolations();
pstBlackKing = pstWhiteKing;
pstBlackKing.InvertColor();
WhitePawn.SetPieceSquareTable( pstWhitePawn );
BlackPawn.SetPieceSquareTable( pstBlackPawn );
WhiteBishop.SetPieceSquareTable( pstWhiteBishop );
BlackBishop.SetPieceSquareTable( pstBlackBishop );
WhiteKnight.SetPieceSquareTable( pstWhiteKnight );
BlackKnight.SetPieceSquareTable( pstBlackKnight );
WhiteRook.SetPieceSquareTable( pstWhiteRook );
BlackRook.SetPieceSquareTable( pstBlackRook );
WhiteQueen.SetPieceSquareTable( pstWhiteQueen );
BlackQueen.SetPieceSquareTable( pstBlackQueen );
WhiteKing.SetPieceSquareTable( pstWhiteKing );
BlackKing.SetPieceSquareTable( pstBlackKing );
}
NoPiece None;
const Piece **AllPieces;
const int AllPiecesSize = 12;
class BoardBase : public Object
{
public:
BoardBase()
{
Initialize();
}
virtual const Piece *Set( int index, const Piece *piece )
{
return ( m_Piece[ index ] = piece );
}
virtual const Piece *Get( int index ) const
{
return ( m_Piece[ index ] );
}
virtual void Initialize()
{
for ( unsigned int i = 0; i < MAX_SQUARES; i++ )
Set( i, &None );
}
virtual const Piece *Set( int i, int j, const Piece *piece )
{
return ( Set( i + ( j << 3 ), piece ) );
}
const Piece *Get( int i, int j ) const
{
return Get( i + ( j << 3 ) );
}
const Piece *Set( const Square &s, const Piece *piece );
const Piece *Get( const Square &s ) const;
void Setup()
{
Initialize();
for ( unsigned int i = 0 ; i < MAX_FILES; i ++ )
{
Set( i, 1, &WhitePawn );
Set( i, 6, &BlackPawn );
}
Set( 0, 0, &WhiteRook );
Set( 7, 0, &WhiteRook );
Set( 0, 7, &BlackRook );
Set( 7, 7, &BlackRook );
Set( 1, 0, &WhiteKnight );
Set( 6, 0, &WhiteKnight );
Set( 1, 7, &BlackKnight );
Set( 6, 7, &BlackKnight );
Set( 2, 0, &WhiteBishop );
Set( 5, 0, &WhiteBishop );
Set( 2, 7, &BlackBishop );
Set( 5, 7, &BlackBishop );
Set( 3, 0, &WhiteQueen );
Set( 4, 0, &WhiteKing ); //-V112
Set( 3, 7, &BlackQueen );
Set( 4, 7, &BlackKing ); //-V112
}
void Flip()
{
const Piece *pTemp;
for ( unsigned int j = 0 ; j < ( MAX_FILES / 2 ); j++ )
for ( unsigned int i = 0; i < MAX_FILES; i++ )
{
pTemp = Get( i, j );
Set( i, j, Get( HIGHEST_FILE - i, HIGHEST_FILE - j ) );
Set( HIGHEST_FILE - i, HIGHEST_FILE - j, pTemp );
}
}
bool IsEmpty( const Square &square ) const;
void Dump() const
{
/* Note this weird for loop, which terminates when an unsigned int
* goes below 0, i.e. gets real big
*/
for ( unsigned int j = ( MAX_FILES - 1 ); j < MAX_FILES; j-- ) //-V621
{
for ( unsigned int i = 0; i < MAX_FILES; i++ )
cout << Get( i, j )->Letter();
cout << endl;
}
}
void Test()
{
Setup();
Dump();
Flip();
Dump();
Flip();
Dump();
}
protected:
const Piece *m_Piece[ MAX_FILES *MAX_FILES ];
};
typedef uint64_t HashValue;
HashValue s_PiecePositionHash[ MAX_SQUARES ][ NUM_PIECES ];
HashValue s_PieceColorHash[ 2 ];
class BoardHashing : public BoardBase
{
typedef BoardBase super;
public:
BoardHashing() : BoardBase(), m_Hash( 0 )
{
Initialize();
}
virtual void Initialize() override
{
m_Hash = 0;
super::Initialize();
}
virtual const Piece *Set( int index, const Piece *piece ) override
{
// first, erase the old piece if it is non-null
const Piece *curPiece = Get( index );
if ( curPiece != &None )
m_Hash ^= s_PiecePositionHash[ index ][ curPiece->Index() ];
// next, place the next piece if it is non-null
if ( piece != &None )
m_Hash ^= s_PiecePositionHash[ index ][ piece->Index() ];
return super::Set( index, piece );
}
/* Because the compiler gets hung up on trying to match the above function
* to the three-argument version of Set... sigh...
*/
virtual const Piece *Set( int i, int j, const Piece *piece ) override
{
return super::Set( i, j, piece );
}
HashValue GetHash() const
{
return m_Hash;
}
HashValue m_Hash;
};
class BoardPieceSquare : public BoardHashing
{
public:
virtual int GetPieceSquareValue( int index, const float fPhase ) const
{
return Get( index )->
GetPieceSquareTable().Get( index, fPhase );
}
virtual int GetPieceSquareValue( const Square &s, const float fPhase ) const;
};
class Board : public BoardPieceSquare {};
class HashInitializer
{
public:
HashInitializer()
{
mt19937_64 mt;
for ( unsigned int i = 0; i < MAX_SQUARES; i++ )
for ( unsigned int j = 0; j < NUM_PIECES; j++ )
s_PiecePositionHash[ i ][ j ] = mt();
for ( unsigned int i = 0; i < 2; i++ )
s_PieceColorHash[ i ] = mt();
}
};
class Square : public Object
{
public:
Square()
{
i = j = 0;
}
Square( int rank, int file )
{
i = rank;
j = file;
}
Square( const string &s )
{
i = s.at( 0 ) - 'a';
j = s.at( 1 ) - '1';
}
bool IsOnBoard() const
{
return ( ( ( i & ~7 ) == 0 ) && ( ( j & ~7 ) == 0 ) );
}
unsigned int ToIndex() const
{
return ( i + j * 8 );
}
int I() const
{
return i;
}
void I( int val )
{
i = val;
}
int J() const
{
return j;
}
void J( int val )
{
j = val;
}
void Set( int ip, int jp )
{
i = ip;
j = jp;
}
operator string() const
{
string s;
if ( IsOnBoard() )
{
s = ( char )( 'a' + i );
s += ( char )( '1' + j );
}
else
s = "-";
return s;
}
void Dump() const
{
if ( IsOnBoard() )
{
cout << ( char )( 'a' + i );
cout << ( char )( '1' + j );
}
else
cout << "??";
}
Square Change( int ip, int jp )
{
i += ip;
j += jp;
return *this;
}
Square Change( const Square &s )
{
i += s.i;