def test_should_select_action_to_let_player_survive_next_two_rounds(self): game = self.data_loader.load(tests.read_test_file("ai/game_4.json")) result = Value('i') self.sut.create_next_action(game, result) self.assertEqual(Action.turn_left, Action.get_by_index(result.value))
def test_should_select_action_to_let_player_survive_next_two_rounds_2(self): game = self.data_loader.load(tests.read_test_file("ai/game_3.json")) sut = SearchTreeAI(game.you, 2, 2) result = Value('i') sut.create_next_action(game, result) self.assertEqual(Action.slow_down, Action.get_by_index(result.value))
def test_should_select_action_to_let_player_survive_next_round(self): game = self.data_loader.load(tests.read_test_file("ai/game_1.json")) sut = SearchTreeAI(game.you, 1, 3, True) result = Value('i') sut.create_next_action(game, result) self.assertEqual(Action.turn_right, Action.get_by_index(result.value))
def test_create_action_should_return_action_with_best_connection(self): player1 = Player(1, 0, 0, Direction.down, 1, True, "") player2 = Player(2, 0, 2, Direction.down, 3, True, "") players = [player1, player2] cells = [[Cell([player1]), Cell(), Cell()], [Cell(), Cell([player2]), Cell()], [Cell([player2]), Cell(), Cell()]] game = Game(3, 3, cells, players, 2, True, datetime.now()) sut = PathfindingAI(player1, 2, 10) result = Value('i') sut.create_next_action(game, result) self.assertEqual(Action.turn_left, Action.get_by_index(result.value))
def __choose_ai_action(self, ai: ArtificialIntelligence, time_to_react: int) -> Action: return_value = multiprocessing.Value('i', Action.get_default().get_index()) process = multiprocessing.Process(target=Controller.call_ai, args=(ai, self._game.copy(), return_value,)) start = datetime.now(time_zone) process.start() process.join(time_to_react - 1) # wait time_to_react seconds minus one for the calculation to be finished if process.is_alive(): # If an execution is terminated, the execution time is set to 1 minute. start = datetime.now(time_zone) - timedelta(seconds=60) process.terminate() self._log_execution_time(ai, (datetime.now(time_zone) - start).total_seconds()) return Action.get_by_index(return_value.value)
def __choose_action(self, game: Game, time_zone: datetime.tzinfo) -> Action: return_value = multiprocessing.Value('i') self.__default_ai.create_next_action(game, return_value) own_time = datetime.now(time_zone) seconds_for_calculation = (game.deadline - own_time).seconds process = multiprocessing.Process(target=Controller.call_ai, args=( self.__ai, game, return_value, )) process.start() process.join(seconds_for_calculation - 1) if process.is_alive(): process.terminate() return Action.get_by_index(return_value.value)