def test2(): player1 = AlphaBetaPlayer() player2 = AlphaBetaPlayer() game = Board(player1, player2, 9, 9) game._board_state = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 31, 22] #print(player1.get_move(game, 6)) moves = game.get_legal_moves() for m in moves: fm = game.forecast_move(m).get_legal_moves() print (str(m) + " -->" + str(fm) + str(player1.score(game.forecast_move(m),player1))) print (player1.get_move(game, 6))
def test_alphabeta_valid(self): test_start = self.time_millis() time_left = lambda: 1000 - (self.time_millis() - test_start) alphabeta = AlphaBetaPlayer() board = Board(alphabeta, RandomPlayer()) # Play two moves to make legal moves array much smaller board.apply_move(random.choice(board.get_legal_moves())) board.apply_move(random.choice(board.get_legal_moves())) self.assertIn(alphabeta.get_move(board, time_left), board.get_legal_moves(alphabeta))
# print(game.get_legal_moves()) # print(len(game.get_legal_moves())) score = custom_score_2(game, player1) time_millis = lambda: 1000 * timeit.default_timer() move_start = time_millis() time_left = lambda: 100 - (time_millis() - move_start) # next_move = player1.get_move(game, time_left) # print(next_move) failed_test_case_7 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 51, 23] game1 = Board(player1, player2, 9, 9) game1._board_state = failed_test_case_7 next_move = player1.get_move(game1, time_left) print(next_move) print(game1.get_legal_moves())
from importlib import reload 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) if __name__ == '__main__': from isolation import Board from game_agent import MinimaxPlayer from game_agent import AlphaBetaPlayer player1 = AlphaBetaPlayer() player2 = MinimaxPlayer() game = Board(player1, player2) game.apply_move((3, 3)) game.apply_move((0, 1)) game.apply_move((4, 5)) game.apply_move((1, 3)) print(game.to_string()) assert (player1 == game.active_player) print(game.get_legal_moves()) print(player1.get_move(game, 15))