-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzombbb.cpp
386 lines (356 loc) · 14.3 KB
/
zombbb.cpp
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
// Project identifier: 9504853406CBAC39EE89AA3AD238AA12CA198043
#include "P2random.h"
#include "median.h"
#include "zomb.h"
#include <getopt.h>
#include <string>
#include <vector>
#include <iostream>
#include <algorithm>
#include <deque>
#include <queue>
#include <utility>
using namespace std;
int main(int argc, char* argv[]) {
std::ios::sync_with_stdio(false);
bool isVerbose = false;
bool isStats = false;
bool isMedian = false;
size_t statsNum = 0;
uint32_t quivCap = 0;
uint32_t seed = 0;
uint32_t maxRandDist = 0;
uint32_t maxRandSpd = 0;
uint32_t maxRandHlth = 0;
// Command Line Processing
opterr = false; // Let us handle all error output for command line options
int choice = 0;
int option_index = 0;
option long_options[] = {
{ "verbose", no_argument, nullptr, 'v' },
{ "median", no_argument, nullptr, 'm' },
{ "statistics", required_argument, nullptr, 's' },
{ "help", no_argument, nullptr, 'h' },
{ nullptr, 0, nullptr, '\0'}
};
while ((choice = getopt_long(argc, argv, "vms:h", long_options, &option_index)) != -1) {
switch (choice) {
case 'h':
cout << "This program simulates a game of The Walking Deadline. Please specify which flags "
"you would like to be active for informational output. Possible flags are v(erbose)"
", s(tatistics), and m(edian).\n";
exit(0);
case 'v':
isVerbose = true;
break;
case 'm':
isMedian = true;
break;
case 's':
isStats = true;
statsNum = atoi(optarg);
break;
} // switch
} // while
// Command Line Processing
// File Header Info Handling
string junk;
getline(cin, junk);
cin >> junk >> quivCap >> junk >> seed >> junk >> maxRandDist
>> junk >> maxRandSpd >> junk >> maxRandHlth;
getline(cin, junk);
//initializes random zombie creation based on file header info
P2random::initialize(seed, maxRandDist, maxRandSpd, maxRandHlth);
// Do game.
uint32_t quiv = 0;
uint32_t round = 1;
uint32_t numKilled = 0;
uint32_t numCreated = 0;
string nxtNm;
uint32_t nxtDst;
uint32_t nxtSpd;
uint32_t nxtHlth;
deque<zomb> masterList;
vector<zomb*> masterPtrs; // use this vec to range-base initialize most/least active at the end of program
vector<zomb*> deadZombs; // use this to print first/last killed at the end
priority_queue<zomb*, vector<zomb*>, revZombCMP> liveZombs;
medianZombQueue median; // push dead zombs in here
bool hasKilled = false; // track whether or not to output median at end of round
uint32_t currFileRound = 0;
string defeatZomb = "alive";
int newDist = 0;
bool playerAlive = true;
while (cin >> junk) {
quiv = quivCap;
cin >> junk >> currFileRound;
while (round != currFileRound) {
quiv = quivCap;
if (isVerbose) {
cout << "Round: " << round << "\n";
}
if (!liveZombs.empty()) {
for (uint32_t i = 0; i < masterPtrs.size(); ++i) { // Move zombs forward if not 1st round
if (masterPtrs[i]->isAlive == true) {
newDist = max(0, (masterPtrs[i]->dist - masterPtrs[i]->speed));
masterPtrs[i]->dist = newDist;
if (isVerbose) {
cout << "Moved: " << masterPtrs[i]->name << " (distance: " << masterPtrs[i]->dist
<< ", speed: " << masterPtrs[i]->speed << ", health: " << masterPtrs[i]->health << ")\n";
}
if (masterPtrs[i]->dist == 0 && defeatZomb == "alive") {
defeatZomb = masterPtrs[i]->name;
playerAlive = false;
}
}
}
}
if (!playerAlive) {
break;
}
zomb* currZomb; // zomb killing block
uint32_t zombHlth;
while (quiv != 0 && !liveZombs.empty()) {
currZomb = liveZombs.top();
zombHlth = currZomb->health;
if (quiv >= zombHlth) {
quiv -= zombHlth;
if (isVerbose) {
cout << "Destroyed: " << currZomb->name << " (distance: "
<< currZomb->dist << ", speed: " << currZomb->speed << ", health: 0)\n";
}
hasKilled = true;
currZomb->isAlive = false;
deadZombs.push_back(currZomb);
median.push(currZomb);
liveZombs.pop();
++numKilled;
}
else {
currZomb->health -= quiv;
quiv = 0;
}
}
if (isMedian && hasKilled) {
cout << "At the end of round " << round << ", the median zombie lifetime is " << median.getMed() << "\n";
}
for (uint32_t i = 0; i < masterPtrs.size(); ++i) {
if (masterPtrs[i]->isAlive) {
masterPtrs[i]->rdsAct += 1;
}
}
++round;
}
if (!playerAlive) {
break;
}
if (isVerbose) {
cout << "Round: " << round << "\n";
}
if (!liveZombs.empty()) {
for (uint32_t i = 0; i < masterPtrs.size(); ++i) { // Move zombs forward if not 1st round
if (masterPtrs[i]->isAlive == true) {
newDist = max(0, (masterPtrs[i]->dist - masterPtrs[i]->speed));
masterPtrs[i]->dist = newDist;
if (isVerbose) {
cout << "Moved: " << masterPtrs[i]->name << " (distance: " << masterPtrs[i]->dist
<< ", speed: " << masterPtrs[i]->speed << ", health: " << masterPtrs[i]->health << ")\n";
}
if (masterPtrs[i]->dist == 0 && defeatZomb == "alive") {
defeatZomb = masterPtrs[i]->name;
playerAlive = false;
}
}
}
}
if (!playerAlive) {
break;
}
uint32_t numRand = 0; // zomb spawning block begins here
uint32_t numCust = 0;
cin >> junk >> numRand >> junk >> numCust;
for (uint32_t i = 0; i < numRand; ++i) {
nxtNm = P2random::getNextZombieName();
nxtDst = P2random::getNextZombieDistance();
nxtSpd = P2random::getNextZombieSpeed();
nxtHlth = P2random::getNextZombieHealth();
zomb newZomb(nxtNm, nxtDst, nxtSpd, nxtHlth);
masterList.push_back(newZomb);
zomb* newZombPtr = &masterList.back();
liveZombs.push(newZombPtr);
masterPtrs.push_back(newZombPtr);
++numCreated;
if (isVerbose) {
cout << "Created: " << nxtNm << " (distance: " << nxtDst
<< ", speed: " << nxtSpd << ", health: " << nxtHlth << ")\n";
}
} // random zomb creation loop
for (uint32_t i = 0; i < numCust; ++i) {
cin >> nxtNm >> junk >> nxtDst >> junk >> nxtSpd >> junk >> nxtHlth;
zomb newZomb(nxtNm, nxtDst, nxtSpd, nxtHlth);
masterList.push_back(newZomb);
zomb* newZombPtr = &masterList.back();
liveZombs.push(newZombPtr);
masterPtrs.push_back(newZombPtr);
++numCreated;
if (isVerbose) {
cout << "Created: " << nxtNm << " (distance: " << nxtDst
<< ", speed: " << nxtSpd << ", health: " << nxtHlth << ")\n";
}
} // custom zomb creation loop
quiv = quivCap;
zomb* currZomb; // zomb killing block
uint32_t zombHlth;
while (quiv != 0 && !liveZombs.empty()) {
currZomb = liveZombs.top();
zombHlth = currZomb->health;
if (quiv >= zombHlth) {
quiv -= zombHlth;
if (isVerbose) {
cout << "Destroyed: " << currZomb->name << " (distance: "
<< currZomb->dist << ", speed: " << currZomb->speed << ", health: 0)\n";
}
hasKilled = true;
currZomb->isAlive = false;
deadZombs.push_back(currZomb);
median.push(currZomb);
liveZombs.pop();
++numKilled;
}
else {
currZomb->health -= quiv;
quiv = 0;
}
}
if (isMedian && hasKilled) {
cout << "At the end of round " << round << ", the median zombie lifetime is " << median.getMed() << "\n";
}
for (uint32_t i = 0; i < masterPtrs.size(); ++i) {
if (masterPtrs[i]->isAlive) {
masterPtrs[i]->rdsAct += 1;
}
}
++round;
}
while (playerAlive && !liveZombs.empty()) {
quiv = quivCap;
if (isVerbose) {
cout << "Round: " << round << "\n";
}
if (!liveZombs.empty()) {
for (uint32_t i = 0; i < masterPtrs.size(); ++i) { // Move zombs forward if not 1st round
if (masterPtrs[i]->isAlive == true) {
newDist = max(0, (masterPtrs[i]->dist - masterPtrs[i]->speed));
masterPtrs[i]->dist = newDist;
if (isVerbose) {
cout << "Moved: " << masterPtrs[i]->name << " (distance: " << masterPtrs[i]->dist
<< ", speed: " << masterPtrs[i]->speed << ", health: " << masterPtrs[i]->health << ")\n";
}
if (masterPtrs[i]->dist == 0 && defeatZomb == "alive") {
defeatZomb = masterPtrs[i]->name;
playerAlive = false;
}
}
}
}
if (!playerAlive) {
break;
}
zomb* currZomb; // zomb killing block
uint32_t zombHlth;
while (quiv != 0 && !liveZombs.empty()) {
currZomb = liveZombs.top();
zombHlth = currZomb->health;
if (quiv >= zombHlth) {
quiv -= zombHlth;
if (isVerbose) {
cout << "Destroyed: " << currZomb->name << " (distance: "
<< currZomb->dist << ", speed: " << currZomb->speed << ", health: 0)\n";
}
hasKilled = true;
currZomb->isAlive = false;
deadZombs.push_back(currZomb);
median.push(currZomb);
liveZombs.pop();
++numKilled;
}
else {
currZomb->health -= quiv;
quiv = 0;
}
}
if (isMedian && hasKilled) {
cout << "At the end of round " << round << ", the median zombie lifetime is " << median.getMed() << "\n";
}
for (uint32_t i = 0; i < masterPtrs.size(); ++i) {
if (masterPtrs[i]->isAlive) {
masterPtrs[i]->rdsAct += 1;
}
}
++round;
}
string lastZomb;
if (!deadZombs.empty()) {
lastZomb = deadZombs.back()->name;
}
// Survival or demise messages here
if (playerAlive) {
cout << "VICTORY IN ROUND " << round - 1 << "! " << lastZomb << " was the last zombie.\n";
}
else {
cout << "DEFEAT IN ROUND " << round << "! " << defeatZomb << " ate your brains!\n";
}
// stats output & most/least active priority queues
if (isStats) {
uint32_t numActive = numCreated - numKilled;
cout << "Zombies still active: " << numActive << "\n";
size_t loop = statsNum;
if (numKilled < statsNum) {
loop = numKilled; // Handles situations where we don't have N zombs to print
}
cout << "First zombies killed:\n";
size_t ctr = 1;
for (uint32_t i = 0; i < loop; ++i) {
cout << deadZombs[i]->name << " " << ctr << "\n";
++ctr;
}
cout << "Last zombies killed:\n";
ctr = loop;
if (numKilled < statsNum) {
if (!deadZombs.empty()) {
for (size_t i = (deadZombs.size() - 1); i > 0; --i) {
cout << deadZombs[i]->name << " " << ctr << "\n";
--ctr;
}
cout << deadZombs[0]->name << " " << ctr << "\n";
}
}
else {
if (!deadZombs.empty()) {
for (size_t i = (deadZombs.size() - 1); i > ((deadZombs.size() - 1) - loop); --i) {
cout << deadZombs[i]->name << " " << ctr << "\n";
--ctr;
}
}
}
priority_queue<zomb*, vector<zomb*>, mostActCMP> mostAct(masterPtrs.begin(), masterPtrs.end());
priority_queue<zomb*, vector<zomb*>, leastActCMP> leastAct(masterPtrs.begin(), masterPtrs.end());
loop = statsNum;
zomb* currentZomb;
if (masterPtrs.size() < statsNum) {
loop = masterPtrs.size(); // Handles situations where we don't have N zombs to print
}
cout << "Most active zombies:\n";
for (uint32_t i = 0; i < loop; ++i) {
currentZomb = mostAct.top();
cout << currentZomb->name << " " << currentZomb->rdsAct << "\n";
mostAct.pop();
}
cout << "Least active zombies:\n";
for (uint32_t i = 0; i < loop; ++i) {
currentZomb = leastAct.top();
cout << currentZomb->name << " " << currentZomb->rdsAct << "\n";
leastAct.pop();
}
}
return 0;
}