dimanche 12 juillet 2020

Problem with creating a list with random results from a loop

I want the following code to generate different outputs from the battle() function which should appear at the end of a script. I managed to do this but the outputs are all the same. What did i wrong?

import math
import random

def battle():
    for c in range(24):
        for _ in range(5):
            if monsterChoice.hp > 0:
                global monster_dmg
                m_chance_1 = [1] * int(m_hitChance)
                m_miss = 100 - int(m_hitChance)
                m_chance_0 = [0] * int(m_miss)
                x = (int(random.choice(m_chance_1 + m_chance_0)))
                if x == 1 and p1.hp > 0:
                    monster_dmg = monsterChoice.atk_power - random.randint(
                        1, 5 * monsterChoice.level) - p1.defence
                    if monster_dmg < 0: monster_dmg = 0
                    p1.hp -= monster_dmg
                    print('You were dealt ' + str(monster_dmg) + ' DMG. ' +
                          str(p1.hp) + ' HP LEFT')
                if x == 0:
                    global get_evasion
                    get_evasion += 1
                    print('You evaded this attack! ')
                elif x == 0 or x == 1 and p1.hp < 0:
                    print('YOU ARE DEAD!')

        for _ in range(1):
            if monsterChoice.hp > 0:
                global player_dmg
                p_chance_1 = [1] * int(p_hitChance)
                p_miss = 100 - int(p_hitChance)
                p_chance_0 = [0] * int(p_miss)
                x = (int(random.choice(p_chance_1 + p_chance_0)))
                if x == 1 and monsterChoice.hp > 0:
                    player_dmg = int(p1.atk_power +
                                     random.randint(1, 5 * p1.level) -
                                     monsterChoice.defence)
                    if player_dmg < 0: player_dmg = 0
                    global get_weapon
                    if player_dmg >= 1: get_weapon += 1
                    monsterChoice.hp -= player_dmg
                    print('You deal ' + str(player_dmg) + ' DMG. ' +
                          str(monsterChoice.hp) + ' HP LEFT')
                if x == 0:
                    print('You missed! ')
                if monsterChoice.hp < 0:
                    print('YOU WON THE BATTLE!')
    return (get_weapon, get_evasion)


weapon_results, evasion_results = [], []

for _ in range(3):
    get_weapon, get_evasion = battle()
    weapon_results.append(get_weapon)
    evasion_results.append(get_evasion)

print(evasion_results)

My list with evasion_results shows a result from only one simulation and it should be randomized every iteration




Aucun commentaire:

Enregistrer un commentaire