-
Notifications
You must be signed in to change notification settings - Fork 1
/
enemy.py
413 lines (341 loc) · 11.8 KB
/
enemy.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
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
from utils.format import format_stats
from entities.player import Player
from random import choice, randint
from typing import List, Dict
class EnemyNameError(Exception):
"""
Indicates that given Enemy's name was empty
:param Exception: Enemy's name cannot be empty
:type Exception: Exception
"""
pass
class Enemy:
"""
Enemy
Contains attributes:
:param name: Name of the enemy, defaults to 'Monster'
:type name: str, optional
:param base_health: Base health of the enemy, defaults to 100
:type base_health: int, optional
:param health: Current health of the enemy, defaults to 100
:type health: int, optional
:param regeneration: Amount of health enemy can regenerate at a time,
defaults to 10
:type regeneration: int, optional
:param strength: Base value of damage delt to player, defaults to 10
:type strength: int, optional
:param random_strength: Random value of damage delt to player,
defaults to 10
:type random_strength: int, optional
:param shouts: List of texts enemy can shout before fight, defaults to None
:type shouts: List[str], optional
:param description: Description of the enemy, defaults to ''
:type description: str, optional
"""
def __init__(self,
name: str = 'Monster',
base_health: int = 100,
health: int = 100,
regeneration: int = 10,
strength: int = 10,
random_strength: int = 10,
shouts: List[str] = None,
description: str = ''
):
"""
Initialize Enemy
:param name: Name of the enemy, defaults to 'Monster'
:type name: str, optional
:param base_health: Base health of the enemy, defaults to 100
:type base_health: int, optional
:param health: Current health of the enemy, defaults to 100
:type health: int, optional
:param regeneration: Amount of health enemy can regenerate at a time,
defaults to 10
:type regeneration: int, optional
:param strength: Base value of damage delt to player, defaults to 10
:type strength: int, optional
:param random_strength: Random value of damage delt to player,
defaults to 10
:type random_strength: int, optional
:param shouts: List of texts enemy can shout before fight,
defaults to None
:type shouts: List[str], optional
:param description: Description of the enemy, defaults to ''
:type description: str, optional
"""
self.set_name(name)
self.set_base_health(base_health)
self.set_health(health)
self.set_regeneration(regeneration)
self.set_strength(strength)
self.set_random_strength(random_strength)
self.set_shouts(shouts),
self.set_description(description)
def as_dict(self) -> Dict:
"""
Get enemy as a dictionary
:return: Dictionary with enemy's data
:rtype: Dict
"""
return {
'name': self._name,
'base_health': self._base_health,
'health': self._health,
'regeneration': self._regeneration,
'strength': self._strength,
'random_strength': self._random_strength,
'shouts': self._shouts,
'description': self._description
}
@staticmethod
def from_dict(dictionary: Dict) -> 'Enemy':
"""
Get new Enemy instance loaded from dictionary
:param dictionary: Dictionary with enemy's data
:type dictionary: Dict
:return: Loaded Enemy
:rtype: Enemy
"""
if dictionary:
return Enemy(
dictionary.get('name', 'Monster'),
dictionary.get('base_health', 100),
dictionary.get('health', 100),
dictionary.get('regeneration', 10),
dictionary.get('strength', 10),
dictionary.get('random_strength', 10),
dictionary.get('shouts', 10),
dictionary.get('description', '')
)
# Getters and setters
def name(self) -> str:
"""
Get enemy's name
:return: Name of the enemy
:rtype: str
"""
return self._name
def set_name(self, name: str):
"""
Set enemy's name
:param name: Name of the enemy
:type name: str
:raises EnemyNameError: Indicates that given name is empty
"""
if not name:
raise EnemyNameError('Name cannot be empty')
self._name = name
def health(self) -> int:
"""
Get current enemy's health
:return: Value of enemy's health
:rtype: int
"""
return self._health
def set_health(self, health: int):
"""
Set player's health
:param health: Value of enemy's health
:type health: int
:raises ValueError: Indicates that given enemy's health was negative
:raises ValueError: Indicates that given enemy's health was greater
than base health
"""
if health <= 0:
raise ValueError("Enemy's Health cannot be negative")
if self._base_health < health:
raise ValueError('Health cannot be greater than base health')
self._health = health
def base_health(self) -> int:
"""
Get enemy's base value of health
:return: Base value of enemy's health
:rtype: int
"""
return self._base_health
def set_base_health(self, base_health: int):
"""
Set enemy's base health value
:param base_health: Base value of enemy's health
:type base_health: int
:raises ValueError: Indicates that given enemy's health was negative
"""
if base_health <= 0:
raise ValueError("Enemy's Base Health cannot be negative")
self._base_health = base_health
def regeneration(self) -> int:
"""
Get amount of health enemy can regenerate once a time
:return: Amount of health
:rtype: int
"""
return self._regeneration
def set_regeneration(self, regeneration: int):
"""
Set amount of health enemy can regenerate once a time
:param regeneration: Amount of health
:type regeneration: int
:raises ValueError: Indicates that given amount was negative
"""
if regeneration < 0:
raise ValueError('Regeneration cannot be negative')
self._regeneration = regeneration
def strength(self) -> int:
"""
Get enemy's base amout of damage delt to player
:return: Base enemy's strength
:rtype: int
"""
return self._strength
def set_strength(self, strength: int):
"""
Set enemy's base amout of damage delt to player
:param strength: Base enemy's strength
:type strength: int
:raises ValueError: Indicates that given value was negative
"""
if strength < 0:
raise ValueError('Strength cannot be negative')
self._strength = strength
def random_strength(self) -> int:
"""
Get enemy's random amout of damage delt to player
:return: Random enemy's strength
:rtype: int
"""
return self._random_strength
def set_random_strength(self, random_strength: int):
"""
Set enemy's random amout of damage delt to player
:param strength: Random enemy's strength
:type strength: int
:raises ValueError: Indicates that given value was negative
"""
if random_strength < 0:
raise ValueError('Random strength cannot be negative')
self._random_strength = random_strength
def shouts(self) -> List[str]:
"""
Get list of texts enemy can shout before fight
:return: List of shouts
:rtype: List[str]
"""
return self._shouts
def set_shouts(self, shouts: List[str]):
"""
Set list of texts enemy can shout before fight
:param shouts: List of shouts
:type shouts: List[str]
"""
self._shouts = shouts if shouts else ['Die Trash!']
def description(self) -> str:
"""
Get enemy's description
:return: Description
:rtype: str
"""
return self._description
def set_description(self, description: str):
"""
Set enemy's description
:param description: Enemy's description
:type description: str
"""
self._description = description
# Custom Methods
def take_damage(self, damage: int) -> bool:
"""
Damage enemy by given amount
:param damage: Amount of damage
:type damage: int
:raises ValueError: Indicates that given damage was negative
:return: Whether enemy is alive
:rtype: bool
"""
if damage < 0:
raise ValueError('Damage cannot be negative')
self._health -= min(damage, self._health)
print(f'{self.name()} took {damage} points of damage.')
return self.is_alive()
def is_alive(self) -> bool:
"""
:return: Whether enemy is alive
:rtype: bool
"""
return self._health > 0
def move(self, player: Player) -> bool:
"""
Decide what to do
:param player: Player
:type player: Player
:return: Whether player is alive after attack
:rtype: bool
"""
if self._regeneration == 0 or self._health == self._base_health:
return self.inflict_damage(player)
elif (randint(0, 1) or
(self._regeneration + self._health) > self._base_health):
return self.inflict_damage(player)
else:
return self.regenerate()
def inflict_damage(self, player: Player) -> bool:
"""
Attack player
:param player: Player
:type player: Player
:return: Whether player is alive after attack
:rtype: bool
"""
damage = self.strength()
damage += randint(0, self.random_strength())
print(f'{self.name()} attacked for {damage} points.')
return player.take_damage(damage)
def regenerate(self) -> bool:
"""
Regenerate health
:return: True - player is alive, no damage delt this time
:rtype: bool
"""
self._health = min(
self._health + self._regeneration, self._base_health)
print(f'{self.name()} is regenerating.')
return True
def shout(self) -> str:
"""
Generates random fight shouts
:return: Battle shout
:rtype: str
"""
shouts = self._shouts
return choice(shouts)
def info(self) -> str:
"""
Returns basic info about Enemy
:return: Information about enemy
:rtype: str
"""
info = f'Name: {self.name()}\n'
info += format_stats('Health', self.health(), self.base_health())
info += format_stats('Strength', self.strength(),
self.strength() + self.random_strength())
return info
def print_info(self):
"""
Prints information about enemy
"""
print(self.info())
def __str__(self) -> str:
"""
:return: Basic enemy information
:rtype: str
"""
return self.info()
def __eq__(self, other):
"""
:param other: Checked object
:type other: Any
:return: Indicates whether self and other are equal
:rtype: bool
"""
return isinstance(other, Enemy) and self.as_dict() == other.as_dict()