-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDuck.cpp
337 lines (299 loc) · 11.3 KB
/
Duck.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
#include <iostream>
#include <string>
#include <ctime>
#include <stdlib.h>
#include "asciiArt.cpp"
using namespace std;
typedef enum { defend, quack, peck} attack_type;
int DUCK_HEALTH = 20;
int CHICKEN_HEALTH = 20;
int DUCK_ATTACK = 4;
int CHICKEN_ATTACK = 4;
double ARMOUR_REDUCTION = (1-0.5);
int ARMOUR_GAIN = 1;
class Unit{
public:
int health;
int armour;
int attackDamage;
int ammoCount;
void attacked(int attackAmount){
//cout << "Duck attacked() called"; // but what if this attack is being called on chicken
if(armour>0){ // ok
health -= (double)attackAmount * ARMOUR_REDUCTION; // not sure if (double) was necessory
armour--;
}
else{
health -= attackAmount;
}
}
int getAmmoCount() const {return ammoCount;}
bool hasArmour() const {return (armour>0);}
int getAttack() const {return attackDamage;}
int getHealth() const {return health;}
};
class Duck : public Unit {
public:
Duck(){
health = DUCK_HEALTH;
attackDamage = DUCK_ATTACK;
armour = 0;
ammoCount = 1;
}
// Description: all turn types, first parameter is the type of attack, either peck, defend or quack,
// second parameter is the chicken you are attacking
Duck * attack(attack_type type, Unit& other){
switch(type){
case peck:
if (ammoCount>0){
other.attacked(attackDamage);
ammoCount--;
printDuckPeck();
}
break;
case defend:
armour += ARMOUR_GAIN;
printDuckDefend();
break;
case quack:
ammoCount++;
printDuckQuack();
break;
/*
case lay:
*/
}
return nullptr;
}
};
class Chicken : public Unit{
public:
// int health; // should we remove this?
// int attack;
Chicken(){
health = CHICKEN_HEALTH;
attackDamage = CHICKEN_ATTACK;
armour = 0;
ammoCount = 1;
}
void attack(Duck*& ducks, int numberOfDucks){
if(numberOfDucks>0){
int attackType;
int leastHealthDuck = 0;
int leastHealth = CHICKEN_HEALTH;
int attackingPower = 0;
int targetDuck = 0;
for(int i = 0; i < numberOfDucks; i++){
int trueHealth = ducks[i].getHealth() + ducks[i].armour * ARMOUR_REDUCTION;
if(trueHealth <= leastHealth){
leastHealth = trueHealth;
leastHealthDuck = i;
}
attackingPower += ducks[i].getAttack();
}
if(ammoCount>0 && leastHealth <= attackDamage){
attackType = peck;
targetDuck = leastHealthDuck;
}
else if(armour>=3){
attackType = rand() % 2 + 1;
if(ammoCount < 1){
attackType = quack;
}
if(ammoCount >= 3)
attackType = peck;
}
else if(ammoCount >=3){
if((rand()%2)==1)
attackType = defend;
else
attackType = peck;
}
else{
attackType = rand() % 6;
if(ammoCount < 1){
attackType %= 2;
}
else{
attackType %= 3;
}
}
if (attackType == defend){
armour += ARMOUR_GAIN;
printChickenDefend();
}
else if (attackType == quack){
ammoCount++;
printChickenCoot();
}
else{
ducks[targetDuck].attacked(attackDamage);
ammoCount--;
printChickenPeck();
}
}
}
int getHealth(){return health;}
bool canPeck(){return ammoCount>0;}
};
void printInfo(Duck& duck, Chicken& chicken){ // We can also print Info with the ascii board
int maxDuckHealth = DUCK_HEALTH;
int maxChickHealth = CHICKEN_HEALTH;
int duckHealth = duck.health;
int chickHealth = chicken.health;
int duckArmour = duck.armour;
int chickArmour = chicken.armour;
int duckAmmo = duck.ammoCount;
int chickAmmo = chicken.ammoCount;
cout << "[";
for(int i = 0; i < duckHealth; i+=4){
cout << "|";
}
for(int i = 0; i < maxDuckHealth - duckHealth; i+=4){
cout << " ";
}
cout << "] Ø : " << duckArmour << " >--|> : " << duckAmmo;
cout << " [";
for(int i = 0; i < chickHealth; i+=4){
cout << "|";
}
for(int i = 0; i < maxChickHealth - chickHealth; i+=4){
cout << " ";
}
cout << "] Ø : " << chickArmour << " >--|> : " << chickAmmo << endl;
}
void printWelcomePage(){
cout << "Welcome to <PECK TO DEATH>!\n\n";
cout << "We need your help to crush the chickens. The chickens have been our enemies for the longest time.\n";
cout << "\"wHat cAmE fiRsT, tHe cHiCken oR tHe eGG?\" I'll tell you, IT WAS DUCKS! DUCKS CAME FIRST!\n";
cout << "How dare they copy our whole vibe.\n";
cout << "Us ducks didn't care at first. You see, we aren't the type to get jealous. We have it so much better.\n";
cout << "We can fly better, go on water, we're prettier, and best of all we have the superior eggs!\n";
cout << "The problems started when the chickens claimed they had tastier eggs than us?!?!\n";
cout << "This was definetly not true, and they knew it. So what did they do? Start attacking us and trying to KILL US!\n";
cout << "That's why we called you. We need to you to go undercover and duck up the chickens! Below we'll teach you how to do it.\n\n";
}
void printInstructions(){
string ready = "start";
cout << "In order to ruin the chickens you're going to battle them.\n";
cout << "Both you and the chicken have 20 HP to start. Taking a peck from the enemy will decrease it by 4 HP, but if you have armour it will only decrease by 2 HP.\n";
cout << "In order to peck, you must have quacked before. Quacking will give you the power to peck. In turn, the chicken will coot to peck.\n";
cout << "To take less damage from an enemies peck, defend youself!\n";
cout << "Have fun and please, show these chicken who the real birds are!\n";
cout << "Below are the controls,\n\n";
cout << "Use the [1], [2], [3] keys on your keyboard to control your duck.\n";
cout << "[1] - Use this key to Peck! A peck will bring down the health of the chicken as long as it's not defending.\n";
cout << "[2] - Use this key to Quack! A quack will give you the ability to peck. Think of it as reloading. If you want, you can quack multiple times and save your pecks.\n";
cout << "[3] - Use this key to Defend! A defend will give you 2 armour's which will reduce the chicken's peck by 50% each.\n\n";
cout << "The Ø symbolizes how much armour you have and the >--|> symbolizes your ammo.\n";
while (ready != "ready"){
if(ready != "start") cout << "\nCome on! You got this! Now ready up!"; // WHY? this is if the person doesnt input ready. its just a way to start the game
cout << "\nType and enter ready, to start : "; //it gives the user more control so the games doesnt just start
cin >> ready;
}
cout << "\n Okay, LET'S DUCK THEM UP!\n\n";
}
void duckTurn(Duck*& ducks, Chicken& chicken){
int playerMove = 0;
while (playerMove > 3 || playerMove < 1){
if(cin.fail()){
cin.clear();
cin.ignore(10000, '\n');
}
cout << "It's your turn! Enter [1], [2], or [3] to make a move";
if (ducks[0].getAmmoCount() < 1){
while(playerMove > 3 || playerMove <= 1){
if(cin.fail()){
cin.clear();
cin.ignore(10000, '\n');
}
cout << "\nYou have no ammo so you must quack[2] or defend[3] :";
cin >> playerMove;
}
}
else{
cout << " : ";
cin >> playerMove;
}
}
cin.ignore(10000, '\n');
cout << endl << endl;
if (playerMove == 1){
ducks[0].attack(peck, chicken);
cout << endl;
}
if (playerMove == 2){
ducks[0].attack(quack, chicken);
cout << endl;
}
if (playerMove == 3){
ducks[0].attack(defend, chicken);
cout << endl;
}
}
void chickenTurn(Duck*& ducks, Chicken& chicken, int& numDucks){
cout << "It is now the chicken's turn!\n";
chicken.attack(ducks, numDucks);
cout << endl;
}
void printGameOver(){
cout << "\nThank you for playing! Until next time!\n";
cout << "and always remember... DUCKS ARE BETTER THAN CHICKENS!";
}
/*void removeDeadDuck(Duck*& ducks, int& numDucks){
for (int i = 0; i < numDucks; i++){
if (ducks[i].getHealth() <= 0){
Duck* d = new Duck[numDucks - 1];
for(int j, k = 0; j < numDucks - 1 && k < numDucks; j++, k++){
if (j != i)d[j] = ducks[k]; // I am tying again
if (j == i) j--; //i see let me think
}
delete[] ducks;
ducks = d;
numDucks--;
cout << "\nOH NO! A duck has died :(\n";
return;
}
}
}*/
void checkForDeadAnimal(Duck*& ducks, Chicken& chicken, int& numDucks, int& i){
if(ducks[0].getHealth() <= 0){
numDucks--;
cout << "\nOH NO! A duck has died :(\n";
i = -1;
}
if(chicken.getHealth() <= 0){
cout << "\nThe Chicken has been killed!!\n";
i = -1;
}
}
void gameplay(Duck*& ducks, Chicken& chicken, int& numDucks){
string filler;
int i = 0;
while (numDucks > 0 && chicken.getHealth() > 0 && i >= 0){
printFeild(0, numDucks, 1, 0);
printInfo(ducks[0], chicken);
if(i % 2 == 0) duckTurn(ducks, chicken);
else chickenTurn(ducks, chicken, numDucks);
cout << "\nEnter any character to continue : ";
cin >> filler;
i++;
checkForDeadAnimal(ducks, chicken, numDucks, i);
}
}
int main(){
srand(time(0));
int numDucks = 1;
char playAgain = 'y';
printWelcomePage();
printInstructions();
while (playAgain == 'y'){
Duck* d = new Duck[numDucks];
Chicken chicken;
gameplay(d, chicken, numDucks);
cout << "\nWould you like to play again? (y/n): ";
cin >> playAgain;
delete[] d;
}
printGameOver();
}