forked from SharifAIChallenge/AIC17-Client-Cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGame.cpp
277 lines (224 loc) · 7.78 KB
/
Game.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
269
270
271
272
273
274
275
276
277
#include "Entity.h"
#include "Game.h"
#include "Teleport.h"
#include "Slippers.h"
#include "Trash.h"
#include "Food.h"
#include "util.h"
#include <ctime>
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/time.h>
#endif
long long getTimeInMilliSeconds() {
#ifdef _WIN32
SYSTEMTIME tv;
GetSystemTime(&tv);
return tv.wSecond * 1000 + tv.wMilliseconds;
#else
timeval tv;
gettimeofday(&tv, NULL);
return 1000 * tv.tv_sec + tv.tv_usec / 1000;
#endif
}
Game::Game() {
turn = 0;
totalTurns = 0;
turnTimeout = 0;
turnStartTime = -1;
myID = 0;
map = NULL;
}
Game::~Game() {
delete map;
}
void Game::changeStrategy(bool wing, CellState left, CellState right, CellState front, Move strategy) {
GameEvent *ev = new GameEvent(Constants::TYPE_CHANGE_STRATEGY);
ev->addArg(wing);
if(right != CellState::ALLY && right != CellState::ENEMY && right != CellState::BLANK)
std::cerr << "invalid strategy choosen from AI\n";
ev->addArg(static_cast<int>(right));
if(front != CellState::ALLY && front != CellState::ENEMY)
std::cerr << "invalid strategy choosen from AI\n";
ev->addArg(static_cast<int>(front));
if(left != CellState::ALLY && left != CellState::ENEMY && left != CellState::BLANK)
std::cerr << "invalid strategy choosen from AI\n";
ev->addArg(static_cast<int>(left));
ev->addArg(static_cast<int>(strategy));
eventHandler->addEvent(ev);
}
void Game::deterministicMove(Beetle &beetle, Move strategy) {
GameEvent *ev = new GameEvent(Constants::TYPE_DETERMINISTIC_MOVE);
ev->addArg(beetle.getId());
ev->addArg(static_cast<int>(strategy));
eventHandler->addEvent(ev);
}
void Game::changeType(Beetle &beetle, bool wing) {
GameEvent *ev = new GameEvent(Constants::TYPE_ANTENNA_CHANGE);
ev->addArg(beetle.getId());
ev->addArg(wing);
eventHandler->addEvent(ev);
}
int Game::getCurrentTurn() {
return this->turn;
}
int Game::getTotalTurns() {
return this->totalTurns;
}
long long Game::getTurnRemainingTime() {
return turnTimeout - getTurnTimePassed();
}
long long Game::getTurnTotalTime() {
return this->turnTimeout;
}
int Game::getTeamId() {
return myID;
}
int Game::getMyScore() {
if(this->getTeamId() == 0)
return score.first;
else
return score.second;
}
int Game::getOppScore() {
if(this->getTeamId() == 0)
return score.second;
else
return score.first;
}
Constants Game::getConstants() const {
return this->constants;
}
Map* Game::getMap() {
return map;
}
void Game::setConstants(Json::Value &msg) {
this->turnTimeout = msg[0u].asInt();
constants.setConstants(msg);
this->totalTurns = msg[21u].asInt();
}
long long Game::getTurnTimePassed() {
return getTimeInMilliSeconds() - turnStartTime;
}
void Game::handleInitMessage(Message &msg) {
Json::Value &argsArray = msg.getArray("args");
Json::UInt I = 0;
this->myID = argsArray[I++].asInt();
Json::Value &sizeArray = argsArray[I++];
Size size = { sizeArray[0u].asInt(), sizeArray[1u].asInt() };
map = new Map(size, myID);
Json::Value &beetleArray = argsArray[I++];
for (Json::UInt i = 0; i < beetleArray.size(); i++) {
CERR("Beetle " << beetleArray[i][0u].asInt() << " at\t" << beetleArray[i][1u].asInt() << ", " << beetleArray[i][2u].asInt() << "\n");
map->addEntity(
new Beetle(beetleArray[i][0u].asInt(),
new Cell(beetleArray[i][1u].asInt(), beetleArray[i][2u].asInt()),
static_cast<Direction>(beetleArray[i][3u].asInt()),
beetleArray[i][4u].asBool(),
static_cast<BeetleType>(beetleArray[i][5u].asInt()),
beetleArray[i][6u].asBool(),
beetleArray[i][7u].asInt()));
}
Json::Value &foodArray = argsArray[I++];
for (int i = 0; i < (int) foodArray.size(); i++) {
CERR("Food at\t\t" << foodArray[i][0u].asInt() << "\t" << foodArray[i][1u].asInt() << ", " << foodArray[i][2u].asInt() << "\n");
map->addEntity(
new Food(foodArray[i][0u].asInt(), new Cell(foodArray[i][1u].asInt(),
foodArray[i][2u].asInt())));
}
Json::Value &trashArray = argsArray[I++];
for (int i = 0; i < (int) trashArray.size(); i++) {
CERR("Trash at\t" << trashArray[i][0u].asInt() << "\t" << trashArray[i][1u].asInt() << ", " << trashArray[i][2u].asInt() << "\n");
map->addEntity(new Trash(trashArray[i][0u].asInt(), new Cell(
trashArray[i][1u].asInt(), trashArray[i][2u].asInt())));
}
Json::Value &slippersArray = argsArray[I++];
for (int i = 0; i < (int) slippersArray.size(); i++) {
CERR("Slippers at\t" << slippersArray[i][0u].asInt() << "\t" << slippersArray[i][1u].asInt() << ", " << slippersArray[i][2u].asInt() << "\n");
map->addEntity(new Slippers(slippersArray[i][0u].asInt(), new Cell(
slippersArray[i][1u].asInt(), slippersArray[i][2u].asInt())));
}
Json::Value &teleportArray = argsArray[I++];
for (int i = 0; i < (int) teleportArray.size(); i++) {
CERR("Teleport at\t" << teleportArray[i][0u].asInt() << "\t" << teleportArray[i][1u].asInt() << ", " << teleportArray[i][2u].asInt() << "\n");
map->addEntity(
new Teleport(teleportArray[i][0u].asInt(), new Cell(
teleportArray[i][1u].asInt(), teleportArray[i][2u].asInt()),
teleportArray[i][3u].asInt()));
}
auto teleportCells = map->getTeleportCells();
for(auto cell : teleportCells) {
Teleport* pair = dynamic_cast<Teleport*>(map->getEntity(cell->getTeleport()->getPairId()));
cell->getTeleport()->setPair(pair);
}
Json::Value &constants = argsArray[I++];
this->setConstants(constants);
}
void Game::handleTurnMessage(Message &msg) {
turnStartTime = getTimeInMilliSeconds();
Json::Value &argsArray = msg.getArray("args");
Json::UInt I = 0;
turn = argsArray[I++].asInt();
CERR("Turn: " << turn << "\n");
Json::Value &scores = argsArray[I++];
score = std::make_pair(scores[0u].asInt(), scores[1u].asInt());
Json::Value &changesArray = argsArray[I++];
for (int i = 0; i < (int) changesArray.size(); i++) {
Json::Value &change = changesArray[i];
Json::Value &changeArgs = change["args"];
std::string changeType = change["type"].asString();
for (int j = 0; j < (int) changeArgs.size(); j++) {
Json::UInt I = 0;
Json::Value &singleChange = changeArgs[j];
if (changeType == "a") {
/* add */
int id = singleChange[I++].asInt();
EntityType type = static_cast<EntityType>(singleChange[I++].asInt());
int row = singleChange[I++].asInt();
int col = singleChange[I++].asInt();
Cell* newCell = new Cell(row, col);
CERR("ADD\t" << id << "\t" << row << ", " << col << "\n");
if (type == EntityType::BEETLE) {
Direction dir = static_cast<Direction>(singleChange[I++].asInt());
bool wing = singleChange[I++].asBool();
BeetleType type = static_cast<BeetleType>(singleChange[I++].asInt());
bool sick = singleChange[I++].asBool();
int team_id = singleChange[I++].asInt();
map->addEntity(new Beetle(id, newCell, dir, wing, type, sick, team_id));
} else if (type == EntityType::FOOD) {
map->addEntity(new Food(id, newCell));
} else if (type == EntityType::SLIPPERS) {
map->addEntity(new Slippers(id, newCell));
} else if (type == EntityType::TRASH) {
map->addEntity(new Trash(id, newCell));
} else {
throw("unknown entity type");
}
} else if (changeType == "d") {
/* delete */
int id = singleChange[I++].asInt();
CERR("DEL\t" << id << "\n");
map->delEntity(id);
} else if (changeType == "m") {
/* move */
int id = singleChange[I++].asInt();
CERR("MOV\t" << id << "\n");
Move move = static_cast<Move>(singleChange[I++].asInt());
map->moveEntity(id, move);
} else if (changeType == "c") {
/* alter */
int id = singleChange[I++].asInt();
int x = singleChange[I++].asInt();
int y = singleChange[I++].asInt();
bool wing = singleChange[I++].asBool();
bool sick = singleChange[I++].asBool();
CERR("ALT\t" << id << "\t" << x << ", " << y << "\t" << wing << " " << sick << "\n");
map->moveEntity(id, x, y, wing, sick);
} else {
/* error */
throw("unknown change type");
}
}
}
}