def main(): battalion_processor = BattalionProcessor(2, horses=1, elephants=2, armoured_tanks=3, sling_guns=4) battalion_processor.add_battalion_substituation(BattalionType.ELEPHANTS, BattalionType.HORSES, 2) battalion_processor.add_battalion_substituation( BattalionType.ARMOUREDTANKS, BattalionType.ELEPHANTS, 2) battalion_processor.add_battalion_substituation( BattalionType.SLINGGUNS, BattalionType.ARMOUREDTANKS, 2) lengaburu_army = Army(horses=100, elephants=50, armoured_tanks=10, sling_guns=5) with open("input.txt", "r") as testcases: for testcase in testcases: input = [int(x.strip()) for x in testcase.split(",")] enemy_army = Army(horses=input[0], elephants=input[1], armoured_tanks=input[2], sling_guns=input[3]) planner = BattlePlanner(battalion_processor, deepcopy(lengaburu_army)) result, army = planner.get_winning_army(enemy_army) display_result(input, result, army)
def test__str__(self): sold = "Soldier's life = 3 and experience = 0" arch = "Archer's life = 3 and experience = 0" cav = "Cavalry's life = 4 and experience = 0" t1 = Army() t2 = Army() t3 = Army() # Test if the string representation of the army matches expected output for low unit values t1._Army__assign_army("t1", 1, 1, 1, 0) t2._Army__assign_army("t2", 0, 1, 0, 0) t3._Army__assign_army("t1", 0, 0, 0, 0) try: self.assertEqual(str(t1.force), sold + "," + arch + "," + cav, msg="String test 1,1,1 failed") except AssertionError as e: self.verificationErrors.append(str(e)) # __str__ tests : testing 0 1 0 try: self.assertEqual(str(t2.force), "Archer's life = 3 and experience = 0", msg="str method failed") except AssertionError as e: self.verificationErrors.append(str(e)) # __str__ tests : testing 0 0 0 try: self.assertEqual(str(t3.force), "", msg="str method failed") except AssertionError as e: self.verificationErrors.append(str(e))
def come(self,player,army): if player == self.player: self army += army else: (newplayer,newarmy) = battle(self.player,Army(self.army),player,Army(army)) self.army = newarmy if newplayer != self.player: self.buildtype = None self.buildtime = 0 self.player =newplayer
def gladiatorial_combat(self, player_one: str, player_two: str) -> int: """ This method reads and creates an army for each player, say army1 and army1, sets them in stack formation @complexity: Best O(n) and worst O(n) """ FORMATION = 0 # constant army_1 = Army() # creating an instance of Army army_1.choose_army(player_one, FORMATION) # creating player stack army_2 = Army() # creating an instance of Army army_2.choose_army(player_two, FORMATION) # creating player stack return self.__conduct_combat(army_1, army_2, FORMATION)
def test_get_winning_army_3(self): horses, elephants, armoured_tanks, sling_guns = 100, 50, 10, 5 our_army = Army(horses=horses, elephants=elephants, armoured_tanks=armoured_tanks, sling_guns=sling_guns) enemy_army = Army(horses=600, elephants=100) horses_order, elephants_order, armoured_tanks_order, sling_guns_order = 1, 2, 3, 4 bp = BattalionProcessor(4, horses=horses_order, elephants=elephants_order, armoured_tanks=armoured_tanks_order, sling_guns=sling_guns_order) bp.add_battalion_substituation(BattalionType.ELEPHANTS, BattalionType.HORSES, 1) battle_result, winning_army = BattlePlanner(bp, deepcopy(our_army)).get_winning_army(deepcopy(enemy_army)) assert battle_result == False assert winning_army == {BattalionType.HORSES: 100, BattalionType.ELEPHANTS: 50}
def fairer_combat(self, player_one: str, player_two: str) -> int: """ Conducts a battle between two armies in queue formation and whenever a fighter survives, it gets appended at the end of the queue and at the end it return the winner from the conduct_combat method @complexity: Best O(n) and worst O(n) """ FORMATION = 1 army_1 = Army() # creating an instance of Army army_1.choose_army(player_one, FORMATION) # creating player stack army_2 = Army() # creating an instance of Army army_2.choose_army(player_two, FORMATION) # creating player stack return self.__conduct_combat(army_1, army_2, FORMATION)
def test__correct_army_given(self): t1 = Army() # Test if a (low) valid combination of unit values is accepted try: self.assertTrue(t1._Army__correct_army_given(1, 1, 1), msg="Stack test 1,1,1 failed") except AssertionError as e: self.verificationErrors.append(str(e)) # put your __correct_army tests here # simple test try: self.assertEqual(t1._Army__correct_army_given(1, 1, 1), True, msg="correct_army method failed") except AssertionError as e: self.verificationErrors.append(str(e)) # Testing for negative inputs try: self.assertEqual(t1._Army__correct_army_given(-1, -1, 1), False, msg="correct_army method failed") except AssertionError as e: self.verificationErrors.append(str(e)) # testing for over the budget inputs try: self.assertEqual(t1._Army__correct_army_given(21, 41, 1), False, msg="correct_army method failed") except AssertionError as e: self.verificationErrors.append(str(e))
def craete_class_from_json(file): armies = [] with open(file, 'r') as f: data = json.load(f) for army in data['armies']: squads = [] for squad in army['squads']: if squad['type'] == 'soldiers': units = [] for unit in squad['units']: unit = Soldier(health=unit['health'], recharge=unit['recharge']) units.append(unit) squads.append(Squad(squad['type'], units)) elif squad['type'] == 'vehicles': units = [] for unit in squad['units']: operators = [] for operator in unit['operators']: operators.append( Soldier(operator['health'], operator['recharge'])) units.append( Vehicle(unit['health'], unit['recharge'], operators)) squads.append(Squad(squad['type'], units)) army = Army(army['name'], army['strategy'], squads) armies.append(army) return armies
def play_select(): if channel_0_for_background_sound.get_volume() == 0 and sounds_volume != 0 and not music_muted: channel_0_for_background_sound.set_volume(sounds_volume) map_test = Map(box_size=20, nb_river=19, nb_mountain=35, nb_forest=61, nb_desert=35) points = random.randint(200, 500) left_army = Army(map_test, False, points, in_simulation=True) while run: for event in pygame.event.get(): main_events(event) screen.fill(BLACK) screen.blit(background_play_select, background_play_select_rect) if music_muted: screen.blit(sound_mute, (0, 0), (-940, -40, 1024, 768)) button("", 929, 37, 57, 57, BLACK_TRANSPARENT, "circle", unmute_music) else: screen.blit(sound_speaking, (0, 0), (-940, -40, 1024, 768)) button("", 929, 37, 57, 57, BLACK_TRANSPARENT, "circle", mute_music) display_army(left_army, 290, -140) button("", 482, 654, 55, 55, BLACK_TRANSPARENT, "circle", menu) button("OPTIMIZE", 87, 225, 281, 78, RED_TRANSPARENT, "box", play_opti, action_argument={'map_test': map_test, 'left_army': left_army, 'points': points}) button("CREATE", 87, 438, 281, 78, RED_TRANSPARENT, "box", play_create, action_argument={'map_test': map_test, 'left_army': left_army, 'points': points}) pygame.display.update() clock.tick(FPS)
def test___conduct_combat(self): t1 = Army() t2 = Army() battle = Battle() formation = 0 # Test if combat is conducted correctly and returns appropriate result for empty p1 army and all Archer p2 army # Assumes __assign_army is working correctly t1._Army__assign_army("", 0, 0, 0, formation) t2._Army__assign_army("", 0, 10, 0, formation) try: self.assertTrue( battle._Battle__conduct_combat(t1, t2, formation) == 2, "Gladiatorial 0,0,0 0,10,0 failed") except AssertionError as e: self.verificationErrors.append(str(e))
def crossover(armies, points, population, mapping): """ Permet de croiser 2 parents. Paramètres : armies : liste contenant tous les objets Army (armées alliées déjà créées) points : entier représentant les fonds disponibles pour créer l'armée population : entier représentant le nombre d'armée alliée à tester pour une génération mapping : objet Map (from mapping import Map) représentant la carte de combat Return : armies : liste contenant les objets Army après croisement """ offspring = [] for _ in range(int(population - len(armies))): parent1, parent2 = None, None while parent1 == parent2: parent1 = choice(armies) parent2 = choice(armies) army_parent1, points_used_parent1 = parent1_choice(parent1, points) army_parent2 = parent2_choice(parent2, points_used_parent1, parent1) child1 = army_parent1 + army_parent2 offspring.append( Army(Map(other_map=mapping), True, points, in_simulation=True, army_base=child1)) armies.extend(offspring) return armies
def main(): characters_list = [] armies_list = [] total_moral = 0 armies_moral = np.zeros(5) chiefs_moral = np.zeros(5) with open('characters.csv') as csv_file: csv_reader = csv.reader(csv_file, delimiter=',') line_count = 0 for line in csv_reader: if line_count != 0: new_character = Character(line[0], line[1], line[2], line[3], line[4]) characters_list.append(new_character) chiefs_moral[line_count - 1] = new_character.moral_boost line_count += 1 i = 0 for character in characters_list: new_army = Army(character, random.uniform(20, 100)) armies_list.append(new_army) army_total_moral = new_army.get_total_moral() print(army_total_moral) total_moral += army_total_moral armies_moral[i] = new_army.moral_base i += 1 print("Moral : {}".format(total_moral)) total_moral_numpy = np.dot(armies_moral, chiefs_moral) print("Moral NumPy : {}".format(total_moral_numpy))
def test_change_battalion_strength(self): horses, elephants, armoured_tanks, sling_guns = 100, 50, 10, 5 army = Army(horses=horses, elephants=elephants, armoured_tanks=armoured_tanks, sling_guns=sling_guns) army.update_battalion_strength(BattalionType.HORSES, -50) army.update_battalion_strength(BattalionType.SLINGGUNS, 5) assert army.get_battalion_strength(BattalionType.HORSES) == 50 assert army.get_battalion_strength(BattalionType.SLINGGUNS) == 10
def test_get_battalions(self): horses, elephants, armoured_tanks, sling_guns = 100, 50, 10, 5 army = Army(horses=horses, elephants=elephants, armoured_tanks=armoured_tanks, sling_guns=sling_guns) assert set(army.get_battalions()) == set([BattalionType.HORSES, BattalionType.ELEPHANTS, BattalionType.SLINGGUNS, BattalionType.ARMOUREDTANKS])
def gladiatorial_combat(self, player_one: str, player_two: str) -> int: '''Sets up two players and creates their desired enemies. Returns the value of the victor. Only uses Stack ADT formation. 0 - Draw 1 - Player 1 wins 2 - Player 2 wins :complexity: Best O(1) occurs when the __conduct_combat method returns the winner, due to one army having no fighters. Worst O(Army*Life), where Army represents the length of the army with the smallest number of fighters and Life represents the life of each figher, and this occurs when the __conduct_combat method iterates through the entire armies. ''' p1 = Army() p1.choose_army(str(player_one), 0) p2 = Army() p2.choose_army(str(player_two), 0) return self.__conduct_combat(p1, p2, 0)
def __init__(self, log_type, file=None): self.armies = [ Army(army["id"], army["chosen_strategy"]) for army in armies_config ] self.battles_counter = 0 self.report = {"introduction": "", "battles": [], "conclusion": ""} self.log_type = log_type self.file = file
def test_army_init(self): horses, elephants, armoured_tanks, sling_guns = 100, 50, 10, 5 army = Army(horses=horses, elephants=elephants, armoured_tanks=armoured_tanks, sling_guns=sling_guns) assert army.get_battalion_strength(BattalionType.HORSES) == horses assert army.get_battalion_strength(BattalionType.ELEPHANTS) == elephants assert army.get_battalion_strength(BattalionType.ARMOUREDTANKS) == armoured_tanks assert army.get_battalion_strength(BattalionType.SLINGGUNS) == sling_guns
def test_get_winning_army_1(self): horses, elephants, armoured_tanks, sling_guns = 100, 50, 10, 5 our_army = Army(horses=horses, elephants=elephants, armoured_tanks=armoured_tanks, sling_guns=sling_guns) enemy_army = Army(horses=200, elephants=100, armoured_tanks=20, sling_guns=10) horses_order, elephants_order, armoured_tanks_order, sling_guns_order = 1, 2, 3, 4 bp = BattalionProcessor(2, horses=horses_order, elephants=elephants_order, armoured_tanks=armoured_tanks_order, sling_guns=sling_guns_order) battle_result, winning_army = BattlePlanner(bp, deepcopy(our_army)).get_winning_army(deepcopy(enemy_army)) assert battle_result == True assert winning_army == {BattalionType.HORSES: 100, BattalionType.ELEPHANTS: 50, BattalionType.ARMOUREDTANKS: 10, BattalionType.SLINGGUNS: 5}
def create_army(self, strat: str, count_of_soldiers_squads, count_of_vehicle_squads): squads = list() for _ in range(count_of_soldiers_squads): squads.append(self.create_squad(Soldier, 8)) for _ in range(count_of_vehicle_squads): squads.append(self.create_squad(Vehicle, 5, 3)) return Army(strategy[strat], squads)
def test__correct_army_given(self): t1 = Army() # Test if a (low) valid combination of unit values is accepted try: self.assertTrue(t1._Army__correct_army_given(1, 1, 1), msg="Stack test 1,1,1 failed") except AssertionError as e: self.verificationErrors.append(str(e))
def test_has_battalion(self): horses, elephants, armoured_tanks, sling_guns = 100, 50, 10, 5 army = Army(horses=horses, elephants=elephants, armoured_tanks=armoured_tanks) assert army.has_battalion(BattalionType.HORSES) == True assert army.has_battalion(BattalionType.ELEPHANTS) == True assert army.has_battalion(BattalionType.ARMOUREDTANKS) == True assert army.has_battalion(BattalionType.SLINGGUNS) == False assert army.has_battalion("NonExistantBattalion") == False
def test_print_standard_output_method_prints_in_the_expected_format(self): with patch.dict(army_data, test_army_data, clear=True): counter_attack = OrderedDict([('TBI', 40), ('TBII', 16)]) self.test_army = Army('armytwo') self.test_army.prepare_battalions(counter_attack) output = WarResultOutputter(self.test_army) with patch('sys.stdout', new=StringIO()) as fakeOutput: output.print_standard_output() self.assertEqual(fakeOutput.getvalue().strip(), 'Armytwo deploys 10 TBI, 5 TBII and loses')
def test__conduct_combat(self): t1 = Army() t2 = Army() battle = Battle() formation = 1 # Test if combat is conducted correctly and if it returns # appropriate result for all Archer p1 army and empty p2 army # Assumes __assign_army is working correctly t1._Army__assign_army("", 0, 10, 0, formation) t2._Army__assign_army("", 0, 0, 0, formation) try: self.assertTrue( battle._Battle__conduct_combat(t1, t2, formation) == 1, "Fairer 0,10,0 0,0,0 failed") except AssertionError as e: self.verificationErrors.append(str(e)) # Tests combat is conducted correctly and if it # returns appropriate result for 1 Soldier p1 army and 1 Archer p2 army # Assumes __assign_army is working correctly t1._Army__assign_army("", 1, 0, 0, formation) t2._Army__assign_army("", 0, 1, 0, formation) try: self.assertTrue( battle._Battle__conduct_combat(t1, t2, formation) == 2, "Fairer 1,0,0 0,1,0 failed") except AssertionError as e: self.verificationErrors.append(str(e)) try: self.assertEqual(str(t1.force), "", msg="Army 1 wrong for Fairer 1,0,0 0,1,0") except AssertionError as e: self.verificationErrors.append(str(e)) try: self.assertEqual(str(t2.force), "Archer's life = 1 and experience = 1", msg="Army 2 wrong for Fairer 1,0,0 0") except AssertionError as e: self.verificationErrors.append(str(e))
def test_resolve_with_similar_battalion(self): horses, elephants, armoured_tanks, sling_guns = 100, 50, 10, 5 army = Army(horses=horses, elephants=elephants, armoured_tanks=armoured_tanks, sling_guns=sling_guns) horses, elephants, armoured_tanks, sling_guns = 1, 2, 3, 4 bp = BattalionProcessor(horses=horses, elephants=elephants, armoured_tanks=armoured_tanks, sling_guns=sling_guns) planner = BattlePlanner(bp, army) assert planner.resolve_with_similar_battalion(BattalionType.HORSES, 50) == 0 assert planner.resolve_with_similar_battalion(BattalionType.ELEPHANTS, 80) == 30 assert planner.resolve_with_similar_battalion(BattalionType.ARMOUREDTANKS, 20) == 10
def test_resolve_battalion_3(self): horses, elephants, armoured_tanks, sling_guns = 100, 50, 10, 5 our_army = Army(horses=horses, elephants=elephants, armoured_tanks=armoured_tanks, sling_guns=sling_guns) horses, elephants, armoured_tanks, sling_guns = 1, 2, 3, 4 bp = BattalionProcessor(3, horses=horses, elephants=elephants, armoured_tanks=armoured_tanks, sling_guns=sling_guns) bp.add_battalion_substituation(BattalionType.ELEPHANTS, BattalionType.HORSES, 2) bp.add_battalion_substituation(BattalionType.ARMOUREDTANKS, BattalionType.HORSES, 4) assert BattlePlanner(bp, deepcopy(our_army)).resolve_battalion(BattalionType.ARMOUREDTANKS, 30) == True assert BattlePlanner(bp, deepcopy(our_army)).resolve_battalion(BattalionType.ARMOUREDTANKS, 104) == True assert BattlePlanner(bp, deepcopy(our_army)).resolve_battalion(BattalionType.ARMOUREDTANKS, 105) == True assert BattlePlanner(bp, deepcopy(our_army)).resolve_battalion(BattalionType.ARMOUREDTANKS, 106) == False
def init_simu(population, in_simulation, mapping, points): """ Permet de créer les premières armées. Paramètres : population : entier représentant le nombre d'armée alliée à tester pour une génération in_simulation : booléen (False si c'est pas une simulation, True si s'en est une) mapping : objet Map (from mapping import Map) représentant la carte de combat points : entier représentant les fonds disponibles pour créer l'armée """ return [ Army(Map(other_map=mapping), True, points, in_simulation=in_simulation) for _ in range(population) ]
def test_resolve_battalion_4(self): horses, elephants, armoured_tanks, sling_guns = 100, 50, 10, 5 our_army = Army(horses=horses, elephants=elephants, armoured_tanks=armoured_tanks, sling_guns=sling_guns) horses, elephants, armoured_tanks, sling_guns = 1, 2, 3, 4 bp = BattalionProcessor(3, horses=horses, elephants=elephants, armoured_tanks=armoured_tanks, sling_guns=sling_guns) assert BattlePlanner(bp, deepcopy(our_army)).resolve_battalion(BattalionType.ARMOUREDTANKS, 29) == True assert BattlePlanner(bp, deepcopy(our_army)).resolve_battalion(BattalionType.ARMOUREDTANKS, 30) == True assert BattlePlanner(bp, deepcopy(our_army)).resolve_battalion(BattalionType.ARMOUREDTANKS, 31) == False assert BattlePlanner(bp, deepcopy(our_army)).resolve_battalion(BattalionType.ARMOUREDTANKS, 104) == False assert BattlePlanner(bp, deepcopy(our_army)).resolve_battalion(BattalionType.ARMOUREDTANKS, 105) == False assert BattlePlanner(bp, deepcopy(our_army)).resolve_battalion(BattalionType.ARMOUREDTANKS, 106) == False
def test___conduct_combat(self): t1 = Army() t2 = Army() t3 = Army() t4 = Army() battle = Battle() formation = 0 # Test if combat is conducted correctly and returns appropriate result for empty p1 army and all Archer p2 army # Assumes __assign_army is working correctly t1._Army__assign_army("", 0, 0, 0, formation) t2._Army__assign_army("", 0, 10, 0, formation) t3._Army__assign_army("", 0, 4, 0, formation) t4._Army__assign_army("", 0, 0, 0, formation) try: self.assertTrue( battle._Battle__conduct_combat(t1, t2, formation) == 2, "Gladiatorial 0,0,0 0,10,0 failed") except AssertionError as e: self.verificationErrors.append(str(e)) # Checking if t1 is an instance of Army try: self.assertEqual(isinstance(t1, Army), True, msg="Object is not an instance of Army") except AssertionError as e: self.verificationErrors.append(str(e)) # Testing __conduct_combat try: self.assertTrue( battle._Battle__conduct_combat(t3, t4, formation) == 1, "Gladiatorial 0,4,0 0,0,0 failed") except AssertionError as e: self.verificationErrors.append(str(e))
def test_resolve_battalion_1(self): horses, elephants, armoured_tanks, sling_guns = 100, 50, 10, 5 our_army = Army(horses=horses, elephants=elephants, armoured_tanks=armoured_tanks, sling_guns=sling_guns) horses, elephants, armoured_tanks, sling_guns = 1, 2, 3, 4 bp = BattalionProcessor(1, horses=horses, elephants=elephants, armoured_tanks=armoured_tanks, sling_guns=sling_guns) bp.add_battalion_substituation(BattalionType.ELEPHANTS, BattalionType.HORSES, 2) assert BattlePlanner(bp, deepcopy(our_army)).resolve_battalion(BattalionType.HORSES, 199) == True assert BattlePlanner(bp, deepcopy(our_army)).resolve_battalion(BattalionType.HORSES, 200) == True assert BattlePlanner(bp, deepcopy(our_army)).resolve_battalion(BattalionType.HORSES, 201) == False assert BattlePlanner(bp, deepcopy(our_army)).resolve_battalion(BattalionType.HORSES, 150) == True assert BattlePlanner(bp, deepcopy(our_army)).resolve_battalion(BattalionType.HORSES, 100) == True assert BattlePlanner(bp, deepcopy(our_army)).resolve_battalion(BattalionType.HORSES, 5) == True assert BattlePlanner(bp, deepcopy(our_army)).resolve_battalion(BattalionType.HORSES, 250) == False
def __init__(self, mapping, right_army, left_army_base, points, in_simulation=False): Thread.__init__(self) self.right_army = right_army self.in_simulation = in_simulation self.fitness = 0 self._return = None self.left_army = Army(mapping, False, points, in_simulation, army_base=left_army_base)