Last active
August 29, 2015 14:04
-
-
Save MonkeyIsNull/cf737bec0b7c95b54a42 to your computer and use it in GitHub Desktop.
Simple Battle System
This file contains hidden or 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
;; Die Roll stuff | |
(defn die-roll [numSides] | |
(int (+ 1 (rand numSides)))) | |
(defn d4 [] | |
(die-roll 4)) | |
(defn d6 [] | |
(die-roll 6)) | |
(defn d8 [] | |
(die-roll 8)) | |
(defn d10 [] | |
(die-roll 10)) | |
(defn d20 [] | |
(die-roll 20)) | |
(defn d100 [] | |
(die-roll 100)) | |
(defn damage [] (d6)) | |
(defn ahit? [acc] | |
(<= (d100) acc)) | |
(defn defend? [acc] | |
(<= (d100) acc)) | |
;; Testing code | |
;;(ahit? 5) | |
;;(defend? 67) | |
(defn calcdamage [p] | |
(if (defend? (:acc p)) | |
p | |
(assoc p :hitpoints (- (:hitpoints p) (damage))))) | |
(defn check4dmg [twochars] | |
(let [[p1 p2] twochars] | |
(if (ahit? (:acc p1)) | |
(list p1 (calcdamage p2)) | |
twochars))) | |
(defn isdead? [p] | |
(if (<= (:hitpoints p) 0) | |
true | |
false)) | |
(defn dead-report [p] | |
p) | |
(defn run-battle [twochars] | |
(let [[p1 p2] (check4dmg twochars)] | |
(if (isdead? p2) | |
(dead-report (list p1 p2)) | |
(recur [p2 p1])))) | |
(defn one-round [twochars] | |
(let [[p1 p2] (check4dmg twochars)] | |
(if (isdead? p2) | |
(dead-report (list p1 p2)) | |
[p2 p1]))) | |
;; Def up the characters we made in the Factor code | |
(def urg {:name "Urg" :hitpoints 20 :acc 70}) | |
(def conan {:name "Conan" :hitpoints 25 :acc 65}) | |
;; Works!, disabled, just uncomment the below line and eval it | |
(run-battle [urg conan]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment