PHP code example of holistic-agency / dice-roll

1. Go to this page and download the library: Download holistic-agency/dice-roll 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/ */

    

holistic-agency / dice-roll example snippets


#!/usr/bin/env php


use HolisticAgency\DiceRoll\DiceFactory;

// signed dice -xDy±z is actually treated as -(xDy±z)
$sub1d4 = DiceFactory::fromFormula('-1D4+1');
echo $sub1d4 . ':' . $sub1d4->roll() . PHP_EOL; // Between -5 and -1, not -3 to 0

#!/usr/bin/env php


use HolisticAgency\DiceRoll\DiceFactory;

$potionOfHealing = DiceFactory::fromFormula('2D4+2');
echo $potionOfHealing . ':' . $potionOfHealing->roll() . PHP_EOL;

$sneakAttack = DiceFactory::fromFormula('1D8+2+3D6');
echo $sneakAttack . ':' . $sneakAttack->roll() . PHP_EOL;

#!/usr/bin/env php


use HolisticAgency\DiceRoll\Dice;
use HolisticAgency\DiceRoll\MersenneTwister;

$best3of4 = (new Dice('4D6'))->bestOf(3);
$best3of4->setNumberGenerator(new MersenneTwister());
for ($i = 0; $i < 6; $i++) {
    echo "Roll of $best3of4:" . $best3of4->roll() . PHP_EOL;
}