-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathexercise_classes.py
More file actions
257 lines (186 loc) · 8.16 KB
/
exercise_classes.py
File metadata and controls
257 lines (186 loc) · 8.16 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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/env python
"""
In this exercise we will create a number of classes and apply some elementary
object-oriented programming.
Implement the following classes describing some of the actors in an elementary
video game.
All actors have a property "strength". Strength is a number that can be
increased by x by calling the method "increase_strength(x)", resp. "strength" is
decreased by x by calling the method "decrease_strength(x)". If the strength drops
below zero, it should remain zero.
The property "is_alive" should start out True, and become False once strength
reaches zero. Contrary to popular opinion, Zombies and Vampires should
start out as alive.
All actors will contain the method "fight()". Actor x can start a fight with
Actor y by calling "x.fight(y)". The one with the highest strength wins,
the other dies. The winner is damaged however, and gets its strength decreased
by the strength of the opponent. If the strengths are equal, both die horribly.
All actors have a "kills" property which increases by one when the actor wins a
fight.
Class Hero has an special property "magic". Hero's "strength" can increase only
if "magic" is above zero. x increase in "strength" causes x decrease in "magic".
Class Vampire will increase its property "strength" after winning a fight, instead
of decreasing it, by half the strength of its enemy.
Class Zombie will increase its "strength" by x at the beginning of every fight
_it starts itself_, where x is a randomly generated number between 1 and 10.
"""
from random import randint
class Actor:
"""
This is supposed to be the most general class, at the top of the hierarchy:
everything that shared between all actors can be put here.
"""
def __init__(self):
self.is_alive = True
self.strength = 100
self.kills = 0
def strength(self):
self.strength = 100
def increase_strength(self, x):
self.strength = self.strength + x
def decrease_strength(self, x):
self.strength = self.strength - x
if self.strength <= 0:
self.strength = 0
self.is_alive = False
def fight(self,y):
if y.strength == self.strength:
self.is_alive = False
self.strength = 0
y.is_alive = False
y.strength = 0
if y.strength > self.strength:
y.kills = y.kills + 1
self.is_alive = False
y.decrease_strength(self.strength)
self.strength = 0
if y.strength < self.strength:
self.kills = self.kills + 1
y.is_alive = False
self.decrease_strength(y.strength)
y.strength = 0
class Hero(Actor):
"""
Heroes start out with 100 strength and 50 magic.
"""
def __init__(self):
self.strength = 100
self.magic = 50
self.is_alive = True
self.kills = 0
def increase_strength(self, x):
if self.magic < x:
self.strength = self.strength + self.magic
self.magic = 0
if self.magic >= x:
self.strength = self.strength + x
self.magic = self.magic - x
class Zombie(Actor):
"""
Zombies start out with 150 strength.
Remember that Zombies increase their strength randomly at the beginning
of each fight. How to generate a random number?
"""
def __init__(self):
self.strength = 150
self.is_alive = True
self.kills = 0
def fight(self,y):
self.strength = self.strength + randint(1,10)
if y.strength == self.strength:
self.is_alive = False
self.strength = 0
y.is_alive = False
y.strength = 0
if y.strength > self.strength:
y.kills = y.kills + 1
self.is_alive = False
y.decrease_strength(self.strength)
self.strength = 0
if y.strength < self.strength:
self.kills = self.kills + 1
y.is_alive = False
self.decrease_strength(y.strength)
y.strength = 0
class Vampire(Actor):
"""
Vampires start out with 70 strength.
Remember that when a Vampire wins a fight it starts, it obtains half of the
strength of the enemy it has beaten.
"""
def __init__(self):
self.strength = 70
self.is_alive = True
self.kills = 0
def fight(self,y):
if y.strength == self.strength:
self.is_alive = False
self.strength = 0
y.strength = 0
y.is_alive = False
if y.strength > self.strength:
print "JA"
y.kills = y.kills + 1
self.is_alive = False
y.decrease_strength(self.strength)
self.strength = 0
if y.strength < self.strength:
self.kills = self.kills + 1
y.is_alive = False
self.increase_strength(y.strength/2)
y.strength = 0
"""
From here on unit tests.
Do not modify this code!
"""
def test_hero_strength():
hero = Hero()
assert hero.strength == 100, 'Hero strength does not initialize to 100'
assert hero.magic == 50, 'Hero magic does not initialize to 50'
assert hero.is_alive, 'Hero aliveness does not intialize to True'
hero.increase_strength(50)
assert hero.strength == 150, 'Hero increase_strength does not increase strength'
assert hero.is_alive, 'Hero increase_strength breaks is_alive'
assert hero.magic == 0, 'Hero increase_strength does not decrease magic'
hero.decrease_strength(149)
assert hero.strength == 1, 'Hero decrease_strength does not decrease strength'
assert hero.is_alive, 'Hero decrease_strength breaks is_alive'
hero.decrease_strength(5)
assert hero.strength == 0, 'Hero decrease_strength breaks when going below 0'
assert not hero.is_alive, 'Hero decrease strength breaks when going below 0'
def test_hero_fights_vs_vampires():
hero = Hero()
assert hero.strength == 100, 'Hero strength does not initialize to 100'
assert hero.kills == 0, 'Hero kills does not initialize to 0'
first_vampire = Vampire()
assert first_vampire.strength == 70, 'Vampire strength does not initialize to 70'
assert first_vampire.is_alive, 'Vampire is_alive does not initialize to True'
first_vampire.fight(hero)
assert hero.strength == 30, 'Vampire fight does not decrease hero strength correctly'
assert hero.is_alive, 'Vampire fight breaks hero is_alive'
assert hero.kills == 1, 'Vampire fight does not increase Hero kills'
assert not first_vampire.is_alive, 'Vampire fight does not kill vampire'
second_vampire = Vampire()
second_vampire.fight(hero)
assert hero.strength == 0, 'Vampire fight does not decrease Hero strength'
assert not hero.is_alive, 'Vampire fight does not kill Hero'
assert hero.kills == 1, 'Vampire fight breaks existing Hero kill count'
assert second_vampire.is_alive, 'Vampire fight breaks Vampire is_alive'
assert second_vampire.strength == 85, 'Vampire fight does not increase strength after win'
assert second_vampire.kills == 1, 'Vampire fight deos not increase Vampire kills'
def test_hero_loses_against_zombie():
hero = Hero()
zombie = Zombie()
assert zombie.strength == 150, 'Zombie strength does not initialize properly'
zombie.fight(hero)
assert not hero.is_alive, 'Zombie fight does not kill Hero'
assert hero.kills == 0, 'Zombie fight breaks Hero kills'
assert zombie.is_alive, 'Zombie fight breaks Zombie is_alive'
assert zombie.kills == 1, 'Zombie fight breaks Zombie kills'
assert zombie.strength > 50, 'Zombie fight does not increase strength'
assert zombie.strength < 61, 'Zombie fight increases strength too much'
if __name__ == '__main__':
from test_runner import run_tests
run_tests()
Contact GitHub API Training Shop Blog About
© 2017 GitHub, Inc. Terms Privacy Security Status Help