GithubHelp home page GithubHelp logo

granam / dice-rolls Goto Github PK

View Code? Open in Web Editor NEW
0.0 2.0 0.0 264 KB

Wanna random number? And what about dices, six-sides, ten-sided, seventy-five sided, whatever?

Home Page: https://www.drdplus.info

License: MIT License

PHP 100.00%
roll php dice random numbers

dice-rolls's Introduction

Build Status Test Coverage License

Let's roll!

Custom dices & rolls

Install

Let's roll!

<?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(); 
}

There are plenty of predefined templates of dices and rolls as 1d4, 1d6, 1d10. You can mix those and any else you create by CustomDice class.

Just think about your needs and check templates. Your requirements may be already satisfied by them.

Custom dices & rolls

There can be situations, where you need crazy combinations. Let's say one roll with 1d5 dice and three rolls with 1d74 dice.

It is easy. The hard part is only to find the way:

<?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();

Install

composer require drd/dice-roll

dice-rolls's People

Contributors

jaroslavtyc avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.