一个分析骰子符号的模块

py-rolldice的Python项目详细描述


py rolldice

一个python模块,用于解析掷骰子和掷骰子的字符串表示(也称为骰子表示法)。

用法:

用法很简单。只需使用以下命令安装py rolldice即可:

python -m pip install py-rolldice

然后照常导入并运行掷骰子功能:

import rolldice
result, explanation = rolldice.roll_dice('12d6 + 10')

就这样。该函数接受骰子掷骰的字符串表示形式,并按该顺序返回一个包含数值结果和构造的表示字符串的元组。

函数和浮动:

默认情况下,骰子滚动器将解析浮点数和一组有限的函数调用。如果需要,可以禁用此功能:

roll_dice('abs(-2)') # Will work
roll_dice('abs(-2)', functions=False) # Won't work

roll_dice('3 / 2') # Division will return a float
roll_dice('3 / 2', floats=False) # Division will be floor division

roll_dice('4.5') # Will work
roll_dice('4.5', floats=False) # Won't work

这些也适用于骰子袋类。

骰子语法:

dice语法基于CritDice语法。

基本语法:

4d10 + 17: Roll a 10-sided die 4 times and add 17 to the result
2d20 - 3: Roll a 20 sided die 2 times and subtract 3
1d4 * 3: Roll a 4 sided die once and multiply by 3
d4 * 3: Same as above, leaving out the number of dice will default to 1
5d6 / 3: Roll 5d6 and divide by 3
5d6 // 3: Same as above but floor division
2d10 ** 1d20: Roll 2d10 and exponentiate to the power of 1d20. Completely useless. Please never do this.
1d6 ** 1d6 ** 1d6 ** 1d6: OH GOD PLEASE NO

你明白了。空格是可选的,实际上可以插入卷中的任何位置。1 5 d 2 0 + 3 5实际上有效。接线员跟着彭达斯。例如:

roll_dice('2*3+5') # Will return 11, following PEMDAS
roll_dice('2*(3+5)') # Will return 16, using parentheses precedence

骰子滚动也支持一些功能,除了因为我可以。

abs(2d6-7): Absolute value of 2d6-7
gcd(2d6, 2d6): GCD of 2d6 and 2d6
lcm(7, 4d20): LCM of 7 and 4d20
floor(2d6 / 2): Floor of 2d6 / 2
ceil(2d6 / 2): Ceiling of 2d6 / 2
prime(2d6): 1 if 2d6 is prime else 0

可以添加其他功能。

关于指数运算的注意事项:

我已经做了一个检查,试图保护您的软,脆弱的CPU免受pow()函数的威胁,但您仍然必须小心。 指数是正确的关联,因为数学。

高级语法:

保持最高(k):

习惯于保持最高的滚动。后面可以跟一个数字来保持骰子的数量,或者不跟任何东西来表示只保留一个。

4d10K: Roll 4d10 and keep only the highest roll
7d12K3: Roll 7d12 and keep the highest three rolls
7d12K3 + 4: Roll as above then add 4
保持最低(k):

同上,但保持最低。

3d3k: Roll 3d3 and keep the lowest roll
100d6k99: Roll 100d6 and keep all but the highest.
2d20k: Roll 2d20 and keep the lowest. This is a disadvantage roll in 5e
最高下降(x):

用来放下最高的一卷。后面可以跟一个数字来丢骰子,也可以不跟任何东西来表示只丢了一个。

6d8X: Roll 6d8 and drop the lowest
5d10X3 Roll 5d10 and drop the highest 3
最低下降(x):

你明白了。

爆炸骰子(!):

爆炸骰子通常被称为“规则6”或“规则10”,因为它是在暗影运行。只要掷骰子通过指定的比较,就会掷另一个骰子并添加到总数中。重复此过程,直到滚动与比较不匹配的数字。

