-
Notifications
You must be signed in to change notification settings - Fork 5
/
【16】Army Battles.py
126 lines (101 loc) · 3.11 KB
/
【16】Army Battles.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
# =====================================
# --*-- coding: utf-8 --*--
# @Author : TRHX
# @Blog : www.itrhx.com
# @CSDN : itrhx.blog.csdn.net
# @FileName: 【16】Army Battles.py
# =====================================
class Army():
def __init__(self):
self.health = 0
self.attack = 0
self.num = 0
def add_units(self, x, num):
self.health = x().health
self.attack = x().attack
self.num = num
class Warrior:
health = 50
attack = 5
is_alive = True
class Knight(Warrior):
attack = 7
def fight(army1, army2):
while army1.health > 0:
army2.health -= army1.attack
army1.health -= army2.attack
if army2.health > army1.health:
army1.is_alive = False
return False
else:
army2.is_alive = False
return True
class Battle:
def fight(self, army1, army2):
x2 = y2 = 0
while army1.num > 0 and army2.num > 0:
x1 = army1.health if x2 == 0 else x2
y1 = army2.health if y2 == 0 else y2
while True:
y1 -= army1.attack
if y1 <= 0:
x2 = x1
y2 = 0
army2.num -= 1
break
x1 -= army2.attack
if x1 <= 0:
y2 = y1
x2 = 0
army1.num -= 1
break
if army1.num:
return True
else:
return False
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
chuck = Warrior()
bruce = Warrior()
carl = Knight()
dave = Warrior()
mark = Warrior()
assert fight(chuck, bruce) == True
assert fight(dave, carl) == False
assert chuck.is_alive == True
assert bruce.is_alive == False
assert carl.is_alive == True
assert dave.is_alive == False
assert fight(carl, mark) == False
assert carl.is_alive == False
print("Coding complete? Let's try tests!")
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
# fight tests
chuck = Warrior()
bruce = Warrior()
carl = Knight()
dave = Warrior()
mark = Warrior()
assert fight(chuck, bruce) == True
assert fight(dave, carl) == False
assert chuck.is_alive == True
assert bruce.is_alive == False
assert carl.is_alive == True
assert dave.is_alive == False
assert fight(carl, mark) == False
assert carl.is_alive == False
# battle tests
my_army = Army()
my_army.add_units(Knight, 3)
enemy_army = Army()
enemy_army.add_units(Warrior, 3)
army_3 = Army()
army_3.add_units(Warrior, 20)
army_3.add_units(Knight, 5)
army_4 = Army()
army_4.add_units(Warrior, 30)
battle = Battle()
assert battle.fight(my_army, enemy_army) == True
assert battle.fight(army_3, army_4) == False
print("Coding complete? Let's try tests!")