-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
275 lines (266 loc) · 8.4 KB
/
main.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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <array>
#include <vector>
#include <cassert>
#define TIMEOUT 10
struct Point {
int x, y;
Point() : Point(0, 0) {}
Point(float x, float y) : x(x), y(y) {}
bool operator==(const Point& rhs) const {
return x == rhs.x && y == rhs.y;
}
bool operator!=(const Point& rhs) const {
return !operator==(rhs);
}
Point operator+(const Point& rhs) const {
return Point(x + rhs.x, y + rhs.y);
}
Point operator-(const Point& rhs) const {
return Point(x - rhs.x, y - rhs.y);
}
};
class GomokuBoard {
public:
enum SPOT_STATE {
EMPTY = 0,
BLACK = 1,
WHITE = 2
};
static const int SIZE = 15;
std::array<std::array<int, SIZE>, SIZE> board;
int empty_count;
int cur_player;
bool done;
int winner;
private:
int get_next_player(int player) const {
return 3 - player;
}
bool is_spot_on_board(Point p) const {
return 0 <= p.x && p.x < SIZE && 0 <= p.y && p.y < SIZE;
}
int get_disc(Point p) const {
return board[p.x][p.y];
}
void set_disc(Point p, int disc) {
board[p.x][p.y] = disc;
}
bool is_disc_at(Point p, int disc) const {
if (!is_spot_on_board(p))
return false;
if (get_disc(p) != disc)
return false;
return true;
}
bool is_spot_valid(Point center) const {
if (get_disc(center) != EMPTY)
return false;
return true;
}
public:
GomokuBoard() {
reset();
}
void reset() {
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = EMPTY;
}
}
cur_player = BLACK;
empty_count = SIZE*SIZE;
done = false;
winner = -1;
}
bool put_disc(Point p) {
if(!is_spot_valid(p)) {
winner = get_next_player(cur_player);
done = true;
return false;
}
set_disc(p, cur_player);
empty_count--;
// Check Win
if (checkwin(cur_player)) {
done = true;
winner = cur_player;
}
if (empty_count == 0) {
done = true;
winner = EMPTY;
}
// Give control to the other player.
cur_player = get_next_player(cur_player);
return true;
}
bool checkwin(int disc){
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
if (is_disc_at(Point(i, j), disc)){
bool iswin = true;
if (i + 4 < SIZE) {
for(int k = 0; k < 5; k++)
if (!is_disc_at(Point(i+k, j), disc)) {
iswin = false;
break;
}
if (iswin) return true;
}
iswin = true;
if (j + 4 < SIZE) {
for(int k = 0; k < 5; k++)
if (!is_disc_at(Point(i, j+k), disc)) {
iswin = false;
break;
}
if (iswin) return true;
}
iswin = true;
if (i + 4 < SIZE && j + 4 < SIZE) {
for(int k = 0; k < 5; k++)
if (!is_disc_at(Point(i+k, j+k), disc)) {
iswin = false;
break;
}
if (iswin) return true;
}
iswin = true;
if (i - 4 >= 0 && j + 4 < SIZE) {
for(int k = 0; k < 5; k++)
if (!is_disc_at(Point(i-k, j+k), disc)) {
iswin = false;
break;
}
if (iswin) return true;
}
}
}
}
return false;
}
std::string encode_player(int state) {
if (state == BLACK) return "O";
if (state == WHITE) return "X";
return "Draw";
}
std::string encode_spot(int x, int y) {
if (is_spot_valid(Point(x, y))) return ".";
if (board[x][y] == BLACK) return "O";
if (board[x][y] == WHITE) return "X";
return " ";
}
std::string encode_output(bool fail=false) {
int i, j;
std::stringstream ss;
ss << "Timestep #" << (SIZE*SIZE-empty_count+1) << "\n";
if (fail) {
ss << "Winner is " << encode_player(winner) << " (Opponent performed invalid move)\n";
} else if (done) {
ss << "Winner is " << encode_player(winner) << "\n";
} else {
ss << encode_player(cur_player) << "'s turn\n";
}
ss << "+-----------------------------+\n";
for (i = 0; i < SIZE; i++) {
ss << "|";
for (j = 0; j < SIZE-1; j++) {
ss << encode_spot(i, j) << " ";
}
ss << encode_spot(i, j) << "|\n";
}
ss << "===============================\n";
return ss.str();
}
std::string encode_state() {
int i, j;
std::stringstream ss;
ss << cur_player << "\n";
for (i = 0; i < SIZE; i++) {
for (j = 0; j < SIZE-1; j++) {
ss << board[i][j] << " ";
}
ss << board[i][j] << "\n";
}
return ss.str();
}
};
const std::string file_log = "gamelog.txt";
const std::string file_state = "state";
const std::string file_action = "action";
const int timeout = TIMEOUT;
void launch_executable(std::string filename) {
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
size_t pos;
std::string command = "start /min " + filename + " " + file_state + " " + file_action;
if((pos = filename.rfind("/"))!=std::string::npos || (pos = filename.rfind("\\"))!=std::string::npos)
filename = filename.substr(pos+1, std::string::npos);
std::string kill = "timeout /t " + std::to_string(timeout) + " > NUL && taskkill /im " + filename + " > NUL 2>&1";
system(command.c_str());
system(kill.c_str());
#elif __linux__
std::string command = "timeout " + std::to_string(timeout) + "s " + filename + " " + file_state + " " + file_action;
system(command.c_str());
#elif __APPLE__
// May require installing the command by:
// brew install coreutils
std::string command = "gtimeout " + std::to_string(timeout) + "s " + filename + " " + file_state + " " + file_action;
system(command.c_str());
#endif
}
int main(int argc, char** argv) {
assert(argc == 3);
std::ofstream log("gamelog.txt");
std::string player_filename[3];
player_filename[1] = argv[1];
player_filename[2] = argv[2];
std::cout << "Player Black File: " << player_filename[GomokuBoard::BLACK] << std::endl;
std::cout << "Player White File: " << player_filename[GomokuBoard::WHITE] << std::endl;
GomokuBoard game;
std::string data;
data = game.encode_output();
std::cout << data;
log << data;
while (!game.done) {
// Output current state
data = game.encode_state();
std::ofstream fout(file_state);
fout << data;
fout.close();
// Run external program
launch_executable(player_filename[game.cur_player]);
// Read action
std::ifstream fin(file_action);
Point p(-1, -1);
while (true) {
int x, y;
if (!(fin >> x)) break;
if (!(fin >> y)) break;
p.x = x; p.y = y;
}
fin.close();
std::cout << "Put: (" << p.x << ',' << p.y << ")\n";
// Reset action file
if (remove(file_action.c_str()) != 0)
std::cerr << "Error removing file: " << file_action << "\n";
// Take action
if (!game.put_disc(p)) {
// If action is invalid.
data = game.encode_output(true);
std::cout << data;
log << data;
break;
}
data = game.encode_output();
std::cout << data;
log << data;
}
log.close();
// Reset state file
if (remove(file_state.c_str()) != 0)
std::cerr << "Error removing file: " << file_state << "\n";
return 0;
}