-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.py
155 lines (143 loc) · 5.08 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
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
from guess_number_game.guess_number_game import GuessNumberGame
from hanoi_towers.hanoi_towers import HanoiTowers
from tateti.tateti import Tateti
from buscaminas.buscaminas import Buscaminas
from connect_four_game.connect_four_game import ConnectFourGame
from damas.dama_game import DamaGameStart
from reversi.ReversiGame import ReversiGame
from four_number.four_number import FourNumber
from generala.game import Generala
from blackjack.blackjack_game import BlackJackGame
from battleship.game import GameBattleship
from poker.game import PokerGame
from sudoku.game import SudokuGame
from craps.game import CrapsGame
from ruleta.game_roullete import GameRoulette
from dungeon_raiders.model.game import DungeonRaidersGame
from ahorcado.ahorcado import Ahorcado
from senku.senku import SenkuGame
from qwixx.qwixx import Qwixx
from rummy_and_burakko.rummy_and_burakko import RummyAndBurakko
from wumpus.game import WumpusGame
from backgammon.game.backgammon import BackgammonGame
class Game(object):
def __init__(self):
super(Game, self).__init__()
self.games = [
GuessNumberGame,
Tateti,
Buscaminas,
ConnectFourGame,
DamaGameStart,
ReversiGame,
FourNumber,
Generala,
BlackJackGame,
GameBattleship,
PokerGame,
SudokuGame,
CrapsGame,
GameRoulette,
DungeonRaidersGame,
Ahorcado,
SenkuGame,
HanoiTowers,
Qwixx,
RummyAndBurakko,
WumpusGame,
BackgammonGame
]
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 += '99: 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:
if self.active_game.input_are_ints:
input_args = [
int(simple_arg)
for simple_arg in inputs.split(' ')
]
else:
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
)
)
if len(input_args) == self.active_game.input_args:
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 == 99:
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))
if __name__ == '__main__':
Game().play()