骰子滚动库

2.0.0 2017-02-21 20:05 UTC

This package is auto-updated.

Last update: 2024-08-29 04:08:51 UTC


README

D 是一个骰子滚动库。它允许实现骰子滚动应用程序和逻辑。

另请参阅 zero-config/d-roll,这是一个实现 D 的应用程序。

安装

composer require zero-config/d:^2.0

实体

该库实现了以下实体

代表骰子的实体被称为 D,因为 die 在语言中是一个保留关键字,并且它与全世界玩家给它的简称相匹配。

服务

该库实现了以下服务

工厂

该库实现了以下工厂

数字生成器

数字生成器来自通过实现其接口的 hylianshield/number-generator 包,数字生成器可以替换。

用例

您可能想使用不同的数字生成器,因为默认的数字生成器随机性不足。让我们看看如何使用自定义数字生成器创建实现

<?php
use ZeroConfig\D\RollFactory;
use ZeroConfig\D\DieFactory;
use ZeroConfig\D\Interpreter;
use HylianShield\NumberGenerator\NumberGeneratorInterface;

/** @var NumberGeneratorInterface $myCustomNumberGenerator */
$interpreter = new Interpreter(
    new DieFactory(
        new RollFactory(),
        $myCustomNumberGenerator
    )    
);

$dice  = $interpreter->interpretDice('2d20+10');
$total = $dice->getModifier();

foreach ($dice->roll() as $roll) {
    $total += $roll->getValue();
}

echo $total;

这将产生一个介于 1150 之间的数字。

可以通过遍历骰子来使此操作更详细

<?php
/** @var \ZeroConfig\D\DiceInterface $dice */
foreach ($dice as $die) {
    echo sprintf(
        'd%d -> %d',
        $die->getNumberOfEyes(),
        $die->roll()->getValue()
    ) . PHP_EOL;
}
echo sprintf('+%d', $dice->getModifier());

这将输出类似的内容

d20 -> 19
d20 -> 4
+10

复杂解释。

如果您想同时解释多个骰子配置并且还需要它们分组和排序,解释器还有一个额外的 interpretList 方法。

<?php
/** ZeroConfig\D\DiceInterpreterInterface $interpreter */
$list = $interpreter->interpretList('3d6', 'D20', '4+10', '4+8', '1 d4');

列表将包含以下结构

20 => Dice => Die x1 (+0),
6  => Dice => Die x3 (+0),
4  => Dice => Die x3 (+8)