Skip to content

Commit 24af5f8

Browse files
committed
Initial Commit
0 parents  commit 24af5f8

File tree

6 files changed

+356
-0
lines changed

6 files changed

+356
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
vendor/

composer.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"require": {
3+
"symfony/console": "^3.3"
4+
},
5+
"autoload": {
6+
"psr-4": {
7+
"Dice\\": "src/"
8+
}
9+
}
10+
}

composer.lock

Lines changed: 248 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dice

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env php
2+
3+
<?php
4+
5+
require_once __DIR__ . '/vendor/autoload.php';
6+
7+
use Dice\RollCommand;
8+
use Symfony\Component\Console\Application;
9+
10+
$app = new Application();
11+
12+
$app->add(new RollCommand());
13+
14+
$app->run();

src/Dice.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
namespace Dice;
4+
5+
class Dice
6+
{
7+
public static function rollOne(int $sides)
8+
{
9+
return (random_int(1, $sides));
10+
}
11+
12+
public static function roll(int $number, int $sides)
13+
{
14+
$rolls = [];
15+
for ($i = 0; $i < $number; $i++) {
16+
$rolls[] = self::rollOne($sides);
17+
}
18+
19+
return $rolls;
20+
}
21+
22+
public static function rollLow($number, $sides, $factor = 2)
23+
{
24+
$rolls = self::rollHigh($number, $sides, $factor);
25+
foreach ($rolls as $num => $roll) {
26+
$rolls[$num] = $sides + 1 - $roll;
27+
}
28+
29+
return $rolls;
30+
}
31+
32+
public static function rollHigh(int $number, int $sides, float $factor = 2)
33+
{
34+
$sides = (int)floor(pow($sides, $factor));
35+
$rolls = [];
36+
for ($i = 0; $i < $number; $i++) {
37+
$rolls[] = (int)ceil(pow(self::rollOne($sides), 1 / $factor));
38+
}
39+
40+
return $rolls;
41+
}
42+
}

src/RollCommand.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace Dice;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputArgument;
7+
use Symfony\Component\Console\Input\InputInterface;
8+
use Symfony\Component\Console\Input\InputOption;
9+
use Symfony\Component\Console\Output\OutputInterface;
10+
11+
class RollCommand extends Command
12+
{
13+
14+
protected function configure()
15+
{
16+
$this->setName("roll")
17+
->setDescription("Rolls fair dice.")
18+
->addArgument('Dice String', InputArgument::REQUIRED, 'e.g.: 2d6, 1d20+4')
19+
->addOption('h', null, InputOption::VALUE_NONE, 'Roll the dice with a bias towards higher numbers')
20+
->addOption('l', null, InputOption::VALUE_NONE, 'Roll the dice with a bias towards lower numbers')
21+
->addOption('f', null, InputOption::VALUE_REQUIRED, 'The factor of the weight', 2)
22+
;
23+
}
24+
25+
protected function execute(InputInterface $input, OutputInterface $output)
26+
{
27+
$dice = new Dice();
28+
$matches = [];
29+
preg_match('/(\d+)d(\d+)\+?(\d)?/i', $input->getArgument('Dice String'), $matches);
30+
$modifier = empty($matches[3]) ? 0 : (int)$matches[3];
31+
if ($input->getOption('h')) {
32+
$roll = $dice::rollHigh((int)$matches[1], (int)$matches[2], (float)$input->getOption('f'));
33+
} elseif ($input->getOption('l')) {
34+
$roll = $dice::rollLow((int)$matches[1], (int)$matches[2], (float)$input->getOption('f'));
35+
} else {
36+
$roll = $dice::roll((int)$matches[1], (int)$matches[2]);
37+
}
38+
$output->writeln('Your roll without modifier was ' . array_sum($roll) . ' [' . implode('|', $roll) . '] + ' . $modifier . ' for a total of ' . (array_sum($roll) + $modifier));
39+
}
40+
41+
}

0 commit comments

Comments
 (0)