Disclaimer: I have checked many questions with similar keywords, but I don't believe they address my situation. Please correct me if I'm wrong, but I can't find anything.
I'm trying to get coverage from a random choice from the Pokedex where the values in the Pokedex dict will correspond to enough keys in the Effective_Against dict, which in turn will result in a subset of the set Types.
So basically, I want to randomly create a team where you will always have a type advantage against any opposing Pokemon. My code is in all sorts of functions and different files, so for the sake of simplicity, I've included what I believe will be enough to explain my problem, but I'll also walk through an example.
EXAMPLE: If Bulbasaur was randomly chosen for the team from the Pokedex, then my team's types would include grass and poison. Grass is effective against ground, rock, and water, so my team would be effective against those 3 types from the set Types. I want a random choice of Pokemon that will be effective against all of the types in the set Types.
Problem is, I keep hitting maximum recursion depth before it gets a result. I need a way to make this more efficient somehow.
Types = {"normal", "flying", "fighting", "poison", "ground", "rock", "bug", "ghost", "fire", "water", "grass", "electric", "psychic", "ice", "dragon"}
Effective_Against = {
"normal": [],
"fighting": ["normal", "rock", "ice"],
"flying": ["fighting", "bug", "grass"],
"poison": ["bug", "grass"],
"ground": ["poison", "rock", "fire", "electric"],
"rock": ["flying", "bug", "fire", "ice"],
"bug": ["poison", "grass", "psychic"],
"ghost": ["ghost"],
"fire": ["bug", "grass", "ice"],
"water": ["ground", "rock", "fire"],
"grass": ["ground", "rock", "water"],
"electric": ["flying", "water"],
"psychic": ["fighting", "poison"],
"ice": ["flying", "ground", "grass", "dragon"],
"dragon": ["dragon"],
"steel": [],
"dark" : [],
"fairy": [],
}
pokedex = {
'bulbasaur': ['grass', 'poison'],
'ivysaur': ['grass', 'poison'],
'venusaur': ['grass', 'poison'],
'charmander': ['fire']
} # and so on, so forth
while len(team) < 6:
pokemon = random.choice(list(pokedex))
team.append(pokemon)
types_effective_against = # the types that my Pokemon have coverage against
if Types.issubset(types_effective_against):
return True
I suggest iterating on the types that you have to cover, starting by types that are hard to cover (for instance, normal is harder to cover than grass, because only fighting is strong against normal, whereas fire, ice, poison and bug are all strong against grass).
Then, select a random pokemon which covers the next type.
If you already have 6 pokemon and some types are still uncovered, then erase the whole team and try again.