Repository navigation
Expand file tree
/
Copy pathDuel2.cpp
More file actions
86 lines (64 loc) · 2.02 KB
/
Copy pathDuel2.cpp
File metadata and controls
86 lines (64 loc) · 2.02 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
#include <iostream>
using namespace std;
class Human {
public:
int attack = 4;
int health = 5;
bool dead = false;
string name;
void TakeDamage(int damage) {
health = health - damage;
}
};
class Monster {
public:
int attack = 4;
int health = 7;
bool dead = false;
string name;
void TakeDamage(int damage) {
health = health - damage;
}
};
void Duel(Monster monster, Human human) {
while (!monster.dead && !human.dead) {
// runs as longs as both monster and human are alive
cout << monster.name << " is attacked!" << endl;
monster.TakeDamage(human.attack);
cout << monster.name << " now has: " << monster.health << " hp" << endl;
if (monster.health <= 0) {
monster.dead = true; // check if monster is dead
} else {
cout << human.name << " is attacked!" << endl;
human.TakeDamage(monster.attack);
cout << human.name << " now has: " << human.health << " hp" << endl;
if (human.health <= 0) {
human.dead = true; // check if human is dead
}
}
}
// we only reach here once one has died
if (monster.dead) {
cout << "The human wins!" << endl;
} else {
cout << "The monster wins!" << endl;
}
}
int main()
{
Monster monster;
Human human;
cout << "Enter the name of our monster" << endl;
cin >> monster.name;
cout << "Enter the attack of our monster" << endl;
cin >> monster.attack;
cout << "Enter the health of our monster" << endl;
cin >> monster.health;
cout << "Enter the name of our human" << endl;
cin >> human.name;
cout << "Enter the attack of our human" << endl;
cin >> human.attack;
cout << "Enter the health of our human" << endl;
cin >> human.health;
Duel(monster, human);
}