/
output.cpp
248 lines (209 loc) · 7.47 KB
/
output.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
/* Copyright (C) 2007-2011 Vincent Ollivier
*
* Purple Haze 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 3 of the License, or
* (at your option) any later version.
*
* Purple Haze 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, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <bitset>
#include "game.h"
static const int WIDE = 10;
void Game::print_thinking_header()
{
if (!output_thinking) return;
std::cout << std::setw(4) << "ply"
<< std::setw(WIDE - 1) << "score"
<< std::setw(WIDE) << "time"
<< std::setw(WIDE + 3) << "nodes"
<< std::setw(WIDE) << "pv"
<< std::endl;
}
void Game::print_thinking(int depth, int score, Move m)
{
if (!output_thinking) return;
std::cout << std::setw(4) << depth
<< std::setw(WIDE - 1) << score
<< std::setw(WIDE) << time.get_elapsed_time()
<< std::setw(WIDE + 3) << nodes_count
<< std::setw(WIDE - 3) << " ";
const int ply = tree.get_ply();
if (current_position().get_turn_color() == BLACK) {
std::cout << " " << 1 + (ply / 2) << ". ...";
}
assert(is_legal(m) || assert_msg(debug_move(m)));
std::cout << output_pv(depth, score, m) << std::endl;
}
bool is_mate(int score)
{
return ((score < -INF + MAX_PLY) || (INF - MAX_PLY < score));
}
std::string Game::output_pv(int depth, int score, Move m)
{
std::ostringstream stream;
stream << " ";
const int ply = tree.get_ply();
if (current_position().get_turn_color() == WHITE) {
stream << 1 + (ply / 2) << ". ";
}
stream << output_move(m);
make_move(m);
bool is_in_check = is_check(current_position().get_turn_color());
// Find next move in TT
bool is_empty;
Transposition trans = tt.lookup(current_position().hash(), &is_empty);
Move move = trans.get_best_move();
if (depth > 0 && is_legal(move) && trans.get_bound() < 3) {
if (is_in_check) stream << "+"; // Check
stream << output_pv(depth - 1, trans.get_value(), move);
} else if (move.is_null() && is_mate(score)) {
if (is_in_check) stream << "#"; // Mate
} else if (is_in_check) {
stream << "+"; // Cut-off
}
undo_move(m);
return stream.str();
}
std::string Game::output_move(Move m)
{
std::ostringstream stream;
// Castling
if (m.is_castle()) {
if (m.get_castle_side() == QUEEN) stream << "O-";
return stream.str() + "O-O";
}
// Type of piece
Square from = m.get_orig();
Piece p = board.get_piece(from);
PieceType t = p.get_type();
if (t > PAWN) stream << Piece(WHITE, t); // Upper case
// Disambiguation
if (t != PAWN) {
Color c = p.get_color();
Square to = m.get_dest();
for (int i = 0; i < pieces.count(c, t); ++i) {
Piece other(c, t, i);
if (other == p) continue;
Square s = pieces.get_position(other);
if (board.can_attack(t, s, to) && board.can_go(other, s, to)) {
// If another piece of the same type can theoretically
// attack the destination (fast answer by array lookup)
// and can really go to this destination (not so fast
// answer) then a disambiguation is needed
stream << static_cast<char>('a' + m.get_orig_file());
break;
}
}
}
// Capture
if (m.is_capture()) {
if (t == PAWN) stream << static_cast<char>('a' + m.get_orig_file());
stream << "x";
}
// Destination
stream << output_square(m.get_dest_file(), m.get_dest_rank());
// Promotion
if (m.is_promotion()) {
stream << "=" << Piece(WHITE, m.get_promotion_type());
}
return stream.str();
}
std::string Game::output_square(File f, Rank r)
{
std::ostringstream stream;
stream << static_cast<char>('a' + f) << static_cast<char>('1' + r);
return stream.str();
}
std::string get_stat(std::string title, double value, std::string unit = "")
{
std::ostringstream stream;
stream << std::left << " " << std::setw(20) << title;
int precision = (unit == "%" ? 2 : 0);
stream << std::fixed << std::setprecision(precision) << value << unit;
return stream.str();
}
std::string get_meta(double value, std::string unit)
{
std::ostringstream stream;
stream << " ("
<< std::fixed << std::setprecision(2) << value << unit
<< ")";
return stream.str();
}
double get_percent(double a, double b)
{
return 100 * a / b;
}
template <class T>
std::string print_table_stats(const HashTable<T>& table, int table_size)
{
long zeros = 0;
long ones = 0;
for (int i = 0; i < table.size(); ++i) {
Hash h = table.get_hash_at(i);
if (!h) continue;
std::bitset<64> b = h;
int z = b.count();
zeros += 64 - z;
ones += z;
}
std::ostringstream stream;
stream << get_stat("Table Size", table_size / 1024 / 1024, "Mb");
stream << std::endl;
stream << get_stat("Entries", table.size());
stream << std::endl;
stream << get_stat("Usage", table.get_usage());
stream << get_meta(get_percent(table.get_usage(), table.size()), "%");
stream << std::endl;
stream << get_stat("0's", get_percent(zeros, 64 * table.get_usage()), "%");
stream << std::endl;
stream << get_stat("1's", get_percent(ones, 64 * table.get_usage()), "%");
stream << std::endl;
stream << get_stat("Lookups", table.get_nb_lookups());
stream << std::endl;
stream << get_stat("Hits", table.get_nb_hits());
stream << get_meta(get_percent(table.get_nb_hits(),
table.get_nb_lookups()), "%");
stream << std::endl;
stream << get_stat("Collisions", table.get_nb_collisions());
stream << get_meta(get_percent(table.get_nb_collisions(),
table.get_nb_lookups()), "%");
stream << std::endl;
stream << get_stat("Misses", table.get_nb_misses());
stream << get_meta(get_percent(table.get_nb_misses(),
table.get_nb_lookups()), "%");
stream << std::endl;
return stream.str();
}
void Game::print_tt_stats()
{
std::cout << "Transposition Table usage:" << std::endl;
std::cout << print_table_stats(tt, TT_SIZE) << std::endl;
std::cout << "Material Table usage:" << std::endl;
std::cout << print_table_stats(material_table, MT_SIZE) << std::endl;
}
std::string Game::debug_move(Move m)
{
std::ostringstream stream;
Color c = current_position().get_turn_color();
stream << std::endl << board << std::endl
<< (c == WHITE ? "White" : "Black") << " to move" << std::endl
<< "m = " << output_move(m) << " (" << m << ")" << std::endl
<< "m is en passant: " << m.is_en_passant() << std::endl
<< "m is promotion: " << m.is_promotion() << std::endl
<< "m is legal: " << is_legal(m) << std::endl
<< std::hex << current_position().hash();
return stream.str();
}