From 32e5efe0e490b47d1d5178c033a86a5d8c97dafe Mon Sep 17 00:00:00 2001 From: Kaivalya Rawal Date: Sun, 19 Mar 2017 04:21:59 +0530 Subject: [PATCH 1/5] move play_game into games class --- games.py | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/games.py b/games.py index f5061f4c8..9ed083f04 100644 --- a/games.py +++ b/games.py @@ -153,18 +153,6 @@ def alphabeta_player(game, state): return alphabeta_full_search(state, game) -def play_game(game, *players): - """Play an n-person, move-alternating game.""" - - state = game.initial - while True: - for player in players: - move = player(game, state) - state = game.result(state, move) - if game.terminal_test(state): - game.display(state) - return game.utility(state, game.to_move(game.initial)) - # ______________________________________________________________________________ # Some Sample Games @@ -204,6 +192,17 @@ def display(self, state): def __repr__(self): return '<{}>'.format(self.__class__.__name__) + + def play_game(self, *players): + """Play an n-person, move-alternating game.""" + state = self.initial + while True: + for player in players: + move = player(self, state) + state = self.result(state, move) + if self.terminal_test(state): + self.display(state) + return self.utility(state, self.to_move(self.initial)) class Fig52Game(Game): From e60f767bd8c6df839ecc1036f4aabf211770321d Mon Sep 17 00:00:00 2001 From: Kaivalya Rawal Date: Sun, 19 Mar 2017 04:22:32 +0530 Subject: [PATCH 2/5] display current state before prompting for action --- games.py | 1 + 1 file changed, 1 insertion(+) diff --git a/games.py b/games.py index 9ed083f04..1e4d42cca 100644 --- a/games.py +++ b/games.py @@ -136,6 +136,7 @@ def min_value(state, alpha, beta, depth): def query_player(game, state): """Make a move by querying standard input.""" + game.display(state) move_string = input('Your move? ') try: move = eval(move_string) From 2f364b82c3a971771d8cf4626c47b33dffee7af6 Mon Sep 17 00:00:00 2001 From: Kaivalya Rawal Date: Sun, 19 Mar 2017 04:30:40 +0530 Subject: [PATCH 3/5] fixed player swap bug --- games.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/games.py b/games.py index 1e4d42cca..dbba2cc05 100644 --- a/games.py +++ b/games.py @@ -255,7 +255,9 @@ def actions(self, state): def result(self, state, move): if move not in state.moves: - return state # Illegal move has no effect + return GameState(to_move=('O' if state.to_move == 'X' else 'X'), + utility=self.compute_utility(state.board, move, state.to_move), + board=state.board, moves=state.moves) # Illegal move has no effect board = state.board.copy() board[move] = state.to_move moves = list(state.moves) From 3a820f2ff9b549299740e02432cecac42935f995 Mon Sep 17 00:00:00 2001 From: Kaivalya Rawal Date: Sun, 19 Mar 2017 04:33:25 +0530 Subject: [PATCH 4/5] display available moves to human players --- games.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/games.py b/games.py index dbba2cc05..d98b7473c 100644 --- a/games.py +++ b/games.py @@ -136,7 +136,10 @@ def min_value(state, alpha, beta, depth): def query_player(game, state): """Make a move by querying standard input.""" + print("current state:") game.display(state) + print("available moves: {}".format(game.actions(state))) + print("") move_string = input('Your move? ') try: move = eval(move_string) From 2e18e77df6c5358976da90a92ea55d7321ea01f8 Mon Sep 17 00:00:00 2001 From: Kaivalya Rawal Date: Tue, 21 Mar 2017 16:31:17 +0530 Subject: [PATCH 5/5] make tests pass --- tests/test_games.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_games.py b/tests/test_games.py index 28644fbc5..35df9c827 100644 --- a/tests/test_games.py +++ b/tests/test_games.py @@ -60,13 +60,13 @@ def test_alphabeta_full_search(): def test_random_tests(): - assert play_game(Fig52Game(), alphabeta_player, alphabeta_player) == 3 + assert Fig52Game().play_game(alphabeta_player, alphabeta_player) == 3 # The player 'X' (one who plays first) in TicTacToe never loses: - assert play_game(ttt, alphabeta_player, alphabeta_player) >= 0 + assert ttt.play_game(alphabeta_player, alphabeta_player) >= 0 # The player 'X' (one who plays first) in TicTacToe never loses: - assert play_game(ttt, alphabeta_player, random_player) >= 0 + assert ttt.play_game(alphabeta_player, random_player) >= 0 if __name__ == '__main__':