summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrant Shangreaux <shshoshin@protonmail.com>2020-04-20 09:46:41 -0500
committerGrant Shangreaux <shshoshin@protonmail.com>2020-04-20 09:46:41 -0500
commit72c0386abe44180af41621d0556828bf8022f775 (patch)
tree0aca9c9b6e8fee56abfa3b25ae69784f06b4fc66
parenta052ebb929d70b3b853109cfe81d8016edb61ca6 (diff)
Clean: some refactoring of dice rolling functions
-rw-r--r--snekeyes.lisp23
1 files changed, 15 insertions, 8 deletions
diff --git a/snekeyes.lisp b/snekeyes.lisp
index 4f9dd95..c55dbac 100644
--- a/snekeyes.lisp
+++ b/snekeyes.lisp
@@ -33,15 +33,22 @@
(defun handle-dice-command (command dice-string)
"Returns a string result of rolling dice according to COMMAND and DICE-STRING."
- (cond ((equal "roll!" command) (format-dice-rolls (roll-dice dice-string)))
+ (cond ((equal "roll!" command) (roll! dice-string))
((equal "craps!" command) (craps))
(t "This should not be!!!!")))
-(defun roll-dice (dice-string)
- (let ((parsed (parse-dice-string dice-string)))
- (if parsed
- (loop for i upto (- (car parsed) 1)
- collect (roll (cdr parsed))))))
+(defun roll! (dice-string)
+ (let* ((parsed (parse-dice-string dice-string))
+ (num (or (car parsed) 1))
+ (sides (cdr parsed)))
+ (format-dice-rolls
+ (if sides (roll-dice num sides)
+ (roll-dice num)))))
+
+(defun roll-dice (num &optional (sides 6))
+ "Rolls the NUM dice with SIDES faces."
+ (loop for i upto (- num 1)
+ collect (roll sides)))
(defun format-dice-rolls (rolls)
(let ((total (total-dice-rolls rolls)))
@@ -57,10 +64,10 @@
(defun parse-dice-string (str)
"Parses strings like 3d6 and returns a cell with (NUMBER . SIDES)"
(multiple-value-bind (res matches) (ppcre:scan-to-strings +dice-regex+ str)
- (if res (cons (read-from-string (elt matches 0)) (read-from-string (elt matches 1))))))
+ (when res (cons (read-from-string (elt matches 0)) (read-from-string (elt matches 1))))))
(defun roll (sides)
- "Dice rolling function. Returns an integer from 1 to SIDES.
+ "Dice rolling function. Returns an integer from 1 to SIDES.
If it is a six sided die, returns the result of calling d6"
(if (= sides 6) (d6)
(+ 1 (random sides))))