Example #1
0
    def moving_phase(self, game: Game)-> None:
        '''
        This function will calculate the odds of winning the battle,
        if the chance is greater than the threshold defined, it will attack.



        :param game: Is the object of the game that contains the map, units etc..

        '''
        game.battles = []
        while len(game.movable) > 0:
            if r.random() > 0.01:
                unit = game.movable[0]
                position = unit.get_position()
                to_tile = r.choice(game.map.board[position[0]][position[1]].neighbours)
                if to_tile.owner != game.current_player:
                    all_units = game.find_movable_in_tile(position)
                    attack_score = self.calc_winning_odds(all_units, to_tile.units)
                    if attack_score > self.attack_threshold:
                        for current_unit in all_units:
                            game.move_unit(game.map.board[position[0]][position[1]], to_tile, current_unit)
                else:
                    game.move_unit(game.map.board[position[0]][position[1]], to_tile, unit)
            else:
                break
        game.phase = 2.5
Example #2
0
    def moving_phase(self, game: Game):
        '''
        This function would make the bot always move towards the enemy industry in an attempt to capture it, and then
        'starve' them of units.
        :param game: Is the object of the game that contains the map, units etc..
        :return:
        '''
        game.battles = []
        # Locate enemy industry
        target_tile = None
        for w in game.map.board:
            for h in w:
                if len(h.constructions) > 0 and h.owner.name is not game.current_player.name:
                    target_tile = h

        if target_tile is not None:
            while len(game.movable) > 0:
                unit = game.movable[0]
                position = unit.get_position()
                new_tile = (-1, None)
                possible_tiles = []
                for tile in game.map.board[position[0]][position[1]].neighbours:
                    value = game.calculate_distance_between_tiles(tile, target_tile)
                    if new_tile[0] > value or new_tile[1] is None:
                        new_tile = (value, tile)
                        if value == 0:
                            possible_tiles.append(tile)
                if new_tile[0] == 0:
                    min_units = (-1, None)
                    for tile in possible_tiles:
                        if len(tile.units) < min_units[0] or min_units[1] is None:
                            min_units = (len(tile.units), tile)
                    game.move_unit(game.map.board[position[0]][position[1]], new_tile[1], unit)
                elif new_tile[0] == -1:
                    game.movable.remove(game.movable[0])
                elif new_tile[1] is not None:
                    game.move_unit(game.map.board[position[0]][position[1]], new_tile[1], unit)

        elif target_tile is None:
            while len(game.movable) > 0:
                if r.random() > 0.01:
                    unit = game.movable[0]
                    position = unit.get_position()
                    to_tile = r.choice(game.map.board[position[0]][position[1]].neighbours)
                    if to_tile.owner != game.current_player:
                        all_units = game.find_movable_in_tile(position)
                        attack_score = self.calc_winning_odds(all_units, to_tile.units)
                        if attack_score > 0.15:
                            for current_unit in all_units:
                                game.move_unit(game.map.board[position[0]][position[1]], to_tile, current_unit)
                    else:
                        game.move_unit(game.map.board[position[0]][position[1]], to_tile, unit)
                else:
                    break
        game.phase = 2.5
Example #3
0
    def combat_phase(self, game: Game) -> None:
        '''
        This is the function thas performs the battle. It starts by picking a battle from the list of battles.
        This is done by using the function game.do_battle(battle_cords). (read that doc string if in doubt)
        :param game: Is the object of the game that contains the map, units etc..
        :return:
        '''
        while len(game.battles) > 0:
            results = game.do_battle(game.battles[0])
            attacker = results[0]
            defender = results[1]
            attack_finished = False
            defend_finished = False
            if attacker[1] > 0:
                attacker_count = game.find_unit_count(attacker[0])
                if attacker[1] >= attacker_count:
                    game.take_casualties(attacker[0], 'All', attacker_count)
                    attack_finished = True
                else:
                    game.current_player.bot.prioritize_casualties(
                        game, attacker)
            if defender[1] > 0:
                defender_count = game.find_unit_count(defender[0])
                if defender[1] >= defender_count:
                    game.take_casualties(defender[0], 'All', defender_count)
                    defend_finished = True
                else:
                    defender_keys = list(defender[0].keys())
                    if not defender[0][defender_keys[0]][0].owner.human:
                        defender[0][defender_keys[0]][
                            0].owner.bot.prioritize_casualties(game, defender)
                    else:
                        game.current_player = defender[0][
                            defender_keys[0]][0].owner
                        return defender

            if defend_finished and not attack_finished:
                game.conquer_tile(
                    game.map.board[game.battles[0][0]][game.battles[0][1]],
                    game.current_player)

            if attack_finished or defend_finished:
                game.battles.remove(game.battles[0])
        game.phase = 3
Example #4
0
 def moving_phase(self, game: Game):
     '''
     This is the phase where one is supposed to move units, in friendly and unfriendly territory.
     If the unit is moved to a unfriendly tile, it will become a battle zone.
     The units are moved with the function game.move_unit(from_tile, to_tile, unit)
     :param game: Is the object of the game that contains the map, units etc..
     '''
     game.battles = []
     while len(game.movable) > 0:
         if r.random() > 0.01:
             unit = game.movable[0]
             pos = unit.get_position()
             to_tile = r.choice(game.map.board[pos[0]][pos[1]].neighbours)
             if to_tile.owner != game.current_player:
                 self.moved[to_tile.cords.__str__()] = pos
             game.move_unit(game.map.board[pos[0]][pos[1]], to_tile, unit)
         else:
             break
     game.phase = 2.5