-
Notifications
You must be signed in to change notification settings - Fork 0
/
State.hh
242 lines (193 loc) · 5.12 KB
/
State.hh
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
#ifndef State_hh
#define State_hh
#include "Structs.hh"
/*! \file
* Contains a class to store the current state of a game.
*/
/**
* Stores the game state.
*/
class State {
public:
/**
* Returns the current round.
*/
int round () const;
/**
* Returns the total score of a player.
*/
int total_score (int pl) const;
/**
* Returns the percentage of cpu time used in the last round, in the
* range [0.0 - 1.0] or a value lesser than 0 if this player is dead.
* Note that this only returns a sensible value in server executions.
*/
double status (int pl) const;
/**
* Returns a copy of the cell at (i, j).
*/
Cell cell (int i, int j) const;
/**
* Returns a copy of the cell at p.
*/
Cell cell (Pos p) const;
/**
* Returns the total number of units (= orks) in the game.
*/
int nb_units () const;
/**
* Returns the information of the unit with (unit) identifier id.
* Units are identified with natural numbers from 0 to nb_units() - 1.
*/
Unit unit (int id) const;
/**
* Returns the number of cities in the game.
*/
int nb_cities () const;
typedef vector<Pos> City; // City type
/**
* Returns the information of the city with (city) identifier id.
* Cities are identified with natural numbers from 0 to nb_cities() - 1.
*/
City city(int id) const;
/**
* Returns the identifier of the player who last conquered the city
* with identifier id, -1 if the city has never been conquered.
*/
int city_owner(int id) const;
/**
* Returns the total number of paths in the game.
*/
int nb_paths () const;
typedef pair< pair<int,int>, vector<Pos> > Path; // Path type
/**
* Returns the information of the path with (path) identifier id.
* Paths are identified with natural numbers from 0 to nb_path() - 1.
*/
Path path(int id) const;
/**
* Returns the identifier of the player who last conquered the path
* with identifier id, -1 if the path has never been conquered.
*/
int path_owner(int id) const;
/**
* Returns the ids of all the orks of a player.
*/
vector<int> orks(int pl);
//////// STUDENTS DO NOT NEED TO READ BELOW THIS LINE ////////
private:
friend class Info;
friend class Board;
friend class Game;
friend class SecGame;
friend class Player;
vector<City> city_;
vector<Path> path_;
vector<vector<Cell>> grid_;
vector<int> city_owner_;
vector<int> path_owner_;
vector<Unit> unit_;
vector< vector<int> > orks_;
int round_;
vector<int> total_score_;
vector<double> cpu_status_; // -1 -> dead, 0..1 -> % of cpu time limit
/**
* Returns whether id is a valid unit identifier.
*/
inline bool unit_ok (int id) const {
return id >= 0 and id < nb_units();
}
inline bool city_ok (int id) const {
return id >= 0 and id < nb_cities();
}
inline bool path_ok (int id) const {
return id >= 0 and id < nb_paths();
}
};
inline int State::round () const {
return round_;
}
inline Cell State::cell (int i, int j) const {
if (i >= 0 and i < (int)grid_.size() and j >= 0 and j < (int)grid_[i].size())
return grid_[i][j];
else {
cerr << "warning: cell requested for position " << Pos(i, j) << endl;
return Cell();
}
}
inline Cell State::cell (Pos p) const {
return cell(p.i, p.j);
}
inline int State::nb_units () const {
return unit_.size();
}
inline int State::nb_cities () const {
return city_.size();
}
inline int State::nb_paths () const {
return path_.size();
}
inline int State::total_score (int pl) const {
if (pl >= 0 and pl < (int)total_score_.size())
return total_score_[pl];
else {
cerr << "warning: total score requested for player " << pl << endl;
return -1;
}
}
inline double State::status (int pl) const {
if (pl >= 0 and pl < (int)cpu_status_.size())
return cpu_status_[pl];
else {
cerr << "warning: status requested for player " << pl << endl;
return -2;
}
}
inline Unit State::unit (int id) const {
if (unit_ok(id))
return unit_[id];
else {
cerr << "warning: unit requested for identifier " << id << endl;
return Unit();
}
}
inline State::City State::city(int id) const {
if (city_ok(id))
return city_[id];
else {
cerr << "warning: city requested for identifier " << id << endl;
return City();
}
}
inline State::Path State::path(int id) const {
if (path_ok(id))
return path_[id];
else {
cerr << "warning: path requested for identifier " << id << endl;
return Path();
}
}
inline int State::city_owner(int id) const {
if (city_ok(id))
return city_owner_[id];
else {
cerr << "warning: city owner requested for identifier " << id << endl;
return -1;
}
}
inline int State::path_owner(int id) const {
if (path_ok(id))
return path_owner_[id];
else {
cerr << "warning: path owner requested for identifier " << id << endl;
return -1;
}
}
inline vector<int> State::orks (int pl) {
if (pl >= 0 and pl < (int)orks_.size()) return orks_[pl];
else {
cerr << "warning: orks requested for player " << pl << endl;
return vector<int>();
}
}
#endif