-
Notifications
You must be signed in to change notification settings - Fork 0
/
reporter.cpp
310 lines (257 loc) · 7.76 KB
/
reporter.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
/*
* Quackle -- Crossword game artificial intelligence and analysis tool
* Copyright (C) 2005-2006 Jason Katz-Brown and John O'Laughlin.
*
* 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 <iomanip>
#include <iostream>
#include "alphabetparameters.h"
#include "computerplayer.h"
#include "datamanager.h"
#include "game.h"
#include "reporter.h"
using namespace Quackle;
void Reporter::reportPosition(const GamePosition &position, ComputerPlayer *computerPlayer, UVString *report)
{
UVOStringStream s;
UVOStringStream titleStream;
if (!position.gameOver())
titleStream << position.currentPlayer().name() << MARK_UV(": Turn ") << position.turnNumber() << MARK_UV('\n');
const Quackle::PlayerList players(position.endgameAdjustedScores());
for (PlayerList::const_iterator it = players.begin(); it != players.end(); ++it)
{
s.width(3);
s << right << ((*it) == position.currentPlayer()? MARK_UV("->") : MARK_UV(" "));
s << MARK_UV(' ');
s.width(24);
s << left << (*it).name() << MARK_UV(' ');
s.width(9);
s << (*it).rack().toString() << MARK_UV(' ');
s.width(4);
s << (*it).score();
s << MARK_UV('\n');
}
if (computerPlayer && !position.gameOver())
{
computerPlayer->setPosition(position);
if (position.committedMove().isAMove())
computerPlayer->considerMove(position.committedMove());
const unsigned int movesToShow = 10;
MoveList moves = computerPlayer->moves(movesToShow);
int ourMoveIndex = 0;
int i = 1;
for (Quackle::MoveList::const_iterator it = moves.begin(); it != moves.end(); ++it, ++i)
{
if ((*it) == position.committedMove())
{
ourMoveIndex = i;
break;
}
}
bool isUrp = false;
if (position.committedMove().isAMove())
{
// our move not in list
if (ourMoveIndex == 0)
{
if (moves.size() == movesToShow)
moves.pop_back();
isUrp = true;
ourMoveIndex = movesToShow;
moves.push_back(position.committedMove());
}
}
int highestScore = 0;
double highestEquity = 0;
unsigned int widestPositionString = 0;
unsigned int widestMove = 0;
bool hasWinPercentages = false;
const Quackle::MoveList::const_iterator end(moves.end());
for (Quackle::MoveList::const_iterator it = moves.begin(); it != end; ++it)
{
if ((*it).prettyTiles().length() > widestMove)
widestMove = (*it).prettyTiles().length();
if ((*it).positionString().length() > widestPositionString)
widestPositionString = (*it).positionString().length();
if ((*it).win > 0)
hasWinPercentages = true;
if ((*it).equity > highestEquity)
highestEquity = (*it).equity;
if ((*it).score > highestScore)
highestScore = (*it).score;
}
s << MARK_UV("--");
UVOStringStream headerStream;
headerStream << computerPlayer->name();
headerStream << "'s choices (your play: ";
if (isUrp)
headerStream << "urp";
else
headerStream << ourMoveIndex;
headerStream << ")";
s.width(43);
s << setfill(MARK_UV('-'));
s << left << headerStream.str() << MARK_UV('\n');
s << setfill(MARK_UV(' '));
i = 1;
for (Quackle::MoveList::const_iterator it = moves.begin(); it != end; ++it, ++i)
{
// column 2, the valuation.
s.width(5);
if ((*it).equity > (highestEquity - .01) && (*it).equity < (highestEquity + .01))
{
s << MARK_UV("best");
}
else
{
s << right << showpoint;
s.precision(3);
s << (highestEquity - (*it).equity);
}
s << (i == ourMoveIndex? MARK_UV("*") : MARK_UV(" "));
// column 3, the position string.
s << left;
s.width(widestPositionString);
switch ((*it).action)
{
case Move::Place:
s << (*it).positionString();
break;
case Move::Exchange:
s << MARK_UV("xch");
break;
case Move::Pass:
s << MARK_UV("pas");
break;
case Move::UnusedTilesBonus:
case Move::TimePenalty:
case Move::Nonmove:
break;
}
s << MARK_UV(" ");
// column 4, the word
s.width(widestMove);
s << left << QUACKLE_ALPHABET_PARAMETERS->userVisible((*it).prettyTiles()) << MARK_UV(" ");
// column 5, the score
s.width(highestScore >= 100? 3 : (highestScore >= 10? 2 : 1));
s << left << (*it).score << MARK_UV(" ");
// column 6, the win percentage
if (hasWinPercentages)
{
s.precision(4);
s.width(5);
s << showpoint << ((*it).win * 100.0) << MARK_UV("% ");
}
// column 7, the leave
s << (position.currentPlayer().rack() - (*it)).toString() << MARK_UV('\n');
}
}
if (position.gameOver())
{
s << MARK_UV("\n Game over.\n\n");
}
int j = 0;
UVString wrappedTiles;
LongLetterString unseenTiles = position.unseenBag().tiles();
for (Quackle::LongLetterString::const_iterator it = unseenTiles.begin(); it != unseenTiles.end(); ++it, ++j)
{
if (j >= 44)
{
wrappedTiles += MARK_UV('\n');
j = 0;
}
wrappedTiles += QUACKLE_ALPHABET_PARAMETERS->userVisible(*it);
}
s << MARK_UV("--");
s.width(43);
s << setfill(MARK_UV('-'));
s << MARK_UV("Tracking") << MARK_UV('\n');
s << wrappedTiles << MARK_UV(" ") << unseenTiles.size() << MARK_UV('\n');
UVString reportString = s.str();
UVString boardString = position.board().toString();
*report = titleStream.str();
// Ensure that the board ends with a newline.
boardString += MARK_UV("\n");
// Put board and report side by side.
UVString::const_iterator boardIt = boardString.begin();
UVString::const_iterator reportIt = reportString.begin();
UVString::const_iterator reportLineBeginning = reportString.begin();
UVString::const_iterator boardLineBeginning = boardString.begin();
const UVString::const_iterator reportEnd = reportString.end();
const UVString::const_iterator boardEnd = boardString.end();
while (true)
{
if (boardIt == boardEnd && reportIt == reportEnd)
break;
if (boardIt != boardEnd && (*boardIt) != MARK_UV('\n'))
{
++boardIt;
continue;
}
if (reportIt != reportEnd && (*reportIt) != MARK_UV('\n'))
{
++reportIt;
continue;
}
if (boardIt != boardEnd)
{
report->append(boardLineBeginning, boardIt);
}
if (reportIt != reportEnd)
{
report->append(MARK_UV(" "));
report->append(reportLineBeginning, reportIt);
}
boardLineBeginning = boardIt + 1;
reportLineBeginning = reportIt + 1;
if (boardIt != boardEnd)
++boardIt;
if (reportIt != reportEnd)
++reportIt;
report->append(MARK_UV("\n"));
}
}
void Reporter::reportHeader(const Game & /* game */, UVString *report)
{
UVOStringStream s;
s << MARK_UV("Game Report\nGenerated by Quackle crossword game AI and analysis software\nhttp://quackle.org\n\n");
*report = s.str();
}
void Reporter::reportGameStatistics(const Game &game, UVString *report)
{
(void) game;
UVOStringStream s;
*report = s.str();
}
void Reporter::reportGame(const Game &game, ComputerPlayer *computerPlayer, UVString *report)
{
UVOStringStream s;
UVString header;
reportHeader(game, &header);
s << header;
const PositionList::const_iterator end(game.history().end());
for (PositionList::const_iterator it = game.history().begin(); it != end; ++it)
{
UVString subreport;
reportPosition(*it, computerPlayer, &subreport);
s << subreport << MARK_UV('\n');
}
UVString stats;
reportGameStatistics(game, &stats);
s << stats;
*report = s.str();
}