【问题标题】:Python: Rolling dice again when at max randint valuePython:在最大randint值时再次掷骰子
【发布时间】:2018-07-13 17:30:09
【问题描述】:

我正在开展一个小项目,以帮助我作为 GM 服务的一些玩家。本质上,我想创建一个自动化系统,使用它的规则在 OpenLegend RPG 系统中掷骰子。

系统的一个元素是“骰子爆炸”。如果你掷骰子的最大值,你再次掷骰子,将先前的值和新的值相加,继续“爆炸”的次数。但是,我很难弄清楚我将如何做到这一点。目前我的代码是这样的:

from random import randint as ri

def action_roll_script(attribute, score):

d20 = ri(1, 20)

if score == 0:
    roll = d20
if score == 1:
    roll = ri(1,4)
if score == 2:
    roll = ri(1,6)
if score == 3:
    roll = ri(1,8)
if score == 4:
    roll = ri(1,10)
if score == 5:
    roll = ri(1,6) + ri(1,6)
if score == 6:
    roll = ri(1,8) + ri(1,8)
if score == 7:
    roll = ri(1,10) + ri(1,10)
if score == 8:
    roll = ri(1, 8) + ri(1, 8) + ri(1, 8)
if score == 9:
    roll = ri(1, 10) + ri(1, 10) + ri(1, 10)
if score == 10:
    roll = ri(1, 8) + ri(1, 10) + ri(1, 10) + ri(1, 10)

print(d20)
print(roll)

outcome = str(roll + d20)

print("You got a " + attribute + " roll of " + outcome + "!")

现在,如果 if 语句太多,显然效率很低。稍后我将为此使用字典/列表。我的问题是:我如何告诉脚本在达到最大值时再次掷骰子?例如,如果它在 d20 上掷出 20,我希望它保存之前的结果并再次掷出,只要得到 20 就继续。

【问题讨论】:

    标签: python random dice


    【解决方案1】:

    尝试使用递归:

    我相信您正在寻找这样的东西:

    from random import randint as ri
    MAX_DICE_NUMBER = 20
    def roll_dice():
        attempt = ri(1, MAX_DICE_NUMBER)
        if attempt == MAX_DICE_NUMBER:
            return attempt + roll_dice()
        else:
            return attempt
    
    def action_roll_script(attribute):
        print("You got a " + attribute + " roll of " + str(roll_dice()) + "!")
    
    action_roll_script('power')
    >>> You got a power roll of 16!
    

    我不明白你为什么需要分数...

    但这篇文章仍然可能有用:)

    【讨论】:

    • 非常感谢! “分数”是指被掷出的相关属性,它决定了掷出什么骰子/掷了多少个。我计划最终将其整合到一个 Discord 机器人中,该机器人将采用函数和三个参数:属性、属性分数和优势/劣势水平(我还没有弄清楚)。我会试一试!再次感谢!
    • 真的希望它有所帮助\o 请也考虑接受这个答案:D
    【解决方案2】:

    没有递归和分数系统:

    from random import randint as ri
    
    score_mapping = {
        0 : None,
        1 : [(1, 4),],
        2 : [(1, 6),],
        3 : [(1, 8),],
        4 : [(1,10),],
        5 : [(1, 6), (1, 6)],
        6 : [(1, 8), (1, 8)],
        7 : [(1, 10), (1, 10)],
        8 : [(1, 8), (1, 8), (1, 8)],
        9 : [(1, 10), (1, 10), (1, 10)],
        10 : [(1, 8), (1, 10), (1, 10), (1, 10)],
    }
    
    def action_roll_script(score, roll_again_at=20):
        return_value = 0
    
        while True:
    
            value = ri(1, 20)
    
            print(f'score={score} rolled value={value}')
    
            if score == 0:
                return_value += value
            else:
                return_value += sum(ri(vf, vt) for (vf, vt) in score_mapping[score])
    
            if value != roll_again_at:
                break
    
        return return_value
    
    print(action_roll_script(0))
    

    可能的输出:

    score=10 rolled value=20
    score=10 rolled value=5
    38
    

    【讨论】:

      猜你喜欢
      • 2012-11-27
      • 1970-01-01
      • 1970-01-01
      • 2021-10-07
      • 1970-01-01
      • 2015-12-09
      • 2014-12-28
      • 2018-12-17
      • 2022-01-21
      相关资源
      最近更新 更多