Skip to content

Commit fc4b3bb

Browse files
author
Jesper Torp Kristensen
committed
Refactoring
1 parent 6cbd1ff commit fc4b3bb

22 files changed

+1210
-1244
lines changed

src/board.cxx

Lines changed: 10 additions & 264 deletions
Original file line numberDiff line numberDiff line change
@@ -7,250 +7,6 @@
77
#include "endgames/endgame_castling.hxx"
88
#include "endgames/endgame_en_passant.hxx"
99

10-
const bool WHITE_PIECE[13] =
11-
{ false,
12-
true, true, true, true, true, true,
13-
false, false, false, false, false, false
14-
};
15-
16-
const bool BLACK_PIECE[13] =
17-
{ false,
18-
false, false, false, false, false, false,
19-
true, true, true, true, true, true
20-
};
21-
22-
const int PIECE_COLOR[13] =
23-
{ -1,
24-
false, false, false, false, false, false,
25-
true, true, true, true, true, true
26-
};
27-
28-
const Piece PIECE_KIND[13] =
29-
{ 0,
30-
PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING,
31-
PAWN, KNIGHT, BISHOP, ROOK, QUEEN, KING
32-
};
33-
34-
const Piece SWAP_PIECE_COLOR[13] =
35-
{ 0,
36-
BPAWN, BKNIGHT, BBISHOP, BROOK, BQUEEN, BKING,
37-
WPAWN, WKNIGHT, WBISHOP, WROOK, WQUEEN, WKING
38-
};
39-
40-
const uchar CASTLING[64] =
41-
{ 0,0,WHITE_LONG_CASTLING,0,WHITE_LONG_CASTLING+WHITE_SHORT_CASTLING,0,WHITE_SHORT_CASTLING,0,
42-
0,0,0,0,0,0,0,0,
43-
0,0,0,0,0,0,0,0,
44-
0,0,0,0,0,0,0,0,
45-
0,0,0,0,0,0,0,0,
46-
0,0,0,0,0,0,0,0,
47-
0,0,0,0,0,0,0,0,
48-
0,0,BLACK_LONG_CASTLING,0,BLACK_LONG_CASTLING+BLACK_SHORT_CASTLING,0,BLACK_SHORT_CASTLING,0
49-
};
50-
51-
const uchar CASTLING_LOST[64] =
52-
{ 0xFF - WHITE_LONG_CASTLING,0xFF,0xFF,
53-
0xFF,0xFF - (WHITE_LONG_CASTLING+WHITE_SHORT_CASTLING),
54-
0xFF,0xFF,0xFF - WHITE_SHORT_CASTLING,
55-
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
56-
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
57-
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
58-
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
59-
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
60-
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
61-
0xFF - BLACK_LONG_CASTLING,0xFF,0xFF,
62-
0xFF,0xFF - (BLACK_LONG_CASTLING+BLACK_SHORT_CASTLING),
63-
0xFF,0xFF,0xFF - BLACK_SHORT_CASTLING
64-
};
65-
66-
const int ROW[64] =
67-
{ 0,0,0,0,0,0,0,0,
68-
1,1,1,1,1,1,1,1,
69-
2,2,2,2,2,2,2,2,
70-
3,3,3,3,3,3,3,3,
71-
4,4,4,4,4,4,4,4,
72-
5,5,5,5,5,5,5,5,
73-
6,6,6,6,6,6,6,6,
74-
7,7,7,7,7,7,7,7
75-
};
76-
77-
const int COLUMN[64] =
78-
{ 0,1,2,3,4,5,6,7,
79-
0,1,2,3,4,5,6,7,
80-
0,1,2,3,4,5,6,7,
81-
0,1,2,3,4,5,6,7,
82-
0,1,2,3,4,5,6,7,
83-
0,1,2,3,4,5,6,7,
84-
0,1,2,3,4,5,6,7,
85-
0,1,2,3,4,5,6,7
86-
};
87-
88-
const bool POS_COLOR[64] =
89-
{ 1,0,1,0,1,0,1,0,
90-
0,1,0,1,0,1,0,1,
91-
1,0,1,0,1,0,1,0,
92-
0,1,0,1,0,1,0,1,
93-
1,0,1,0,1,0,1,0,
94-
0,1,0,1,0,1,0,1,
95-
1,0,1,0,1,0,1,0,
96-
0,1,0,1,0,1,0,1
97-
};
98-
99-
const Position CR_TO_POS[8][8] = // Colum, Row
100-
{ { 0, 8,16,24,32,40,48,56},
101-
{ 1, 9,17,25,33,41,49,57},
102-
{ 2,10,18,26,34,42,50,58},
103-
{ 3,11,19,27,35,43,51,59},
104-
{ 4,12,20,28,36,44,52,60},
105-
{ 5,13,21,29,37,45,53,61},
106-
{ 6,14,22,30,38,46,54,62},
107-
{ 7,15,23,31,39,47,55,63}
108-
};
109-
110-
/*
111-
struct {
112-
uchar pieces_left[2];
113-
uchar num_pieces_left;
114-
uchar num_non_zugzwang_pieces_left;// 4 bits for each side
115-
} individual;
116-
*/
117-
const uint PIECE_COUNT_CONSTANTS[13] =
118-
{ 0,
119-
120-
0x00010001,
121-
0x01010001,
122-
0x01010001,
123-
0x01010001,
124-
0x01010001,
125-
0x00010001,
126-
127-
0x00010100,
128-
0x10010100,
129-
0x10010100,
130-
0x10010100,
131-
0x10010100,
132-
0x00010100
133-
};
134-
135-
/*
136-
struct {
137-
bool insufficient_material_b;
138-
bool insufficient_material_a;
139-
ushort endgame_hashing;
140-
} individual;
141-
*/
142-
const uint ENDGAME_HASHING_INSUFFICIENT_MATERIAL_CONSTANTS[13][2] = // [piece][POS_COLOR[pos]]
143-
{ {0,0},
144-
{0x0101 + (DB_WPAWN_VALUE<<16), 0x0101 + (DB_WPAWN_VALUE<<16)},
145-
{0x00E1 + (DB_WKNIGHT_VALUE<<16), 0x00E1 + (DB_WKNIGHT_VALUE<<16)},
146-
{0x0021 + (DB_WBISHOP_VALUE<<16), 0x0100 + (DB_WBISHOP_VALUE<<16)},
147-
{0x0101 + (DB_WROOK_VALUE<<16), 0x0101 + (DB_WROOK_VALUE<<16)},
148-
{0x0101 + (DB_WQUEEN_VALUE<<16), 0x0101 + (DB_WQUEEN_VALUE<<16)},
149-
{0x0000 + (DB_WKING_VALUE<<16), 0x0000 + (DB_WKING_VALUE<<16)},
150-
{0x0101 + (DB_BPAWN_VALUE<<16), 0x0101 + (DB_BPAWN_VALUE<<16)},
151-
{0x00E1 + (DB_BKNIGHT_VALUE<<16), 0x00E1 + (DB_BKNIGHT_VALUE<<16)},
152-
{0x0021 + (DB_BBISHOP_VALUE<<16), 0x0100 + (DB_BBISHOP_VALUE<<16)},
153-
{0x0101 + (DB_BROOK_VALUE<<16), 0x0101 + (DB_BROOK_VALUE<<16)},
154-
{0x0101 + (DB_BQUEEN_VALUE<<16), 0x0101 + (DB_BQUEEN_VALUE<<16)},
155-
{0x0000 + (DB_BKING_VALUE<<16), 0x0000 + (DB_BKING_VALUE<<16)}
156-
};
157-
158-
159-
const string game_result_texts[4] = {"*", "1/2-1/2", "1-0", "0-1"};
160-
161-
const string game_status_texts[7] =
162-
{ "* {Game still open}",
163-
"1/2-1/2 {Fifty move rule}",
164-
"1-0 {White mates}",
165-
"0-1 {Black mates}",
166-
"1/2-1/2 {Stalemate}",
167-
"1/2-1/2 {Draw by repetition}",
168-
"1/2-1/2 {Insufficient material}"
169-
};
170-
171-
172-
//######################################################
173-
174-
const char PIECE_CHAR[13] =
175-
{' ','P','N','B','R','Q','K','p','n','b','r','q','k'};
176-
const string PIECE_SCHAR[13] =
177-
{" ","P","N","B","R","Q","K","p","n","b","r","q","k"};
178-
179-
const string PIECE_NAME[13] =
180-
{ "no piece",
181-
"Pawn","Knight","Bishop","Rook","Queen","King",
182-
"pawn","knight","bishop","rook","queen","king"
183-
};
184-
const string PPIECE_NAME[13] =
185-
{ "no piece",
186-
"white pawn","white knight","white bishop","white rook","white queen","white king",
187-
"black pawn","black knight","black bishop","black rook","black queen","black king"
188-
};
189-
190-
const char PLAYER_CHAR[2] = {'w','b'};
191-
const string PLAYER_NAME[2] = {"white","black"};
192-
193-
194-
const string POS_NAME[66] =
195-
{ "a1","b1","c1","d1","e1","f1","g1","h1",
196-
"a2","b2","c2","d2","e2","f2","g2","h2",
197-
"a3","b3","c3","d3","e3","f3","g3","h3",
198-
"a4","b4","c4","d4","e4","f4","g4","h4",
199-
"a5","b5","c5","d5","e5","f5","g5","h5",
200-
"a6","b6","c6","d6","e6","f6","g6","h6",
201-
"a7","b7","c7","d7","e7","f7","g7","h7",
202-
"a8","b8","c8","d8","e8","f8","g8","h8",
203-
"##","##"
204-
};
205-
206-
const string COLUMN_NAME[66] =
207-
{ "a","b","c","d","e","f","g","h",
208-
"a","b","c","d","e","f","g","h",
209-
"a","b","c","d","e","f","g","h",
210-
"a","b","c","d","e","f","g","h",
211-
"a","b","c","d","e","f","g","h",
212-
"a","b","c","d","e","f","g","h",
213-
"a","b","c","d","e","f","g","h",
214-
"a","b","c","d","e","f","g","h",
215-
"#","#"
216-
};
217-
218-
const string ROW_NAME[66] =
219-
{ "1","1","1","1","1","1","1","1",
220-
"2","2","2","2","2","2","2","2",
221-
"3","3","3","3","3","3","3","3",
222-
"4","4","4","4","4","4","4","4",
223-
"5","5","5","5","5","5","5","5",
224-
"6","6","6","6","6","6","6","6",
225-
"7","7","7","7","7","7","7","7",
226-
"8","8","8","8","8","8","8","8",
227-
"#","#"
228-
};
229-
230-
//######################################################
231-
232-
Position REFLECTION_TABLE[8*64] =
233-
{ 0,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,
234-
7,6,5,4,3,2,1,0,15,14,13,12,11,10,9,8,23,22,21,20,19,18,17,16,31,30,29,28,27,26,25,24,39,38,37,36,35,34,33,32,47,46,45,44,43,42,41,40,55,54,53,52,51,50,49,48,63,62,61,60,59,58,57,56,
235-
56,57,58,59,60,61,62,63,48,49,50,51,52,53,54,55,40,41,42,43,44,45,46,47,32,33,34,35,36,37,38,39,24,25,26,27,28,29,30,31,16,17,18,19,20,21,22,23,8,9,10,11,12,13,14,15,0,1,2,3,4,5,6,7,
236-
63,62,61,60,59,58,57,56,55,54,53,52,51,50,49,48,47,46,45,44,43,42,41,40,39,38,37,36,35,34,33,32,31,30,29,28,27,26,25,24,23,22,21,20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1,0,
237-
0,8,16,24,32,40,48,56,1,9,17,25,33,41,49,57,2,10,18,26,34,42,50,58,3,11,19,27,35,43,51,59,4,12,20,28,36,44,52,60,5,13,21,29,37,45,53,61,6,14,22,30,38,46,54,62,7,15,23,31,39,47,55,63,
238-
56,48,40,32,24,16,8,0,57,49,41,33,25,17,9,1,58,50,42,34,26,18,10,2,59,51,43,35,27,19,11,3,60,52,44,36,28,20,12,4,61,53,45,37,29,21,13,5,62,54,46,38,30,22,14,6,63,55,47,39,31,23,15,7,
239-
7,15,23,31,39,47,55,63,6,14,22,30,38,46,54,62,5,13,21,29,37,45,53,61,4,12,20,28,36,44,52,60,3,11,19,27,35,43,51,59,2,10,18,26,34,42,50,58,1,9,17,25,33,41,49,57,0,8,16,24,32,40,48,56,
240-
63,55,47,39,31,23,15,7,62,54,46,38,30,22,14,6,61,53,45,37,29,21,13,5,60,52,44,36,28,20,12,4,59,51,43,35,27,19,11,3,58,50,42,34,26,18,10,2,57,49,41,33,25,17,9,1,56,48,40,32,24,16,8,0
241-
};
242-
243-
//######################################################
244-
245-
const int ENDGAME_HASHING_CONSTANTS[13] =
246-
{ 0,
247-
DB_WPAWN_VALUE, DB_WKNIGHT_VALUE, DB_WBISHOP_VALUE,
248-
DB_WROOK_VALUE, DB_WQUEEN_VALUE, DB_WKING_VALUE,
249-
DB_BPAWN_VALUE, DB_BKNIGHT_VALUE, DB_BBISHOP_VALUE,
250-
DB_BROOK_VALUE, DB_BQUEEN_VALUE, DB_BKING_VALUE
251-
};
252-
253-
//######################################################
25410

