-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_vs_zombies.cpp
More file actions
285 lines (241 loc) · 8.13 KB
/
code_vs_zombies.cpp
File metadata and controls
285 lines (241 loc) · 8.13 KB
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
#include <cmath>
#include <iostream>
#include <ostream>
#include <string>
#include <tuple>
#include <vector>
#include <algorithm>
#include <list>
#include <memory>
#include <map>
#include <math.h>
#include <chrono>
#include <set>
using namespace std;
#define INT_MAX 2147483647
/**
* Save humans, destroy zombies!
**/
struct Position {
Position():x(0), y(0){
}
Position(int x_coord, int y_coord){
x = x_coord;
y = y_coord;
}
Position(const Position& p){
x = p.x;
y = p.y;
}
friend bool operator!=(const Position& lhs, const Position& rhs) { return !(lhs == rhs);}
friend bool operator==(const Position& lhs, const Position& rhs) { return (lhs.x == rhs.x && lhs.y == rhs.y);}
friend Position operator+(const Position& lhs, const Position& rhs) { return Position (lhs.x + rhs.x, lhs.y + rhs.y); }
int x = 0;
int y = 0;
int distanceTo(const Position& pos) const {
return sqrt(pow((pos.x - x),2.0) + pow((pos.y - y),2.0));
}
};
string to_string(const Position& p){
return (to_string(p.x) + " " + (to_string(p.y)));
}
string to_string(std::shared_ptr<Position> p){
return (to_string(p->x) + " " + (to_string(p->y)));
}
class Player {
public:
Player(int x, int y){
mPos.x = x;
mPos.y = y;
}
Position mPos = {};
int mSpeed = 1000;
int mRange = 1000;
};
class Human {
public:
Human( int id, int x, int y){
mId = id;
mPos.x = x;
mPos.y = y;
}
Human(const Human& h){
mId = h.mId;
mPos = h.mPos;
}
int mId = 0;
Position mPos = {};
};
string to_string(const Human& h){
return ("Id: " + to_string(h.mId) + " Pos:" + to_string(h.mPos.x) + "," + (to_string(h.mPos.y)));
}
class Zombie {
public:
Zombie(int id,int x, int y, int nextX, int nextY){
mId = id;
mPos.x = x;
mPos.y = y;
mNextPos.x = nextX;
mNextPos.y = nextY;
}
Zombie(const Zombie& z){
mId = z.mId;
mPos = z.mPos;
mNextPos = z.mNextPos;
}
int mId = 0;
Position mPos = {};
Position mNextPos = {};
int mSpeed = 400;
int mRange = 400;
};
string to_string(const Zombie& z){
return ("Id: " + to_string(z.mId) + " Pos:" + to_string(z.mPos.x) + "," + (to_string(z.mPos.y)));
}
class Threat {
public:
Threat( int distance,Zombie z, Human h): mDistance(distance), mZombie(z), mHuman(h){ }
friend bool operator<(const Threat& lhs, const Threat& rhs) {return lhs.mDistance<rhs.mDistance;}
int mDistance;
Zombie mZombie;
Human mHuman;
};
/*************** GLOBALS ***************/
static Player g_Player (0,0);
static vector<Human> g_HumanList;
static vector<Zombie> g_ZombieList;
//Refresh them after every loop
void clearGlobals(){
g_HumanList.clear();
g_ZombieList.clear();
}
/*********************************************/
std::shared_ptr<Zombie> getCloserZombieToPos(Position pos);
std::multiset<Threat> getOrderedThreatsList();
std::shared_ptr<Human> getCloserHumanToMe();
//A list of threats which is defined by a zombie, a human and the distance between them
std::multiset<Threat> getOrderedThreatsList(){
int closest_zombie_distance = INT_MAX;
std::multiset<Threat> ret;
for (auto human : g_HumanList){
std::shared_ptr<Zombie> z = getCloserZombieToPos(human.mPos);
if(z != nullptr){
ret.insert(Threat( z->mNextPos.distanceTo(human.mPos),*z, human));
}
}
return ret;
}
std::shared_ptr<Zombie> getCloserZombieToPos(Position pos){
int distance = INT_MAX;
std::shared_ptr<Zombie> ret = nullptr;
for (auto zombie : g_ZombieList){
int new_distance = zombie.mNextPos.distanceTo(pos);
if(new_distance < distance){
distance = new_distance;
ret = std::make_shared<Zombie>(zombie);
}
}
return ret;
}
std::shared_ptr<Zombie> getCloserZombieToMe(){
return getCloserZombieToPos(g_Player.mPos);
}
std::shared_ptr<Human> getCloserHumanToMe(){
int distance = INT_MAX;
std::shared_ptr<Human> ret = nullptr;
for (auto human : g_HumanList){
int new_distance = human.mPos.distanceTo(g_Player.mPos);
if(new_distance < distance){
distance = new_distance;
ret = std::make_shared<Human>(human);
}
}
return ret;
}
//Calculate how much turns:
//1. the zombie needs to get to the human
//2. the player needs to get to the zombie next pos !!
bool canReachDestinationInTime(Position human_pos, Zombie zombie, Position player_pos){
cerr << "human_pos " << to_string(human_pos) << ", zombie_pos " << to_string(zombie.mPos) << ", player_pos " << to_string(player_pos) << endl;
//TODO: Should it be float ? Do we need that precise comparison?
int turns_to_kill = zombie.mPos.distanceTo(human_pos) / 800;
int turns_to_save = player_pos.distanceTo(zombie.mNextPos) / 2000;
cerr << "Turns needed to kill human :"<< turns_to_kill << endl;
cerr << "Turns needed to save human :"<< turns_to_save << endl;
if(turns_to_kill+1 >= turns_to_save){ //+1 as the zombie kills at the end of the turn, we kill before
return true;
}
return false;
}
std::shared_ptr<Position> getReachableThreadDestination(){
std::multiset threats = getOrderedThreatsList();
if(threats.size() != 0){
for ( auto threats_it = threats.begin(); threats_it != threats.end(); threats_it++ ){
cerr << "dangerous_zombie at "<< to_string(threats_it->mZombie.mPos) << endl;
if (canReachDestinationInTime(threats_it->mHuman.mPos, threats_it->mZombie,g_Player.mPos)){
cerr << "Can reach zombie [" << to_string(threats_it->mZombie) << "] in time, let's go "<< endl;
return make_shared<Position>(threats_it->mZombie.mNextPos);
//cout << to_string(dangerous_zombie->mNextPos) << endl;
} else {
cerr << "Discarding zombie " << to_string(threats_it->mZombie) << ", we would never save the human"<< endl;
}
}
}
return nullptr;
}
int main()
{
// game loop
while (1) {
int x;
int y;
cin >> x >> y; cin.ignore();
clearGlobals();
g_Player.mPos = Position(x,y);
int human_count;
cin >> human_count; cin.ignore();
for (int i = 0; i < human_count; i++) {
int human_id;
int human_x;
int human_y;
cin >> human_id >> human_x >> human_y; cin.ignore();
Human h(human_id, human_x, human_y);
g_HumanList.push_back(h);
}
int zombie_count;
cin >> zombie_count; cin.ignore();
for (int i = 0; i < zombie_count; i++) {
int zombie_id;
int zombie_x;
int zombie_y;
int zombie_xnext;
int zombie_ynext;
cin >> zombie_id >> zombie_x >> zombie_y >> zombie_xnext >> zombie_ynext; cin.ignore();
Zombie z(zombie_id, zombie_x, zombie_y,zombie_xnext,zombie_ynext );
g_ZombieList.push_back(z);
}
cerr << "g_ZombieList size "<< g_ZombieList.size() << endl;
cerr << "g_HumanList size "<< g_HumanList.size() << endl;
// Write an action using cout. DON'T FORGET THE "<< endl"
// To debug: cerr << "Debug messages..." << endl;
//TODO: Create a threshold to kill nearby enemies if needed
std::shared_ptr<Position> next_destination = getReachableThreadDestination();
if(next_destination != nullptr){
cout << to_string(next_destination) << endl;
} else {
cerr << "Could not find a human to save "<< endl;
std::shared_ptr<Zombie> z = getCloserZombieToMe();;
if(z != nullptr){
cout << to_string(z->mNextPos) << endl;
} else {
std::shared_ptr<Human> h = getCloserHumanToMe();
if(h != nullptr){
cout << to_string(h->mPos) << endl;
} else {
cerr << "Something is bad!" << endl;
cout << "0 0" << endl; // Your destination coordinates
}
}
}
}
}