I'm primarily a C++ developer, but I fairly frequently end up writing Python scripts. I'm currently writing a dice simulator for a game, and I'm not certain of the best way, in Python, the solve my problem.
There are three player skills, and each player is strong at one, medium at one, and weak at one. I've written some classes that calculate the faces of the die from each of the player skils.
The skills live in an enum, rather than writing strings all over the place. However, there's also a chance that the skill will be "doubled", and become stronger.
Given that I am returning a list of Skill enum element, what is the best way to indicate a skill has been doubled?
Things I've considered so far:
- Extending the Skill Enum to include doubled skills, but that makes the mapping of player skills to faces harder
- Creating a DoubleSkill Enum, but then any changes to the Skill Enum would need to be replicated in DoubleSkill
- My preferred option, which is creating a wrapper DoubleSkill class that can be constructed from a Skill. However, then it isn't of type enum, which makes my strongly-typed C++ instincts nervous
import random
from abc import ABC, abstractmethod
from enum import Enum
from collections import namedtuple
class Skill(Enum):
Might = 1,
Wisdom = 2,
Cunning = 3
class Die(ABC):
@abstractmethod
def _faces(self):
'''Returns the faces of this die'''
pass
def roll(self):
'''Returns a random face'''
return random.choice(self._faces())
PlayerSkills = namedtuple("PlayerSkills", ("strong", "medium", "weak"))
class PlayerDie(Die):
@abstractmethod
def _skills(self):
'''Returns the characer's skills'''
pass
def _faces(self):
'''Work out the faces of the die based off the skills'''
skills = self._skills()
#I want this to return a representation of the skill, not a string.
#But then I'd be mixing Enums and not-Enums
return [
self._double(skills.strong),
self._double(skills.medium),
skills.strong.name,
skills.strong.name,
skills.medium.name,
skills.weak.name
]
def _double(self, skill):
return f"Double {skill.name} (block)"
class CookDie(PlayerDie):
def _skills(self):
return PlayerSkills(Skill.Might, Skill.Cunning, Skill.Wisdom)
print(CookDie().roll())