-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMegatron747.cpp
More file actions
529 lines (439 loc) · 13.1 KB
/
Copy pathMegatron747.cpp
File metadata and controls
529 lines (439 loc) · 13.1 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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int> PII;
typedef vector< string >VS;
typedef vector< VS >VVS;
struct Cell {
int x, y;
Cell():x(-1), y(-1) { }
Cell(int x, int y):x(x), y(y) {}
bool operator==(const Cell& c) const {
return x==c.x&&y==c.y;
}
bool operator<(const Cell& c) const {
if (x==c.x) return y < c.y;
return x < c.x;
}
};
Cell operator+(const Cell &p1, const Cell &p2)
{
return Cell(p1.x+p2.x, p1.y+p2.y);
}
struct Move {
Cell c;
int strength;
bool operator < (const Move& m) const {
return strength > m.strength;
}
};
vector< Cell >dir = {Cell(1, 0), Cell(-1, 0), Cell(0, 1), Cell(0, -1)};
const int DIM = 8;
const int MAX_SCORE = 10000;
//const int MAX_HASH = 10000019;
const int MAX_HASH = 1000003;
//const int MAX_HASH = 100003;
//const int MAX_HASH = 10007;
class Grid {
const int MAX_LOOP = 1000;
const int PRIME_FACTOR = 997;
VVS s;
queue< Cell >unstable;
int cnt[2];
int atm[2];
/// R -> 0
/// G -> 1
int charToInt(char c) {
assert(c=='R'||c=='G');
return c=='G';
}
bool isValidCell(const Cell& p) {
return 0 <= p.x && p.x < DIM && 0 <= p.y && p.y < DIM;
}
int neighbourCount(const Cell& p) {
int ans = 0;
for (Cell c : dir) {
c = c+p;
ans += isValidCell(c);
}
return ans;
}
void update() {
int loop = 0;
while (!unstable.empty() && !gameEnded()) {
loop++;
if (loop > MAX_LOOP) {
cout << "Loop more than " << MAX_LOOP << endl;
break;
}
assert(unstable.size() < DIM*DIM);
Cell p = unstable.front();
unstable.pop();
char cur = s[p.x][p.y][0];
int atom = s[p.x][p.y][1]-'0';
int react = neighbourCount(p);
assert(react <= atom);
if (react==atom) {
s[p.x][p.y] = "No";
cnt[charToInt(cur)]--;
} else {
int tmp = atom-react;
if (tmp >= react) unstable.push(p);
s[p.x][p.y][1] = tmp+'0';
}
for (Cell d : dir) {
d = d+p;
if (!isValidCell(d)) continue;
addAtom(d, cur, true);
}
}
while (unstable.size()) unstable.pop();
}
public:
Grid(VVS s) : s(s) {
clearCount();
}
void clearCount() {
cnt[0] = cnt[1] = 0;
atm[0] = atm[1] = 0;
}
void addAtom(Cell p, char cur, bool fromUpdate) {
int player = charToInt(cur);
if (fromUpdate==false) atm[player]++;
if (s[p.x][p.y]=="No") {
s[p.x][p.y][0] = cur;
s[p.x][p.y][1] = '1';
cnt[player]++;
return;
}
if (s[p.x][p.y][0]!=cur) {
// assert(fromUpdate);
cnt[player]++;
cnt[player^1]--;
atm[player] += s[p.x][p.y][1]-'0';
atm[player^1] -= s[p.x][p.y][1]-'0';
}
s[p.x][p.y][0] = cur;
s[p.x][p.y][1]++;
if (neighbourCount(p)==s[p.x][p.y][1]-'0') {
unstable.push(p);
if (!fromUpdate) update();
}
}
void setCell(int i, int j, string str) {
s[i][j] = str;
if (s[i][j]!="No") {
cnt[charToInt(s[i][j][0])]++;
atm[charToInt(s[i][j][0])] += s[i][j][1]-'0';
}
}
int score1(char player) {
/// calculate score for bot1
int v = charToInt(player);
if (cnt[v] > 0 && cnt[v^1]==0) return MAX_SCORE;
if (cnt[v^1] > 0 && cnt[v]==0) return -MAX_SCORE;
return atm[v];
}
bool isEmpty(int i, int j) {
return s[i][j]=="No";
}
char getPlayer(int i, int j) {
return s[i][j][0];
}
int getAtoms(int i, int j) {
return s[i][j][1]-'0';
}
bool gameEnded() {
return cnt[0]==0||cnt[1]==0;
}
int getHashValue() {
long long ans = 0;
for (int i = 0; i < DIM; i++) {
for (int j = 0; j < DIM; j++) {
for (char c : s[i][j]) {
ans += (ans*PRIME_FACTOR)%MAX_HASH+c;
ans %= MAX_HASH;
}
}
}
return ans;
}
};
const int MIN_DEPTH = 2;
const int MAX_DEPTH = 4;
char otherPlayer(char now)
{
if (now=='R') return 'G';
return 'R';
}
class Timer {
const int TIME_LIMIT = 2950;
chrono::time_point<chrono::system_clock> start_time, current_time;
public:
Timer() {}
void startTimer() {
start_time = chrono::high_resolution_clock::now();
}
int elapsedMilis() {
current_time = chrono::high_resolution_clock::now();
return chrono::duration_cast<std::chrono::milliseconds>(current_time - start_time).count();
}
bool timesUp() {
return elapsedMilis() > TIME_LIMIT;
}
};
class Megatron747 {
Timer timer;
int botVersion;
char me, other;
Grid gr;
int killXP[MAX_DEPTH][DIM][DIM];
Cell ttable[2][MAX_DEPTH][MAX_HASH];
int times[MAX_DEPTH], calc[MAX_DEPTH];
void addKill(Cell c, int depth) {
killXP[depth][c.x][c.y]++;
return;
}
void removeKill(Cell c, int depth) {
killXP[depth][c.x][c.y]--;
return;
}
int killerScore(Cell c, int depth) {
return killXP[depth][c.x][c.y];
}
void clearShit() {
memset(killXP, 0, sizeof killXP);
}
bool readFile() {
ifstream in;
in.open("shared_file.txt");
char now;
in >> now;
if (now!=me) {
in.close();
return false;
}
gr.clearCount();
cout << "reading" << endl;
for (int i = 0; i < DIM; i++) {
for (int j = 0; j < DIM; j++) {
string str;
in >> str;
gr.setCell(i, j, str);
}
}
in.close();
cout << "read done" << endl;
return true;
}
void writeFile(Cell mv) {
cout << "move: " << mv.x << " " << mv.y << "\n";
ofstream out;
out.open("shared_file.txt");
out << 0 << "\n";
out << mv.x << " " << mv.y << "\n";
out.close();
}
int allowedDepth;
void getMoves(Grid &gr, int boardHash, int depth, vector<Move> &moves) {
int _v = depth&1;
char currentPlayer;
if (_v) currentPlayer = me;
else currentPlayer = other;
int height = allowedDepth-1-depth;
Cell tt;
for (int idx = MAX_DEPTH-1; idx >= height; idx--) {
Cell in = ttable[_v][idx][boardHash];
if (in.x==-1) continue;
tt = in;
break;
}
for (int i = 0; i < DIM; i++) {
for (int j = 0; j < DIM; j++) {
if (gr.isEmpty(i, j)||gr.getPlayer(i, j)==currentPlayer) {
Move mv;
mv.c = Cell(i, j);
mv.strength = 0;
mv.strength += killerScore(mv.c, depth);
if (i==tt.x&&j==tt.y) mv.strength += 1e8;
moves.push_back(mv);
}
}
}
sort(moves.begin(), moves.end());
}
int heuristic(Grid& gr, int depth, int alpha, int beta) {
/// greedily make move to the cell which
/// maximizes score1
int _v = depth&1;
char currentPlayer;
if (_v) currentPlayer = me;
else currentPlayer = other;
if (depth==allowedDepth||gr.gameEnded()||timer.timesUp()) {
return gr.score1(me);
}
int boardHash = gr.getHashValue();
int height = allowedDepth-1-depth;
vector<Move>moves;
getMoves(gr, boardHash, depth, moves);
Cell bestMove;
int mx = -MAX_SCORE, mn = MAX_SCORE;
for (Move m : moves) {
Cell now = m.c;
Grid tmp(gr);
tmp.addAtom(now, currentPlayer, false);
int score = heuristic(tmp, depth+1, alpha, beta);
if (currentPlayer==me&&score>mx) {
mx = score;
bestMove = now;
}
if (currentPlayer!=me&&score<mn) {
mn = score;
bestMove = now;
}
if (currentPlayer==me&&score>alpha) alpha = score;
if (currentPlayer!=me&&score<beta) beta = score;
if (alpha >= beta) {
addKill(now, depth);
break;
} else {
if (!timer.timesUp()) removeKill(now, depth);
}
}
if (_v) {
if (alpha >= beta || !timer.timesUp())
ttable[1][height][boardHash] = bestMove;
return alpha;
} else {
if (alpha >= beta || !timer.timesUp())
ttable[0][height][boardHash] = bestMove;
return beta;
}
}
Cell bot21(int depth = MIN_DEPTH) {
/// greedily make move to the cell which
/// maximizes score2
allowedDepth = depth;
vector<Cell>vc;
int mx = -MAX_SCORE;
for (int i = 0; i < DIM; i++) {
for (int j = 0; j < DIM; j++) {
if (gr.isEmpty(i, j)||gr.getPlayer(i, j)==me) {
Grid tmp(gr);
tmp.addAtom(Cell(i, j), me, false);
int score = heuristic(tmp, 0, -MAX_SCORE, MAX_SCORE);
if (score > mx) {
mx = score;
vc.clear();
}
if (mx==score) vc.emplace_back(i, j);
}
}
}
cout << "depth " << depth << " max score " << mx << " found " << vc.size()
<< " cells using " << timer.elapsedMilis() << " milis" << endl;
times[depth] += timer.elapsedMilis();
calc[depth]++;
if (vc.size()==0) vc.push_back(bot0());
int idx = rand()%((int)vc.size());
// cout << idx << endl;
return vc[idx];
}
Cell bot31() {
/// iterative deepening of bot21
int depth = 0;
Cell bestMove = bot21(depth);
while (!timer.timesUp() && ++depth < MAX_DEPTH) {
Cell tmp = bot21(depth);
if (timer.timesUp()) break;
cout << "went " << depth << " steps deeper!" << endl;
bestMove = tmp;
}
for (int i = 0; i < depth; i++) {
if (calc[i]==0) continue;
cout << i << "->" << fixed << setprecision(2) << 1.0*times[i]/calc[i] << " ";
}
cout << endl;
return bestMove;
}
Cell bot11() {
/// greedily make move to the cell which
/// maximizes my color
vector<Cell>vc;
int mx = -MAX_SCORE*2;
for (int i = 0; i < DIM; i++) {
for (int j = 0; j < DIM; j++) {
if (gr.isEmpty(i, j)||gr.getPlayer(i, j)==me) {
Grid tmp(gr);
tmp.addAtom(Cell(i, j), me, false);
int score1 = tmp.score1(me);
// cout << score1 << " for " << i << " " << j << endl;
if (score1 > mx) {
mx = score1;
vc.clear();
}
if (mx==score1) vc.emplace_back(i, j);
}
}
}
cout << "max score " << mx << " found "
<< vc.size() << " cells" << endl;
if (vc.size()==0) vc.push_back(bot0());
int idx = rand()%((int)vc.size());
return vc[idx];
}
Cell bot0() {
/// randomized move
vector<Cell>vc;
for (int i = 0; i < DIM; i++) {
for (int j = 0; j < DIM; j++) {
if (gr.isEmpty(i, j)||gr.getPlayer(i, j)==me)
vc.emplace_back(i, j);
}
}
assert(vc.size() > 0);
int idx = rand()%((int)vc.size());
return vc[idx];
}
Cell makeMove() {
timer.startTimer();
clearShit();
Cell mv;
if (botVersion==0) mv = bot0();
else if (botVersion==11) mv = bot11();
else if (botVersion==21) mv = bot21();
else if (botVersion==31) mv = bot31();
else {
// assert(false);
mv = bot0();
}
cout << "made move after " << timer.elapsedMilis() << " milis" << endl;
return mv;
}
void run() {
while (true) {
if (!readFile()) continue;
Cell mv = makeMove();
writeFile(mv);
}
}
public:
Megatron747(char me, int botVersion):gr(VVS(DIM, VS(DIM))) {
this->me = me;
this->botVersion = botVersion;
if (me=='R') other = 'G';
else other = 'R';
run();
}
};
const int DEFAULT_BOT = 31;
int main(int argc, char *argv[])
{
srand(time(0));
cout << argv[1][0] << " started!" << endl;
int v = DEFAULT_BOT;
if (argc > 2) v = atoi(argv[2]);
// Megatron747 mt747(argv[1][0], v);
Megatron747 *mt747 = new Megatron747(argv[1][0], v);
delete mt747;
return 0;
}