drd/dice-rolls

此包已被弃用且不再维护。作者建议使用 drd-plus/dice-rolls 包。

可配置骰子投掷

6.1.0 2021-03-23 15:35 UTC

This package is auto-updated.

Last update: 2021-03-24 09:35:34 UTC


README

Build Status Test Coverage License

让我们开始投掷吧!

自定义骰子和投掷

安装

让我们开始投掷吧!

<?php
use Granam\DiceRolls\Templates\Rollers\Roller1d6;
use Granam\DiceRolls\Templates\Rollers\Roller2d6DrdPlus;

$roller1d6 = new Roller1d6();
$rolledValue = $roller1d6->roll();
if ($rolledValue === 6) {
    echo 'Hurray! You win!';
} else {
    echo 'Try harder';
}

$roller2d6Granam = new Roller2d6DrdPlus();
while (($roll = $roller2d6Granam->roll()) && $roll->getValue() <= 12) {
    echo 'Still no bonus :( ...';
}
echo 'There it is! Bonus roll comes, with final value of '
. $roll->getValue() . '
Rolls were quite dramatic, consider by yourself: ';
foreach ($roll->getDiceRolls() as $diceRoll) {
    echo 'Rolled number ' . $diceRoll->getRolledNumber() . ', evaluated as value ' . $diceRoll->getValue(); 
}

有很多预定义的骰子和投掷模板,例如1d4、1d6、1d10。您可以通过CustomDice类将它们混合,并创建任何您想要的。

只需思考您的需求,检查模板。您的要求可能已经被它们满足。

自定义骰子和投掷

有时您可能需要一些疯狂的组合。比如说,一个1d5的骰子投掷和三个1d74的骰子投掷。

这很简单。困难的部分只是找到方法

<?php
use Granam\DiceRolls\Templates\Dices\CustomDice;
use Granam\Integer\IntegerObject;
use Granam\DiceRolls\Templates\Dices\Dices;
use Granam\DiceRolls\Roller;
use Granam\DiceRolls\Templates\Evaluators\OneToOneEvaluator;
use Granam\DiceRolls\Templates\RollOn\NoRollOn;

$dice1d5 = new CustomDice(new IntegerObject(1) /* minimum of the dice */, new IntegerObject(5) /* maximum of the dice */);
$dice1d74 = new CustomDice(new IntegerObject(1) /* minimum of the dice */, new IntegerObject(74) /* maximum of the dice */);
$diceCombo = new Dices([$dice1d5, $dice1d74, $dice1d74, $dice1d74]);

$roller = new Roller(
    $diceCombo,
    new IntegerObject(1) /* roll with them all just once */,
    new OneToOneEvaluator() /* "what you roll is what you get" */,
    new NoRollOn() /* no bonus roll at all */,
    new NoRollOn() /* no malus roll at all */
);

// here it is!
$roller->roll();

安装

composer require drd/dice-roll