-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhex.cpp
125 lines (109 loc) · 3.42 KB
/
hex.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
/*------------------------------------------------------------------------------
hex.cpp
Run a game of hex either:
- in a multiplayer environment, with API by Alexandre
Kharlamov, https://github.com/alexkh/hexai/blob/master/dummy.cpp
Big chunks of the code below are taken from the repository above.
- interactively, AI vs AI, Human vs Human, AI vs Human.
------------------------------------------------------------------------------*/
#include <chrono>
#include <climits>
#include <cstdlib> // atoi, rand
#include <ctime>
#include <iostream>
#include <sstream>
#include <utility> // pair
#include "hexgame.hpp"
#include "moveeval.hpp"
using namespace std;
static const unsigned max_size = 16u;
static void usage_print()
{
cout << "Usage:" << endl;
cout << "hexjakt size" << endl;
cout << "- interactive play" << endl;
cout << " size: size of the board side, less than " << max_size << endl;
cout << endl;
cout << "hexjakt P [size] [iter]" << endl;
cout << "- automatic play" << endl;
cout << " P: the player color (X or O). X plays first, north/south."
<< endl;
cout << " size: size of the board side, less than " << max_size << endl;
cout << " iter: max number of Monte-Carlo iterations per test move."
<< endl;
}
static void interactive_game(const unsigned size)
{
HexGame game(size);
cout << size <<endl;
game.start_prompt();
game.player_setup_prompt_and_set();
bool win;
do {
win = game.next_prompt_and_play();
} while (!win);
game.winner_print();
}
static void automatic_game(const unsigned size, const char color,
const unsigned iter)
{
HexGame game(size, iter);
int result = game.autoplay_handshake(color);
if (result < 0) {
exit(result);
}
game.player_autoplay_setup(color);
bool game_over;
do {
game_over = game.autoplay_next_move_play();
} while (!game_over);
}
int main(int argc, char *argv[])
{
// --- 8< -----
// partly taken from https://github.com/alexkh/hexai/blob/master/dummy.cpp
char color = 'X'; // can be X or O
unsigned short board_side = 11; // side of the board minimum 3
size_t iter = 1000; // number of iterations should be selectable
// parse command line parameters
argc = argc > 4? 4: argc; // forward compatibility measure
switch(argc) {
case 4:
{
stringstream ss; // used for reading numbers from strings
ss << argv[3]; // put third argument into the stringstream
ss >> iter; // read its numeric value
}
case 3:
{
stringstream ss;
ss << argv[2];
ss >> board_side;
if (board_side > max_size) {
cerr << color << " E: max board size is " << max_size << endl;
return -2;
}
board_side = board_side < 3? 3: board_side; // minimum 3
}
case 2:
{
color = argv[1][0];
if (color == 'X' || color == 'O') {
automatic_game(board_side, color, iter);
} else if (static_cast<unsigned>(atoi(argv[1])) <= max_size) {
// More input checking would be nice.
board_side = static_cast<unsigned>(atoi(argv[1]));
interactive_game(board_side);
} else {
usage_print();
return -1; // there is some error
}
}
break;
default:
usage_print();
break;
}
// --- >8 -----
return 0;
}