25511
Board::Board() {
25612
if (PRINT_CONSTRUCTOR_DESTRUCTOR_CALLS)
@@ -277,10 +33,9 @@ void Board::reset_all() {
27733
moves_played_since_progress = 0;
27834

27935
piece_count.as_pattern = 0;
280-
endgame_hashing_insufficient_material.as_pattern = 0;
36+
endgame_material.as_pattern = 0;
28137

28238
played_from_scratch = true; // whatever
283-
// cerr << "reset_all: Setting played_from_scratch = true\n";
28439
}
28540

28641
Board::~Board() {
@@ -1060,10 +815,7 @@ void Board::print_counters(ostream &os) {
1060815
<< (int)get_num_non_zugzwang_pieces(WHITE) << ","
1061816
<< (int)get_num_non_zugzwang_pieces(BLACK) << ")\n"
1062817
<< "endgame_hashing_insufficient_material: Pattern = "
1063-
<< toString(endgame_hashing_insufficient_material.as_pattern, 8, 16) << ":\n"
1064-
<< "\tget_insufficient_material_a() = " << (int)get_insufficient_material_a() << "\n"
1065-
<< "\tget_insufficient_material_b() = " << (int)get_insufficient_material_b() << "\n"
1066-
<< "\tget_endgame_hashing() = " << (int)get_endgame_hashing() << "\n";
818+
<< toString(endgame_material.as_pattern, 8, 16) << "\n";
1067819
}
1068820

1069821
void Board::print_board(ostream& os) const {
@@ -1134,9 +886,8 @@ void Board::print_board(ostream& os) const {
1134886
void Board::remove_piece(Position pos) {
1135887
assert(board[pos] && legal_pos(pos));
1136888

1137-
piece_count.as_pattern -= PIECE_COUNT_CONSTANTS[board[pos]];
1138-
endgame_hashing_insufficient_material.as_pattern -=
1139-
ENDGAME_HASHING_INSUFFICIENT_MATERIAL_CONSTANTS[board[pos]][POS_COLOR[pos]];
889+
piece_count_remove(piece_count, board[pos]);
890+
remove_endgame_material(endgame_material, board[pos], pos);
1140891

1141892
board[pos] = 0;
1142893
}
@@ -1146,27 +897,22 @@ void Board::insert_piece(Position pos, Piece piece) {
1146897

1147898
if (board[pos]) {
1148899
// capture piece
1149-
piece_count.as_pattern -= PIECE_COUNT_CONSTANTS[board[pos]];
1150-
endgame_hashing_insufficient_material.as_pattern -=
1151-
ENDGAME_HASHING_INSUFFICIENT_MATERIAL_CONSTANTS[board[pos]][POS_COLOR[pos]];
900+
piece_count_remove(piece_count, board[pos]);
901+
remove_endgame_material(endgame_material, board[pos], pos);
1152902
}
1153-
piece_count.as_pattern += PIECE_COUNT_CONSTANTS[piece];
1154-
endgame_hashing_insufficient_material.as_pattern +=
1155-
ENDGAME_HASHING_INSUFFICIENT_MATERIAL_CONSTANTS[piece][POS_COLOR[pos]];
903+
piece_count_add(piece_count, piece);
904+
add_endgame_material(endgame_material, piece, pos);
1156905

1157906
board[pos] = piece;
1158907
}
1159908

1160909
void Board::move_piece(Position from, Position to) {
1161910
assert(legal_pos(from) && legal_pos(to) && board[from]);
1162-
assert(ENDGAME_HASHING_INSUFFICIENT_MATERIAL_CONSTANTS[board[from]][POS_COLOR[from]] ==
1163-
ENDGAME_HASHING_INSUFFICIENT_MATERIAL_CONSTANTS[board[from]][POS_COLOR[to]]);
1164911

1165912
if (board[to]) {
1166913
// capture piece
1167-
piece_count.as_pattern -= PIECE_COUNT_CONSTANTS[board[to]];
1168-
endgame_hashing_insufficient_material.as_pattern -=
1169-
ENDGAME_HASHING_INSUFFICIENT_MATERIAL_CONSTANTS[board[to]][POS_COLOR[to]];
914+
piece_count_remove(piece_count, board[to]);
915+
remove_endgame_material(endgame_material, board[to], to);
1170916
}
1171917

1172918
board[to] = board[from];

0 commit comments

Comments
 (0)