Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2b07ba9

Browse files
kaivalyarnorvig
authored andcommitted
Fixed bugs in games.py (#380)
* move play_game into games class * display current state before prompting for action * fixed player swap bug * display available moves to human players * make tests pass
1 parent 2922ab6 commit 2b07ba9

File tree

2 files changed

+21
-16
lines changed

2 files changed

+21
-16
lines changed

games.py

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,10 @@ def min_value(state, alpha, beta, depth):
136136

137137
def query_player(game, state):
138138
"""Make a move by querying standard input."""
139+
print("current state:")
140+
game.display(state)
141+
print("available moves: {}".format(game.actions(state)))
142+
print("")
139143
move_string = input('Your move? ')
140144
try:
141145
move = eval(move_string)
@@ -153,18 +157,6 @@ def alphabeta_player(game, state):
153157
return alphabeta_full_search(state, game)
154158

155159

156-
def play_game(game, *players):
157-
"""Play an n-person, move-alternating game."""
158-
159-
state = game.initial
160-
while True:
161-
for player in players:
162-
move = player(game, state)
163-
state = game.result(state, move)
164-
if game.terminal_test(state):
165-
game.display(state)
166-
return game.utility(state, game.to_move(game.initial))
167-
168160
# ______________________________________________________________________________
169161
# Some Sample Games
170162

@@ -204,6 +196,17 @@ def display(self, state):
204196

205197
def __repr__(self):
206198
return '<{}>'.format(self.__class__.__name__)
199+
200+
def play_game(self, *players):
201+
"""Play an n-person, move-alternating game."""
202+
state = self.initial
203+
while True:
204+
for player in players:
205+
move = player(self, state)
206+
state = self.result(state, move)
207+
if self.terminal_test(state):
208+
self.display(state)
209+
return self.utility(state, self.to_move(self.initial))
207210

208211

209212
class Fig52Game(Game):
@@ -255,7 +258,9 @@ def actions(self, state):
255258

256259
def result(self, state, move):
257260
if move not in state.moves:
258-
return state # Illegal move has no effect
261+
return GameState(to_move=('O' if state.to_move == 'X' else 'X'),
262+
utility=self.compute_utility(state.board, move, state.to_move),
263+
board=state.board, moves=state.moves) # Illegal move has no effect
259264
board = state.board.copy()
260265
board[move] = state.to_move
261266
moves = list(state.moves)

tests/test_games.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,13 @@ def test_alphabeta_full_search():
6060

6161

6262
def test_random_tests():
63-
assert play_game(Fig52Game(), alphabeta_player, alphabeta_player) == 3
63+
assert Fig52Game().play_game(alphabeta_player, alphabeta_player) == 3
6464

6565
# The player 'X' (one who plays first) in TicTacToe never loses:
66-
assert play_game(ttt, alphabeta_player, alphabeta_player) >= 0
66+
assert ttt.play_game(alphabeta_player, alphabeta_player) >= 0
6767

6868
# The player 'X' (one who plays first) in TicTacToe never loses:
69-
assert play_game(ttt, alphabeta_player, random_player) >= 0
69+
assert ttt.play_game(alphabeta_player, random_player) >= 0
7070

7171

7272
if __name__ == '__main__':

0 commit comments

Comments
 (0)