承接 kusabi/dice 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

kusabi/dice

Composer 安装命令:

composer require kusabi/dice

包简介

A table top dice library for PHP

README 文档

README

Tests codecov Licence Badge Release Badge Tag Badge Issues Badge Code Size

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.

composer require kusabi/dice

Or simply add it to your composer.json file

{
    "require": {
        "kusabi/dice": "^1.0"
    }
}

Using the library

The simplest way to use the library is by using the Dice factory class.

A simple example would be

use Kusabi\Dice\DiceFactory;

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

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.

use Kusabi\Dice\Dice;

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

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

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.

use Kusabi\Dice\SingleDice;

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

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

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.

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

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

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.

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

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.

use Kusabi\Dice\DiceFactory;

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

or more simply

use Kusabi\Dice\DiceFactory;

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

The class will throw an /InvalidArgumentException if it fails to parse the string so make sure you plan for that.

use Kusabi\Dice\DiceFactory;

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

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-11-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固