Skip to content

Instantly share code, notes, and snippets.

@fffej
Created July 4, 2009 12:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fffej/140570 to your computer and use it in GitHub Desktop.
Save fffej/140570 to your computer and use it in GitHub Desktop.
(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