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
|
#include <iostream>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <unistd.h>
#include <sys/time.h>
#include "board.h"
#include AI_HEADER
#include "ui.h"
using namespace std;
#define STR_(x) #x
#define STR(x) STR_(x)
#ifndef AI
#define AI MC
#endif
static uint8_t readPlace(Board &bd) {
string line;
getline(cin, line);
if (line.size() >= 1 && tolower(line[0]) == 'q') {
exit(0);
} else if (line.size() >= 6 && memcmp(line.data(), "place ", 6) == 0) {
istringstream ss(line.substr(6));
int x, y, clr;
ss >> x >> y >> clr;
return bd.putCW(BSZ * (y + BMID) + x + BMID, clr);
} else {
cerr << "Expected place line, got '" << line << "'" << endl;
exit(1);
}
}
static void protocolIO() {
Board bd = Board::makeEmpty();
readPlace(bd);
string line;
char c = cin.peek();
uint8_t myclr;
if (c == 's' || c == 'S') {
myclr = 1;
getline(cin, line);
} else if (c == 'q' || c == 'Q') {
return;
} else {
myclr = 2;
}
uint8_t onturn = 1;
while (true) {
if (onturn == myclr) {
int idx = AI::calcMove(bd, onturn);
int x = idx % BSZ - BMID, y = idx / BSZ - BMID;
cout << x << ' ' << y << endl;
readPlace(bd);
} else {
readPlace(bd);
}
onturn = NEXTTURN(onturn);
}
}
int main() {
struct timeval tv;
gettimeofday(&tv, nullptr);
srandom(tv.tv_sec * 1000000U + tv.tv_usec);
cerr << "Using AI " << STR(AI) << endl;
if (!isatty(STDOUT_FILENO)) {
protocolIO();
return 0;
}
Board bd = Board::makeEmpty();
// cerr << "Initial stone at " << Idx(BSZ * BMID + BMID) << endl;
bd.put(BSZ * BMID + BMID, bd.bag.drawRandom());
cout << bd << endl;
uint8_t win = 0;
uint8_t onturn = 1;
while (bd.bag.totalLeft() > 0) {
cout << "--- NEXT TURN: " << Stone(onturn) << " ---" << endl;
int idx;
if (onturn == 1) {
cout << "YOUR TURN." << endl;
idx = UI::getMove(bd);
} else {
idx = AI::calcMove(bd, onturn);
}
uint8_t clr = bd.bag.drawRandom();
win = bd.putCW(idx, clr);
cout << bd << endl;
if (win != 0) break;
onturn = NEXTTURN(onturn);
}
if (win == 0) {
cout << "TIE" << endl;
} else {
cout << "Winner: " << Stone(win) << endl;
}
}
|