2d20!: Roll 2d20 and explode every time a 20 is rolled
7d20!3: Roll 7d20 and explode every time a 3 is rolled
4d6! Roll 4d6 and explode every time a 6 is rolled
d20!>10: Roll a d20 and explode every time a number higher than 10 is rolled
3d12!<2: Roll 3d12 and explode every time a 1 is rolled.
计算成功次数(>;或<;):

计算高于或低于某个值的卷数。

4d20>19: Rolls 4d20 and counts the number of rolls above 19
10d12<3: Rolls 10d12 and counts the number of rolls below 3
计数失败(f):

除了计数成功之外,还要指定一个额外的“失败”条件。每次失败将减少1分,而每次成功仍将增加1分。

10d10>6f<3: Roll 10d10 and count successes over 6 and failures under 3
4d20<5f>19: Roll 4d20 and count successes under 5 and failures over 19
5d100<5f>3: Invalid, you cannot have your failure and success comparison both be more than or less than.
穿透骰子(!p):

穿透骰子和爆炸骰子是一样的,除了所有爆炸骰子加上-1修正。

2d20!: Roll 2d20 and penetrate every time a 20 is rolled
7d20!3: Roll 7d20 and penetrate every time a 3 is rolled
4d6! Roll 4d6 and penetrate every time a 6 is rolled
d20!>10: Roll a d20 and penetrate every time a number higher than 10 is rolled
3d12!<2: Roll 3d12 and penetrate every time a 1 is rolled.
单个修饰语(a,s,m):

加、减或乘每个骰子卷。

2d20a3: Add three to each roll
4d12s4: Subtract four from each roll
6d4m3: Multiply each roll by 3
重拨号码(R或R):

在1、特定数字或比较上重新滚动。r在不满足条件之前重新滚动,r仅重新滚动一次。

4d20R: Reroll until there are no ones
5d6r6: Reroll once on a 6
7d12R>4: Reroll until there are no numbers above 4

骰子袋类:

dicebag类提供了一种简单的方法来存储特定的卷并重新滚动。 用法:

from rolldice import *
dicebag = DiceBag() #Initializes with a roll of '0'
# OR
dicebag = DiceBag('12d6 + 2d20K') #Initializes with a roll of '12d6 + 2d20K'

result, explanation = dicebag.roll_dice() #Repeat as needed

# The last roll is also stored in dicebag.lastroll
assert result = dicebag.last_roll and explanation = dicebag.last_explanation

就这些!

计划功能:

  • [X]允许在特定数字上爆炸,而不仅仅是比较
  • [X]按critice语法计算失败次数
  • [X]穿透骰子
  • [X]dicebag对象,用于重复掷骰子并作为对象存储。
  • [X]在给定条件下重新滚动一次或任意次数
  • [X]正确分析pemdas
  • [X]安全的数学分析器
  • [X]更好的支持所有优点的AST解析器

建议

如果你对功能有任何其他想法,只需提出建议,我会看看我能做什么!

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何将jaxb插件扩展与gradlejaxbplugin一起使用   java Hibernate列表<Object[]>到特定对象   java使用多态性显示arraylist的输出   java水平堆叠卡,带有一定偏移量   java错误:找不到符号方法liesInt()   java客户机/服务器文件收发中的多线程流管理   在java中可以基于访问重载方法吗?   包含空元素的java排序数组   swing Java按钮/网格布局   java BottomNavigationView getmaxitemcount   java空指针异常字符串生成器   java Xamarin升级导致“类文件版本错误52.0,应为50.0”错误   java我正在尝试打印它,而不只是对每一行进行println   Tomcat7中的java是否需要复制上下文。将xml转换为conf/Catalina/locahost以使其生效   带有注入服务的java REST端点在何处引发自定义WebServiceException?   在Java中使用GPS数据   java如何将JFreeChart ChartPanel导出到包含添加的CrosshairOverlay的图像对象?   内置Eclipse期间的Java 8堆栈溢出   java在GWT编译的JavaScript中如何表示BigDecimal