-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_template_method.py
71 lines (56 loc) · 2.34 KB
/
test_template_method.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
from unittest import TestCase
from abc import ABC
class Creature:
def __init__(self, attack, health):
self.health = health
self.attack = attack
class CardGame(ABC):
def __init__(self, creatures):
self.creatures = creatures
# return -1 if both creatures alive or both dead after combat
# otherwise, return the _index_ of winning creature
def combat(self, c1_index, c2_index):
first = self.creatures[c1_index]
second = self.creatures[c2_index]
self.hit(first, second)
self.hit(second, first)
first_alive = first.health > 0
second_alive = second.health > 0
if first_alive == second_alive: return -1
return c1_index if first_alive else c2_index
def hit(self, attacker, defender):
pass # implement this in derived classes
class TemporaryDamageCardGame(CardGame):
def hit(self, attacker, defender):
old_health = defender.health
defender.health -= attacker.attack
if defender.health > 0:
defender.health = old_health
class PermanentDamageCardGame(CardGame):
def hit(self, attacker, defender):
defender.health -= attacker.attack
class Evaluate(TestCase):
def test_impasse(self):
c1 = Creature(1, 2)
c2 = Creature(1, 2)
game = TemporaryDamageCardGame([c1, c2])
self.assertEqual(-1, game.combat(0, 1), 'Combat should yield -1 since nobody died.')
self.assertEqual(-1, game.combat(0, 1), 'Combat should yield -1 since nobody died.')
def test_temporary_murder(self):
c1 = Creature(1, 1)
c2 = Creature(2, 2)
game = TemporaryDamageCardGame([c1, c2])
self.assertEqual(1, game.combat(0, 1))
def test_double_murder(self):
c1 = Creature(2, 1)
c2 = Creature(2, 2)
game = TemporaryDamageCardGame([c1, c2])
self.assertEqual(-1, game.combat(0, 1))
def test_permanent_damage_death(self):
c1 = Creature(1, 2)
c2 = Creature(1, 3)
game = PermanentDamageCardGame([c1, c2])
self.assertEqual(-1, game.combat(0, 1), 'Nobody should win this battle.')
self.assertEqual(1, c1.health)
self.assertEqual(2, c2.health)
self.assertEqual(1, game.combat(0, 1), 'Creature at index 1 should win this')