Last active
May 8, 2021 14:39
-
-
Save aaron-price/c0461320e10b7cc463dc9609ecbc87a1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns random-roll) | |
; My friends: "Let's play this stupid game over zoom. Everyone needs some dice" | |
; Me 10 minutes later: | |
(defn single-roll [sides] | |
(inc (rand-int sides))) | |
(defn roll | |
([sides] (roll 1 sides)) | |
([times sides] | |
(let [rolls (repeatedly times #(single-roll sides)) | |
sum (reduce + rolls)] | |
{:sum sum :rolls rolls})) | |
([times sides modifier] | |
(let [result (roll times sides)] | |
(assoc result :modified (+ modifier (:sum result)))))) | |
(comment | |
; This follows D&D markup syntax for dice rolls | |
; Roll a d20 | |
(roll 20) | |
; => {:sum 6 :rolls '(6)} | |
; Roll 3d6 | |
(roll 3 6) | |
; => {:sum 9 :rolls '(5 1 3)} | |
; Roll 2d8 + 1 | |
(roll 2 8 1)) | |
; => {:sum 10, :rolls '(7 3), :modified 11} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment