-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
96 lines (76 loc) · 1.96 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
#include "snake/snakews.cpp"
#include "libs/json.hpp"
#include "snake/utils.cpp"
#include <bits/stdc++.h>
using json = nlohmann::json;
using namespace std;
class MyStrategy: public SnakeClient {
public:
string nick;
string myId;
int cx, cy;
void onUpdate(Snake* snake, const json& j) override {
//cerr << j.dump(4) << endl;
if (cx == -1 || cy == -1) return;
SnakeMap& map = snake->map;
SnakeIds& ids = snake->ids;
auto res = bfs(map, ids, cx, cy);
auto field = res.first;
auto q = res.second;
if (q.size() < 2) return;
int x, y;
tie(y, x) = q.back();
for (int i = 1; i < q.size(); i++) {
int tx, ty;
tie(ty, tx) = q[i];
if (ids[map[ty][tx].id].type == "food") {
x = tx;
y = ty;
break;
}
}
while (true) {
int fx = field[y][x].fx;
int fy = field[y][x].fy;
assert(fx != -1 && fy != -1);
if (field[fy][fx].start) {
snake->go(GO_MSG[field[y][x].dir]);
return;
}
y = fy;
x = fx;
}
}
void onInit(Snake* snake, const SnakeIds& snakeIds, const SnakeMap& snakeMap) override {
nick = "C++ Bot #";
for (int i = 0; i < 6; i++) {
nick += '0' + (rand() % 10);
}
snake->join(nick);
snake->chat("Hello from C++!");
}
void onNewObject(Snake* snake, ObjectInfo obj) override {
// cerr << "new object(id=" << obj.id << ", type=" << obj.type;
// if (obj.type == "player") cerr << ", nick=" << obj.nick;
// cerr << ")" << endl;
if (obj.type == "player" && obj.nick == nick) { //if new object has the same nick as bot
myId = obj.id; //then bot id is that object id
cerr << "Player id => " << myId << endl;
}
}
void onCellUpdate(Snake* snake, int x, int y, CellUpdate upd, ObjectInfo info) override {
if (upd.id == myId) {
cx = x;
cy = y;
//cerr << "head position: " << y << " " << x << endl;
}
}
MyStrategy(): cx(-1), cy(-1) {}
};
int main() {
while (true) {
MyStrategy strategy;
SnakeWS::connect("ws://wrt.qjex.xyz:8080/snake/ws/faster", &strategy);
}
return 0;
}