Skip to content

Commit

Permalink
Minor update to parsing
Browse files Browse the repository at this point in the history
- Now no longer strips whitespace to avoid unexpected results
- Add some typing
- Small efficiency gains
- Updated docs and examples
  • Loading branch information
JiFish committed May 15, 2022
1 parent 1d78d62 commit b736491
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 24 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ Here's a basic example
require 'ezdice.php';
$ezd = new ezdice\EZDice();
echo($ezd->roll('1d20+2d4').PHP_EOL);
echo($ezd->roll('1d20+2d4'));
echo(' Rolls:');
foreach($ezd->getDiceStates() as $die) {
echo(' ['.$die['value'].'] ');
}
```

Output:
Example Output:
```
23
Rolls: [17] [4] [2]
23 Rolls: [17] [4] [2]
```

## Installing with Composer
Expand Down Expand Up @@ -68,12 +68,13 @@ e.g. if you rolled `1d8+10+1d4-2` this method would return `+8`.

## Dice Notation

- Dice notation is in the form (number of dice)d(dice sides). e.g. `2d10`.
- Additional dice can be chained with + and - operators. e.g. `2d10+1d6`.
- Dice notation is in the form (number of dice)**d**(dice sides). e.g. `2d10`.
- Additional dice can be chained with **+** and **-** operators. e.g. `2d10+1d6`.
- Modifiers can also be specified. e.g. `2d10-5`
- d% can be used as a shorthand for a percentile dice. `1d%` and `1d100` are equivalent.
- Append a roll with -L to drop the lowest dice in that group, or -H to drop the highest. Dropped dice are excluded from the total. e.g. `2d20-L` will roll 2 twenty sided dice and drop the lowest.
- No notation is currently provided for fudge dice. You can use `1d3-2` instead.
- Whitespace, and anything else not recognised as a dice or a modifier, is treated like a **+** operator. e.g. `foo10 1d4bar1d4 5` is equivalent to `5+1d4+1d4+10`, or simply `2d4+15`.

## Replacing the random number generator

Expand Down
4 changes: 1 addition & 3 deletions example.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,5 @@

$ezd = new ezdice\EZDice();
echo($ezd->roll('1d20+2d4-L+6').PHP_EOL);
foreach($ezd->getDiceStates() as $die) {
echo(' ['.$die['value'].'] ');
}
print_r($ezd->getDiceStates());
echo($ezd->getModifier());
43 changes: 28 additions & 15 deletions ezdice.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ class EZDice {
private $states = [];
private $modifier = 0;

public function roll($diceStr)
public function roll(string $diceStr)
{
// Reset result values
$this->total = 0;
$this->states = [];
$this->modifier = 0;

// Strip all whitespace
$diceStr = preg_replace('/\s+/', '', $diceStr);
// No dice to roll?
if (is_numeric($diceStr)) {
$this->total = (int)$diceStr;
$this->modifier = $this->total;
return $this->total;
}

// Search for dice groups and modifiers
$re = '/(?<operator>[\+-])?(?<number>\d+)(?:[dD](?<sides>(?:\d+|%))(?:-(?<variant>[LlHh]))?)?/m';
Expand All @@ -32,35 +36,40 @@ public function roll($diceStr)
return $this->total;
}

private function addState($sides, $value, $dropped = false) {
private function addState(int $sides, int $value, bool $dropped = false): void
{
$this->states[] = [
'sides' => $sides,
'value' => $value,
'dropped' => $dropped
];
}

private function processGroup($group) {
private function processGroup(array $group): void
{
// Collect information about group
$operator = $group['operator'] ?? '+';
$number = $group['number'];
$sides = $group['sides'] ?? null;
$variant = (isset($group['variant']) ? strtoupper($group['variant']) : null);

// Scaler makes the output postive or negative
$scaler = ($operator=='-' ? -1 : 1);

// If sides isn't specified, this is a modifier
if($sides === null) {
if ($sides === null) {
$this->total += $number*$scaler;
$this->modifier += $number*$scaler;
return;
}

// Is it is a valid group of dice?
elseif ($sides && $number > 0) {
// 'd%' can be used as shorthand for 'd100'
$sides = $sides=="%" ? 100 : (int)$sides;
// Collect variant information from group
$variant = (isset($group['variant']) ? strtoupper($group['variant']) : null);

// 'd%' can be used as shorthand for 'd100'
$sides = $sides=="%" ? 100 : $sides;

// Is it is a valid group of dice?
if ($sides && $number > 0) {
// Roll Dice
$results = [];
for ($c = 0; $c < $number; $c++) {
Expand Down Expand Up @@ -89,19 +98,23 @@ private function processGroup($group) {
}
}

protected function getRandomNumber($max) {
protected function getRandomNumber(int $max): int
{
return mt_rand(1,$max);
}

public function getTotal() {
public function getTotal(): int
{
return $this->total;
}

public function getDiceStates() {
public function getDiceStates(): array
{
return $this->states;
}

public function getModifier() {
public function getModifier(): string
{
if (!$this->modifier) return "";
return sprintf("%+d",$this->modifier);
}
Expand Down

0 comments on commit b736491

Please sign in to comment.