-
Notifications
You must be signed in to change notification settings - Fork 0
/
State.cc
269 lines (245 loc) · 7.76 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
#include "State.h"
using namespace std;
//constructor
State::State()
{
gameover = 0;
turn = 0;
bug.open("./debug.txt");
};
//deconstructor
State::~State()
{
bug.close();
};
//sets the state up
void State::setup()
{
grid = vector<vector<Square> >(rows, vector<Square>(cols, Square()));
};
//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++)
if(!grid[row][col].isWater)
grid[row][col].reset();
};
//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)
{
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);
};
//returns the new location from moving in a given direction with the edges wrapped
Location State::getLocation(const Location &loc, int direction)
{
return Location( (loc.row + DIRECTIONS[direction][0] + rows) % rows,
(loc.col + DIRECTIONS[direction][1] + cols) % cols );
};
/*
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;
for(int a=0; a<(int) myAnts.size(); a++)
{
sLoc = myAnts[a];
locQueue.push(sLoc);
std::vector<std::vector<bool> > visited(rows, std::vector<bool>(cols, 0));
grid[sLoc.row][sLoc.col].isVisible = 1;
grid[sLoc.row][sLoc.col].seen = 1;
visited[sLoc.row][sLoc.col] = 1;
while(!locQueue.empty())
{
cLoc = locQueue.front();
locQueue.pop();
for(int d=0; d<TDIRECTIONS; d++)
{
nLoc = getLocation(cLoc, d);
if(!visited[nLoc.row][nLoc.col] && distance(sLoc, nLoc) <= viewradius)
{
grid[nLoc.row][nLoc.col].isVisible = 1;
grid[nLoc.row][nLoc.col].seen = 1;
locQueue.push(nLoc);
}
visited[nLoc.row][nLoc.col] = 1;
}
}
}
};
/*
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
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 == "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;
}
else if(inputType == "f") //food square
{
is >> row >> col;
state.grid[row][col].isFood = 1;
state.food.push_back(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.grid[row][col].deadAnts.push_back(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));
else
state.enemyHills.push_back(Location(row, col));
}
else if(inputType == "players") //player information
is >> state.noPlayers;
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
getline(is, junk);
}
}
return is;
};