-
Notifications
You must be signed in to change notification settings - Fork 1
/
hexgame.cpp
323 lines (276 loc) · 8.7 KB
/
hexgame.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
/*------------------------------------------------------------------------------
Hex game class implementation
------------------------------------------------------------------------------*/
#include <chrono>
#include <iostream>
#include <utility> // pair
#include "hexboard.hpp"
#include "hexgame.hpp"
#include "moveeval.hpp"
enum {
FLAG_NO_READ = 1
};
using namespace std;
void HexGame::start_prompt()
{
cout << "+---------------------------+" << endl;
cout << "| Hex game |" << endl;
cout << "| End the game with ctrl+c. |" << endl;
cout << "+---------------------------+" << endl;
cout << endl;
}
void HexGame::player_setup_prompt_and_set()
{
cout << "=========================" << endl;
cout << "Setup:" << endl;
bool entry_ok;
do {
cout << "Player X (plays first): (A)I or (H)uman?: ";
string type;
cin >> type;
// This is not very clean, but suffice for now.
entry_ok = char_to_player_type_set(type[0], player_X_type);
} while (!entry_ok);
do {
cout << "Player O: (A)I or (H)uman?: ";
string type;
cin >> type;
entry_ok = char_to_player_type_set(type[0], player_O_type);
} while (!entry_ok);
}
void HexGame::player_autoplay_setup(const char color)
{
if (color == 'X') {
player_X_type = PlayerType::AI;
player_O_type = PlayerType::OPPONENT_AI;
} else {
player_O_type = PlayerType::AI;
player_X_type = PlayerType::OPPONENT_AI;
}
}
int HexGame::autoplay_handshake(const char color)
{
// send handshake message color: name of program by author
// this string should uniquely identify the player
cout << color
<< ": Hexjakt by Gauthier Östervall, https://github.com/fleutot/hex"
<< endl;
if(color == 'X') {
char c;
// wait for other player's handshake message
cin >> c; // should be the other player's color
if(c != 'O') {
cerr << "X. E: expecting handshake message from O" << endl;
return -2;
}
cin >> c; // should be ':'
if(c != ':') {
cerr << "X. E: expecting : after O in handshake message" << endl;
return -3;
}
// ignore the rest of the line
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
return 0;
}
bool HexGame::autoplay_next_move_play()
{
pair<unsigned, unsigned> move;
int error_code = 0;
double elapsed_milli = 0.0;
bool opponent_give_up = false;
if (current_player_type_get() == PlayerType::AI) {
error_code = autoplay_input_move_make(move, elapsed_milli);
} else if (current_player_type_get() == PlayerType::OPPONENT_AI) {
do {
error_code = autoplay_opponent_move_read(move, opponent_give_up);
} while (error_code != 0);
}
if (error_code < 0) {
exit(error_code);
}
if (opponent_give_up) {
// game over.
return true;
}
bool win = board.play(move, current_player);
if (current_player_type_get() == PlayerType::AI) {
autoplay_move_print(move, win, elapsed_milli);
}
if (win) {
winner = current_player;
} else {
current_player.swap();
}
return win;
}
void HexGame::autoplay_move_print(pair<unsigned, unsigned> const& move,
const bool win, const double elapsed_milli)
{
static unsigned move_count = 0;
// Output for this new move.
cout << current_player;
move_print(move);
if (win) {
cout << ".";
} else {
cout << " ";
}
cout << "#" << ++move_count << " ";
cout << "t=" << static_cast<int>(elapsed_milli) << "ms" << endl;
}
void HexGame::autoplay_capitulate_print()
{
cout << current_player << "." << endl;
}
bool HexGame::next_prompt_and_play()
{
cout << "=========================" << endl;
cout << board << endl << endl;
cout << "X plays north and south." << endl;
cout << "O plays west and east." << endl;
cout << endl;
pair<unsigned, unsigned> move;
bool valid_move;
do {
cout << "Player " << current_player << ", please enter your move: ";
if (current_player_type_get() == PlayerType::HUMAN) {
valid_move = human_input_get(move);
} else if (current_player_type_get() == PlayerType::AI) {
valid_move = ai_input_get(move);
}
} while (!valid_move);
bool win = board.play(move, current_player);
if (win) {
winner = current_player;
} else {
current_player.swap();
}
return win;
}
void HexGame::winner_print()
{
cout << endl;
cout << board << endl << endl;
cout << "\t\t!!! Player " << winner << " wins !!!" << endl;
}
PlayerType HexGame::current_player_type_get()
{
if (current_player.get() == player_e::X) {
return player_X_type;
} else if (current_player.get() == player_e::O) {
return player_O_type;
} else {
cerr << __func__ << ": error in player type." << endl;
exit(1);
}
}
// Get the input pair in 0 based integers, return false if there was an error.
bool HexGame::human_input_get(pair<unsigned, unsigned>& move)
{
string in;
cin >> in;
unsigned col = toupper(in[0]) - 'A';
// Make the first char of the string a leading 0 (as suggested in the
// forums).
in[0] = '0';
unsigned row = atoi(in.c_str()) - 1;
if ((col >= board.size_get()) || (row >= board.size_get()) ||
board.occupied_check(col, row)
) {
cout << "############ Unauthorized move";
move_print(make_pair(col, row));
cout << ", try again! ###########" << endl;
return false;
}
move.first = col;
move.second = row;
return true;
}
bool HexGame::ai_input_get(pair<unsigned, unsigned>& move)
{
cout << "thinking... " << flush;
chrono::time_point<chrono::system_clock> start, end;
start = chrono::system_clock::now();
MoveEvaluator evaluator(board, current_player, max_simulations,
simulations_per_test_move);
move = evaluator.best_move_calculate();
cout << " -> ";
move_print(move);
cout << endl;
end = chrono::system_clock::now();
chrono::duration<double> elapsed_seconds = end - start;
cout << "Elapsed time: " << elapsed_seconds.count() << " s" << endl;
return true; // The AI only gives valid moves.
}
int HexGame::autoplay_input_move_make(pair<unsigned, unsigned>& move,
double& milliseconds)
{
chrono::time_point<chrono::system_clock> start, end;
start = chrono::system_clock::now();
MoveEvaluator evaluator(board, current_player, max_simulations,
simulations_per_test_move);
move = evaluator.best_move_calculate();
end = chrono::system_clock::now();
chrono::duration<double, milli> elapsed_milliseconds = end - start;
milliseconds = elapsed_milliseconds.count();
return 0; // Assume the AI only gives valid moves.
}
int HexGame::autoplay_opponent_move_read(pair<unsigned, unsigned>& move,
bool& give_up)
{
give_up = false;
char column;
unsigned col_n, row_n; // zero-based indeces/
char color;
cin >> color;
cin >> column; // lower case letter representing board column. May be dot if
// the player wants to quit.
if (color != 'O' && color != 'X') {
cerr << "E: received illegal color: " << color << endl;
return -6;
}
if (column == ':') {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return FLAG_NO_READ; // return with no error, but a flag that nothing was read.
}
if(column == '.') {
give_up = true;
return 0;
}
col_n = tolower(column) - 'a';
if(col_n >= board.size_get()) {
cerr << color << ": E: " << color <<
" received illegal column: '" << column << "'\n";
return -4;
}
cin >> row_n;
if(row_n > board.size_get()) {
cerr << color << ": E: " << color <<
" received illegal row: '" << row_n << "'\n";
return -5;
}
move = make_pair(col_n, row_n - 1);
char c = cin.peek();
if(c == '.') { // dot at the end of the other player's move
// means that he wins, or maybe he gives up - game over
return 0;
}
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return 0;
}
bool HexGame::char_to_player_type_set(const char c, PlayerType& type)
{
if (toupper(c) == 'H') {
type = PlayerType::HUMAN;
return true;
} else if (toupper(c) == 'A') {
type = PlayerType::AI;
return true;
} else {
return false;
}
}