-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
400 lines (374 loc) · 11.9 KB
/
main.cpp
File metadata and controls
400 lines (374 loc) · 11.9 KB
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
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <array>
#include <vector>
#include <cassert>
bool humanplayer_white = false;
bool humanplayer_black = false;
bool input_board = false;
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);
}
};
Point Record;
class OthelloBoard {
public:
enum SPOT_STATE {
EMPTY = 0,
BLACK = 1,
WHITE = 2
};
static const int SIZE = 8;
const std::array<Point, 8> directions{{
Point(-1, -1), Point(-1, 0), Point(-1, 1),
Point(0, -1), /*{0, 0}, */Point(0, 1),
Point(1, -1), Point(1, 0), Point(1, 1)
}};
std::array<std::array<int, SIZE>, SIZE> board;
std::vector<Point> next_valid_spots;
std::array<int, 3> disc_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;
for (Point dir: directions) {
// Move along the direction while testing.
Point p = center + dir;
if (!is_disc_at(p, get_next_player(cur_player)))
continue;
p = p + dir;
while (is_spot_on_board(p) && get_disc(p) != EMPTY) {
if (is_disc_at(p, cur_player))
return true;
p = p + dir;
}
}
return false;
}
void flip_discs(Point center) {
for (Point dir: directions) {
// Move along the direction while testing.
Point p = center + dir;
if (!is_disc_at(p, get_next_player(cur_player)))
continue;
std::vector<Point> discs({p});
p = p + dir;
while (is_spot_on_board(p) && get_disc(p) != EMPTY) {
if (is_disc_at(p, cur_player)) {
for (Point s: discs) {
set_disc(s, cur_player);
}
disc_count[cur_player] += discs.size();
disc_count[get_next_player(cur_player)] -= discs.size();
break;
}
discs.push_back(p);
p = p + dir;
}
}
}
public:
OthelloBoard() {
reset();
}
void reset() {
for (int i = 0; i < 3; i++)
disc_count[i] = 0;
if (input_board)
{
std::ifstream board_in;
board_in.open("board_initialize.txt");
for (int i = 0; i < SIZE; i++)
for (int k = 0; k < SIZE; k++)
{
board_in >> board[i][k];
disc_count[board[i][k]]++;
}
}
else
{
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
board[i][j] = EMPTY;
}
}
board[3][4] = board[4][3] = BLACK;
board[3][3] = board[4][4] = WHITE;
disc_count[EMPTY] = 8*8-4;
disc_count[BLACK] = 2;
disc_count[WHITE] = 2;
}
cur_player = BLACK;
next_valid_spots = get_valid_spots();
done = false;
winner = -1;
}
std::vector<Point> get_valid_spots() const {
std::vector<Point> valid_spots;
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) {
Point p = Point(i, j);
if (board[i][j] != EMPTY)
continue;
if (is_spot_valid(p))
valid_spots.push_back(p);
}
}
return valid_spots;
}
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);
disc_count[cur_player]++;
disc_count[EMPTY]--;
flip_discs(p);
// Give control to the other player.
cur_player = get_next_player(cur_player);
next_valid_spots = get_valid_spots();
// Check Win
if (next_valid_spots.size() == 0) {
cur_player = get_next_player(cur_player);
next_valid_spots = get_valid_spots();
if (next_valid_spots.size() == 0) {
// Game ends
done = true;
int white_discs = disc_count[WHITE];
int black_discs = disc_count[BLACK];
if (white_discs == black_discs) winner = EMPTY;
else if (black_discs > white_discs) winner = BLACK;
else winner = WHITE;
}
}
return true;
}
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 #" << (8*8-4-disc_count[EMPTY]+1) << "\n";
ss << "O: " << disc_count[BLACK] << "; X: " << disc_count[WHITE] << "\n";
if (fail) {
ss << "Winner is " << encode_player(winner) << " (Opponent performed invalid move)\n";
} else if (next_valid_spots.size() > 0) {
ss << encode_player(cur_player) << "'s turn ----> The opponent puts disc on (" << Record.x << ", " << Record.y << ")\n";
} else {
ss << "Winner is " << encode_player(winner) << "\n";
}
if (humanplayer_black or humanplayer_white)
{
ss << "\n 0 1 2 3 4 5 6 7 \n";
ss << " +---------------+\n";
for (i = 0; i < SIZE; i++) {
ss << i << "|";
for (j = 0; j < SIZE; j++) {
ss << encode_spot(i, j) << "|";
}
ss << "\n";
if (i != SIZE - 1)
ss << " |---------------|\n";
}
ss << " +---------------+\n\n";
}
else
{
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";
}
ss << next_valid_spots.size() << " valid moves: {";
if (next_valid_spots.size() > 0) {
Point p = next_valid_spots[0];
ss << "(" << p.x << "," << p.y << ")";
}
for (size_t i = 1; i < next_valid_spots.size(); i++) {
Point p = next_valid_spots[i];
ss << ", (" << p.x << "," << p.y << ")";
}
ss << "}\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";
}
ss << next_valid_spots.size() << "\n";
for (size_t i = 0; i < next_valid_spots.size(); i++) {
Point p = next_valid_spots[i];
ss << p.x << " " << p.y << "\n";
}
return ss.str();
}
};
const std::string file_log = "gamelog.txt";
const std::string file_state = "state";
const std::string file_action = "action";
// Timeout is set to 10 when TA test your code.
const int timeout = 10;
std::string player_filename[3];
void launch_executable(std::string filename)
{
if ((humanplayer_white and filename == player_filename[2]) or (humanplayer_black and filename == player_filename[1]))
{
int row, col;
char ch;
do
{
std::cout << std::endl << "ENTER ROW AND COLUMN: ";
std::cin >> row >> col;
std::cout << "sure? (y/n) ";
std::cin >> ch;
} while (ch != 'y' and std::cin.good());
std::ofstream fin;
fin.open("action");
if (!fin.is_open())
{
std::cout << "doesn't open action" << std::endl;
return;
}
fin << row << ' ' << col << std::endl;
}
else
{
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
std::string command = "start /min " + filename + " " + file_state + " " + file_action;
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");
player_filename[1] = argv[1];
player_filename[2] = argv[2];
std::cout << "Player Black(O) File: " << player_filename[OthelloBoard::BLACK] << std::endl;
std::cout << "Player White(X) File: " << player_filename[OthelloBoard::WHITE] << std::endl;
std::cout << "Input Board state: (1/0)";
std::cin >> input_board;
std::cout << "Black(O) Human Player: (1/0)";
std::cin >> humanplayer_black;
std::cout << "White(X) Human Player: (1/0)";
std::cin >> humanplayer_white;
std::cin.get();
std::cout << "------- Press Any Key To Start -------";
std::cin.get();
OthelloBoard 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;
Record.x = x; Record.y = y;
p.x = x; p.y = y;
}
fin.close();
// 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;
}
std::cout << "Player Black(O) File: " << player_filename[OthelloBoard::BLACK] << std::endl;
std::cout << "Player White(X) File: " << player_filename[OthelloBoard::WHITE] << std::endl;
log.close();
// Reset state file
// if (remove(file_state.c_str()) != 0)
// std::cerr << "Error removing file: " << file_state << "\n";
return 0;
}