FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fixed bugs in games.py by kaivalyar · Pull Request #380 · aimacode/aima-python · GitHub

Repository navigation

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
31 changes: 18 additions & 13 deletions games.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL