forked from tiyd-python-2015-05/game-of-sticks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_of_sticks.py
223 lines (172 loc) · 6.58 KB
/
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
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
import os
import random
class SticksGame:
# X Print out rules
# X create and start turns
# X remove turn sticks from pile
# X display remaining sticks
# X Stop when player starts turn with one stick in pile
def __init__(self, player1, player2):
self.pile = 20
self.player1 = player1
self.player2 = player2
self.current_player = player1
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
def display_pile(self):
return "Pile contains {} sticks.".format(self.pile) \
+ (" / " * self.pile)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
def switch_players(self):
if self.current_player == self.player1:
self.current_player = self.player2
else:
self.current_player = self.player1
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
def remove(self, silent=False):
if not silent:
print("Currently {}'s turn\n".format(self.current_player.name))
self.current_player.pick_up(self.pile)
self.pile -= self.current_player.sticks
self.current_player.sticks = 0
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
def winner(self):
if self.pile > 0:
return None
else:
return self.current_player
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
def start(self):
self.pile = 20
while not self.winner():
os.system('clear')
print(self.display_pile())
self.remove()
self.switch_players()
self.current_player.win_state = True
os.system('clear')
print("Game Over!")
self.player1.win_check()
self.player2.win_check()
self.play_again()
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
def play_again(self):
go_again = input("Would you like to play again (y/n)? ").lower()
if go_again[0] == "y":
self.start()
elif go_again[0] == "n":
return
else:
self.play_again()
###############################################################################
class Player:
# X decide how many sticks to pick up
def __init__(self, name="Player"):
self.name = name
self.sticks = 0
self.win_state = False
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
def pick_up(self, remaining):
try:
num = int(input("How many sticks would you like to pick up? "))
if num in [1, 2, 3]:
self.sticks = num
else:
print("Invalid choice!")
return self.pick_up(remaining)
except ValueError:
print("Invalid choice!")
return self.pick_up(remaining)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
def win_check(self):
if self.win_state:
print("The winner is {}!\n".format(self.name))
self.win_state = False
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
def __repr__(self):
return self.name
###############################################################################
class AIPlayer(Player):
def __init__(self, name="AI"):
super().__init__(name)
self.chosen = {}
self.possibilities = {}
self.tally = 0
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
def choices(self, remaining):
return self.possibilities.setdefault(remaining, [1, 2, 3])
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
def pick_up(self, remaining):
choice = random.choice(self.choices(remaining))
self.chosen[remaining] = choice
self.sticks = choice
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
def win_check(self, silent=False):
if self.win_state:
if not silent:
print("The winner is {}!\n".format(self.name))
self.integrate_win()
self.tally += 1
self.win_state = False
else:
self.chosen = {}
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
def integrate_win(self):
"""for each choice made, add that choice into the
possible choices"""
for key in self.chosen:
self.possibilities[key].append(self.chosen[key])
self.chosen = {}
###############################################################################
class AISticksGame(SticksGame):
def start(self):
count = 100000
while count > 0:
self.pile = 20
while not self.winner():
self.remove(silent=True)
self.switch_players()
self.current_player.win_state = True
self.player1.win_check(silent=True)
self.player2.win_check(silent=True)
count -= 1
if self.player1.tally >= self.player2.tally:
return self.player1
else:
return self.player2
###############################################################################
def menu():
os.system('clear')
print("Welcome to the Game of Sticks!\n\
In the Game of Sticks there is a heap of sticks on a board.\n\
On their turn, each player picks up 1 to 3 sticks.\n\
The one who has to pick the final stick will be the loser.\n")
set_up = input("Would you like to play against another [h]uman, \
a [c]omputer, or a [t]rained computer? ").lower()
if set_up[0] == "h":
player_name1 = input("First Player's Name: ")
player_name2 = input("Second Player's Name: ")
player1 = Player(player_name1)
player2 = Player(player_name2)
game = SticksGame(player1, player2)
game.start()
elif set_up[0] == "c":
player_name = input("Your Name: ")
player1 = Player(player_name)
player2 = AIPlayer()
game = SticksGame(player1, player2)
game.start()
elif set_up[0] == "t":
ai1 = AIPlayer()
ai2 = AIPlayer()
AI_game = AISticksGame(ai1, ai2)
player_name = input("Your Name: ")
player1 = Player(player_name)
player2 = AI_game.start()
game = SticksGame(player1, player2)
game.start()
else:
os.system('clear')
print("Invalid choice, please try again")
return menu()
if __name__ == '__main__':
menu()