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
def prioritize_casualties(self, game: Game, values): """ This bot will prioritize to delete infs over tanks. """ to_be_deleted = dict() if 'Inf' in values[0]: if len(values[0]['Inf']) >= values[1]: for unit in values[0]['Inf']: if unit.type not in to_be_deleted: to_be_deleted[unit.type] = [] to_be_deleted[unit.type].append(unit) if len(to_be_deleted[unit.type]) == values[1]: break elif len(values[0]['Inf']) < values[1]: for unit in values[0]['Inf']: if unit.type not in to_be_deleted: to_be_deleted[unit.type] = [] to_be_deleted[unit.type].append(unit) diff = values[1] - len(to_be_deleted[unit.type]) for unit in values[0]['Tank']: if unit.type not in to_be_deleted: to_be_deleted[unit.type] = [] to_be_deleted[unit.type].append(unit) if len(to_be_deleted[unit.type]) == diff: break elif 'Tank' in values[0]: for unit in values[0]['Tank']: if unit.type not in to_be_deleted: to_be_deleted[unit.type] = [] to_be_deleted[unit.type].append(unit) if len(to_be_deleted[unit.type]) == values[1]: break for key in to_be_deleted: game.take_casualties(to_be_deleted, to_be_deleted[key][0].type, len(to_be_deleted[key]))
def prioritize_casualties(self, game: Game, values: tuple) -> None: ''' This function is used to randomly pick units to delete after a battle. After the units has been selected they are deleted by the use of game.take_casualties(). :param game: Is the object of the game that contains the map, units etc.. :param values: A tuple with units (dict), hit counter. ''' to_be_deleted = dict() attacker_types = list(values[0].keys()) for i in range(values[1]): unit_type = r.choice(attacker_types) unit = values[0][unit_type][0] if unit.type not in to_be_deleted: to_be_deleted[unit.type] = [] to_be_deleted[unit.type].append(unit) values[0][unit_type].remove(unit) if len(values[0][unit_type]) == 0: values[0].pop(unit_type, None) attacker_types = list(values[0].keys()) for key in to_be_deleted: game.take_casualties(to_be_deleted, to_be_deleted[key][0].type, len(to_be_deleted[key]))