def game(): cost = int(input("Стоимость армии: ")) army_1 = army.Army(cost, "🔴 Красные", possible_units) army_2 = army.Army(cost, "🔵 Синие", possible_units) print(army_1) print(army_2) sides = [army_1, army_2] for i in range(1, 1 << 31 - 1): s = [f"\nХод №{i}"] actions = [] side_1 = random.choice([0, 1]) side_2 = abs(side_1 - 1) friends, enemies = sides[side_1], sides[side_2] if len(friends) > 0 and len(enemies) > 0: actions.append(friends.attack(enemies)) special_action_string = friends.trigger_special_actions(enemies) if special_action_string != "": actions.append(special_action_string) if len(friends) > 0 and len(enemies) > 0: actions.append(enemies.attack(friends)) if len(friends) == 0 or len(enemies) == 0: break actions = filter(lambda x: x != "", actions) s.append("🔫 Действия") s.extend(actions) s.append("📊 Состояние армий") s.append(str(army_1)) s.append(str(army_2)) game_snapshots.append("\n".join(s)) i = 0 print(game_snapshots[i]) while True: inp = input("вперёд/назад/конец/выйти (в/н/к/е): ") if inp == "в": if i < len(game_snapshots) - 1: i += 1 else: print(game_snapshots[i]) break elif inp == "н" and i > 0: i -= 1 elif inp == "к": i = len(game_snapshots) - 1 elif inp == "е": break print(game_snapshots[i]) for s in sides: if len(s) > 0: print(f"\nАрмия {s} выиграла")
def setUp(self): self.unit1 = army.Warrior() self.unit2 = army.Vampire() self.unit3 = army.Healer() self.unit4 = army.Mechnik() self.unit5 = army.Konnica() self.A1 = army.Army(army.Vampire, 10) self.A2 = army.Army(army.Healer, 20) self.B1 = army.Battle(self.A1, self.A2)
def train_armies(self): player = self.current_player tiles_owned_by_player = [] for tile in self.world.values(): if tile.owner == player: tiles_owned_by_player.append(tile) # First apply base growth for tile in tiles_owned_by_player: if not tile.locality: continue if tile.locality.category == "City": if not tile.army: tile.army = army.Army(army.BASE_GROWTH_CITY, 1 / 2 * army.BASE_GROWTH_CITY, tile.owner) elif tile.army.manpower < army.MAX_STACK_SIZE: tile.army.manpower += army.BASE_GROWTH_CITY tile.army.morale += 1 / 2 * army.BASE_GROWTH_CITY elif tile.locality.category == "Capital": if not tile.army: tile.army = army.Army(army.BASE_GROWTH_CAPITAL, 1 / 2 * army.BASE_GROWTH_CAPITAL, tile.owner) elif tile.army.manpower < army.MAX_TRAVEL_DISTANCE: tile.army.manpower += army.BASE_GROWTH_CITY tile.army.morale += 1 / 2 * army.BASE_GROWTH_CITY if tile.army: if tile.army.manpower > army.MAX_STACK_SIZE: tile.army.manpower = army.MAX_STACK_SIZE if tile.army.morale > army.MAX_STACK_SIZE: tile.army.morale = army.MAX_STACK_SIZE # Then apply bonus growth player_bonus_growth = len( tiles_owned_by_player) * army.BONUS_GROWTH_PER_TILE while player_bonus_growth > 0: tiles_with_max_army_stack = 0 for tile in tiles_owned_by_player: if tile.locality and tile.army.manpower < army.MAX_STACK_SIZE: tile.army.manpower += 1 tile.army.morale += 0.5 player_bonus_growth -= 1 else: tiles_with_max_army_stack += 1 # break if we can't apply the bonus anywhere if len(tiles_owned_by_player) == tiles_with_max_army_stack: player_bonus_growth = 0 break # Round morale values for tile in self.world.values(): if tile.army: tile.army.morale = min(army.MAX_STACK_SIZE, tile.army.morale) tile.army.morale = round(tile.army.morale)
def test_init_army(self): """Checking initialization of Army object""" self.army = army.Army(0, "random_squad") self.assertEqual(len(self.army.squads), 4) for sq in self.army.squads: self.assertIsInstance(sq, Squad)
def parse(self): our_army = [] their_army = [] for line in self.simfile: splitted = line.split('->', 1) if len(splitted[0].strip()) > 0: our_army.append(self.monstify(splitted[0])) if len(splitted) > 1: their_army.append(self.monstify(splitted[1])) remover = lambda x: x is not None our_army = filter(remover, our_army) their_army = filter(remover, their_army) return (army.Army(our_army), army.Army(their_army))
def setUp(self): """Initialization of Army object contains 2 squads with different strength values """ self.army = army.Army(0, "random_squad") self.army.squads[0].get_damage(0.6) self.army.squads[1].get_damage(0.9)
def get_attack_power(self, variation): attacker = army.Army(variation) power = attacker.power result, report = attacker.attack(self.defender, report=False, dry_run=True) if not result: return 0 return power
def test(): grunt0 = troop.Grunt("Ranger1", 25, 25, 1, 20, 10, 5, 20) grunt1 = troop.Grunt("Ranger2", 25, 25, 1, 20, 10, 5, 20) grunt2 = troop.Grunt("Ranger3", 25, 25, 1, 20, 10, 5, 20) grunt3 = troop.Grunt("Soldier1", 50, 50, 1, 1, 20, 20, 10) grunt4 = troop.Grunt("Soldier2", 50, 50, 1, 1, 20, 20, 10) grunt5 = troop.Grunt("Soldier3", 50, 50, 1, 1, 20, 20, 10) champion = troop.Grunt("Champion", 1000, 1000, 1, 1, 20, 20, 50) test0 = battalion.Battalion("bat1", grunt0, 30) test1 = battalion.Battalion("bat2", grunt1, 30) test2 = battalion.Battalion("bat3", grunt2, 30) test3 = battalion.Battalion("bat4", grunt3, 30) test4 = battalion.Battalion("bat5", grunt4, 30) test5 = battalion.Battalion("bat6", grunt5, 30) army0 = army.Army() army0.add_troop(test0) army0.add_troop(test1) army0.add_troop(test2) army0.add_troop(test3) army0.add_troop(test4) army0.add_troop(test5) army0.insert_troop(champion, 2) army1 = army.Army() army1.add_troop(test0) army1.add_troop(test1) army1.add_troop(test2) army1.add_troop(test3) army1.add_troop(test4) army1.add_troop(test5) battle0 = battle.Battle(army0.army, army1.army) print(len(battle0.big_army)) print(battle0)
def grave_touching(player1, all_graves, all_armies, grave_counter): # if the player sprite collides with the graveyard sprite, instantiate an army, add it the army group then kill grave_touch = pygame.sprite.spritecollideany(player1, all_graves) if grave_touch: if grave_counter != 0: grave_counter -= 1 elif grave_counter == 0: new_army_guy = army.Army((grave_touch.rect.x) + 39, (grave_touch.rect.y) + 30, 40, grave_touch.speed) all_armies.add(new_army_guy) all_graves.remove(grave_touch) grave_counter = 60 else: grave_counter = 60 return grave_counter
def reset(player1, all_enemies, PLAYER_HEALTH, WIDTH, HEIGHT, screen, all_graves, all_armies, all_powerups): for i in all_enemies: all_enemies.remove(i) for i in all_graves: all_graves.remove(i) for i in all_armies: all_armies.remove(i) for i in all_powerups: all_powerups.remove(i) all_armies.add( army.Army(random.randint(100, WIDTH - 100), random.randint(100, HEIGHT - 100), 40, 5)) screen.fill(colors.red) text.draw_final_score(screen, player1.score, WIDTH, HEIGHT) text.draw_final_message(screen, WIDTH, HEIGHT) pygame.display.flip() player1.rect.x = WIDTH / 2 player1.rect.y = HEIGHT / 2
def brute(self): stack_variations = [] for variation in itertools.permutations(self.attacker): if self.try_attack(variation): stack_variations.append(variation) unit_variations = [] for n, variation in enumerate(stack_variations): result = self.brute_units(variation, n + 1, len(stack_variations)) unit_variations.append((result, variation)) if len(unit_variations) == 0: return None best = min(unit_variations, key=lambda x: x[0]) best_var = best[0][1] best_basevar = best[1] for n, var in enumerate(best_var): best_basevar[n].stack = var return army.Army(best_basevar)
def main(): # Global variables WIDTH = 800 HEIGHT = 600 ktime = 0 PLAYER_SIZE = 40 PLAYER_SPEED = 2 PLAYER_HEALTH = 5 FPS = 60 powerup_counter = 240 grave_counter = 60 enemy_spawn_counter = 1 enemy_spawn_multipler = 1 pygame.init() screen = pygame.display.set_mode((WIDTH, HEIGHT)) pygame.display.set_caption("Necromancer") background = pygame.image.load("Background.png") clock = pygame.time.Clock() all_players = pygame.sprite.Group() player1 = player.Player(WIDTH / 2, HEIGHT / 2, PLAYER_SIZE, PLAYER_SPEED, PLAYER_HEALTH) # Set the doc icon the the main player pygame.display.set_icon(player1.image) all_armies = pygame.sprite.Group() all_bullets = pygame.sprite.Group() armies = army.Army(random.randint(100, WIDTH - 100), random.randint(100, HEIGHT - 100), 40, 5) camera1 = camera.Camera(WIDTH / 2, HEIGHT / 2) all_players.add(player1) all_armies.add(armies) all_enemies = pygame.sprite.Group() all_graves = pygame.sprite.Group() all_powerups = pygame.sprite.Group() music() frequency = 100 running = True while running: # keep loop running at the right speed clock.tick(FPS) ktime += 1 if ktime % frequency == 0: frequency -= 5 if frequency <= 50: frequency = 50 # Detect Collisions, collisions(all_enemies, all_armies, player1, all_graves, all_bullets) # Detect grave touching grave_counter = grave_touching(player1, all_graves, all_armies, grave_counter) powerup_counter = powerup_touching(player1, all_powerups, powerup_counter) # Detect wizard touching enemy wizard_touching(player1, all_enemies, WIDTH, HEIGHT, all_graves) # Process exit event for event in pygame.event.get(): # check for closing window if event.type == pygame.QUIT: running = False if player1.health != 0: # Draw / render screen.fill(colors.black) updates(screen, all_enemies, all_players, all_armies, WIDTH, HEIGHT, background, player1, camera1, all_bullets, all_graves, all_powerups) # Spawn enemies based on frequency (spawn_bool, enemy_spawn_counter, enemy_spawn_multipler) = spawnEnemies(enemy_spawn_counter, enemy_spawn_multipler) if spawn_bool: # world size is window size * 2 e = enemy.Enemy(random.randint(-WIDTH * 2, WIDTH * 2), random.randint(-HEIGHT * 2, HEIGHT * 2), 0, random.randint(2, 5), 40) all_enemies.add(e) # Spawn powerups if spawnItem(ktime, 500): p = powerup.PowerUp(random.randint(0, WIDTH * 2), random.randint(0, HEIGHT * 2), 40) all_powerups.add(p) text.draw_score(screen, player1.score, WIDTH) text.draw_health(screen, player1.health, WIDTH) # Draw bullets if player1.check_shoot(FPS) == True: new_bullet = bullet.Bullet(player1, 10) all_bullets.add(new_bullet) # If the player has died, show the score and lose message if player1.health == 0: enemy_spawn_multipler = 1 reset(player1, all_enemies, PLAYER_HEALTH, WIDTH, HEIGHT, screen, all_graves, all_armies, all_powerups) keystate = pygame.key.get_pressed() if keystate[pygame.K_RETURN]: player1.health = PLAYER_HEALTH player1.score = 0 # *after* drawing everything, flip the display pygame.display.flip() pygame.quit()
import numpy as np characters_list = [] with open('characters.csv', newline='') as fichier: a = csv.reader(fichier, delimiter=',', quotechar=',') compteur = 0 for row in a: if (compteur != 0): characters_list.append(character.Character(*row)) else: compteur = compteur + 1 valTotal = 0 for charac in characters_list: armee = army.Army(charac, uniform(20.0, 100.0)) valTotal = valTotal + armee.get_total_moral() """ print("Valeur totale des armées : "+str(valTotal)) valeurMoraleTroupes=np.array([uniform(20.0,100.0),uniform(20.0,100.0),uniform(20.0,100.0),uniform(20.0,100.0),uniform(20.0,100.0)]) boostMoralTroupe=np.array([uniform(0.1,10.0),uniform(0.1,10.0),uniform(0.1,10.0),uniform(0.1,10.0),uniform(0.1,10.0)]) print(valeurMoraleTroupes) print(boostMoralTroupe) et = EtLogique.EtLogique() et.calcul() et.afficherListe()
def __init__(self, name, stability, literacy, economy, population, public, save, armies=None): self.save=save '''print(economy) print(literacy) print(population) print(public) print(name)''' self.name = name.lower() self.army = army.Army(armies) armies = {"sol": 100, "cav": 50, "arch": 40, "con": 10, "art": 0} self.army.add_template("Deafult1", armies) armies = {"ligh": 30, "heav": 5, "bord": 50} self.army.add_template("Deafult2", armies) self.public = public self.spendings = { "total": 0, "education_spending": 0, "economy_spending": 0, "benefits_spending": 0 } self.spendings["army"] = self.army.return_cost(100) self.population = Population("pop", population[0], population[1], population[2], [ "population", "population_tier"]) self.literacy = Literacy("literacy", literacy[0], literacy[1], literacy[2], [ "literacy", "literacy_tier"]) self.stability = Stability("stability", stability[0], stability[1], stability[2], [ "stability", "stability_tier"]) self.economy = Economy("economy", economy[0], economy[1], economy[2], economy[3], economy[4], [ "budget", "economy_tier"]) self.population.update_ratio( self.literacy.return_value(), self.army.return_man() ) # Getting the basic population proportions self.mod = [ "tax", "form", "capital", "education_spending", "economy_spending", "benefits_spending", "name" ] # All the values user can modify self.ad_mod = [ "population", "population_tier", "literacy", "literacy_tier", "stability", "stability_tier", "budget", "economy_tier", "trade", "culture", "area" ] # All the additional values admin can modify self.changes = {} # Constains all the names of vars that can be changed for feature in [self.population, self.literacy, self.stability, self.economy]: self.changer_dic(feature) for feature in list(self.public.keys())+list(self.spendings.keys()): self.changes[feature] = self self.changes["name"] = self
armee_list = [] total_erreur = np.zeros((11, 11)) with open('characters.csv') as f: f_csv = csv.reader(f) en_tetes = next(f_csv) for ligne in f_csv: name = ligne[0] first_name = ligne[1] age = ligne[2] profession = ligne[3] moralBoost = ligne[4] personnage = ch.Character(name, first_name, age, profession, moralBoost) characters_list.append(personnage) armee = ar.Army(personnage, randint(20, 100)) armee_list.append(armee) """for i in characters_list: print(i) for e in armee_list: print(e.get_total_moral()) """ print("Valeur totale de toutes les armée : ") val_army = (np.random.random_sample(5)) * (100 - 20) val_persos = [0.97, 2, 1.3, 1.5, 0.1] result = np.dot(val_army, val_persos) print(result) list_and = [[0, 0], [0, 1], [1, 0], [1, 1]]
def executeOrders(self, playerNo, Commands): try: if playerNo == 1: p1Cities = [] for aCity in self.cities: if aCity.getOwner() == 1: p1Cities.append(aCity) for aCommand in Commands: if aCommand.startswith("cclub power:"): self.p1ChosenCity = int(aCommand[len("cclub power:"):]) else: [fCity, tCity, rock, paper, scissors] = aCommand.split() [fCity, tCity, rock, paper, scissors] = list( map(int, [fCity, tCity, rock, paper, scissors])) if rock < 0 or paper < 0 or scissors < 0: raise CommandError() worked = False for aCity in p1Cities: if aCity.getCityID() == fCity: worked = True break if not worked: raise CommandError() if not tCity in self.cityIDs: raise CommandError() for aCity in p1Cities: if aCity.getCityID() == fCity: theCity = aCity break theCity.decreaseSoldiers([rock, paper, scissors]) self.armies.append( army.Army( self.lastArmyID + 1, fCity, tCity, 1, self.distanceBetweenCitiesID(fCity, tCity), [rock, paper, scissors], self.cityDictionary[fCity].getCoordinates(), self.cityDictionary[tCity].getCoordinates())) self.lastArmyID += 1 elif playerNo == 2: p2Cities = [] for aCity in self.cities: if aCity.getOwner() == 2: p2Cities.append(aCity) for aCommand in Commands: if aCommand.startswith("cclub power:"): self.p2ChosenCity = int(aCommand[len("cclub power:"):]) else: [fCity, tCity, rock, paper, scissors] = aCommand.split() [fCity, tCity, rock, paper, scissors] = list( map(int, [fCity, tCity, rock, paper, scissors])) if rock < 0 or paper < 0 or scissors < 0: raise CommandError() worked = False for aCity in p2Cities: if aCity.getCityID() == fCity: worked = True break if not worked: raise CommandError() if not tCity in self.cityIDs: raise CommandError() for aCity in p2Cities: if aCity.getCityID() == fCity: theCity = aCity break theCity.decreaseSoldiers([rock, paper, scissors]) self.armies.append( army.Army( self.lastArmyID + 1, fCity, tCity, 2, self.distanceBetweenCitiesID(fCity, tCity), [rock, paper, scissors], self.cityDictionary[fCity].getCoordinates(), self.cityDictionary[tCity].getCoordinates())) self.lastArmyID += 1 except: return "error"