def move(self, r, c, game_id): reversi = self._conn.reversi games = reversi.games game = games.find_one({"game": game_id}) if game['state'] == 'game_over': return True, 'game_over' board = Board(game[u'board']) board = board.move(r, c, 1) games.update({'_id': game['_id']}, {"$set": {'board': board._internal_state}, '$push': {'moves': (r, c, 1)}}, upsert=False) if not board.has_moves(2): self.game_over(game_id) return True, 'game_over' ai = ReversiSimpleAI() x, y = ai.get_best_move(board, 2) board = board.move(x, y, 2) games.update({'_id': game['_id']}, {"$set": {'board': board._internal_state}, '$push': {'moves': (x, y, 2)}}, upsert=False) if not board.has_moves(1): self.game_over(game_id) return True, 'game_over' return True, 'next_step'
from AI import ReversiAI, RandomMoveAI initial_state = [[0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 2, 0, 0, 0], [0, 0, 0, 2, 1, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0, 0]] board = Board(initial_state) ai = RandomMoveAI() current_player = 1 steps = 0 while board.has_moves(current_player): x, y = ai.get_best_move(board, current_player, Depth=4 - current_player) print('move:', steps, 'player:', current_player, 'action', (x, y)) board = board.move(x, y, current_player) board.pprint() steps += 1 # take a next player current_player = current_player % 2 + 1 print 'The End', 'SCORE 1:', board.score(1),'SCORE 2:', board.score(2)