-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZombie.cpp
More file actions
432 lines (313 loc) · 11.1 KB
/
Copy pathZombie.cpp
File metadata and controls
432 lines (313 loc) · 11.1 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
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
/**************************************
** World War Z Simulation **
** Created By Charles Davis **
** CPSC 246-01 Dr. Valentine **
**************************************/
/* World War Z is a program that uses cellular automation simulation
* to model a zombie invasion. The program asks the user for the
* location of the first zombie and displays each frame as an animation
* showing how the zombies turn humans into new zombies and then die
* off and decay. Then it spits out the resulting statistics. How
* many humans are still alive? How many itterations did it take?
* What percentage of the crowd turned zombie?
*/
#include <iostream>
#include <string>
#include <stdio.h>
#include <time.h>
using namespace std;
const int SIZE = 30; //Array is SIZE x SIZE
//Function Prototypes
void setupGame(char current[][SIZE]);
void getFirstZombie(char current[][SIZE]);
void updateState(char current[][SIZE]);
int getNeighbors(const char current[][SIZE], int currentRow, int currentCol);
int getNumHumans(const char current[][SIZE]);
void wait(float seconds);
bool isValidCoordinate(int row, int col);
bool becomesZombie(int percentProbability);
void outputState(const char current[][SIZE]);
void outputResult(const char current[][SIZE], int gameCnt, int humansRemain);
bool hasNextZombie(const char current[][SIZE]);
/************************** Main **************************/
int main()
{
//Variable Dictionary
char gameStateArray[SIZE][SIZE]; //our main array
int count = 0; //count of iterations of the sim
int numHumansRemaining = 0; //final num of humans remaining
//1.0 Init and Input
setupGame(gameStateArray); //sets up the array
getFirstZombie(gameStateArray); //gets first zombie from the user
outputState(gameStateArray); //print orig state with 1st zombie
wait(.5); //the system waits to see original array
//2.0 Process
while(hasNextZombie(gameStateArray) == true)
{
count++; //update the iteration count
updateState(gameStateArray); //process this state to next state
outputState(gameStateArray); //print the next state
wait(.5); //pause to show animation
}
//3.0 Output
numHumansRemaining = getNumHumans(gameStateArray); //remaining humans
outputResult(gameStateArray, count, numHumansRemaining); //print result
//program run was successful print out and return 0
cout <<"\n\n\n\t Normal Termination \n\n\n" << endl;
return 0;
} //end main
/*********************** setupGame ************************/
void setupGame(char current[][SIZE])
{
// recieves the current game state array to setup.
// prints out name, course, and assignment name.
// initializes the boarder of the array to be decayed zombies
// and everthing else to be healthy humans. It also seeds
// a random number generator
cout << "\n\n================================================" << endl;
cout << "Charles Davis - CPSC 246.01 - Zombie Simulation" << endl;
cout << "================================================\n\n" << endl;
// chunk of code initializing the board with a frame of decayed zombies
// '.' and every other cell to healthy humans '+'
for (int c = 0; c < SIZE; c++) //for the top and bottom rows
{
current[0][c] = '.'; //row 0 with all decayed zombies '.'
current[SIZE-1][c] = '.' ; //row SIZE-1 with all decayed zombies '.'
}
for (int r = 0; r < SIZE; r++) //for the first and last columnn;
{
current[r][0] = '.'; //column 0 with decayed zombies '.'
current[r][SIZE-1] = '.'; //column SIZE-1 with decayed zombies '.'
}
for(int r = 1; r < SIZE-1; r++) // for everything else
{
for(int c = 1; c < SIZE-1; c++)
{
current[r][c] = '+'; //other cells to be healthy humans '+'
}
}
// end board init chunk
//Seed a random number generator with the current time
srand((unsigned) time(0));
}
/********************* getFirstZombie *********************/
void getFirstZombie(char current[][SIZE])
{
// will get a coordinate from the user for the first zombie's position
outputState(current);
int row, col;
do{
cout << "Enter first zombie coordinates. 1 They must be greater "<< endl;
cout << "than 0 and less than 29 with a space between them " << endl;
cout << "(e.g. 5 5): " << endl;
cin >> row;
cin >> col;
cout << "You entered { " << row << " , " << col << " }" << endl;
wait(2)
}while(!isValidCoordinate(row,col));
current[row][col] = 'Z';
}
/********************** updateState ***********************/
void updateState(char current[][SIZE])
{
// will update the state of the game by copying the current array
// killing off current zombies for the next state, and making
// new zombies from healthy humans based on probablility. Finally
// it copies the array back to the array referenced passed in.
char tempArray [SIZE][SIZE]; //temporary array to update
int tempNeighbors; // temporary int to store the num of neighbors
for(int r = 0; r < SIZE; r++) // copy every element to tempArray
{
for(int c = 0; c < SIZE; c++)
{
tempArray[r][c] = current[r][c]; //copy curr elmnt into temp array
}
}//end copy
for(int r = 1; r < SIZE-1; r++) //excluding the top and bottom
{
for(int c = 1; c < SIZE-1; c++) //and both sides
{
// if it is a zombie already kill it for the next state
if(current[r][c] == 'Z')
{
tempArray[r][c] = '.'; //turns current cell into decayed human
}
tempNeighbors = 0;
// if the current char is not a zombie or a decayed zombie then
// its a human. Get its zombie neighbors and convert it
// according the rules of the game
tempNeighbors = getNeighbors(current,r,c);
if(current[r][c] == '+')
{
//get num zombie neighbors of current cell
if(tempNeighbors == 0)
{
//do nothing
}
//neighbors are between 1 and 2
else if(tempNeighbors > 0 && tempNeighbors <= 2)
{
//there is a 50% chance of becoming a zombie
if(becomesZombie(50) == true)
{
tempArray[r][c] = 'Z'; //make into zombie
}
else
{
//do nothing
}
}
// neighbors are between 3 and 5
else if(tempNeighbors > 2 && tempNeighbors <= 5)
{
//there is a 70% chance of becoming a zombie
if(becomesZombie(70) == true)
{
tempArray[r][c] = 'Z';
}
else
{
//do nothing
}
}
//neighbors are more than 5
else if(tempNeighbors > 5)
{
//99% you are probably going to be a zombie
if(becomesZombie(99) == true)
{
tempArray[r][c] = 'Z';
}
}else{ /*do nothing*/ }
}else{ /* do nothing*/ }
}
} //end getNeighbors / kill zombies if else chunk
//lastly we copy our temp array back to main array
for(int r = 0; r < SIZE; r++)
{
for(int c = 0; c < SIZE; c++)
{
current[r][c] = tempArray[r][c];
}
} //end copy
} //end updateState
/********************** getNeighbors **********************/
int getNeighbors(const char current[][SIZE], int currentRow, int currentCol)
{
//will get the number of zombie neighbors of the current cell
int neighbors = 0; //temp variable for the number of neighbors
//offset array for checking each direction around the current cell
int offset[8][2] = {{-1,0}, // N
{-1,1}, // NE
{0,1}, // E
{1,1}, // SE
{1,0}, // S
{1,-1}, // SW
{0,-1}, // W
{-1,-1}}; // NW
for(int r = 0; r < 8; r++) // for each direction
{
// if the current neighbor is a zombie increment neighbors count
if(current[currentRow+offset[r][0]][currentCol+offset[r][1]] == 'Z')
{
neighbors++; //icrement count
}
else{ /*do nothing*/ }
}
return neighbors;
}
/********************** getNumHumans **********************/
int getNumHumans(const char current[][SIZE])
{
//gets the number of healthy humans in the whole array
int tempHumans = 0;
for(int r = 0; r < SIZE; r++) //search the array for humans
{
for(int c = 0; c < SIZE; c++)
{
//if the current cell is a human increment the count
if(current[r][c] == '+'){
tempHumans++;
}
}
}
return tempHumans;
}
/************************** wait **************************/
void wait(float seconds)
{
//pauses the system for a given time in seconds
clock_t endWait;
endWait = clock() + seconds * CLOCKS_PER_SEC;
while(clock() < endWait) { /* do nothing */ }
}
/******************** isValidCoordinate *******************/
bool isValidCoordinate(int row, int col)
{
// will check to see if the coordinates given are valid
// prints out an error message for invalid pair
//make sure values are greater than 0 and less than the size
//of the array minus one for the boarder.
if(row > 0 && row < SIZE-1 && col > 0 && col < SIZE-1)
{
return true;
}
else
{
cerr << "\nThe coordinate pair is invalid. "<< endl;
cerr << "Please try again\n\n " << endl;
return false;
}
}
/********************** becomesZombie *********************/
bool becomesZombie(int percentProbability)
{
// randomizes a number and returns a bool based on the
// given probability percentage threshold
return rand() % 100 < percentProbability;
}
/*********************** outputState **********************/
void outputState(const char current[][SIZE])
{
//will print out each element in the array sent to it
for(int r = 0; r < SIZE; r++) //for every element
{
for(int c = 0; c < SIZE; c++)
{
cout << current[r][c] << " "; //print current char and a space
}
cout << endl; //after each row print a new line
}
cout << "\n\n\n"; // print a couple new lines as a buffer
}
/********************** outputResult **********************/
void outputResult(const char current[][SIZE], int gameCnt, int humansRemain)
{
// will output the final game board, the num of humans remaining,
// the num of iterations it took to complete and the % of humans
// that became zombies
double originalNumHumans; //orig humans is area of board minus boarder
double percentZombie; // the difference in humans / orig humans
originalNumHumans = (SIZE * SIZE) - ((SIZE * 4) - 4);
percentZombie = ((originalNumHumans - humansRemain) / originalNumHumans) * 100;
//outputState(current); //output the final state of the board
cout << "There are " << humansRemain << " humans remaining out of ";
cout << originalNumHumans << " humans. " << percentZombie << "%";
cout <<" were made into zombies. It took " << gameCnt << " iterations.";
}
/********************** hasNextZombie *********************/
bool hasNextZombie(const char current[][SIZE])
{
// will search the entire array to see if any zombies still exist
for(int r = 1; r < SIZE-1; r++) //excluding the boarder
{
for(int c = 1; c < SIZE-1; c++)
{
if(current[r][c] == 'Z') //if current cell is a Zombie
{
return true; // yes there is a next zombie
}
}
}
return false; // there are no zombies left
}