Exemple #1
0
    def _create_nth_level_turns(self, prev_lvl_turns):
        nth_lvl_turns = []

        for turn in prev_lvl_turns:
            if turn.is_terminal:
                continue

            for pa in self._get_list_of_possible_actions(turn.game_state):
                turn_cpy = deepcopy(turn)
                turn_cpy.actions.append(pa)

                turn_cpy.game_state = self._transform(turn.game_state, pa)
                # Perform game_state consistency checks
                pl_utils.cleanup_all_dead_minions(turn_cpy.game_state)
                if turn_cpy.game_state.is_terminal_state():
                    turn_cpy.is_terminal = True

                if turn_cpy not in self.turns_set:
                    self.turns_set.add(turn_cpy)
                    nth_lvl_turns.append(turn_cpy)

                # If there is no time left...
                self.current_time = time.time()
                if self._time_limit_exceeded():
                    return nth_lvl_turns

        return nth_lvl_turns
    def play_turn(self, game_state):
        while True:
            possible_actions = pl_utils.get_possible_actions(game_state)

            if possible_actions['no_actions'] or random.random() > 0.5:
                if config.VERBOSE:
                    print(RandomAgent.__name__, 'chose END_TURN')
                break

            pa = (*possible_actions['spell_plays'],
                  *possible_actions['minion_plays'],
                  *possible_actions['minion_puts'])

            chosen_action = random.choice(pa)
            ag_utils.perform_action(RandomAgent, chosen_action)

            pl_utils.cleanup_all_dead_minions(game_state)
    def play_turn(self, game_state):
        config.VERBOSE = True
        while True:
            possible_actions = pl_utils.get_possible_actions(game_state)

            if possible_actions['no_actions']:
                if config.VERBOSE:
                    print(AggressiveAgent.__name__, 'chose END_TURN')
                break

            player, opponent = game_state.get_players()

            # Try to attack enemy hero
            if possible_actions['minion_plays']:
                for pa in possible_actions['minion_plays']:
                    func, args = pa
                    _, target_idx, _ = args
                    if target_idx == -1:  # -1 means opponent hero
                        ag_utils.perform_action(AggressiveAgent, pa)

                    # If enemy died end the turn (and game)
                    if opponent.is_dead():
                        return

            actions = [
                *possible_actions['minion_puts'],
                *possible_actions['spell_plays']
            ]

            # Check field
            if actions:
                if ag_utils.score_field(opponent) > ag_utils.score_field(
                        player):

                    best_action = ag_utils.get_best_action(
                        actions, lambda a: ag_utils.action_to_card(a, player).
                        aggressive_rate)

                    ag_utils.perform_action(AggressiveAgent, best_action)
                    pl_utils.cleanup_all_dead_minions(game_state)
                else:
                    if config.VERBOSE:
                        print(AggressiveAgent.__name__, 'chose END_TURN')
                    break
Exemple #4
0
    def _create_initial_turns(self, game_state):
        turns = []
        for pa in self._get_list_of_possible_actions(game_state):
            turn = Turn()
            turn.actions.append(pa)
            turn.game_state = self._transform(game_state, pa)
            # Perform game_state consistency checks
            pl_utils.cleanup_all_dead_minions(turn.game_state)
            if turn.game_state.is_terminal_state():
                turn.is_terminal = True

            if turn not in self.turns_set:
                self.turns_set.add(turn)
                turns.append(turn)

            # If there is no time left...
            self.current_time = time.time()
            if self._time_limit_exceeded():
                return turns

        return turns
Exemple #5
0
    def play_turn(self, game_state):
        while True:
            # --- TODO REMOVE ---
            from game.gui import gui_preparer

            # Will double the gui output (but will show the state after
            # each action of the RealPlayer)
            print(gui_preparer.prepare_state(game_state))

            from pprint import pprint
            pprint(utils.get_possible_actions(game_state))

            # from mcts.turn import TurnGenerator
            # from copy import deepcopy
            # turns = TurnGenerator().generate_all_turns(deepcopy(game_state))
            # pprint(turns)
            # print(len(turns))

            # --- TODO END REMOVE ---

            action_str = "Player {name}, get one action from listed below:\n" \
                         "0. 'PUT_MINION', 1. 'PLAY_MINION', " \
                         "2. 'PLAY_SPELL', 3. 'END_TURN':"\
                .format(name=self.name)
            action = int(input(action_str))

            if action == 0:  # PUT_MINION
                self._put_minion(game_state)
            elif action == 1:  # PLAY_MINION
                self._play_minion(game_state)
            elif action == 2:  # PLAY_SPELL
                self._play_spell(game_state)
            elif action == 3:  # END_TURN
                break
            else:
                print('Unknown command!')

            utils.cleanup_all_dead_minions(game_state)
            if game_state.is_terminal_state():
                break
    def play_turn(self, game_state):
        config.VERBOSE = True

        # ------------- Check field ----------------
        print('ControllingAgent check field')
        while True:
            possible_actions = pl_utils.get_possible_actions(game_state)

            if possible_actions['no_actions']:
                if config.VERBOSE:
                    print(ControllingAgent.__name__, 'chose END_TURN')
                break

            player, opponent = game_state.get_players()

            minion_minion_actions = []
            for pa in possible_actions['minion_plays']:
                _, args = pa
                _, target_idx, _ = args
                if target_idx != -1:
                    minion_minion_actions.append(pa)

            actions = [
                *possible_actions['minion_puts'],
                *possible_actions['spell_plays'], *minion_minion_actions
            ]

            if actions and ag_utils.score_field(
                    opponent) >= ag_utils.score_field(player):
                best_action = ag_utils.get_best_action(
                    actions, lambda a: ag_utils.action_to_card(a, player).
                    controlling_rate)

                ag_utils.perform_action(ControllingAgent, best_action)
            else:
                if config.VERBOSE:
                    print(ControllingAgent.__name__, 'chose END_TURN')
                break

        # ------------- Attack -----------------
        print('ControllingAgent attack')
        while True:
            possible_actions = pl_utils.get_possible_actions(game_state)

            if possible_actions['no_actions']:
                if config.VERBOSE:
                    print(ControllingAgent.__name__, 'chose END_TURN')
                break

            player, opponent = game_state.get_players()

            actions = possible_actions['minion_plays']

            if actions:
                # Try to attack enemy hero, if no enemy minions
                if not opponent.minions:
                    for pa in actions:
                        func, args = pa
                        _, target_idx, _ = args
                        if target_idx == -1:  # -1 means opponent hero
                            ag_utils.perform_action(ControllingAgent, pa)

                            # If enemy died end the turn (and game)
                            if opponent.is_dead():
                                return

                            break

                # There are enemy minions
                else:
                    for pa in actions:
                        func, args = pa
                        _, target_idx, _ = args
                        if target_idx != -1:
                            ag_utils.perform_action(ControllingAgent, pa)
                            pl_utils.cleanup_all_dead_minions(game_state)
                            break
            else:
                if config.VERBOSE:
                    print(ControllingAgent.__name__, 'chose END_TURN')
                break

        pl_utils.cleanup_all_dead_minions(game_state)