summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuke Shumaker <LukeShu@sbcglobal.net>2013-09-07 14:08:05 -0400
committerLuke Shumaker <LukeShu@sbcglobal.net>2013-09-07 14:08:05 -0400
commit1ca059968cf09d513f28f77b27fe792d6b510a00 (patch)
tree92b8f3360574cb761254f16915f5779db66eb309
parentab168ab329416eecb5564e2808d925dc1387a25e (diff)
clean up roll.php; add usage text
-rw-r--r--roll.php42
1 files changed, 28 insertions, 14 deletions
diff --git a/roll.php b/roll.php
index d888f64..c52f762 100644
--- a/roll.php
+++ b/roll.php
@@ -1,21 +1,35 @@
#!/usr/bin/env php
<?php
-$string = $argv[1];
+function usage() {
+ echo "Arguments are in the format [<COUNT>]d<SIZE>[+MOD]\n";
+}
-preg_match('/([0-9]*)d([0-9]+)(([+-][0-9]+))?/', $string, $matches);
-$dice = (int)$matches[1];
-$die_size = (int)$matches[2];
-@$mod = (int)$matches[3];
+function roll($input) {
+ preg_match('/([0-9]*)d([0-9]+)([+-][0-9]+)?/', $input, $matches);
+ if (sizeof($matches) < 2) {
+ usage();
+ return;
+ }
+ $dice = (int)$matches[1];
+ $die_size = (int)$matches[2];
+ @$mod = (int)$matches[3];
+ if ($dice<1) $dice = 1;
-if ($dice<1) $dice = 1;
+ $total = 0;
+ for ($i=0; $i < $dice; $i++) {
+ $v = mt_rand(1, $die_size);
+ echo $v.'+';
+ $total += $v;
+ }
+ $total += $mod;
+ echo $mod.' = '.$total."\n";
+}
-$total = 0;
-for ($i=0; $i < $dice; $i++) {
- $v = mt_rand(1, $die_size);
- echo $v.'+';
- $total += $v;
+if (sizeof($argv) == 1) {
+ usage();
+}
+array_shift($argv);
+foreach ($argv as $arg) {
+ roll($arg);
}
-echo $mod;
-$total += $mod;
-echo ' = '.$total."\n";