-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
106 lines (94 loc) · 3.28 KB
/
game.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
from ahorcado.ahorcado import Ahorcado
from guess_number_game.guess_number_game import GuessNumberGame
from hanoi_towers.hanoi_towers import HanoiTowers
from senku.senku import SenkuGame
class Game(object):
def __init__(self):
super(Game, self).__init__()
self.games = [
GuessNumberGame,
SenkuGame,
Ahorcado,
HanoiTowers,
]
def output(self, text):
print(text)
def get_input(self, text):
return input(text)
def game_inputs(self):
game_inputs = 'Select Game\n'
option_number = 0
for game in self.games:
game_inputs += '{}: {}\n'.format(
option_number,
game.name,
)
option_number += 1
game_inputs += '9: to quit\n'
return game_inputs
def get_turn_input(self, text):
input_args = ''
if isinstance(self.active_game.input_args, tuple):
input_arg_qtys = self.active_game.input_args
expecting_input_args = ' or '.join(
str(input_arg_qty)
for input_arg_qty in self.active_game.input_args
)
else:
input_arg_qtys = (self.active_game.input_args,)
expecting_input_args = self.active_game.input_args
expecting_str = (
'{} numbers separated with spaces'.format(
expecting_input_args,
)
)
while True:
inputs = self.get_input('{} (expecting {})\n'.format(
text,
expecting_str,
))
try:
input_args = inputs.split(' ')
if len(input_args) in input_arg_qtys:
break
else:
self.output(
'Wrong input count, expecting {} values'.format(
self.active_game.input_args
)
)
except Exception:
self.output('Wrong input, try again!')
return input_args
def select_game(self):
result = ''
while(not result.isdigit()):
result = self.get_input(self.game_inputs())
return int(result)
def play(self):
while True:
game_selection = self.select_game()
if game_selection == 9:
break
if game_selection < len(self.games):
self.active_game = self.games[game_selection]()
# try:
while (
(
hasattr(self.active_game, 'playing') and
self.active_game.playing
) or (
hasattr(self.active_game, 'is_playing') and
self.active_game.is_playing
)
):
self.output(self.active_game.board)
game_input = self.get_turn_input(
self.active_game.next_turn(),
)
self.output(self.active_game.play(*game_input))
# except Exception as e:
# self.output('Sorry... {}'.format(e))
self.output(self.active_game.board)
if __name__ == '__main__':
Game().play()