PHP code example of kusabi / dice

1. Go to this page and download the library: Download kusabi/dice library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kusabi / dice example snippets


use Kusabi\Dice\DiceFactory;

$result = DiceFactory::createFromString('5d12+4')->getRoll();

use Kusabi\Dice\Dice;

$dice = new Dice(12, 2, 5);
$min = $dice->getMinimumRoll();
$max = $dice->getMaximumRoll();
$result = $dice->getRoll();

$string = (string) $dice; // 2d12+5

use Kusabi\Dice\SingleDice;

$dice = new SingleDice(4);
$min = $dice->getMinimumRoll();
$max = $dice->getMaximumRoll();
$result = $dice->getRoll();

$string = (string) $dice; // 1d4

use Kusabi\Dice\SingleDice;
use Kusabi\Dice\DiceModifier;

$dice = new DiceModifier(New SingleDice(12), 4);
$min = $dice->getMinimumRoll();
$max = $dice->getMaximumRoll();
$result = $dice->getRoll();

use Kusabi\Dice\Dice;
use Kusabi\Dice\DiceModifier;
use Kusabi\Dice\DiceGroup;
use Kusabi\Dice\SingleDice;

$dice = new DiceModifier(
    new DiceGroup(
        new SingleDice(12), 
        new SingleDice(12), 
        new SingleDice(12), 
        new Dice(12), 
        new Dice(12)
    ), 4
);
$min = $dice->getMinimumRoll();
$max = $dice->getMaximumRoll();
$result = $dice->getRoll();

use Kusabi\Dice\DiceFactory;

$dice = DiceFactory::createFromString('5d12+4');
$min = $dice->getMinimumRoll();
$max = $dice->getMaximumRoll();
$result = $dice->getRoll();

use Kusabi\Dice\DiceFactory;

$result = DiceFactory::createFromString('5d12+4')->getRoll();

use Kusabi\Dice\DiceFactory;

try {
    $result = DiceFactory::createFromString('5d12+4')->getRoll();
} catch(\InvalidArgumentException $exception) {
    echo "Could not parse the string";
}