-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconnect4-bot.cpp
268 lines (268 loc) · 7.76 KB
/
connect4-bot.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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <chrono>
#include <regex>
#include "eval.hpp"
#include "board.hpp"
#include "minimax.hpp"
using namespace std;
void print_help() {
ifstream file("../connect4-bot/README.md");
if(!file.good()) {
cout << "See at https://github.com/tokox/connect4-bot#readme" << endl;
} else {
string line;
getline(file, line);
while(!file.eof()) {
cout << line << endl;
getline(file, line);
}
}
};
int csswces(string s) {
regex reg("\033\\[(A|[\\d]+m)");
return regex_replace(s, reg, "").size();
}
int prev_cms = 0;
int prev_ums = 0;
void print(string comp_mess, Board board, string user_mess, bool override_prev = false) {
for(int i = 0; override_prev && i < board.HEIGHT+3; i++) {
cout << "\033[A";
}
int cms = csswces(comp_mess);
int ums = csswces(user_mess);
cout << comp_mess;
for(int i = 0; i < prev_cms-cms && override_prev; i++)
cout << " ";
cout << endl;
board.print_out();
cout << user_mess;
for(int i = 0; i < prev_ums-ums && override_prev; i++)
cout << " ";
cout << endl;
prev_cms = cms;
prev_ums = ums;
}
string double_to_string(double value, string::size_type precision = string::npos) {
string res = to_string(value);
auto pos = res.find('.');
if(pos != string::npos && precision != string::npos)
res.erase(pos+precision+(precision>0?1:0));
return res;
}
int main(int argc, char* argv[])
{
bool move = false;
bool start = false;
bool print_moves = false;
bool history = false;
long long time = -1;
int depth = -1;
char comp = '\0';
Board board;
string pxo;
string moves;
for(int i = 1; i < argc; i++) {
string arg = argv[i];
if(arg == "help") {
print_help();
return 0;
} else if(arg == "start") {
start = true;
} else if(arg == "X" || arg == "O") {
if(comp == '\0') {
comp = arg[0];
} else {
cout << "Unexpected multiple `X|O` arguments" << endl;
return 1;
}
} else if(arg == "print_moves") {
print_moves = true;
} else if(arg == "history") {
history = true;
} else if(arg == "depth") {
if(depth < 0) {
if(i+1 < argc) {
string next_arg = argv[++i];
try {
depth = stoi(next_arg);
} catch(...) {
cout << "Expected `[any number]` after `depth`. Got this: " << next_arg << endl;
return 1;
}
} else {
cout << "Expected `[any number]` after `depth`" << endl;
return 1;
}
} else {
cout << "Unexpected multiple `depth` arguments" << endl;
return 1;
}
} else if(arg == "time") {
if(time < 0) {
if(i+1 < argc) {
string next_arg = argv[++i];
try {
time = stoll(next_arg);
} catch(...) {
cout << "Expected `[any number]` after `time`. Got this: " << next_arg << endl;
return 1;
}
} else {
cout << "Expected `[any number]` after `time`" << endl;
return 1;
}
} else {
cout << "Unexpected multiple `time` arguments" << endl;
return 1;
}
} else if(arg == "position") {
if(board.empty()) {
if(i+1 < argc) {
string next_arg = argv[++i];
if(!board.load(next_arg)) {
cout << "Expected `[position]` after `position`. Got this: " << next_arg << endl;
return 1;
}
if(!board.validate("XO", &pxo)) {
cout << "Invalid `[position]` after `position`: " << next_arg << endl;
return 1;
}
} else {
cout << "Expected `[position]` after `position`" << endl;
return 1;
}
} else {
cout << "Unexpected multiple `position` arguments" << endl;
return 1;
}
} else if(arg == "moves") {
if(moves.empty()) {
if(i+1 < argc) {
moves = argv[++i];
if(moves.find_first_not_of("1234567") != string::npos) {
cout << "Expected `[moves]` after `moves`. Got this: " << moves << endl;
return 1;
}
} else {
cout << "Expected `[moves]` after `moves`" << endl;
return 1;
}
} else {
cout << "Unexpected multiple `moves` arguments" << endl;
return 1;
}
} else {
cout << "Unexpected argument: " << arg << endl;
return 1;
}
}
if(time < 0 && depth < 0) {
time = 5;
depth = -1;
}
if(!board.empty() && pxo.size() == 1) {
if(comp == '\0') {
if(start) {
comp = pxo[0];
} else {
comp = 'O';
if(comp == pxo[0])
start = true;
}
} else {
if(comp != pxo[0] && start) {
cout << "Cannot start as '" << comp << "' from this position" << endl;
return 1;
}
if(comp == pxo[0])
start = true;
}
} else if(comp == '\0')
comp = 'O';
move = start;
for(auto& m: moves) {
if(!board.move((int)m-'1', move?comp:(comp=='O'?'X':'O')) || !board.validate("XO")) {
cout << "Invalid `[moves]` after `moves`: " << moves << endl;
return 1;
}
move = !move;
}
if(system("stty -echo -icanon && tput civis") != 0) {
cout << "Terminal error!" << endl;
return 1;
}
string comp_mess;
string user_mess;
if(!history)
cout << "\n\n\n\n\n\n\n\n\n";
while(!board.full() && eval(board) != 1) {
if(history) {
comp_mess.clear();
user_mess.clear();
cout << endl;
}
if(move) {
comp_mess.clear();
} else {
user_mess.clear();
}
print((string)(move?"\033[42m\033[1m":"")+"["+comp+"]"+(move?"\033[49m\033[22m":"")+": "+comp_mess, board, (string)(!move?"\033[42m\033[1m":"")+"["+(comp=='O'?'X':'O')+"]"+(!move?"\033[49m\033[22m":"")+": "+user_mess, !history);
if(move) {
int pev = 0, pmove = -1;
auto stime = chrono::steady_clock::now();
auto ptime = stime;
chrono::microseconds pttime = chrono::microseconds::zero();
chrono::microseconds pdtime = chrono::microseconds::zero();
for(int d = 0; (d <= depth || depth < 0) && (pttime.count() * 5 <= time*1000000-pdtime.count() || time < 0) && pev == 0 && d <= board.left(); d++) {
auto[ev, move] = minimax(board, d, comp);
pev = ev;
pmove = move;
auto etime = chrono::steady_clock::now();
chrono::microseconds dtime = chrono::duration_cast<chrono::microseconds>(etime-stime);
pdtime = dtime;
chrono::microseconds ttime = chrono::duration_cast<chrono::microseconds>(etime-ptime);
pttime = ttime;
ptime = etime;
comp_mess = (string)"depth "+(d<10?" ":"")+to_string(d)+(depth<0?"":"/"+to_string(depth))+" ("+double_to_string((double)ttime.count()/1000000, 2)+"s) time "+double_to_string((double)dtime.count()/1000000, 2)+"s"+(time<0?"":"/"+to_string(time)+"s")+" move "+to_string(move+1)+" result "+(ev==0?"draw":(ev>0?(d==0?"won":"win in "+to_string(d/2)):"lose in "+to_string(d/2+1)));
print((string)"\033[42m\033[1m["+comp+"]\033[49m\033[22m: "+comp_mess, board, (string)"["+(comp=='O'?'X':'O')+"]: "+user_mess, true);
}
if(!board.move(pmove, comp)) {
cout << "Computer error!" << endl;
return 1;
}
if(print_moves)
cerr << pmove+1 << endl;
if(eval(board) == 1) {
user_mess += " \033[41m\033[3m\033[1mYou lost!\033[22m\033[23m\033[49m";
}
} else {
char pm = '\0';
auto stime = chrono::steady_clock::now();
while(true) {
cin >> pm;
if(!board.move((int)(pm-'1'), comp=='O'?'X':'O')) {
user_mess = (string)"\033[31mInvalid move `"+pm+"`\033[39m";
print((string)"["+comp+"]: "+comp_mess, board, (string)"\033[42m\033[1m["+(comp=='O'?'X':'O')+"]\033[49m\033[22m: "+user_mess, true);
} else {
user_mess = (string)"\033[32mCorrect move `"+pm+"`\033[39m";
break;
}
}
if(eval(board) == 1) {
user_mess += " \033[42m\033[3m\033[1mYou won!\033[22m\033[23m\033[49m";
}
}
if(board.full() && eval(board) == 0) {
user_mess += " \033[43m\033[3m\033[1mDraw!\033[22m\033[23m\033[49m";
}
print((string)(move?"\033[42m\033[1m":"")+"["+comp+"]"+(move?"\033[49m\033[22m":"")+": "+comp_mess, board, (string)(!move?"\033[42m\033[1m":"")+"["+(comp=='O'?'X':'O')+"]"+(!move?"\033[49m\033[22m":"")+": "+user_mess, true);
move = !move;
}
if(system("stty echo icanon && tput cnorm") != 0) {
cout << "Terminal error!" << endl;
return 1;
}
}