forked from tiyd-python-2015-05/game-of-sticks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_game_of_sticks.py
113 lines (89 loc) · 2.64 KB
/
test_game_of_sticks.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
from game_of_sticks import SticksGame, Player, AIPlayer, AISticksGame
def test_SticksGame_has_pile():
game = SticksGame("p1", "p2")
assert game.pile == 20
class TestGame(SticksGame):
def play_again(self):
return None
def test_SticksGame_ends():
game = TestGame("p1", "p2")
assert not game.winner() == "p1"
assert not game.winner() == "p2"
game.pile = 0
assert game.winner() == "p1"
assert not game.winner() == "p2"
def test_SticksGame_displays_pile():
game = SticksGame("p1", "p2")
assert game.display_pile() == "Pile contains 20 sticks."
game.pile = 3
assert game.display_pile() == "Pile contains 3 sticks."
def test_SticksGame_switches_players():
game = SticksGame("p1", "p2")
game.switch_players()
assert game.current_player == "p2"
game.switch_players()
assert game.current_player == "p1"
class TestPlayer(Player):
def __init__(self):
super().__init__()
self.check = 2
def pick_up(self, remaining):
if self.check in [1, 2, 3]:
self.sticks = self.check
def test_player_decides_how_many_sticks_to_take():
player = TestPlayer()
assert player.sticks == 0
player.pick_up(1)
assert player.sticks == 2
player.check = 1
player.pick_up(1)
assert player.sticks == 1
player.check = 3
player.pick_up(1)
assert player.sticks == 3
player.check = 4
player.pick_up(1)
assert not player.sticks == 4
def test_SticksGame_removes_sticks_from_pile():
player = TestPlayer()
game = SticksGame(player, player)
assert game.pile == 20
game.remove()
assert game.pile == 18
player.check = 4
game.remove()
assert game.pile == 18
def test_SticksGame_goes_until_winner():
p1 = TestPlayer()
p2 = TestPlayer()
game = TestGame(p1, p2)
game.start()
print(game.pile)
print(game.current_player)
assert game.winner() is not None
def test_AI_test_game():
ai = AIPlayer()
ai.pick_up(17)
ai.pick_up(12)
ai.pick_up(3)
ai.integrate_win()
assert len(ai.choices(17)) == 4
ai.pick_up(17)
ai.pick_up(10)
ai.integrate_win()
assert len(ai.choices(17)) == 5
assert len(ai.choices(10)) == 4
ai.pick_up(17)
ai.pick_up(10)
ai.win_check()
assert len(ai.choices(17)) == 5
assert len(ai.choices(10)) == 4
def test_AI_choices():
"""An initial player should have 1, 2, 3 as choices for all numbers."""
ai = AIPlayer()
assert ai.choices(13) == [1, 2, 3]
def test_AISticksGame():
HAL = AIPlayer()
GLaDOS = AIPlayer()
game = AISticksGame(HAL, GLaDOS)
assert game.start() == HAL or game.start() == GLaDOS