Skip to content

Instantly share code, notes, and snippets.

@vitalipe
Created May 2, 2020 21:48
Show Gist options
  • Save vitalipe/ec5e1a3aa4b31697997df1564a99107e to your computer and use it in GitHub Desktop.
Save vitalipe/ec5e1a3aa4b31697997df1564a99107e to your computer and use it in GitHub Desktop.
(def player (ref {:health 500 :attack 10 :items #{}}))
(def mob1 (ref {:health 100 :attack 2 :items #{"big-banana"}}))
(def mob2 (ref {:health 100 :attack 4 :items #{"banana"}}))
(defn attack! [attacker enemy]
(dosync
(let [attacker-value (:attack @attacker)
enemy-value (:attack @enemy)]
(alter enemy update :health - attacker-value)
(alter attacker update :health - (/ enemy-value 2)))))
(defn loot! [to from]
(dosync
(when-let [item (first (:items @from))]
(alter to update :items conj item)
(alter from update :items disj item))))
(comment
(attack! player mob1)
(attack! player mob2)
(loot! player mob2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment