def test_army_unit_type(): army = Army("Dark", 100) type(army.unit[0]) assert type(army.unit[0]) == Orc army = Army("Light", 100) type(army.unit[0]) assert type(army.unit[0]) != Orc
def __init__(self): self.dark_army = Army("Dark", 1000) self.light_army = Army("Light", 1000) self.victory = False self.victor = "" self.count = 0 self.tick_rate = 60 self.screen_title = 'Python Fantasy Battlefield Simulator' self.width = 1000 self.height = 1000 self.game_screen = pygame.display.set_mode((self.width, self.height)) self.clock = pygame.time.Clock() pygame.display.set_caption(self.screen_title)
def test_army_generation_size(): army = Army("Dark", 100) assert army.get_size() == 100 assert type(army.unit[0]) == Orc army.generate_army("Light", 250) assert army.get_size() == 250 assert type(army.unit[0]) != Orc
def load_test(squads_count, units_count, capsys): armies = [ Army(src.CountriesConstants.GREAT_BRITAIN, src.EnglishUnitFactory.EnglishUnitFactory()), Army(src.CountriesConstants.FRANCE, src.FrenchUnitFactory.FrenchUnitFactory()) ] for army in armies: for _ in range(squads_count): squad = Squad(army) for _ in range(units_count): unit = ArmyAssembler.create_random_unit( army.get_unit_factory(), squad) squad.add_unit(unit) army.add_squad(squad) battle = Battle(armies[0], armies[1]) start = time.time() battle.begin_battle() duration = time.time() - start with capsys.disabled(): print('\nBattle with {} squads each with {} units took {}s'.format( squads_count, units_count, duration))
def test_army(): army = Army(src.CountriesConstants.FRANCE, None) assert (army.get_country().ID == 2) assert (army.is_in_battle() is False) assert (isinstance(army.economics_development(), src.ArmyEconomicsDevelopment.ArmyEconomicsDevelopment)) assert (army.get_coins_count() == GameplayParameters.INITIAL_MONEY_COUNT) army.spend_coins(50) army.income(30) assert (army.get_coins_count() == GameplayParameters.INITIAL_MONEY_COUNT - 20) sq1 = FictionSquad() sq2 = FictionSquad() sq2.health = 20 army.add_squad(sq1) army.add_squad(sq2) assert (army.get_health() == 30) assert (army.find_least_healthy_squad().get_health() == 10) assert (army.find_most_healthy_squad().get_health() == 20) army.reset_battle_parameters() assert sq1.reset assert sq2.reset ev1 = EventListener() ev2 = EventListener() army.add_battle_events_subscriber(ev1) army.add_battle_events_subscriber(ev2) army.set_martial_law() assert ev1.battle_begins assert ev2.battle_begins assert army.remove_battle_events_subscriber(ev2) army.remove_martial_law() assert ev1.battle_ends assert not ev2.battle_ends army.set_martial_law() assert army.is_in_battle() is True army.remove_martial_law() assert army.is_in_battle() is False
def init(self): self._english_army = Army(CountriesConstants.GREAT_BRITAIN, EnglishUnitFactory()) self._french_army = Army(CountriesConstants.FRANCE, FrenchUnitFactory()) self._english_army.add_battle_events_subscriber(PrintManager.inst()) self._french_army.add_battle_events_subscriber(PrintManager.inst()) PrintManager.inst().global_info('Armies created')
class Game: _english_army = None _french_army = None def init(self): self._english_army = Army(CountriesConstants.GREAT_BRITAIN, EnglishUnitFactory()) self._french_army = Army(CountriesConstants.FRANCE, FrenchUnitFactory()) self._english_army.add_battle_events_subscriber(PrintManager.inst()) self._french_army.add_battle_events_subscriber(PrintManager.inst()) PrintManager.inst().global_info('Armies created') def run(self): ArmyAssembler.assemble_armies(self._english_army, self._french_army) while True: print('You choose:') print('1 - battle') print('2 - economics upgrade') print('3 - armies status') print('4 - exit') res = input() if res == '3': PrintManager.inst().print_armies(self._english_army, self._french_army) elif res == '2': self._action_economics_upgrade() elif res == '1': battle = Battle(self._english_army, self._french_army) battle.begin_battle() elif res == '4': break else: print('No such action') def _action_economics_upgrade(self): print('Choose army:') print('1 - English') print('2 - French') army_id = input() if army_id not in ('1', '2'): print('There is no such army') return army = self._english_army if army_id == '2': army = self._french_army print('Choose upgrade scheme:') print('1 - upgrade initial health of a squad. {} coins.'.format(economics_params.UPGRADE_HEALTH_PRICE)) print('2 - upgrade initial protection of a squad. {} coins.'.format(economics_params.UPGRADE_PROTECTION_PRICE)) print('3 - make an archer a magic archer. {} coins.'.format(economics_params.UPGRADE_ARCHERS_PRICE)) print('Your army has now {} coins.'.format(army.get_coins_count())) scheme_id = input() if scheme_id not in ('1', '2', '3'): print('Maybe such scheme will be added in future, but not now') return squads = [] archers_in_squads = [] squads_text = '' for squad in army.get_squads(): if scheme_id == '3': has_archers = False for unit in squad.get_units(): if isinstance(unit, Archer): archers_in_squads.append(unit) has_archers = True break if not has_archers: continue squads.append(squad) squads_text += '{} - {}\n'.format(len(squads), str(squad)) if scheme_id == '3' and len(squads) == 0: print('Sorry, there is no archers in selected army') return print('Choose squad:') print(squads_text, end='') squad_id = input() if not str.isdecimal(squad_id): squad_id = 0 else: squad_id = int(squad_id) if squad_id <= 0 or squad_id > len(squads): print('Oh no, bad squad id') return squad = squads[squad_id - 1] result = None if scheme_id == '1': result = army.economics_development().upgrade_health(squad) elif scheme_id == '2': result = army.economics_development().upgrade_protection(squad) elif scheme_id == '3': result = army.economics_development().upgrade_archers(archers_in_squads[squad_id - 1]) if not result: print('Upgrade failure. Check your army money!') else: print('Army upgraded!') print(str(army))
class Simulator: def __init__(self): self.dark_army = Army("Dark", 1000) self.light_army = Army("Light", 1000) self.victory = False self.victor = "" self.count = 0 self.tick_rate = 60 self.screen_title = 'Python Fantasy Battlefield Simulator' self.width = 1000 self.height = 1000 self.game_screen = pygame.display.set_mode((self.width, self.height)) self.clock = pygame.time.Clock() pygame.display.set_caption(self.screen_title) def run_simulation(self): start = time.time() self.count = 0 self.victory = False self.victor = "" if self.get_light_army_size() > 0 and self.get_dark_army_size() > 0: while self.victory is False: self.count += 1 self.game_screen.fill((0, 0, 0)) self.draw_image(self.light_army, self.dark_army) # Light Turn self.victory = self.turn(self.light_army, self.dark_army) if self.victory: self.victor = "Light" break # Dark Turn self.victory = self.turn(self.dark_army, self.light_army) if self.victory: self.victor = "Darkness" break pygame.display.update() self.clock.tick(self.tick_rate) for event in pygame.event.get(): if event.type == pygame.QUIT: self.victory = True else: self.victor = "Army has 0 units" print(time.time() - start) print(str(self.count)) def get_dark_army_size(self) -> int: return self.dark_army.get_size() def get_light_army_size(self) -> int: return self.light_army.get_size() def get_victor(self) -> str: return self.victor def turn(self, attacker, defender): actor = attacker.get_random_actor() enemy = defender.find_nearest_actor(actor) actor.move_actor(enemy) enemy.take_damage(actor.fight(enemy)) victory = defender.check_if_destroyed() return victory def draw_image(self, light, dark): for index in range(light.get_size()): self.game_screen.blit( light.unit[index].playerImage(), (light.unit[index].location[0], light.unit[index].location[1])) for index in range(dark.get_size()): self.game_screen.blit( dark.unit[index].playerImage(), (dark.unit[index].location[0], dark.unit[index].location[1]))
def test_get_size(): army = Army("Dark", 123) assert army.get_size() == 123 and army.get_size() == len(army.unit)
def test_check_if_destroyed(): army = Army("Dark", 1) assert army.check_if_destroyed() is False army.kill_actor(0) assert army.check_if_destroyed() is True
def test_kill_actor(): army = Army("Dark", 2) army.kill_actor(1) assert army.get_size() == 1
def test_find_nearest_actor(): army = Army("Dark", 1) enemy = Army("Light", 1) assert army.find_nearest_actor(enemy.unit[0]) == army.unit[0]
def test_get_random_actor(): army = Army("Dark", 100) assert type(army.get_random_actor()) == Orc
def test_army_to_string(): army = Army("Dark", 100) assert army.to_string().__contains__(army.unit[0].to_string()) and army.to_string().__contains__( army.unit[99].to_string())
def test_economics_proxy(): army = Army(src.CountriesConstants.GREAT_BRITAIN, None) army.income(2 * (GameplayParameters.UPGRADE_HEALTH_PRICE + GameplayParameters.UPGRADE_ARCHERS_PRICE + GameplayParameters.UPGRADE_PROTECTION_PRICE)) economy = army.economics_development() sq = FictionSquad() # Health assert economy.upgrade_health(sq) assert sq.updated sq.updated = False army.set_martial_law() assert not economy.upgrade_health(sq) assert not sq.updated army.remove_martial_law() # Protection assert economy.upgrade_protection(sq) assert sq.updated sq.updated = False army.set_martial_law() assert not economy.upgrade_protection(sq) assert not sq.updated army.remove_martial_law() # Archer arch = FictionArcher() assert economy.upgrade_archers(arch) assert arch.get_squad().updated arch.get_squad().updated = False army.set_martial_law() assert not economy.upgrade_archers(arch) assert not arch.get_squad().updated army.remove_martial_law()