Created
July 4, 2009 12:55
-
-
Save fffej/140570 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
(defstruct game :current-score :throws) | |
(def darts | |
(concat | |
(range 1 21) | |
(map (partial * 2) (range 1 21)) | |
(map (partial * 3) (range 1 21)) | |
'(25 50))) | |
(def finishes | |
(set (concat (map (partial * 2) (range 1 21)) '(25 50)))) | |
(defn next-throw | |
"Given a value return the next valid darts" | |
[n] | |
(filter | |
(partial not= 1) | |
(filter | |
(partial <= 0) | |
(map (fn [x] | |
(let [result (- n x)] | |
(if (not= result 0) | |
result | |
(if (finishes x) 0 -1)))) | |
darts)))) | |
(defn next-dart | |
"Given a value, return the next darts and record the state" | |
[d] | |
(map (fn [new-score] | |
(struct game | |
new-score | |
(conj (:throws d) new-score))) | |
(next-throw (:current-score d)))) | |
(defn finished? [d] (zero? (:current-score d))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment