Download the PHP package kusabi/dice without Composer
On this page you can find all versions of the php package kusabi/dice. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package dice
PHP Dice
A library designed to simulate the table-top dice for games like Dungeons and Dragons.
Compatibility and dependencies
This library is compatible with PHP version 5.6
, 7.0
, 7.1
, 7.2
, 7.3
and 7.4
.
This library has no dependencies.
Installation
Installation is simple using composer.
Or simply add it to your composer.json
file
Using the library
The simplest way to use the library is by using the Dice factory class.
A simple example would be
Using the dice class
This library contains 4 dice implementations that when used together can simulate a huge range of possibilities.
The first class is Dice
.
A Dice
object can represent most basic rolls in a table-top game.
It takes 3 optional parameters to set the number of sides, the multiplier and the offset.
Without any parameters it will default to represent 1d6.
Using the single dice class
The other three dice classes can be used in combination, to create much more complex dice setups.
The first of these is SingleDice
. A SingleDice
object takes a single parameter which represents the number of sides it has.
Using the dice modifier class
The DiceModifier
class uses the Decorator pattern to augment the results of another implementation of DiceInterface
.
It takes two arguments, the first is another object that implements DiceInterface
and the second is an integer to augment the result by.
The example below simulates how you might represent 1D12+4
.
Using the dice group class
The DiceGroup
can cluster multiple implementations of DiceInterface
together, and returns the sum of results from all of them.
Because one of those instances can be a Dice
, SingleDice
, DiceModifier
or even another DiceGroup
and because this object can itself by placed into a DiceModifier
instance, the possibilities are fairly sufficient.
The example below simulates how you might represent 5D12+4
.
Using the dice factory
The DiceFactory
makes creating a die implementation simpler.
You can pass it the common string form of a die instead of figuring out how to build it.
or more simply
The class will throw an /InvalidArgumentException
if it fails to parse the string so make sure you plan for that.