forked from ambi88dex/crazy-robots
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcrazy_robots.cpp
More file actions
305 lines (269 loc) · 7.04 KB
/
Copy pathcrazy_robots.cpp
File metadata and controls
305 lines (269 loc) · 7.04 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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include <iostream>
#include <vector>
#include <cstdlib>
#include <thread>
#include <chrono>
#include <algorithm>
using namespace std;
struct Robot
{
char name;
int hp;
int x1;
int y1;
int x2;
int y2;
Robot(char name, int hp, int x1, int y1, int x2, int y2) :
name(name),
hp(hp),
x1(x1),
y1(y1),
x2(x2),
y2(y2)
{
}
};
struct Shoot
{
int x;
int y;
int dir;
Shoot(int x, int y, int dir) :
x(x),
y(y),
dir(dir)
{
}
};
vector<Robot> robots;
vector<Shoot> shoots;
bool locationIsInRobot(const Robot& robot, const int& xPosition, const int& yPosition)
{
return xPosition >= robot.x1 && xPosition <= robot.x2 && yPosition >= robot.y1 && yPosition <= robot.y2;
}
bool isShootLocation(const int& xPosition,const int& yPosition)
{
for (Shoot &shoot : shoots)
if (xPosition == shoot.x && yPosition == shoot.y)
return true;
return false;
}
void printRobotHP(const Robot& robot)
{
for (int hp = 0; hp < robot.hp; hp++)
cout << "#";
for (int hits = 0; hits < (10 - robot.hp); hits++)
cout << " ";
}
void printGameField()
{
for (int yPosition = 0; yPosition < 20; yPosition++) {
for (int xPosition = 0; xPosition < 20; xPosition++) {
bool printFlag = false;
for (Robot &robot : robots) {
if (locationIsInRobot(robot,xPosition,yPosition)) {
cout << robot.name;
printFlag = true;
break;
}
}
if (!printFlag)
cout << (isShootLocation(xPosition, yPosition) ? "*" : "_");
}
cout << "|" << endl;
}
}
void printHPArea()
{
for (Robot &robot : robots) {
cout << "HP BOT " << robot.name << ": ";
printRobotHP(robot);
cout << "|" << endl;
}
}
void printscreen()
{
#ifdef _WIN32
system("CLS"); //clears screen for Windows OS
#else
system("clear"); //clears screen for UNIX OS
#endif
printGameField();
printHPArea();
}
void robot_movements(string acts[], Robot& r)
{
string c = acts[rand() % 8];
if (c == "a") {
if (r.x1 > 0) {
r.x1--;
r.x2--;
}
}
else if (c == "d") {
if (r.x2 < 19) {
r.x1++;
r.x2++;
}
}
else if (c == "w") {
if (r.y1 > 0) {
r.y1--;
r.y2--;
}
}
else if (c == "s") {
if (r.y2 < 19) {
r.y1++;
r.y2++;
}
}
else if (c == "f1")// left
shoots.push_back(Shoot(r.x1 - 1, r.y1, 0));
else if (c == "f2") // right
shoots.push_back(Shoot(r.x2 + 1, r.y1, 1));
else if (c == "f3") // up
shoots.push_back(Shoot(r.x1, r.y1 - 1, 2));
else if (c == "f4") // down
shoots.push_back(Shoot(r.x1, r.y2 + 1, 3));
}
void removeDeadRobots() {
robots.erase(remove_if(robots.begin(), robots.end(), [](Robot& x) {return x.hp < 1; }), robots.end());
}
void handleHits() {
for (auto it = shoots.begin(); it != shoots.end(); ) {
for (Robot &robot : robots) {
if (locationIsInRobot(robot,(*it).x,(*it).y)) {
robot.hp--;
it = shoots.erase(it);
continue;
}
}
++it;
}
}
bool thereIsOneLastSurvivor(){
//check winning condition: last survivor
if (robots.size() == 1) {
cout << "ROBOT "<< robots[0].name <<" WINS!!!" << endl;
return true;
}
else if (robots.size() == 0) {
cout << "ALL ROBOTS HAVE BEEN DESTROYED. IT'S A DRAW" << endl;
return true;
}
return false;
}
void doRobotMovement() {
static string acts[] = {"a", "d", "w", "s", "f1", "f2", "f3", "f4"};
for (Robot &robot : robots)
robot_movements(acts, robot);
}
void doShootMovement() {
for (Shoot &s : shoots) {
if (s.dir == 0) {
if (s.x > 0)
s.x--;
else {
s.x = -1;
s.y = -1;
}
}
else if (s.dir == 1) {
if (s.x < 19)
s.x++;
else {
s.x = -1;
s.y = -1;
}
}
else if (s.dir == 2) {
if (s.y > 0)
s.y--;
else {
s.x = -1;
s.y = -1;
}
}
else if (s.dir == 3) {
if (s.y < 19)
s.y++;
else {
s.x = -1;
s.y = -1;
}
}
}
vector<Shoot> tmp;
for (Shoot &s : shoots)
if (s.x != -1 && s.y != -1)
tmp.push_back(s);
shoots.clear();
for (Shoot &s : tmp)
shoots.push_back(s);
}
bool robotCrash(const Robot& firstRobot, const Robot& secondRobot) {
return locationIsInRobot(secondRobot,firstRobot.x1, firstRobot.y1) ||
locationIsInRobot(secondRobot, firstRobot.x1, firstRobot.y2) ||
locationIsInRobot(secondRobot, firstRobot.x2, firstRobot.y1) ||
locationIsInRobot(secondRobot, firstRobot.x2, firstRobot.y2);
}
void checkCrash() {
//crashFlags handles when multiple robots crash at once
int crashFlags[] = { 0,0,0,0 };
int counter = 0; //flag counter for parent loop
int next = 0; //flag counter for child loop and other robot
for (auto firstRobot = robots.begin(); firstRobot != robots.end(); firstRobot++){
for (auto secondRobot = firstRobot; secondRobot != robots.end(); secondRobot++) {
if (secondRobot != firstRobot && robotCrash((*firstRobot),(*secondRobot))){
if (crashFlags[counter]!=1){
(*firstRobot).hp--; //remove health from Robot closer to the front of vector
crashFlags[counter] = 1;
}
if (crashFlags[counter + next] != 1){
(*secondRobot).hp--; //remove the health from Robot it crashed into
crashFlags[counter + next] = 1;
}
}
next++;
}
next = 0;
counter++;
}
}
int main(int argc, char *argv[])
{
int numberOfRobots;
if (argc == 2) {
numberOfRobots = atoi(argv[1]);
if (numberOfRobots < 2 || numberOfRobots > 4) {
cout << "2 <= Number of robots <= 4\n";
return 1;
}
}
else {
cout << "One argument expected: number of robots.\n";
return 1;
}
srand(time(0));
if (numberOfRobots >= 2) {
robots.push_back(Robot('X',10, 0, 0, 1, 1));
robots.push_back(Robot('Y',10, 18, 18, 19, 19));
}
if (numberOfRobots >= 3)
robots.push_back(Robot('A',10, 0, 18, 1, 19));
if (numberOfRobots >= 4)
robots.push_back(Robot('B',10, 18, 0, 19, 1));
while (true) {
this_thread::sleep_for(chrono::microseconds(50000));
handleHits();
removeDeadRobots();
printscreen();
if(thereIsOneLastSurvivor())
return 0;
doRobotMovement();
doShootMovement();
checkCrash();
}
return 0;
}