def play_game(): content = request.get_json(silent=True) move_history = content['move_history'] # create an isolation board (by default 7x7) player1 = RandomPlayer() player2 = AlphaBetaPlayer(score_fn=custom_score_3) game = Board(player1, player2) assert (player1 == game.active_player) for move in move_history: game.apply_move(move) isWinner = '' if game.is_winner(player1): isWinner = 'White Knight' if game.is_winner(player2): isWinner = 'Black Knight' if isWinner: response = jsonify({ 'legal_moves': game.get_legal_moves(), 'player_position': list(player_position), 'ai_position': list(ai_position), 'move_history': move_history, 'isWinner': isWinner }) player2.time_left = time_left ai_move = player2.alphabeta(game, 6) game.apply_move(ai_move) player_position = game.get_player_location(player1) move_history.append(ai_move) ai_position = game.get_player_location(player2) if game.is_winner(player1): isWinner = 'White Knight' if game.is_winner(player2): isWinner = 'Black Knight' response = jsonify({ 'legal_moves': game.get_legal_moves(), 'player_position': list(player_position), 'ai_position': list(ai_position), 'move_history': move_history, 'isWinner': isWinner }) # place player 1 on the board at row 2, column 3, then place player 2 on # the board at row 0, column 5; display the resulting board state. Note # that the .apply_move() method changes the calling object in-place. return response
class IsolationTest(unittest.TestCase): """Unit tests for isolation agents""" def setUp(self): reload(game_agent) self.player1 = "Player1" self.player2 = "Player2" self.game = isolation.Board(self.player1, self.player2) def test_minimax(self): self.game._board_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, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 56, 57 ] self.player1 = AlphaBetaPlayer(2, score_fn=open_move_score) self.player2 = AlphaBetaPlayer(2, score_fn=open_move_score) move = self.player1.alphabeta(self.game, 2)
# -*- coding: utf-8 -*- """ Created on Tue Oct 3 11:28:01 2017 @author: sapereira """ #from game_agent import MinimaxPlayer from game_agent import AlphaBetaPlayer from isolation import Board #import isolation #import game_agent player1 = AlphaBetaPlayer() player2 = AlphaBetaPlayer() game = Board(player1, player2) game.apply_move((2, 3)) game.apply_move((0, 5)) print(game.to_string()) assert (player1 == game.active_player) print(game.get_legal_moves()) player1.terminal_test(game) game.move_count game.play() player1.alphabeta(game, 1)