forked from tiyd-python-2015-05/game-of-sticks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sticks_ui.py
136 lines (100 loc) · 3.61 KB
/
sticks_ui.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
from sticks import Game, Human, Computer
from hard_mode import HardComputer
import os
import random
class Talker:
def __init__(self):
self.game = Game()
def go(self):
self.get_names()
self.play()
def get_players(self):
number = input('Single Player? [y]es or [n]o: ')
if number.lower()[0] in 'yn':
return number.lower()[0] == 'y'
else:
print('Try again! Answer [yes] or [no].')
number = self.get_players()
def get_names(self):
os.system('clear')
players = []
if not self.get_players():
players.append(input('Enter Player 1\'s name: '))
players.append(input('Enter Player 2\'s name: '))
else:
players.append(Human(input('Enter your name: ')))
players.append(self.set_mode())
self.game.set_players(*players)
os.system('clear')
def set_mode(self):
mode = input('What mode? [E]asy [H]ard [U]nicron: ')
mode = mode.lower()[0]
if mode in 'ehu':
if mode == 'e':
return Computer()
if mode == 'h':
return HardComputer()
if mode == 'u':
new_game = Game()
unicron1 = HardComputer('Unicron')
unicron2 = HardComputer('Unicron')
new_game.set_players(unicron1, unicron2)
wins = {unicron1: 0, unicron2: 0}
for _ in range(10000):
wins[self.quick_play(new_game)] += 1
if wins[unicron1] > wins[unicron2]:
return unicron1
return unicron2
def get_sticks(self):
number = input("How many sticks are on the table (10-100)? ")
if number.isdigit():
number = int(number)
if number not in list(range(10, 101)):
number = self.get_sticks()
else:
number = self.get_sticks()
return number
def quick_play(self, game):
game.start(random.randint(10, 100))
while not game.is_done():
game.turn()
game.switch_player()
game.current_player.finish() # learn
game.current_player.reset() # get ready for next round
game.switch_player()
game.current_player.reset()
game.switch_player()
return game.current_player
def play(self):
self.game.start(self.get_sticks())
self.loop()
while self.play_again():
self.game.start(self.get_sticks())
self.loop()
# if isinstance(self.game.players[1], HardComputer):
# for key, item in self.game.players[1].start_hats.items():
# print(item)
def play_again(self):
yes_no = input("Would you like to play again? [Y]es or [N]o: ")
if yes_no.lower()[0] in 'yn':
os.system('clear')
return yes_no.lower()[0] == 'y'
else:
return self.play_again()
def update(self, *args):
os.system('clear')
print("{} took {} sticks. {} left!".format(*args))
def loop(self):
while not self.game.is_done():
self.game.turn()
self.update(*self.game.status())
self.game.switch_player()
self.winner(self.game.current_player)
if isinstance(self.game.current_player, HardComputer):
self.game.current_player.finish()
self.game.current_player.reset()
def winner(self, player):
print("{} won the game!".format(player.name))
if __name__ == '__main__':
talker = Talker()
talker.go()