-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathZoo.cpp
More file actions
508 lines (380 loc) · 16.9 KB
/
Zoo.cpp
File metadata and controls
508 lines (380 loc) · 16.9 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
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
/****************************************************************************************************
* * Program name: CS162 Project2
* * Author: Taekyoung Kim
* * Date: 01/21/2019
* * Description: This is Zoo.cpp file for CS162 Project2
* * This project shows a zoo that has a few animals. We needs inheritance and class......
******************************************************************************************************/
#include "Zoo.h"
#include "Animal.h"
#include "inputVal.h"
#include "Tiger.h"
#include "Penguin.h"
#include "Turtle.h"
#include "DArray.h"
#include <iostream>
#include <random>
#include <memory>
#include <fstream>
#include <string>
#include <iomanip>
Zoo::Zoo()=default;
Zoo::~Zoo()=default;
double Zoo::getDailyCost(){
return this->dailyCost;
}
void Zoo::setDailyCost(double dayCost){
dailyCost =dayCost;
}
double Zoo::getBonus(){
return this->bonus;
}
void Zoo::setBonus(double bo){
bonus = bo;
}
double Zoo::getAccount(){
return this->account;
}
void Zoo::setAccount(double acc){
account = acc;
}
void Zoo::play(){
std::cout<<"Welcome to the Zoo! Now you run this Fantastic Zoo."<<std::endl;
std::cout<<"First, you need to buy animals. You can buy three animals: Tiger, Penguin, and Turtle."<<std::endl;
std::cout<<"You can only buy 1 or 2 per animals this first day with the basic given money, $100,000"<<std::endl;
std::cout<<"You need to pay $10,000 for tiger, $1,000 for Penguin, and $100 for Turtle."<<std::endl;
std::cout<<"You need to feed those animals everyday. The basic food cost is $10 per day."<<std::endl;
std::cout<<"The tiger needs 5 times more than the basic food cost, and the turtle only needs half of that cost."<<std::endl;
std::cout<<"Yes, it's too much cost, But don't worry. Those animals make money for you everyday"<<std::endl;
std::cout<<"Tiger makes 20% of its cost, Penguin makes 10% of its cost, and turtle makes 5% of its cost"<<std::endl;
std::cout<<std::endl;
setAccount(100000.00);
Animal animal;
Tiger tiger;
Penguin penguin;
Turtle turtle;
tiger.setCost(10000.00);
penguin.setCost(1000.00);
turtle.setCost(100.00);
tiger.setBaseFoodCost(baseCost*5);
penguin.setBaseFoodCost(baseCost);
turtle.setBaseFoodCost(baseCost*0.5);
tiger.setPayoff(tiger.getCost()*0.2);
penguin.setPayoff(penguin.getCost()*0.1);
turtle.setPayoff(turtle.getCost()*0.05);
DArray room;
room.setRoomOfTi(10);
room.setRoomOfPe(10);
room.setRoomOfTu(10);
//Tiger * arrayOfTi = new Tiger[room.getRoomOfTi()];
//Penguin * arrayOfPe = new Penguin[room.getRoomOfPe()];
//Turtle * arrayOfTur = new Turtle[room.getRoomOfTu()];
arrayOfTi = new Tiger[room.getRoomOfTi()];
arrayOfPe = new Penguin[room.getRoomOfPe()];
arrayOfTur = new Turtle[room.getRoomOfTu()];
for(int i=0; i<room.getRoomOfTi(); i++){
arrayOfTi[i] = Tiger(0, 0.00, 0, 0.00, 0.00);
}
for(int i=0; i<room.getRoomOfPe(); i++){
arrayOfPe[i] = Penguin(0, 0.00, 0, 0.00, 0.00);
}
for(int i=0; i<room.getRoomOfTu(); i++){
arrayOfTur[i] = Turtle(0, 0.00, 0, 0.00, 0.00);
}
double fBuyTi;
double fBuyP;
double fBuyTu;
int playAgain;
//std::cin.clear();
//std::cin.ignore(INT_MAX, '\n');
std::cout<<std::endl;
std::cout<<"Now, did you decide how many each animals buy"<<std::endl;
std::cout<<"How many tigers do you want? input just 1 or 2: ";
std::cin >> fBuyTi;
numOfTig = inputVal(fBuyTi);
numOfTig =choiceBTwo(numOfTig);
for(int i =0; i <numOfTig; i++) {
arrayOfTi[i] = Tiger(0, 10000, 0, baseCost * 5, 10000 * 0.2);
}
setAccount(getAccount()-(numOfTig*tiger.getCost()));
std::cout<<getAccount()<<std::endl;
std::cout<<numOfTig*tiger.getCost()<<std::endl;
std::cout<<getAccount()-(numOfTig*tiger.getCost())<<std::endl;
std::cin.clear();
std::cin.ignore(INT_MAX, '\n');
std::cout<<"How many Penguins are you willing to buy? choose 1 or 2: ";
std::cin >> fBuyP;
numOfPeng = inputVal(fBuyP);
numOfPeng =choiceBTwo(numOfPeng);
for(int i =0; i <numOfPeng; i++) {
arrayOfPe[i] = Penguin(0, 1000, 0, baseCost, 1000 * 0.1);
}
setAccount(getAccount()-(numOfPeng * penguin.getCost()));
std::cin.clear();
std::cin.ignore(INT_MAX, '\n');
std::cout<<"How many Turtles do you want to buy? Choose 1 or 2: ";
std::cin>>fBuyTu;
numOfTurt = inputVal(fBuyTu);
numOfTurt =choiceBTwo(numOfTurt);
for(int i =0; i <numOfTurt; i++) {
arrayOfTur[i] = Turtle(0, 100,0,baseCost * 0.5, 100 * 0.05);
//(0, turtle.getCost(), 0, turtle.getBaseFoodCost(), turtle.getPayoff());
}
setAccount(getAccount()-(numOfTurt* turtle.getCost()));
std::cout<<"After the purchase, your account balance is "<<getAccount()<<std::endl;
std::cout<<std::endl;
do{
day +=1;
std::cout<<"The day: "<<day<<std::endl;
for (int i=0; i<numOfTig ; i++){
arrayOfTi[i].setAge(arrayOfTi[i].getAge()+1);
}
for (int i=0; i<numOfPeng ; i++){
arrayOfPe[i].setAge(arrayOfPe[i].getAge()+1);
}
for (int i=0; i<numOfTurt; i++){
arrayOfTur[i].setAge(arrayOfTur[i].getAge()+1);
}
double option;
int feedOpt;
//std::cin.clear();
//std::cin.ignore(INT_MAX, '\n');
std::cout<<"You have 3 options for feeding animals; Cheap, Generic, and Premium"<<std::endl;
std::cout<<"If you choose the Cheap one, you can save the cost by half but sickness become twice."<<std::endl;
std::cout<<"If you choose the premium, you need to pay twice, but sickness becomes half."<<std::endl;
std::cout<<"Please input 1 for Cheap one, 2 for Generic, or 3 for Premium!";
std::cin>>option;
feedOpt = inputVal(option);
feedOpt = choiceBThree(feedOpt);
/************************************************************************************************************
* * Call the dayEvent function to pick today's event!
************************************************************************************************************/
dayEvent(feedOpt);
double todayAIncome =numOfTig*tiger.getPayoff()+numOfPeng*penguin.getPayoff()+numOfTurt*turtle.getPayoff();
double profit = todayAIncome + getBonus() - getDailyCost();
setAccount(getAccount() + profit);
std::cout<<"\nNow it's the time to check your balance."<<std::endl;
std::cout<<"Today's Tiger payoff "<<numOfTig * tiger.getPayoff()<<std::endl;
std::cout<<"The Penguin's payoff "<<numOfPeng * penguin.getPayoff()<<std::endl;
std::cout<<"The Turtle's payoff "<<numOfTurt * turtle.getPayoff()<<std::endl;
std::cout<<"So, Your today's income from admission fee is "<<todayAIncome<<std::endl;
std::cout<<"And, today's your bonus income is "<<getBonus()<<std::endl;
std::cout<<"Today's your profit(Admission income + bonus - daily cost)= "<<profit<<std::endl;
std::cout<<"Now your balance is "<<getAccount()<<std::endl;
/************************************************************************************************************
* * Call the function buyOneMore to ask users if they want to buy one more adult animal.
************************************************************************************************************/
buyOneMore();
std::cout<<"Now you have "<<numOfTig<<" Tigers, "<<numOfPeng<<" Penguins, "<<numOfTurt<<" Turtles\n"<<std::endl;
std::cout<<"The Tiger is: "<<std::endl;
for(int i=0; i<room.getRoomOfTi(); i++){
std::cout<<"Number " <<std::setw(2)<<i + 1 <<" Age: "<<std::setw(2)<<arrayOfTi[i].getAge()<<" Cost: "<<std::setw(5);
std::cout<<arrayOfTi[i].getCost()<<std::setw(2)<<" Babies: "<<arrayOfTi[i].getNumOfBabies()<<std::setw(8);
std::cout<<" Base_food_cost: "<<std::setw(5)<<arrayOfTi[i].getBaseFoodCost()<<" payoff: ";
std::cout<<std::setw(5)<<arrayOfTi[i].getPayoff()<<std::endl;
}
std::cout<<"The Penguin is: "<<std::endl;
for(int i=0; i<room.getRoomOfPe(); i++) {
std::cout<<"Number " <<std::setw(2)<<i + 1 <<" Age: "<<std::setw(2)<<arrayOfPe[i].getAge()<<" Cost: "<<std::setw(5);
std::cout<<arrayOfPe[i].getCost()<<std::setw(2)<<" Babies: "<<arrayOfPe[i].getNumOfBabies()<<std::setw(8);
std::cout<<" Base_food_cost: "<<std::setw(5)<<arrayOfPe[i].getBaseFoodCost()<<" p ayoff: ";
std::cout<<std::setw(5)<<arrayOfPe[i].getPayoff()<<std::endl;
}
std::cout << "The Turtle is: " << std::endl;
for (int i = 0; i < room.getRoomOfTu(); i++) {
std::cout<<"Number " <<std::setw(2)<<i + 1 <<" Age: "<<std::setw(2)<<arrayOfTur[i].getAge()<<" Cost: "<<std::setw(5);
std::cout<<arrayOfTur[i].getCost()<<std::setw(2)<<" Babies: "<<arrayOfTur[i].getNumOfBabies()<<std::setw(8);
std::cout<<" Base_food_cost: "<<std::setw(5)<<arrayOfTur[i].getBaseFoodCost()<<" payoff: ";
std::cout<<std::setw(5)<<arrayOfTur[i].getPayoff()<<std::endl;
}
std::cout<<"The room for tiger: "<<room.getRoomOfTi()<<std::endl;
std::cout<<"The room for Penguin "<<room.getRoomOfPe()<<std::endl;
std::cout<<"the room for turtle "<<room.getRoomOfTu()<<std::endl;
std::cin.clear();
std::cin.ignore(INT_MAX, '\n');
std::cout<<"\nIf you want to play again, input 1. Otherwise, click any keys! ";
std::cin>>playAgain;
std::cout<<std::endl;
}while(playAgain == 1 && getAccount() > 0);
if (getAccount() < 0){
std::cout<<"\nI'm sorry, but your account is empty. Play next time. Good buy!"<<std::endl;
}
std::cout <<"\nYou want to stop. Good bye! See you again!" << std::endl;
delete [] arrayOfTi;
delete [] arrayOfPe;
delete [] arrayOfTur;
}
int Zoo::choiceBTwo(int a){
double getInput;
if(a==1 || a==2) {
return a;
}
else{
do{
std::cin.clear();
std::cin.ignore(INT_MAX, '\n');
std::cout<<"Wrong input. You can buy only 1 or 2 Tigers, Please try again: ";
std::cin >> getInput;
a = inputVal(getInput);
}while(!(a == 1 || a == 2));
}
return a;
}
int Zoo::choiceBThree(int b){
double getInput;
if(b==1 || b==2 || b==3) {
return b;
}
else{
do{
std::cin.clear();
std::cin.ignore(INT_MAX, '\n');
std::cout<<"Wrong input. You can buy only 1 or 2 Tigers, Please try again: ";
std::cin >> getInput;
b = inputVal(getInput);
}while(!(b == 1 || b == 2 || b==3));
}
return b;
}
void Zoo::dayEvent(int feedOpt){
DArray room;
//double costPerDay;
//Feed the animals and deduct the cost from the account.
setDailyCost((numOfTig*baseCost*5)+(numOfPeng*baseCost)+(numOfTurt*baseCost*0.5));
int event;
// double bonus =0.00;
std::random_device ran;
std::mt19937 mt(ran());
if (feedOpt == 1) {
std::cout<<"\nYou chose cheap feeding option!"<<std::endl;
setDailyCost(getDailyCost()/2);
std::cout<<"Today's feeding cost is "<<getDailyCost()<<std::endl;
int cheapOpt;
int dice[]={1,2,3,1,1,1};
std::uniform_int_distribution <int> dist7(0, 5);
cheapOpt = dist7(mt);
std::cout<<"feedOpt 1 random number: "<<std::endl;
event = dice[cheapOpt];
}
else if (feedOpt == 3) {
std::cout<<"\nYou chose premium feeding option!"<<std::endl;
setDailyCost(getDailyCost()*2);
std::cout<<"Today's feeding cost is "<<getDailyCost()<<std::endl;
int premOpt;
int dice2[] ={1,2,3,2,3,2};
std::uniform_int_distribution <int> dist8(0, 5);
premOpt = dist8(mt);
std::cout<<"feedOpt 3 random number: "<<premOpt<<std::endl;
event = dice2[premOpt];
}
else{ // feedOpt == 2
std::cout<<"\nYou chose generic feeding option!"<<std::endl;
std::cout<<"Today's feeding cost is "<<getDailyCost()<<std::endl;
std::uniform_int_distribution <int> dist(1, 3);
event = dist(mt);
std::cout<<"feedOpt 2 random number: "<<event<<std::endl;
}
std::cout<<std::endl;
std::cout<<"Today's event is: "<<event<<std::endl;
std::ofstream text;
text.open("note.txt", std::ios::out);
if(text.fail()) {
std::cout<<"note.txt"<<" could not be found"<<std::endl;
exit(1);
}
//Sickness occurs and one animal that is chosen will die.
if (event == 1) {
text<<"'I'm so sorry, but today one of your animals gets sick and die.'\n"<<std::endl;
text.close();
std::cout<<std::ifstream("note.txt").rdbuf();
std::ofstream del("note.txt", std::ios::out | std::ios::trunc);
del.close();
room.pickRemove(numOfTig, numOfPeng, numOfTurt, arrayOfTi, arrayOfPe, arrayOfTur);
}
// A boom in zoo attendance occurs
else if(event == 2){
//another random need to set the bonus
text<<"'Good news for you, you win the bonus. Let's check how much it is!'\n"<<std::endl;
text.close();
double rot;
std::cout<<std::ifstream("note.txt").rdbuf();
std::ofstream del("note.txt", std::ios::out | std::ios::trunc);
del.close();
std::uniform_int_distribution <int> dist3(250, 500);
rot= dist3(mt);
setBonus(rot*numOfTig); //random amount * number of tiger
std::cout<<"Your today bonus is "<<getBonus()<<std::endl;
}
else{//This event gives babies to the animal, "event ==3"
text<<"'Congratulations! One of your animal is going to be a mom or dad!'\n"<<std::endl;
text.close();
std::cout<<std::ifstream("note.txt").rdbuf();
std::ofstream del("note.txt", std::ios::out | std::ios::trunc);
del.close();
room.whoHasBaby(numOfTig, numOfPeng, numOfTurt, arrayOfTi, arrayOfPe, arrayOfTur);
}
}
void Zoo::buyOneMore(){
DArray room;
Tiger tiger;
Penguin penguin;
Turtle turtle;
double buyNewOne;
int buyNew;
std::cin.clear();
std::cin.ignore(INT_MAX, '\n');
std::cout<<"\nPretty busy day, right? Now you can buy an adult animal."<<std::endl;
std::cout<<"Currently, you have "<<numOfTig<<" Tigers, "<<numOfPeng<<" Penguins, "<<numOfTurt<<" Turtles"<<std::endl;
std::cout<<"If you want to buy, input 1, otherwise (it you don't want to buy), click 2: ";
std::cin>>buyNewOne;
buyNew =inputVal(buyNewOne);
buyNew =choiceBTwo(buyNew);
if(buyNew==1) {
double buyChoice;
int buyOneMore;
std::cout
<< "\nSo, you want to buy. Good! which animal will you buy; input 1 for Tiger, 2 for Penguin, and 3 for Turtle: ";
std::cin >> buyChoice;
buyOneMore = inputVal(buyChoice);
buyOneMore =choiceBThree(buyOneMore);
if (buyOneMore == 1) {
numOfTig +=1;
std::cout << "So, you decided to buy a Tiger. Best Choice!" << std::endl;
if(numOfTig >= room.getRoomOfTi()-1) {
room.arrayTiger(arrayOfTi, room.getRoomOfTi());
}
//Adds a new tiger to the array.
arrayOfTi[numOfTig-1] = Tiger(3, 10000, 0, baseCost*5, 10000*0.2);
//(3, tiger.getCost(), 0, tiger.getBaseFoodCost(), tiger.getPayoff());
setAccount(getAccount()- tiger.getCost());
std::cout<<"After you buy the tiger, your balance is "<<getAccount()<<std::endl;
}
else if (buyOneMore == 2) {
numOfPeng+=1;
std::cout << "So, you decided to buy a Penguin. Fantastic Choice!" << std::endl;
if(numOfPeng >= room.getRoomOfPe()-1) {
room.arrayPen(arrayOfPe, room.getRoomOfPe());
//resizePen(arrayOfPe, room.getRoomOfPe());
}
arrayOfPe[numOfPeng-1] = Penguin(3, 1000, 0, baseCost, 1000*0.1);
//(3, penguin.getCost(), 0, penguin.getBaseFoodCost(), penguin.getPayoff());
setAccount(getAccount()-penguin.getCost());
std::cout<<"After you buy the penguin, your balance is "<<getAccount()<<std::endl;
}
else // (buyOneMore == 3)
{ numOfTurt+=1;
std::cout <<"So, you decided to buy a Turtle. Beautiful Choice!"<< std::endl;
if(numOfTurt >= room.getRoomOfTu()-1) {
room.arrayTur(arrayOfTur, room.getRoomOfTu());
}
arrayOfTur[numOfTurt-1] = Turtle(3, 100, 0, baseCost*0.5, 100*0.05);
//(3, turtle.getCost(), 0, turtle.getBaseFoodCost(), turtle.getPayoff());
setAccount(getAccount() - turtle.getCost());
std::cout<<"After you buy the turtle, your balance is "<<getAccount()<<std::endl;
}
}
else //(buyNew ==2)
{
std::cout<<"\nso you don't want to buy more. That's fine."<<std::endl;
}
}