gpt4 book ai didi

python - 表示 "doubled"枚举值的最佳方式

转载 作者:行者123 更新时间:2023-12-04 10:22:29 30 4
gpt4 key购买 nike

我主要是一名 C++ 开发人员,但我经常编写 Python 脚本。我目前正在为游戏编写骰子模拟器,但我不确定在 Python 中解决我的问题的最佳方法。

一共有三种玩家技能,每个玩家一强、中一、弱一。我编写了一些类来计算每个玩家技能的骰子面。

这些技能存在于一个枚举中,而不是到处写字符串。不过,技能也有机会“翻倍”,变得更强。

鉴于我正在返回一个技能枚举元素列表,指示技能翻倍的最佳方法是什么?

到目前为止我考虑过的事情:

  • 扩展技能枚举以包含双倍技能,但这会使玩家技能与面孔的映射更加困难
  • 创建一个 DoubleSkill 枚举,但随后对技能枚举的任何更改都需要在 DoubleSkill 中复制
  • 我的首选选项是创建一个可以从技能构造的包装器 DoubleSkill 类。然而,它不是枚举类型,这让我对强类型 C++ 的直觉感到紧张
  • 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())

    最佳答案

    一种可能的方法是使您的枚举成为 Flag反而:

    from enum import Flag, auto
    class Skill(Flag):
    Might = auto()
    Wisdom = auto()
    Cunning = auto()
    Doubled = auto()

    并在使用中:
    >>> for i in (1, 2, 4, 9, 10, 12):
    ... i, Skill(i)

    (1, <Skill.Might: 1>)
    (2, <Skill.Wisdom: 2>)
    (4, <Skill.Cunning: 4>)
    (9, <Skill.Doubled|Might: 9>)
    (10, <Skill.Doubled|Wisdom: 10>)
    (12, <Skill.Doubled|Cunning: 12>)

    如果你想要更漂亮的 str() (或一个联合成员(member)),您可以添加自己的 __str__ :
    class Skill(Flag):
    Might = auto()
    Wisdom = auto()
    Cunning = auto()
    Doubled = auto()
    #
    def __str__(self):
    cls = self.__class__
    cls_name = cls.__name__
    doubled = (self & cls.Doubled or "") and " x2"
    base = (self & ~cls.Doubled) or self
    name = base.name
    if name is None:
    name = '|'.join(s.name for s in Skill if s & base)
    return "%s.%s%s" % (cls_name, name, doubled)

    并在使用中:
    for i in (1, 2, 4, 9, 10, 12):
    i, str(Skill(i))

    (1, 'Skill.Might')
    (2, 'Skill.Wisdom')
    (4, 'Skill.Cunning')
    (9, 'Skill.Might x2')
    (10, 'Skill.Wisdom x2')
    (12, 'Skill.Cunning x2')

    关于python - 表示 "doubled"枚举值的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60786460/

    30 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com