forked from ironhack-labs/mini-project-vikings-en
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvikingsClasses.py
178 lines (130 loc) · 4.62 KB
/
vikingsClasses.py
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
import random
# Soldier
class Soldier:
def __init__(self, health, strength):
"""
Initializes a soldier instance with health and strength.
Parameters:
health (int): The health of the soldier.
strength (int): The strength of the soldier.
"""
self.health = health
self.strength = strength
def attack(self):
"""
Returns the strength of the soldier, with 0 arguments.
"""
return self.strength
def receiveDamage(self, damage):
"""
Receives the damage as an argument, and reduces the health of the soldier..
Parameter:
damage (int): damage to be subtracted from health.
"""
self.health -= damage
# Viking
class Viking(Soldier):
def __init__(self, name, health, strength):
super().__init__(health, strength)
self.name = name
def receiveDamage(self, damage):
"""
Subtracts damage received from the health of the individual.
Parameters:
damage(int): damage to be subtracted from health.
Returns:
str: message about the Viking based on their remaining health.
"""
self.health -= damage
if self.health > 0:
return f"{self.name} has received {damage} points of damage"
else:
return f"{self.name} has died in act of combat"
def battleCry(self):
"""
Returns the vikings battle cry.
Returns:
str: the Vikings battle cry.
"""
return "Odin Owns You All!"
# Saxon
class Saxon(Soldier):
def __init__(self, health, strength):
"""
Initializes a Saxon instance, inheriting from Soldier, with two arguments, health and strength.
"""
super().__init__(health, strength)
def receiveDamage(self, damage):
"""
Reduces health based on the damage received and returns a message regarding the health of the Saxon.
Parameters:
damage (int): damage to be substracted from the health of the Saxon.
Returns:
str: message regarding the health of the Saxon.
"""
self.health -= damage
if self.health > 0:
return f"A Saxon has received {damage} points of damage"
else:
return "A Saxon has died in combat"
# War
class War():
def __init__(self):
"""
"""
self.vikingArmy = []
self.saxonArmy = []
def addViking(self, viking):
"""
Adds a viking to the viking army.
Parameters:
viking (Viking): a Viking instance.
"""
self.vikingArmy.append(viking)
def addSaxon(self, saxon):
"""
Adds a Saxon to the Saxon army.
Parameters:
saxon (Saxon): Saxon instance.
"""
self.saxonArmy.append(saxon)
def vikingAttack(self):
"""
Simulates a Viking-Saxon attack.
Returns:
str: message with the result of the attack.
"""
if not self.saxonArmy:
return "No Saxons are left to continue."
attacking_viking = random.choice(self.vikingArmy)
defending_saxon = random.choice(self.saxonArmy)
result = defending_saxon.receiveDamage(attacking_viking.attack())
if defending_saxon.health <= 0:
self.saxonArmy.remove(defending_saxon)
return result
def saxonAttack(self):
"""
Simulates a Saxon attacking a Viking.
Returns:
str: message with the result of the attack.
"""
if not self.vikingArmy:
return "There are no Vikings left to continue."
attacking_saxon = random.choice(self.saxonArmy)
defending_viking = random.choice(self.vikingArmy)
result = defending_viking.receiveDamage(attacking_saxon.attack())
if defending_viking.health <= 0:
self.vikingArmy.remove(defending_viking)
return result
def showStatus(self):
"""
Returns the status of the war based on the remaining armies.
Returns:
str: a message indicating the current status of the war.
"""
if not self.saxonArmy:
return "Vikings have won the war of the century!"
elif not self.vikingArmy:
return "Saxons have fought for their lives and survive another day..."
else:
return "Vikings and Saxons are still in the thick of battle."