Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I randomly assign powers to my heroes?

I’m creating a hero game. I’ve created a dictionary containing magic powers, my problem is how can I create a method that will randomly assign 12 magic powers to 4 of my heroes (each hero get’s 3 powers) without one magic power being assigned multiple times?
Here’s my code:

import random
class Heroes:
    def __init__(self,name,type,magic,health):
        self.name = name
        self.type = type
        self.magic = magic
        self.health = health

class Stats:
    def __init__(self,hero):
        self.hero = hero

    def magic(self):
        magic_powers = {1: {'Name': 'Apparition', 'Damage': 10},
                  2: {'Name': 'Astral Spirit', 'Damage': 15},
                  3: {'Name': 'Dawnbreaker', 'Damage': 20},
                  4: {'Name': 'Solar Guardian Land', 'Damage': 25},
                  5: {'Name': 'Pulse Nova', 'Damage': 30},
                  6: {'Name': 'Lifestealer', 'Damage': 35},
                  7: {'Name': 'Medusa', 'Damage': 40},
                  8: {'Name': 'Tree Dance', 'Damage': 45},
                  9: {'Name': 'Assassin', 'Damage': 50},
                  10: {'Name': 'Psionic', 'Damage': 55},
                  11: {'Name': 'Mine', 'Damage': 60},
                  12: {'Name': 'Templar', 'Damage': 65}}
        print(random.choice(magic_powers))

hero1=Heroes('Gandalf','Wizard', '?', 500)
hero2=Heroes('Gandalf','Wizard', '?', 500)
hero3=Heroes('Gandalf','Wizard', '?', 500)
hero4=Heroes('Gandalf','Wizard', '?', 500)
heroes=Heroes(hero1,hero2,hero3,hero4)
stats = Stats(heroes)
magic_powers=stats.magic()

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This goes through the set of 12, and if you ask for more, it will reshuffle them again.

import random

class Heroes:
    def __init__(self,name,type,magic,health):
        self.name = name
        self.type = type
        self.magic = magic
        self.health = health

class Stats:
    magic_powers = {1: {'Name': 'Apparition', 'Damage': 10},
              2: {'Name': 'Astral Spirit', 'Damage': 15},
              3: {'Name': 'Dawnbreaker', 'Damage': 20},
              4: {'Name': 'Solar Guardian Land', 'Damage': 25},
              5: {'Name': 'Pulse Nova', 'Damage': 30},
              6: {'Name': 'Lifestealer', 'Damage': 35},
              7: {'Name': 'Medusa', 'Damage': 40},
              8: {'Name': 'Tree Dance', 'Damage': 45},
              9: {'Name': 'Assassin', 'Damage': 50},
              10: {'Name': 'Psionic', 'Damage': 55},
              11: {'Name': 'Mine', 'Damage': 60},
              12: {'Name': 'Templar', 'Damage': 65}}

    def __init__(self,hero):
        self.hero = hero
        self.powers = None

    def magic(self):
        if not self.powers:
            self.powers = list(range(1,13))
            random.shuffle(self.powers)
        return self.powers.pop(0)

hero1=Heroes('Gandalf','Wizard', '?', 500)
hero2=Heroes('Gandalf','Wizard', '?', 500)
hero3=Heroes('Gandalf','Wizard', '?', 500)
hero4=Heroes('Gandalf','Wizard', '?', 500)
heroes=Heroes(hero1,hero2,hero3,hero4)
stats = Stats(heroes)

print( stats.magic() )
print( stats.magic() )
print( stats.magic() )
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading