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

Skip to content

Fixed bugs in games.py #380

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 18 additions & 13 deletions games.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +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)
Expand All @@ -153,18 +157,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

Expand Down Expand Up @@ -204,6 +196,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):
Expand Down Expand Up @@ -255,7 +258,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'),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll accept this for now, but in general, this isn't right: the to_move need not be 'O' and 'X' in other games.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I agree completely. I wondered the same thing. There were two reasons I chose such an implementation:

  1. this result function is specific to the TicTacToe class, which inherits from Game - and thus won't apply to other Game classes. The TicTacToe class specifically mentions that the only valid states are 'X' and 'O' here.
  2. the same approach had already been used in the function, in line 268 below.

However, I would be happy to discuss alternative implementations. Maybe we could have a turn function defined in the Game class itself, which could then be appropriately overridden in all subclasses (including TicTacTow), thus effectively generalising this part of the code.

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)
Expand Down
6 changes: 3 additions & 3 deletions tests/test_games.py
Original file line number Diff line number Diff line change
Expand Up @@ -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__':
Expand Down