import random from os import urandom import statistics import csv def nominalizeOld(val, max_val): return_value = "NONE" if val > 0.8 * max_val: return_value = "VERY_HIGH" elif val > 0.6 * max_val: return_value = "HIGH" elif val > 0.4 * max_val: return_value = "MEDIUM" elif val > 0.2 * max_val: return_value = "LOW" elif val > 0: return_value = "VERY_LOW" return return_value def nominalize(val, max_val): return_value = "NONE" if val > 0.66 * max_val: return_value = "HIGH" elif val > 0.33 * max_val: return_value = "MEDIUM" elif val > 0: return_value = "LOW" return return_value class Stats: def __init__(self): self.strength = random.randint(1, 10) self.agility = random.randint(1, 10) self.wisdom = random.randint(1, 10) self.health = random.randint(1, 50) self.melee_wep_damage = random.randint(1, 10) self.ranged_wep_damage = random.randint(1, 10) self.magic_wep_damage = random.randint(1, 10) self.armor_defence = random.randint(0, 5) self.armor_magic_protection = random.randint(0, 5) self.damage = 0 def meleeAttack(self, opponent): attackValue = self.strength + random.randint(1, 6) defenseValue = opponent.strength + opponent.armor_defence damage = attackValue - defenseValue if damage > 0: opponent.damage += (damage + self.melee_wep_damage) def rangeAttack(self, opponent): attackValue = self.agility + random.randint(1, 6) defenseValue = opponent.agility damage = attackValue - defenseValue if (damage > 0) and (damage + self.ranged_wep_damage - opponent.armor_defence > 0): opponent.damage += (damage + self.ranged_wep_damage - opponent.armor_defence) def magicAttack(self, opponent): attackValue = self.wisdom + random.randint(1, 6) defenseValue = opponent.wisdom damage = attackValue - defenseValue if (damage > 0) and (damage + self.magic_wep_damage - opponent.armor_magic_protection > 0): opponent.damage += (damage + self.magic_wep_damage - opponent.armor_magic_protection) def reset(self): self.damage = 0 FIELDNAMES = ["p_strength", "p_agility", "p_wisdom", "p_health", "p_melee_damage", "p_ranged_damage", "p_magic_damage", "p_armor_defence", "p_armor_magic_protection", "e_strength", "e_agility", "e_wisdom", "e_health", "e_damage", "e_armor_defence", "e_armor_magic_protection", "e_attack_type", "strategy"] RESULT_FILE = open('data.csv', 'w', newline='') FILE_WRITER = csv.writer(RESULT_FILE, dialect='excel', delimiter=',', quotechar='"', quoting=csv.QUOTE_MINIMAL) FILE_WRITER.writerow(FIELDNAMES) SETUP_RESULTS = [[], [], []] MAX_COMBAT_TIME = 20 def try_combat(my_seed, p, e, player_att_type, enemy_att_type): random.seed(my_seed) current_iteration = 0 while True: if player_att_type == 0: p.meleeAttack(e) elif player_att_type == 1: p.rangeAttack(e) else: p.magicAttack(e) if e.damage >= e.health: SETUP_RESULTS[player_att_type].append(p.health - p.damage) break if enemy_att_type == 0: e.meleeAttack(p) elif enemy_att_type == 1: e.rangeAttack(p) else: e.magicAttack(p) if p.damage >= p.health: SETUP_RESULTS[player_att_type].append(0) break current_iteration += 1 if current_iteration >= MAX_COMBAT_TIME: SETUP_RESULTS[player_att_type].append(0) break p.reset() e.reset() for trial in range(10000): stat_seed = urandom(16) random.seed(stat_seed) player = Stats() enemy = Stats() enemy_attack_type = random.randint(0, 2) # Enemy weapon choice for i in range(30): combat_seed = urandom(16) try_combat(combat_seed, player, enemy, 0, enemy_attack_type) try_combat(combat_seed, player, enemy, 1, enemy_attack_type) try_combat(combat_seed, player, enemy, 2, enemy_attack_type) for i, series in enumerate(SETUP_RESULTS): SETUP_RESULTS[i] = statistics.mean(series) strategy = "PASS" if any(SETUP_RESULTS): max_index = SETUP_RESULTS.index(max(SETUP_RESULTS)) if max_index == 0: strategy = "MELEE" elif max_index == 1: strategy = "RANGED" elif max_index == 2: strategy = "MAGIC" enemy_damage = 0 if enemy_attack_type == 0: enemy_attack_type = "MELEE" enemy_damage = enemy.melee_wep_damage elif enemy_attack_type == 1: enemy_attack_type = "RANGED" enemy_damage = enemy.ranged_wep_damage elif enemy_attack_type == 2: enemy_attack_type = "MAGIC" enemy_damage = enemy.magic_wep_damage FILE_WRITER.writerow([nominalize(player.strength, 10), nominalize(player.agility, 10), nominalize(player.wisdom, 10), nominalize(player.health, 50), nominalize(player.melee_wep_damage, 10), nominalize(player.ranged_wep_damage, 10), nominalize(player.magic_wep_damage, 10), nominalize(player.armor_defence, 5), nominalize(player.armor_magic_protection, 5), nominalize(enemy.strength, 10), nominalize(enemy.agility, 10), nominalize(enemy.wisdom, 10), nominalize(enemy.health, 50), nominalize(enemy_damage, 10), nominalize(enemy.armor_defence, 5), nominalize(enemy.armor_magic_protection, 5), enemy_attack_type, strategy]) SETUP_RESULTS = [[], [], []] if trial%100 == 0: print("Trials done: " + str(trial))