def did_monster_die(monster, hero): if monster.resistance == 0: main.clear_screen() main.print_hero_stats(hero) print(f"\n{monster.name} dog!") monster.died() input('\n-- Tryck på enter för att fortsätta --')
def run_tournament(self): for gen_num in tqdm(range(1, self.num_generations + 1)): self.gen_num = gen_num time.sleep(1) print(f'\nBEGINNING GENERATION {gen_num:03d}') self.init_parents() # self.output_parent_genomes() self.generate_population() matchup_count = Value('i', 0) while len(self.population) > self.num_parents: self.generate_tournament_mactchups() self.num_matchups = sum([2**i for i in range(int(math.log2(self.num_parents)), int(math.log2(self.population_size)))]) matchup_lock = Lock() update_lock = Lock() temp = self.matchups self.matchups = Queue() for m in temp: self.matchups.put(m) processes = [] for i in range(cpu_count()): processes.append(Process(target=self.do_work, args=(matchup_count, matchup_lock, update_lock))) processes[i].start() for i in range(cpu_count()): processes[i].join() self.population = sorted(self.population, key=lambda k: k['wins'], reverse=True) self.population = self.population[:len(self.population)//2] self.parents = self.population[:self.num_parents] self.save_parents() clear_screen()
def run_parallel(self): for gen_num in tqdm(range(1, self.num_generations + 1)): self.gen_num = gen_num time.sleep(1) print(f'\nBEGINNING GENERATION {gen_num:03d}') self.init_parents() self.output_parent_genomes() self.generate_population() self.generate_matchups() self.num_matchups = len(self.matchups) matchup_lock = Lock() update_lock = Lock() temp = self.matchups self.matchups = Queue() for m in temp: self.matchups.put(m) processes = [] for i in range(cpu_count()): processes.append(Process(target=self.do_work, args=(matchup_lock, update_lock))) processes[i].start() for i in range(cpu_count()): processes[i].join() print('\n') self.select_parents() self.save_parents() clear_screen()
def set_turn_order(monster_list, hero): creature_list = monster_list.copy() creature_list.append(hero) creature_list = sort_turn_list(creature_list) main.clear_screen() main.print_hero_stats(hero) print("\nSpelordningen följer:\n") i = 1 for creature in creature_list: print(f"{i}. {creature.name}") i += 1 input('\n-- Tryck på enter för att fortsätta --') return creature_list
def hero_attack(hero, monster_list, current_monster, room, game_board, grid_size, room_list): main.clear_screen() main.print_hero_stats(hero) monster = monster_list[current_monster] if monster.resistance == 0: try: current_monster += 1 monster = monster_list[current_monster] except IndexError: check_for_living_monsters(room, game_board, hero, grid_size, room_list) attack_func(hero, monster, hero) did_monster_die(monster, hero) check_for_living_monsters(room, game_board, hero, grid_size, room_list)
def dead_message(hero): while True: main.clear_screen() print(f"\n{hero.name} har dött.") save_collected_treasure(hero) print("\n[1] - Main menu\n" "[2] - Avsluta spelet") choice = input("\nSkriv in ditt val: ") if choice == "1": main.main_menu() elif choice == "2": exit() else: print("\n-- Felaktig input, ange en siffra från menyn. --") input('-- Tyck på enter för att fortsätta --')
def check_room(coordinates, room_list, hero, game_board, start_coordinates, grid_size): for room in room_list: if room.coordinates == coordinates: if room.symbol == "[ ]": undiscovered_room(room, hero, room_list, game_board, grid_size) elif room.symbol == "[O]": main.print_board(game_board, hero) exit_to_menu = main.exit_map( game_board, (start_coordinates[0], start_coordinates[1]), hero) if exit_to_menu: main.clear_screen() save_collected_treasure(hero) main.main_menu() elif room.symbol == "[.]": main.print_board(game_board, hero) print('\nInga monster i rummet.') print('Inga skatter hittades i rummet.')
def heroes_turn(hero, monster_list, current_monster, room, game_board, grid_size, room_list): while True: print(f"\n-{hero.name}s tur-\n\n" f"[1] - Attackera\n" "[2] - Pröva att fly") menu_choice = input("\nSkriv in ditt val: ") if menu_choice == "1": hero_attack(hero, monster_list, current_monster, room, game_board, grid_size, room_list) break elif menu_choice == "2": hero_flee(hero, game_board, monster_list, room, grid_size, room_list) break else: print("\n-- Felaktig input, ange en siffra från menyn. --") input('-- Tyck på enter för att fortsätta --') main.clear_screen() main.print_hero_stats(hero)
def fight(hero, grid_size, game_board, room_list, room): knight_ability = False if hero.hero_class == "Riddare": knight_ability = True monster_list = room.monster_list current_monster = 0 creature_list = set_turn_order(monster_list, hero) while True: for creature in creature_list: if creature.is_alive: if creature.name == hero.name: main.clear_screen() main.print_hero_stats(hero) heroes_turn(hero, monster_list, current_monster, room, game_board, grid_size, room_list) else: main.clear_screen() main.print_hero_stats(hero) knight_ability = monster_attacks(knight_ability, creature, hero)