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()
CodePudding user response:
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() )