-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTemplate_method_exercise_20.cpp
More file actions
64 lines (54 loc) · 1.7 KB
/
Copy pathTemplate_method_exercise_20.cpp
File metadata and controls
64 lines (54 loc) · 1.7 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
#include <iostream>
#include <vector>
#include <complex>
#include <tuple>
using namespace std;
struct Creature
{
int attack, health;
Creature(int attack, int health) : attack(attack), health(health) {}
};
struct CardGame
{
vector<Creature> creatures;
CardGame(const vector<Creature> &creatures) : creatures(creatures) {}
// return the index of the creature that won (is a live)
// example:
// - creature1 alive, creature2 dead, return creature1
// - creature1 dead, creature2 alive, return creature2
// - no clear winner: return -1
int combat(int creature1, int creature2)
{
// todo
hit(creatures[creature1], creatures[creature2]);
if (creatures[creature1].health > 0 && creatures[creature2].health <= 0)
return creature1;
else if (creatures[creature1].health <= 0 && creatures[creature2].health > 0)
return creature2;
else
return -1;
}
virtual void hit(Creature& attacker, Creature& other) = 0;
};
struct TemporaryCardDamageGame : CardGame
{
TemporaryCardDamageGame(const vector<Creature> &creatures) : CardGame(creatures) {}
void hit(Creature &attacker, Creature &other) override
{
// todo
if (attacker.health <= other.attack)
attacker.health = 0;
if (other.health <= attacker.attack)
other.health = 0;
}
};
struct PermanentCardDamageGame : CardGame
{
PermanentCardDamageGame(const vector<Creature> &creatures) : CardGame(creatures) {}
void hit(Creature &attacker, Creature &other) override
{
// todo
attacker.health -= other.attack;
other.health -= attacker.attack;
}
};