-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
235 lines (213 loc) · 5.07 KB
/
main.cpp
File metadata and controls
235 lines (213 loc) · 5.07 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
// Example program
#include <iostream>
#include <string>
using namespace std;
//A combatant contolled by the CPU.
class CPU
{
private:
int lightAttack(); //70% hit chance, 1-5 damage
int heavyAttack(); //40% hit chance, 3-8 damage
int recover(); //50% success rate, 2-7 recovery
public:
int HP = 20;
string name = "Floyd";
void updateHP(int); // Decrements HP by amount supplied by parameter.
int turn(); // Generates the CPUs move choice and calls corresponding function.
};
//A combatant controlled by the user.
class Human
{
private:
int lightAttack(); //70% hit chance, 1-5 damage
int heavyAttack(); //40% hit chance, 3-8 damage
int recover(); //50% success rate, 2-7 recovery
public:
int HP = 20;
string name;
void updateHP(int); // Decrements HP by amount supplied by parameter.
int turn();
};
int main()
{
srand(time(NULL));
int p1Damage;
int p2Damage;
Human p1;
CPU p2;
while(p1.HP > 0 && p2.HP > 0)
{
p1Damage = p1.turn();
p2.updateHP(p1Damage);
p2Damage = p2.turn();
p1.updateHP(p2Damage);
cout << endl << endl << "CPU HP = " << p2.HP << endl << "Human HP = " << p1.HP << endl << endl;
}
if(p1.HP < 0)
cout << "CPU wins!";
else
cout << "Human wins!";
return 0;
};
// Decrements the CPU's HP by adjust.
void CPU::updateHP(int adjust)
{
HP -= adjust;
};
// Generates the CPU's move choice and calls the corresponding function to execute the move.
// Calls lightAttack() 65% of the time, heavyAttack() 20% and recover() 15%.
int CPU::turn()
{
int choice = rand() % 100 + 1;
if(choice <= 65)
{
int damage = lightAttack();
return damage;
}
else if(choice <= 85)
{
int damage = heavyAttack();
return damage;
}
else
{
recover();
return 0;
}
};
// Light attack which hits 70% of the time for 1-5 damage.
int CPU::lightAttack()
{
int hit = rand() % 100 + 1;
if(hit <= 70)
{
int damage;
damage = rand() % 5 + 1;
cout << "CPU light attack damage = " << damage << endl;
return damage;
}
else
{
cout << "CPU light attack miss" << endl;
return 0;
}
};
//Heavy attack which hits 40% of the time for 3-8 damage.
int CPU::heavyAttack()
{
int hit = rand() % 100 + 1;
if(hit <= 40)
{
int damage;
damage = rand() % 5 + 3;
cout << "CPU heavy attack damage = " << damage << endl;
return damage;
}
else
{
cout << "CPU heavy attack miss" << endl;
return 0;
}
};
//Recovers between 2 and 7 HP 50% of the time.
int CPU::recover()
{
int hit = rand() % 100 + 1;
if(hit <= 50)
{
int heal;
heal = rand() % 5 + 2;
this->HP += heal;
cout << "CPU recovery = " << heal << endl;
return 0;
}
else
{
cout << "CPU recovery miss" << endl;
return 0;
}
};
// Decrements the Human's HP by adjust.
void Human::updateHP(int adjust)
{
HP -= adjust;
};
// Light attack which hits 70% of the time for 1-5 damage.
int Human::lightAttack()
{
int hit = rand() % 100 + 1;
if(hit <= 70)
{
int damage;
damage = rand() % 5 + 1;
cout << "Human light attack damage = " << damage << endl;
return damage;
}
else
{
cout << "Human light attack miss" << endl;
return 0;
}
};
//Heavy attack which hits 40% of the time for 3-8 damage.
int Human::heavyAttack()
{
int hit = rand() % 100 + 1;
if(hit <= 40)
{
int damage;
damage = rand() % 5 + 3;
cout << "Human heavy attack damage = " << damage << endl;
return damage;
}
else
{
cout << "Human heavy attack miss" << endl;
return 0;
}
};
//Recovers between 2 and 7 HP 50% of the time.
int Human::recover()
{
int hit = rand() % 100 + 1;
if(hit <= 50)
{
int heal;
heal = rand() % 5 + 2;
this->HP += heal;
cout << "Human recovery = " << heal << " , total HP= " << endl;
return 0;
}
else
{
cout << "Human recovery miss" << endl;
return 0;
}
};
//Asks the user for a move choice and validates the choice. Then calls the appropriate function.
int Human::turn()
{
cout << "What do you want to do? Enter 'L' for Light Attack, 'H' for Heavy Attack or 'R' for Recover: ";
char choice;
cin >> choice;
while(!(choice == 'l' || choice == 'L' || choice == 'h' || choice == 'H' || choice == 'r' || choice == 'R'))
{
cout <<"Not a valid selection. Enter 'L' for Light Attack, 'H' for Heavy Attack or 'R' for Recover: ";
cin >> choice;
};
if(choice == 'L' || choice == 'l')
{
int damage = lightAttack();
return damage;
}
else if(choice == 'H' || choice == 'h')
{
int damage = heavyAttack();
return damage;
}
else
{
recover();
return 0;
}
};