-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame_interface.py
127 lines (102 loc) · 4.5 KB
/
game_interface.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
"""
The module used to play our games.
Please fill in the TODO's (i.e. importing games and strategies as needed)
Note: You do not have to run python_ta on this file.
You may import your games from A1 (i.e. Chopsticks). However, the minimax
strategy cannot be used on Chopsticks unless you account for infinite loops.
(You do not have to worry about this for the assignment: only do it for
your own curiousity!)
"""
from typing import Any, Callable
from strategy import interactive_strategy, rough_outcome_strategy, \
iterative_minimax, recursive_minimax
from subtract_square_game import SubtractSquareGame
from stonehenge import StonehengeGame
playable_games = {'s': SubtractSquareGame,
'h': StonehengeGame}
usable_strategies = {'i': interactive_strategy,
'ro': rough_outcome_strategy,
'mr': recursive_minimax,
'mi': iterative_minimax}
class GameInterface:
"""
A game interface for a two-player, sequential move, zero-sum,
perfect-information game.
"""
def __init__(self, game: Any, p1_strategy: Callable,
p2_strategy: Callable[[Any], Any]) -> None:
"""
Initialize this GameInterface, setting its active game to game, and
using the strategies p1_strategy for Player 1 and p2_strategy for
Player 2.
:param game: The game to be played.
:type game:
:param p1_strategy: The strategy for Player 1.
:type p1_strategy:
:param p2_strategy: The strategy for Play 2.
:type p2_strategy:
"""
first_player = input("Type y if player 1 is to make the first move: ")
is_p1_turn = False
if first_player.lower() == 'y':
is_p1_turn = True
self.game = game(is_p1_turn)
self.p1_strategy = p1_strategy
self.p2_strategy = p2_strategy
def play(self) -> None:
"""
Play the game.
"""
current_state = self.game.current_state
print(self.game.get_instructions())
print(current_state)
# Pick moves until the game is over
while not self.game.is_over(current_state):
move_to_make = None
# Print out all of the valid moves
possible_moves = current_state.get_possible_moves()
print("The current available moves are:")
for move in possible_moves:
print(move)
# Pick a (legal) move.
while not current_state.is_valid_move(move_to_make):
current_strategy = self.p2_strategy
if current_state.get_current_player_name() == 'p1':
current_strategy = self.p1_strategy
move_to_make = current_strategy(self.game)
# Apply the move
current_player_name = current_state.get_current_player_name()
new_game_state = current_state.make_move(move_to_make)
self.game.current_state = new_game_state
current_state = self.game.current_state
print("{} made the move {}. The game's state is now:".format(
current_player_name, move_to_make))
print(current_state)
# Print out the winner of the game
if self.game.is_winner("p1"):
print("Player 1 is the winner!")
elif self.game.is_winner("p2"):
print("Player 2 is the winner!")
else:
print("It's a tie!")
if __name__ == '__main__':
games = ", ".join(["'{}': {}".format(key, playable_games[key].__name__) if
playable_games[key] is not None else
"'{}': None".format(key) for key in playable_games])
strategies = ", ".join(["'{}': {}".format(key,
usable_strategies[key].__name__)
if usable_strategies[key] is not None else
"'{}': None".format(key)
for key in usable_strategies])
chosen_game = ''
while chosen_game not in playable_games.keys():
chosen_game = input(
"Select the game you want to play ({}): ".format(games))
p1 = ''
p2 = ''
while p1 not in usable_strategies.keys():
p1 = input("Select the strategy for Player 1 ({}): ".format(strategies))
while p2 not in usable_strategies.keys():
p2 = input("Select the strategy for Player 2 ({}): ".format(strategies))
GameInterface(playable_games[chosen_game], usable_strategies[p1],
usable_strategies[p2]).play()