forked from butthurtclub/nice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunit.py
212 lines (164 loc) · 5.48 KB
/
unit.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
__author__ = 'nice'
import unittest
class UnitIsDeadException(Exception):
"""A class for exceptions."""
pass
class Unit:
"""
A class for Unit representing.
Usage:
>>> unit = Unit()
>>> print(unit)
Name: unit
Damage: 10
Hit points: 100
>>> knight = Unit('Knight', 150, 150, 10)
>>> soldier = Unit('Soldier', 100, 100, 10)
>>> soldier.attack(knight)
>>> print(soldier)
>>> print(knight)
Name: Soldier
Damage: 10
Hit points: 95
Name: Knight
Damage: 10
Hit points: 140
>>> soldier.add_hit_points(50)
>>> print(soldier)
Name: Soldier
Damage: 10
Hit points: 100
"""
def __init__(self, name='unit', hit_points_limit=100, hit_points=100, damage=10):
"""
The initializer.
:param name: Unit name
:type name: string
:param hit_points_limit: Unit hit points limit.
:type hit_points_limit: integer, float.
:param hit_points: Current unit hit points amount.
:type hit_points: integer, float.
:param damage: Unit damage.
:type damage: integer, float.
"""
self._name = name
self._hit_points_limit = hit_points
self._hit_points = hit_points
self._damage = damage
@property
def name(self):
return self._name
@name.setter
def name(self, value):
self._name = value
@property
def hit_points_limit(self):
return self._hit_points_limit
@hit_points_limit.setter
def hit_points_limit(self, value):
self._hit_points_limit = value
@property
def hit_points(self):
return self._hit_points
@hit_points.setter
def hit_points(self, value):
self._hit_points = value
@property
def damage(self):
return self._damage
@damage.setter
def damage(self, value):
self._damage = value
def __str__(self):
return 'Name: {}\nDamage: {}\nHit Points: {}\n'.format(self.name, self.damage, self.hit_points)
def ensure_is_alive(self):
"""
Checks if the unit is alive.
:raise UnitIsDeadException: When trying to access a dead unit.
"""
if self.hit_points == 0:
raise UnitIsDeadException('Unit is dead')
def add_hit_points(self, hp):
"""
Adds life points to the unit.
:param hp: Amount of life points to add.
:type hp: integer, float.
:raise UnitIsDeadException: When trying to access a dead unit.
"""
self.ensure_is_alive()
if hp >= self.hit_points_limit - self.hit_points:
self.hit_points = self.hit_points_limit
else:
self.hit_points += hp
def take_damage(self, dmg):
"""
Reduce units(self) life points.
:param dmg: The amount of damage the unit receives.
:type dmg: integer, float.
:raise UnitIsDeadException: When trying to access a dead unit.
"""
self.ensure_is_alive()
if dmg > self.hit_points:
self.hit_points = 0
else:
self.hit_points -= dmg
def attack(self, enemy):
"""
Reduce units(enemy) life points.
:param enemy: Unit to attack.
:type enemy: Unit().
:raise UnitIsDeadException: When trying to access a dead unit.
"""
enemy.ensure_is_alive()
enemy.take_damage(self.damage)
enemy.counter_attack(self)
def counter_attack(self, enemy):
"""
Reduce units(self) life points.
:param enemy: Unit to attack.
:type enemy: Unit().
:raise UnitIsDeadException: When trying to access a dead unit.
"""
enemy.ensure_is_alive()
enemy.take_damage(self.damage/2)
class UnitTest(unittest.TestCase):
def test_initial(self):
soldier = Unit('Soldier', 100, 100, 10)
self.assertIsInstance(soldier, Unit)
self.assertEqual(soldier.name, 'Soldier')
self.assertEqual(soldier.hit_points_limit, 100)
self.assertEqual(soldier.hit_points, 100)
self.assertEqual(soldier.damage, 10)
def test_ensure_is_alive(self):
soldier = Unit()
self.assertRaises(UnitIsDeadException(), soldier.ensure_is_alive())
def test_add_hit_points(self):
soldier = Unit('Soldier', 100, 100, 10)
self.assertEqual(soldier.hit_points, 100)
soldier.take_damage(30)
self.assertEqual(soldier.hit_points, 70)
soldier.add_hit_points(500)
self.assertEqual(soldier.hit_points, 100)
def test_take_damage(self):
soldier = Unit('Soldier', 100, 100, 10)
self.assertEqual(soldier.hit_points, 100)
soldier.take_damage(30)
self.assertEqual(soldier.hit_points, 70)
soldier.take_damage(30)
self.assertEqual(soldier.hit_points, 40)
soldier.take_damage(30)
self.assertEqual(soldier.hit_points, 10)
self.assertRaises(UnitIsDeadException(), soldier.take_damage(30))
def test_counter_attack(self):
soldier = Unit('Soldier', 100, 100, 10)
knight = Unit('Knight', 150, 150, 10)
self.assertEqual(soldier.hit_points, 100)
self.assertEqual(knight.hit_points, 150)
soldier.attack(knight)
self.assertEqual(soldier.hit_points, 95)
self.assertEqual(knight.hit_points, 140)
knight.attack(soldier)
self.assertEqual(soldier.hit_points, 85)
self.assertEqual(knight.hit_points, 135)
if __name__ == '__main__':
unittest.main()