-
Notifications
You must be signed in to change notification settings - Fork 0
/
initialize.cpp
799 lines (714 loc) · 26.2 KB
/
initialize.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
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
//SIMCITY - CSCE2110.201 - Group 10
//Initialization, reading in config/region layout written by Natalio Castaneda
//Residential written by Natalio Castaneda
//Industrial written by Mamerto Cruz
//Commercial written by Natalio Castaneda
//Pollution written by Natalio Castaneda
//Region analysis written by Mamerto Cruz
#include "initialize.h"
string initialize::readConfig(string configName){ //FUNCTION: READ CONFIG FILE
ifstream configFile;
string layoutFileName;
configFile.open(configName); //open config
while(!configFile.is_open()){ //check if open
cout << configName << " is an invalid file name. Please enter a new name: " << endl; //prompt user for new name and retry opening
cin >> configName;
configFile.open(configName);
}
configFile.ignore(15,':'); //read contents of file
configFile >> layoutFileName; //store layout filename for reading layout file
configFile.ignore(15,':');
configFile >> timeLimit; //store timeLimit and refreshRate in initialization class for simulation
configFile.ignore(15,':');
configFile >> refreshRate;
configFile.close();
return layoutFileName; //return layout filename to main to call readLayout function
}
void initialize::readLayout(string layoutName) { //FUNCTION: READ LAYOUT FILE
ifstream layoutFile;
layoutFile.open(layoutName); //open layout
if(!layoutFile.is_open()){ //check if open
cout << "Could not find layout file " << layoutName << ". Exiting program..."<< endl;
exit (EXIT_FAILURE); //exit program if layout program not found
}
char data; //read in initial region layout data and store in 2D vector of class celldata
int x = 0;
int y = 0;
while(!layoutFile.eof()){ //stop reading at file end
layoutFile.get(data); //read in character
if(data != ',' && data != '\n'){ //only store cell data from CSV
col.push_back(new celldata(data, 0, 0, x, y)); //allocate memory to vector elements
x = x + 1;
}
if(data == '\n'){ //add row of cells to vector when reaching end of line
row.push_back(col);
y = y + 1;
x = 0;
col.clear(); //clear inidividual cell (column) vector (is not needed for storing data long term)
}
}
layoutFile.close(); //close layout file
residentialPopulation = 0;
industrialPopulation = 0;
commercialPopulation = 0;
}
void initialize::printRegion(){ //FUNCTION: PRINT REGION
for(int k = 0; k < row.size(); k++){cout << "--";} //print proper size top border
cout << endl;
for(int i = 0; i < row.size(); i++){ //traverse through first vector
for(int j = 0; j < row[i].size(); j++){ //traverse through second vector
if(j == 0){cout << "|";} //print left border
if(row[i][j]->getPopulation() == 0){
cout << row[i][j]->getType() << " "; //print cell type if population is zero
}else{
cout << row[i][j]->getPopulation() << " "; //print cell population if population is not zero
}
if(j == row[i].size() - 1){cout << "|";} //print right border
}
if(i != row.size() - 1){cout << endl;} //go to next row
}
for(int k = 0; k < row.size(); k++){cout << "--";} //print proper size bottom border
cout << endl;
}
void initialize::cleanData() { //FUNCTION: CLEAN MEMORY
for(int i = 0; i < row.size(); i++){ //traverse vectors
for(int j = 0; j < row[i].size(); j++){
delete row[i][j]; //delete allocated memory
}
}
}
int initialize::getTimeLimit(){
return timeLimit;
}
int initialize::getRefreshRate(){
return refreshRate;
}
void initialize::incWorkers(){
workers = workers + 1;
}
void initialize::decWorkers(){
workers = workers - 1;
}
void initialize::setWorkers(int numWorkers){
workers = numWorkers;
}
int initialize::getWorkers(){
return workers;
}
void initialize::incGoods(){
goods = goods + 1;
}
void initialize::decGoods(){
goods = goods - 1;
}
void initialize::setGoods(int numGoods){
goods = numGoods;
}
int initialize::getGoods(){
return goods;
}
int initialize::regionSizeY()
{
return row.size()-2;
}
int initialize::regionSizeX()
{
return row[0].size()-1;
}
void initialize::printAvailableWG(){
cout << "Available Workers " << workers << " Available Goods " << goods << endl;
}
bool initialize::checkAdjacent(int i, int j){ //checks if adjacent cell exists
bool exists;
if(i >= 0 && i < row[i].size() && j >= 0 && j < row.size()){
exists = true;
}else{
exists = false;
}
return exists;
}
//counts the total number of cells with a certain population that surround a specified cell
int initialize::countAdjacentPop(int i, int j, int pop){
int count = 0;
if(checkAdjacent(i-1, j-1)){//makes sure adjacent cell exists
if(row[i-1][j-1]->getPopulation() >= pop){ //if it exists and has atleast pop population
count = count + 1; //increases counter by one
}
}
if(checkAdjacent(i-1,j)){
if(row[i-1][j]->getPopulation() >= pop){
count = count + 1;
}
}
if(checkAdjacent(i-1,j+1)){
if(row[i-1][j+1]->getPopulation() >= pop){
count = count + 1;
}
}
if(checkAdjacent(i,j-1)){
if(row[i][j-1]->getPopulation() >= pop){
count = count + 1;
}
}
if(checkAdjacent(i,j+1)){
if(row[i][j+1]->getPopulation() >= pop){
count = count + 1;
}
}
if(checkAdjacent(i+1,j-1)){
if(row[i+1][j-1]->getPopulation() >= pop){
count = count + 1;
}
}
if(checkAdjacent(i+1,j)){
if(row[i+1][j]->getPopulation() >= pop){
count = count + 1;
}
}
if(checkAdjacent(i+1,j+1)){
if(row[i+1][j+1]->getPopulation() >= pop){
count = count + 1;
}
}
return count; //returns counter value
}
int initialize::totalAdjacentPop(int i, int j){//calculates the total population for surrounding cells
int count = 0;
if(checkAdjacent(i-1, j-1)){//checks if adjacent cell exists
count = count + row[i-1][j-1]->getPopulation(); //adds population of adjacent cell to counter
}
if(checkAdjacent(i-1,j)){
count = count + row[i-1][j]->getPopulation();
}
if(checkAdjacent(i-1,j+1)){
count = count + row[i-1][j+1]->getPopulation();
}
if(checkAdjacent(i,j-1)){
count = count + row[i][j-1]->getPopulation();
}
if(checkAdjacent(i,j+1)){
count = count + row[i][j+1]->getPopulation();
}
if(checkAdjacent(i+1,j-1)){
count = count + row[i+1][j-1]->getPopulation();
}
if(checkAdjacent(i+1,j)){
count = count + row[i+1][j]->getPopulation();
}
if(checkAdjacent(i+1,j+1)){
count = count + row[i+1][j+1]->getPopulation();
}
return count; //returns counter
}
void initialize::updateResidential(){
//WRITTEN BY NATALIO CASTANEDA
temp = row;//set temp vector to row vector
updateCheck = row;
updatedR = true;
int tempPop = residentialPopulation; //sets temp total population to current total population of zone
for(int i = 0; i < row.size(); i++){
for(int j = 0; j < row[i].size(); j++){
if(row[i][j]->getType() == 'R'){ //searches 2d vector for residential cells
temp[i][j]->setTempPopulation(row[i][j]->getPopulation()); //sets temp population to current cell population
if(residentialPopulation == 0){ //if total residential population is 0
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i-1, j-1) && temp[i][j]->getTempPopulation() == 0){ //if cell population is 0, adjacent exists, and adjacent is powerline
if(row[i-1][j-1]->getType() == 'T' || row[i-1][j-1]->getType() == '#'){
temp[i][j]->incTempPopulation(); //increases cell population by 1
workers = workers + 1; //increase available workers by 1
tempPop = tempPop + 1; //increase total residential population by 1
}
} //repeat for every adjacent cell
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i-1,j) && temp[i][j]->getTempPopulation() == 0){
if(row[i-1][j]->getType() == 'T' || row[i-1][j]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers + 1;
tempPop = tempPop + 1;
}
}
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i-1,j+1) && temp[i][j]->getTempPopulation() == 0){
if(row[i-1][j+1]->getType() == 'T' || row[i-1][j+1]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers + 1;
tempPop = tempPop + 1;
}
}
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i,j-1) && temp[i][j]->getTempPopulation() == 0){
if(row[i][j-1]->getType() == 'T' || row[i][j-1]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers + 1;
tempPop = tempPop + 1;
}
}
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i,j+1) && temp[i][j]->getTempPopulation() == 0){
if(row[i][j+1]->getType() == 'T' || row[i][j+1]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers + 1;
tempPop = tempPop + 1;
}
}
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i+1,j-1) && temp[i][j]->getTempPopulation() == 0){
if(row[i+1][j-1]->getType() == 'T' || row[i+1][j-1]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers + 1;
tempPop = tempPop + 1;
}
}
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i+1,j) && temp[i][j]->getTempPopulation() == 0){
if(row[i+1][j]->getType() == 'T' || row[i+1][j]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers + 1;
tempPop = tempPop + 1;
}
}
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i+1,j+1) && temp[i][j]->getTempPopulation() == 0){
if(row[i+1][j+1]->getType() == 'T' || row[i+1][j+1]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers + 1;
tempPop = tempPop + 1;
}
}
}
if(residentialPopulation > 0){ //if total population is greater than one
if(temp[i][j]->getPopulation() == 0 && countAdjacentPop(i, j, 1) >= 1){
temp[i][j]->incTempPopulation(); //each if statement follows rules for residential cell updates
workers = workers + 1;
tempPop = tempPop + 1;
}
if(temp[i][j]->getPopulation() == 1 && countAdjacentPop(i, j, 1) >= 2){
temp[i][j]->incTempPopulation();
workers = workers + 1;
tempPop = tempPop + 1;
}
if(temp[i][j]->getPopulation() == 2 && countAdjacentPop(i, j, 2) >= 4){
temp[i][j]->incTempPopulation();
workers = workers + 1;
tempPop = tempPop + 1;
}
if(temp[i][j]->getPopulation() == 3 && countAdjacentPop(i, j, 3) >= 6){
temp[i][j]->incTempPopulation();
workers = workers + 1;
tempPop = tempPop + 1;
}
if(temp[i][j]->getPopulation() == 4 && countAdjacentPop(i, j, 4) >= 8){
temp[i][j]->incTempPopulation();
workers = workers + 1;
tempPop = tempPop + 1;
}
}
}
}
}
row = temp; //set new temp values to row for output/next timestep
for(int i = 0; i < row.size(); i++){
for(int j = 0; j < row[i].size(); j++){
if(row[i][j]->getType() == 'R'){
row[i][j]->setPopulation(temp[i][j]->getTempPopulation());
}
}
}
residentialPopulation = tempPop;
if(updateCheck == row){
updatedR = false;
}
}
void initialize::updateIndustrial(){
//WRITTEN BY MAMERTO CRUZ
temp = row;
updateCheck = row;
int tempPop = industrialPopulation;
int k = 0;
int tempI = -1;
int tempJ = -1;
vector<celldata*> industrialPriority;
updatedI = true;
for(int i = 0; i < row.size(); i++){ //loop that iterates through the region
for(int j = 0; j < row[i].size(); j++){
if(row[i][j]->getType() == 'I'){
industrialPriority.push_back(new celldata('I', 0, 0, i, j));
industrialPriority[k]->setTotalAdjPop(totalAdjacentPop(i, j));
k++;
}
}
}
k = 0;
int keep = 0;
while(k < industrialPriority.size()){
int greatest = 0;
for(int m = 0; m < industrialPriority.size(); m++){
int currVal = industrialPriority[m]->getTotalAdjPop();
if(currVal > greatest){
greatest = currVal;
keep = m;
}else if(currVal <= greatest){
if(industrialPriority[m]->getY() < industrialPriority[k]->getY()){
keep = m;
}else if(industrialPriority[m]->getY() >= industrialPriority[k]->getY()){
if(industrialPriority[m]->getX() < industrialPriority[k]->getX()){
keep = m;
}
}
}
}
tempI = industrialPriority[k]->getX();
tempJ = industrialPriority[k]->getY();
industrialPriority[k]->setX(industrialPriority[keep]->getX());
industrialPriority[k]->setY(industrialPriority[keep]->getY());
industrialPriority[keep]->setX(tempI);
industrialPriority[keep]->setY(tempJ);
k++;
}
for(int k = 0; k < industrialPriority.size(); k++){
int i = industrialPriority[k]->getX();
int j = industrialPriority[k]->getY();
temp[i][j]->setTempPopulation(row[i][j]->getPopulation());
if(industrialPopulation == 0 && workers >= 2){ //checks for 0 population in cell and at least 2 workers
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i-1, j-1) && temp[i][j]->getTempPopulation() == 0 && workers >= 2){ //checking the adjacent cells
if(row[i-1][j-1]->getType() == 'T' || row[i-1][j-1]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers - 2;
tempPop = tempPop + 1;
goods = goods + 1;
}
}
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i-1,j) && temp[i][j]->getTempPopulation() == 0 && workers >= 2){
if(row[i-1][j]->getType() == 'T' || row[i-1][j]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers - 2;
tempPop = tempPop + 1;
goods = goods + 1;
}
}
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i-1,j+1) && temp[i][j]->getTempPopulation() == 0 && workers >= 2){
if(row[i-1][j+1]->getType() == 'T' || row[i-1][j+1]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers - 2;
tempPop = tempPop + 1;
goods = goods + 1;
}
}
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i,j-1) && temp[i][j]->getTempPopulation() == 0 && workers >= 2){
if(row[i][j-1]->getType() == 'T' || row[i][j-1]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers - 2;
tempPop = tempPop + 1;
goods = goods + 1;
}
}
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i,j+1) && temp[i][j]->getTempPopulation() == 0 && workers >= 2){
if(row[i][j+1]->getType() == 'T' || row[i][j+1]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers - 2;
tempPop = tempPop + 1;
goods = goods + 1;
}
}
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i+1,j-1) && temp[i][j]->getTempPopulation() == 0 && workers >= 2){
if(row[i+1][j-1]->getType() == 'T' || row[i+1][j-1]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers - 2;
tempPop = tempPop + 1;
goods = goods + 1;
}
}
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i+1,j) && temp[i][j]->getTempPopulation() == 0 && workers >= 2){
if(row[i+1][j]->getType() == 'T' || row[i+1][j]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers - 2;
tempPop = tempPop + 1;
goods = goods + 1;
}
}
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i+1,j+1) && temp[i][j]->getTempPopulation() == 0 && workers >= 2){
if(row[i+1][j+1]->getType() == 'T' || row[i+1][j+1]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers - 2;
tempPop = tempPop + 1;
goods = goods + 1;
}
}
}
if(industrialPopulation > 0){ //checks for population greater than 0
if(temp[i][j]->getPopulation() == 2 && countAdjacentPop(i, j, 2) >= 4 && workers >= 2){ //checks for population size of 2 and 4 adjacent cells with population 2
temp[i][j]->incTempPopulation();
workers = workers - 2;
tempPop = tempPop + 1;
goods = goods + 1;
}
if(temp[i][j]->getPopulation() == 1 && countAdjacentPop(i, j, 1) >= 2 && workers >= 2){ //checks for population size of 1 and 2 adjacent cells with population 1
temp[i][j]->incTempPopulation();
workers = workers - 2;
tempPop = tempPop + 1;
goods = goods + 1;
}
if(temp[i][j]->getPopulation() == 0 && countAdjacentPop(i, j, 1) >= 1 && workers >= 2){ //checks for population size of 0 and adjacent cell with population 1
temp[i][j]->incTempPopulation();
workers = workers - 2;
tempPop = tempPop + 1;
goods = goods + 1;
}
}
}
row = temp;
for(int i = 0; i < row.size(); i++){
for(int j = 0; j < row[i].size(); j++){
if(row[i][j]->getType() == 'I'){
row[i][j]->setPopulation(temp[i][j]->getTempPopulation());
row[i][j]->setPollution(temp[i][j]->getTempPopulation());
}
}
}
industrialPopulation = tempPop;
if(updateCheck == row){
updatedI = false;
}
}
void initialize::updateCommercial(){
//WRITTEN BY NATALIO CASTANEDA
temp = row;
updateCheck = row;
int tempPop = commercialPopulation;
int k = 0;
int tempI = -1;
int tempJ = -1;
vector<celldata*> commercialPriority;
updatedC = true;
for(int i = 0; i < row.size(); i++){
for(int j = 0; j < row[i].size(); j++){
if(row[i][j]->getType() == 'C'){
commercialPriority.push_back(new celldata('C', 0, 0, i, j));
commercialPriority[k]->setTotalAdjPop(totalAdjacentPop(i, j));
k++;
}
}
}
k = 0;
int keep = 0;
int currVal = 0;
int greatest = 0;
while(k < commercialPriority.size()){
currVal = commercialPriority[k]->getTotalAdjPop();
for(int m = 0; m < commercialPriority.size(); m++){
currVal = commercialPriority[m]->getTotalAdjPop();
if(currVal > greatest){
greatest = currVal;
keep = m;
}else if(currVal <= greatest){
if(commercialPriority[m]->getY() < commercialPriority[k]->getY()){
keep = m;
}else if(commercialPriority[m]->getY() >= commercialPriority[k]->getY()){
if(commercialPriority[m]->getX() < commercialPriority[k]->getX()){
keep = m;
}
}
}
}
tempI = commercialPriority[k]->getX();
tempJ = commercialPriority[k]->getY();
commercialPriority[k]->setX(commercialPriority[keep]->getX());
commercialPriority[k]->setY(commercialPriority[keep]->getY());
commercialPriority[keep]->setX(tempI);
commercialPriority[keep]->setY(tempJ);
k++;
}
for(int k = 0; k < commercialPriority.size(); k++){
int i = commercialPriority[k]->getX();
int j = commercialPriority[k]->getY();
temp[i][j]->setTempPopulation(row[i][j]->getPopulation());
if(temp[i][j]->getTempPopulation() == 0 && workers >= 1 && goods >= 1){
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i-1, j-1) && temp[i][j]->getTempPopulation() == 0 && workers >= 1 && goods >= 1){
if(row[i-1][j-1]->getType() == 'T' || row[i-1][j-1]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers - 1;
tempPop = tempPop + 1;
goods = goods - 1;
}
}
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i-1,j) && temp[i][j]->getTempPopulation() == 0 && workers >= 1 && goods >= 1){
if(row[i-1][j]->getType() == 'T' || row[i-1][j]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers - 1;
tempPop = tempPop + 1;
goods = goods - 1;
}
}
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i-1,j+1) && temp[i][j]->getTempPopulation() == 0 && workers >= 1 && goods >= 1){
if(row[i-1][j+1]->getType() == 'T' || row[i-1][j+1]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers - 1;
tempPop = tempPop + 1;
goods = goods - 1;
}
}
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i,j-1) && temp[i][j]->getTempPopulation() == 0 && workers >= 1 && goods >= 1){
if(row[i][j-1]->getType() == 'T' || row[i][j-1]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers - 1;
tempPop = tempPop + 1;
goods = goods - 1;
}
}
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i,j+1) && temp[i][j]->getTempPopulation() == 0 && workers >= 1 && goods >= 1){
if(row[i][j+1]->getType() == 'T' || row[i][j+1]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers - 1;
tempPop = tempPop + 1;
goods = goods - 1;
}
}
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i+1,j-1) && temp[i][j]->getTempPopulation() == 0 && workers >= 1 && goods >= 1){
if(row[i+1][j-1]->getType() == 'T' || row[i+1][j-1]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers - 1;
tempPop = tempPop + 1;
goods = goods - 1;
}
}
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i+1,j) && temp[i][j]->getTempPopulation() == 0 && workers >= 1 && goods >= 1){
if(row[i+1][j]->getType() == 'T' || row[i+1][j]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers - 1;
tempPop = tempPop + 1;
goods = goods - 1;
}
}
if(row[i][j]->getPopulation() == 0 && checkAdjacent(i+1,j+1) && temp[i][j]->getTempPopulation() == 0 && workers >= 1 && goods >= 1){
if(row[i+1][j+1]->getType() == 'T' || row[i+1][j+1]->getType() == '#'){
temp[i][j]->incTempPopulation();
workers = workers - 1;
tempPop = tempPop + 1;
goods = goods - 1;
}
}
}
if(commercialPopulation > 0){
if(temp[i][j]->getPopulation() == 1 && countAdjacentPop(i, j, 1) >= 2 && workers >= 1 && goods >= 1){
temp[i][j]->incTempPopulation();
workers = workers - 1;
tempPop = tempPop + 1;
goods = goods - 1;
}
if(temp[i][j]->getPopulation() == 0 && countAdjacentPop(i, j, 1) >= 1 && workers >= 1 && goods >= 1){
temp[i][j]->incTempPopulation();
workers = workers - 1;
tempPop = tempPop + 1;
goods = goods - 1;
}
}
}
row = temp;
for(int i = 0; i < row.size(); i++){
for(int j = 0; j < row[i].size(); j++){
if(row[i][j]->getType() == 'C'){
row[i][j]->setPopulation(temp[i][j]->getTempPopulation());
}
}
}
commercialPopulation = tempPop;
if(updateCheck == row){
updatedC = false;
}
}
void initialize::printDesiredArea(int x1, int y1, int x2, int y2)
{
int rPop = 0, iPop = 0, cPop = 0, areaPollution = 0;
for(int i = x1; i < (x2+1); i++) //for loop to iterate through desired region
{
for(int j = y1; j < (y2+1); j++)
{
if(row[i][j]->getType() == 'R') //check for residential
{
rPop += row[i][j]->getPopulation();
}
if(row[i][j]->getType() == 'I') //check for industrial
{
iPop += row[i][j]->getPopulation();
}
if(row[i][j]->getType() == 'C') //check for commercial
{
cPop += row[i][j]->getPopulation();
}
areaPollution = areaPollution + row[i][j]->getPollution();
}
}
//print out total results in the desired area
cout << "The total populations for the requested area are:" << endl << "Residential: " << rPop << endl << "Industrial: " << iPop << endl << "Commercial: " << cPop << endl;
cout << "The total amount of pollution for the requested area is " << areaPollution << " units" << endl;
}
void initialize::printTotalPopulations(){
cout << "The total populations for the region are:" << endl;
cout << "Residential: " << residentialPopulation << endl;
cout << "Industrial: " << industrialPopulation << endl;
cout << "Commercial: " << commercialPopulation << endl;
}
void initialize::setUpdated(bool resetUpdated){
updatedR = resetUpdated;
updatedI = resetUpdated;
updatedC = resetUpdated;
}
bool initialize::getUpdatedR(){
return updatedR;
}
bool initialize::getUpdatedI(){
return updatedI;
}
bool initialize::getUpdatedC(){
return updatedC;
}
void initialize::updatePollution(){
for(int i = 0; i < row.size(); i++){
for(int j = 0; j < row[i].size(); j++){
if(row[i][j]->getType() == 'I'){ //updates all cells adjacent to industrial cells
if(row[i][j]->getPollution() > 1){
if(checkAdjacent(i-1, j-1) && row[i-1][j-1]->getPollution() < row[i][j]->getPollution()){
row[i-1][j-1]->setPollution(row[i][j]->getPollution() - 1);
}
if(checkAdjacent(i-1, j) && row[i-1][j]->getPollution() < row[i][j]->getPollution()){
row[i-1][j]->setPollution(row[i][j]->getPollution() - 1);
}
if(checkAdjacent(i-1, j+1) && row[i-1][j+1]->getPollution() < row[i][j]->getPollution()){
row[i-1][j+1]->setPollution(row[i][j]->getPollution() - 1);
}
if(checkAdjacent(i, j-1) && row[i][j-1]->getPollution() < row[i][j]->getPollution()){
row[i][j-1]->setPollution(row[i][j]->getPollution() - 1);
}
if(checkAdjacent(i, j+1) && row[i][j+1]->getPollution() < row[i][j]->getPollution()){
row[i][j+1]->setPollution(row[i][j]->getPollution() - 1);
}
if(checkAdjacent(i+1, j-1) && row[i+1][j-1]->getPollution() < row[i][j]->getPollution()){
row[i+1][j-1]->setPollution(row[i][j]->getPollution() - 1);
}
if(checkAdjacent(i+1, j) && row[i+1][j]->getPollution() < row[i][j]->getPollution()){
row[i+1][j]->setPollution(row[i][j]->getPollution() - 1);
}
if(checkAdjacent(i+1, j+1) && row[i+1][j+1]->getPollution() < row[i][j]->getPollution()){
row[i+1][j+1]->setPollution(row[i][j]->getPollution() - 1);
}
}
}
}
}
}
void initialize::printPollution(){ //prints layout the of pollution values
cout << "Pollution" << endl;
for(int k = 0; k < row.size(); k++){cout << "--";}
cout << endl;
for(int i = 0; i < row.size(); i++){
for(int j = 0; j < row[i].size(); j++){
if(j == 0){cout << "|";}
cout << row[i][j]->getPollution() << " ";
if(j == row[i].size() - 1){cout << "|";}
}
if(i != row.size() - 1){cout << endl;}
}
for(int k = 0; k < row.size(); k++){cout << "--";}
cout << endl;
}
int initialize::totalPollution(){ //calculates pollution for all cells
int totalPollution = 0;
for(int i = 0; i < row.size(); i++){
for(int j = 0; j < row[i].size(); j++){
totalPollution = totalPollution + row[i][j]->getPollution();
}
}
return totalPollution;
}