forked from ironhack-labs/mini-project-vikings-en
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vikingsClasses.py
204 lines (152 loc) · 6.09 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
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
import random
# Soldier
class Soldier:
def __init__(self, health, strength):
self.health = health
self.strength = strength
def attack(self):
return self.strength
def receiveDamage(self, damage):
self.health -= damage
# Viking
class Viking(Soldier):
def __init__(self, name, health, strength):
super().__init__(health, strength)
self.name = name
def battleCry(self):
return "Odin Owns You All!"
def receiveDamage(self, damage):
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"
# Saxon
class Saxon(Soldier):
def __init__(self, health, strength):
super().__init__(health, strength)
def receiveDamage(self, damage):
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):
self.vikingArmy.append(viking)
def addSaxon(self, saxon):
self.saxonArmy.append(saxon)
def vikingAttack(self):
import random
saxon = random.choice(self.saxonArmy)
viking = random.choice(self.vikingArmy)
result = saxon.receiveDamage(viking.strength)
if saxon.health <= 0:
self.saxonArmy.remove(saxon)
return result
def saxonAttack(self):
import random
saxon = random.choice(self.saxonArmy)
viking = random.choice(self.vikingArmy)
result = viking.receiveDamage(saxon.strength)
if viking.health <= 0:
self.vikingArmy.remove(viking)
return result
def showStatus(self):
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."
import unittest
from vikingsClasses import Viking
from inspect import signature
class TestViking(unittest.TestCase):
@classmethod
def setUp(cls):
cls.name = 'Harald'
cls.strength = 150
cls.health = 300
cls.viking = Viking(cls.name, cls.health, cls.strength)
def testShouldReciveThreeParams(self):
self.assertEqual(len(signature(Viking).parameters), 3)
def testName(self):
self.assertEqual(self.viking.name, self.name)
def testHealth(self):
self.assertEqual(self.viking.health, self.health)
def testStrenght(self):
self.assertEqual(self.viking.strength, self.strength)
def testAttackShouldBeFunction(self):
self.assertEqual(callable(self.viking.attack), True)
def testAttackReciveNoParameters(self):
self.assertEqual(len(signature(self.viking.attack).parameters), 0)
def testAttackShouldReturnStrength(self):
self.assertEqual(self.viking.attack(), self.strength)
def testReceiveDamageIsFunction(self):
self.assertEqual(callable(self.viking.receiveDamage), True)
def testReceiveDamageReciveOneParam(self):
self.assertEqual(
len(signature(self.viking.receiveDamage).parameters), 1)
def testReciveDamageShouldRestHealth(self):
self.viking.receiveDamage(50)
self.assertEqual(self.viking.health, self.health - 50)
def testReciveDamageShouldReturnString50(self):
self.assertEqual(self.viking.receiveDamage(50), self.name +
' has received 50 points of damage')
def testReciveDamageShouldReturnString70(self):
self.assertEqual(self.viking.receiveDamage(70), self.name +
' has received 70 points of damage')
def testReceiveDamageShouldReturnStringDeath(self):
self.assertEqual(self.viking.receiveDamage(self.health),
self.name + ' has died in act of combat')
def testBattleCry(self):
self.assertEqual(callable(self.viking.battleCry), True)
def testBattleCryReturnString(self):
self.assertEqual(self.viking.battleCry(), 'Odin Owns You All!')
if __name__ == '__main__':
unittest.main()
import unittest
from vikingsClasses import Saxon
from inspect import signature
class TestSaxon(unittest.TestCase):
@classmethod
def setUp(cls):
cls.health = 60
cls.strength = 25
cls.saxon = Saxon(cls.health, cls.strength)
def testSaxonShouldReceiveTwoParams(self):
self.assertEqual(len(signature(Saxon).parameters), 2)
def testHealth(self):
self.assertEqual(self.saxon.health, self.health)
def testStrength(self):
self.assertEqual(self.saxon.strength, self.strength)
def testAttack(self):
self.assertEqual(callable(self.saxon.attack), True)
def testAttackShouldReceiveNoParams(self):
self.assertEqual(len(signature(self.saxon.attack).parameters), 0)
def testAttackREturnStrength(self):
self.assertEqual(self.saxon.attack(), self.strength)
def testReceiveDamageIsFunction(self):
self.assertEqual(callable(self.saxon.receiveDamage), True)
def testReceiveDamageShouldReceiveOneParam(self):
self.assertEqual(
len(signature(self.saxon.receiveDamage).parameters), 1)
def testReceiveDamage(self):
self.saxon.receiveDamage(1)
self.assertEqual(self.saxon.health, self.health - 1)
def testReceiveDamageString45(self):
self.assertEqual(self.saxon.receiveDamage(
45), 'A Saxon has received 45 points of damage')
def testReceiveDamageString10(self):
self.assertEqual(self.saxon.receiveDamage(
10), 'A Saxon has received 10 points of damage')
def testReceiveDamageStringDied(self):
self.assertEqual(self.saxon.receiveDamage(self.health),
'A Saxon has died in combat')
if __name__ == '__main__':
unittest.main()