From d0b1f82ef6d4e92a53f39ca149912cccfab93ebd Mon Sep 17 00:00:00 2001 From: "J. B. Saige" Date: Mon, 18 Dec 2017 14:12:33 -0800 Subject: [PATCH] This code matches what is in post 1. http://blog.gce.lupecode.com/2017/11/26/rolling-the-dice/ --- src/Dice.php | 20 -------------------- src/RollCommand.php | 19 +++++-------------- 2 files changed, 5 insertions(+), 34 deletions(-) diff --git a/src/Dice.php b/src/Dice.php index 837deb0..e142f8e 100644 --- a/src/Dice.php +++ b/src/Dice.php @@ -19,24 +19,4 @@ public static function roll(int $number, int $sides) return $rolls; } - public static function rollLow($number, $sides, $factor = 2) - { - $rolls = self::rollHigh($number, $sides, $factor); - foreach ($rolls as $num => $roll) { - $rolls[$num] = $sides + 1 - $roll; - } - - return $rolls; - } - - public static function rollHigh(int $number, int $sides, float $factor = 2) - { - $sides = (int)floor(pow($sides, $factor)); - $rolls = []; - for ($i = 0; $i < $number; $i++) { - $rolls[] = (int)ceil(pow(self::rollOne($sides), 1 / $factor)); - } - - return $rolls; - } } diff --git a/src/RollCommand.php b/src/RollCommand.php index 7f6ca1d..e046832 100644 --- a/src/RollCommand.php +++ b/src/RollCommand.php @@ -5,7 +5,6 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class RollCommand extends Command @@ -13,12 +12,10 @@ class RollCommand extends Command protected function configure() { - $this->setName("roll") - ->setDescription("Rolls fair dice.") - ->addArgument('Dice String', InputArgument::REQUIRED, 'e.g.: 2d6, 1d20+4') - ->addOption('h', null, InputOption::VALUE_NONE, 'Roll the dice with a bias towards higher numbers') - ->addOption('l', null, InputOption::VALUE_NONE, 'Roll the dice with a bias towards lower numbers') - ->addOption('f', null, InputOption::VALUE_REQUIRED, 'The factor of the weight', 2) + $this + ->setName("roll") + ->setDescription("Rolls fair dice.") + ->addArgument('Dice String', InputArgument::REQUIRED, 'e.g.: 2d6, 1d20+4') ; } @@ -28,13 +25,7 @@ protected function execute(InputInterface $input, OutputInterface $output) $matches = []; preg_match('/(\d+)d(\d+)\+?(\d)?/i', $input->getArgument('Dice String'), $matches); $modifier = empty($matches[3]) ? 0 : (int)$matches[3]; - if ($input->getOption('h')) { - $roll = $dice::rollHigh((int)$matches[1], (int)$matches[2], (float)$input->getOption('f')); - } elseif ($input->getOption('l')) { - $roll = $dice::rollLow((int)$matches[1], (int)$matches[2], (float)$input->getOption('f')); - } else { - $roll = $dice::roll((int)$matches[1], (int)$matches[2]); - } + $roll = $dice::roll((int)$matches[1], (int)$matches[2]); $output->writeln('Your roll without modifier was ' . array_sum($roll) . ' [' . implode('|', $roll) . '] + ' . $modifier . ' for a total of ' . (array_sum($roll) + $modifier)); }