-
Notifications
You must be signed in to change notification settings - Fork 0
/
State.cc
354 lines (328 loc) · 10.8 KB
/
State.cc
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
#include "State.h"
#include <sstream>
using namespace std;
//constructor
State::State()
: rows(200), cols(200) // for grid
{
rows = cols = 0;
gameover = 0;
turn = 0;
noPlayers = noHills = 0;
diedByPlayer.resize(10, 0);
ateByPlayer.resize(10, 0);
avgHillSpacing = 0;
stringstream ss;
ss << "debug." << getpid() << ".txt";
bug.open(ss.str());
}
//deconstructor
State::~State()
{
bug.close();
}
//sets the state up
void State::setup()
{
_row.init(rows);
_col.init(cols);
GridBase::rows = rows;
GridBase::cols = cols;
visionNeighborhood = neighborhood_offsets(viewradius);
combatNeighborhood = dialate_neighborhood(neighborhood_offsets(attackradius), 2);
for (int d = 0; d < TDIRECTIONS + 1; ++d)
visionArc.push_back(neighborhood_arc(viewradius, d));
}
//resets all non-water squares to land and clears the bots ant vector
void State::reset()
{
myAnts.clear();
enemyAnts.clear();
myHills.clear();
enemyHills.clear();
food.clear();
for(int row=0; row<rows; row++)
for(int col=0; col<cols; col++)
grid[row][col].reset();
fill(diedByPlayer.begin(), diedByPlayer.end(), 0);
fill(ateByPlayer.begin(), ateByPlayer.end(), 0);
}
//outputs move information to the engine
void State::makeMove(const Location &loc, int direction)
{
cout << "o " << loc.row << " " << loc.col << " " << CDIRECTIONS[direction] << endl;
Location nLoc = getLocation(loc, direction);
grid[nLoc.row][nLoc.col].ant = grid[loc.row][loc.col].ant;
grid[loc.row][loc.col].ant = -1;
}
//returns the euclidean distance between two locations with the edges wrapped
double State::distance(const Location &loc1, const Location &loc2) const
{
int d1 = abs(loc1.row-loc2.row),
d2 = abs(loc1.col-loc2.col),
dr = min(d1, rows-d1),
dc = min(d2, cols-d2);
return sqrt(dr*dr + dc*dc);
}
/*
This function will update update the lastSeen value for any squares currently
visible by one of your live ants.
BE VERY CAREFUL IF YOU ARE GOING TO TRY AND MAKE THIS FUNCTION MORE EFFICIENT,
THE OBVIOUS WAY OF TRYING TO IMPROVE IT BREAKS USING THE EUCLIDEAN METRIC, FOR
A CORRECT MORE EFFICIENT IMPLEMENTATION, TAKE A LOOK AT THE GET_VISION FUNCTION
IN ANTS.PY ON THE CONTESTS GITHUB PAGE.
*/
void State::updateVisionInformation()
{
std::queue<Location> locQueue;
Location sLoc, cLoc, nLoc;
visibleSquares = 0;
for (iterator it = myAnts.begin(); it != myAnts.end(); ++it) {
for (iterator vt = visionNeighborhood.begin(); vt != visionNeighborhood.end(); ++vt) {
if (grid(deltaLocation(*it, (*vt).row, (*vt).col)).setVisible(turn))
++visibleSquares;
}
}
for (LocationSet::iterator it = allEnemyHills.begin(); it != allEnemyHills.end();) {
const Square &square = grid[(*it).row][(*it).col];
if (square.isVisible && !square.isHill)
allEnemyHills.erase(it++);
else
++it;
}
for (LocationSet::iterator it = allMyHills.begin(); it != allMyHills.end();) {
const Square &square = grid[(*it).row][(*it).col];
if (square.isVisible && !square.isHill)
allMyHills.erase(it++);
else
++it;
}
for (LocationSet::iterator it = allFood.begin(); it != allFood.end();) {
const Square &square = grid[(*it).row][(*it).col];
if (square.isVisible && !square.isFood) {
int eater = -1;
for (int d = 0; d < TDIRECTIONS; ++d) {
int player = grid(getLocation(*it, d)).ant;
if (player == -1) {
continue;
} else if (eater == -1) {
eater = player;
} else if (eater != player) {
eater = -1;
break;
}
}
if (eater >= 0)
ateByPlayer[eater]++;
allFood.erase(it++);
} else
++it;
}
}
/*
This is the output function for a state. It will add a char map
representation of the state to the output stream passed to it.
For example, you might call "cout << state << endl;"
*/
ostream& operator<<(ostream &os, const State &state)
{
for(int row=0; row<state.rows; row++)
{
for(int col=0; col<state.cols; col++)
{
if(state.grid[row][col].isWater)
os << '%';
else if(state.grid[row][col].isFood)
os << '*';
else if(state.grid[row][col].isHill)
os << (char)('A' + state.grid[row][col].hillPlayer);
else if(state.grid[row][col].ant >= 0)
os << (char)('a' + state.grid[row][col].ant);
else if(state.grid[row][col].isVisible)
os << '.';
else if(state.grid[row][col].wasVisible)
os << ',';
else
os << '?';
}
os << endl;
}
return os;
}
//input function
istream& operator>>(istream &is, State &state)
{
int row, col, player;
string inputType, junk;
//finds out which turn it is
while(is >> inputType)
{
if(inputType == "end")
{
state.gameover = 1;
break;
}
else if(inputType == "turn")
{
is >> state.turn;
break;
}
else //unknown line
getline(is, junk);
}
if(state.turn == 0)
{
//reads game parameters
while(is >> inputType)
{
if(inputType == "loadtime")
is >> state.loadtime;
else if(inputType == "turntime")
is >> state.turntime;
else if(inputType == "rows")
is >> state.rows;
else if(inputType == "cols")
is >> state.cols;
else if(inputType == "turns")
is >> state.turns;
else if(inputType == "player_seed")
is >> state.seed;
else if(inputType == "players") //player information
is >> state.noPlayers;
else if(inputType == "viewradius2")
{
is >> state.viewradius;
state.viewradius = sqrt(state.viewradius);
}
else if(inputType == "attackradius2")
{
is >> state.attackradius;
state.attackradius = sqrt(state.attackradius);
}
else if(inputType == "spawnradius2")
{
is >> state.spawnradius;
state.spawnradius = sqrt(state.spawnradius);
}
else if(inputType == "ready") //end of parameter input
{
state.timer.start();
break;
}
else //unknown line
getline(is, junk);
}
}
else
{
//reads information about the current turn
while(is >> inputType)
{
if(inputType == "w") //water square
{
is >> row >> col;
state.grid[row][col].isWater = 1;
for (int d = 0; d < TDIRECTIONS; ++d)
state.grid(state.getLocation(Location(row,col), d)).byWater++;
}
else if(inputType == "f") //food square
{
is >> row >> col;
state.grid[row][col].isFood = 1;
state.food.push_back(Location(row, col));
state.allFood.insert(Location(row, col));
}
else if(inputType == "a") //live ant square
{
is >> row >> col >> player;
state.grid[row][col].ant = player;
if(player == 0)
state.myAnts.push_back(Location(row, col));
else
state.enemyAnts.push_back(Location(row, col));
}
else if(inputType == "d") //dead ant square
{
is >> row >> col >> player;
state.diedByPlayer[player]++;
state.grid[row][col].putDeadAnt(player);
}
else if(inputType == "h")
{
is >> row >> col >> player;
state.grid[row][col].isHill = 1;
state.grid[row][col].hillPlayer = player;
if(player == 0) {
state.myHills.push_back(Location(row, col));
state.allMyHills.insert(Location(row, col));
} else {
state.enemyHills.push_back(Location(row, col));
state.allEnemyHills.insert(Location(row, col));
}
}
else if(inputType == "scores") //score information
{
state.scores = vector<double>(state.noPlayers, 0.0);
for(int p=0; p<state.noPlayers; p++)
is >> state.scores[p];
}
else if(inputType == "go") //end of turn input
{
if(state.gameover)
is.setstate(std::ios::failbit);
else
state.timer.start();
break;
}
else {//unknown line
state.bug << "unknown " << inputType << endl;
getline(is, junk);
}
}
}
return is;
}
// NB: not quite the same as ants.py. Includes (0,0), does not
// do offsetting of negative
vector<Location>
State::neighborhood_offsets(double max_dist) const
{
vector<Location> neighborhood;
int d = max_dist + 1;
for (int dr = -d; dr <= d; ++dr) {
for (int dc = -d; dc <= d; ++dc) {
if (sqrt(dr*dr + dc*dc) <= max_dist)
neighborhood.push_back(Location(dr, dc));
}
}
return neighborhood;
}
vector<Location>
State::dialate_neighborhood(const vector<Location> &orig, int n) const
{
LocationSet neighborhood(orig.begin(), orig.end());
while (n--) {
LocationSet dialated;
for (LocationSet::iterator it = neighborhood.begin(); it != neighborhood.end(); ++it) {
for (int d = 0; d < TDIRECTIONS + 1; ++d) {
dialated.insert(Location((*it).row + DIRECTIONS[d][0],
(*it).col + DIRECTIONS[d][1]));
}
}
swap(dialated, neighborhood);
}
return vector<Location>(neighborhood.begin(), neighborhood.end());
}
vector<Location>
State::neighborhood_arc(double max_dist, int d) const
{
vector<Location> neighborhood = neighborhood_offsets(max_dist);
vector<Location> arc;
for (State::iterator it = neighborhood.begin(); it != neighborhood.end(); ++it) {
Location test((*it).row + DIRECTIONS[d][0],
(*it).col + DIRECTIONS[d][1]);
if (find(neighborhood.begin(), neighborhood.end(), test) == neighborhood.end())
arc.push_back(*it);
}
return arc;
}