Skip to content

Instantly share code, notes, and snippets.

@CrypticSwarm
Created September 24, 2010 17:15
Show Gist options
  • Save CrypticSwarm/595711 to your computer and use it in GitHub Desktop.
Save CrypticSwarm/595711 to your computer and use it in GitHub Desktop.
(def items [:rock :paper :scissors])
(defn winner [m1 m2]
(mod (- m1 m2 1) 3))
(defn player-string [name symbol]
(str name (nth items symbol) " wins!"))
(defn player-wins [m1 m2]
(str (nth items m1) " vs. " (nth items m2) " !! "
(nth [(player-string "player1" m1)
(player-string "player2" m2) "tie"] (winner m1 m2))))
(defn indexof-failed [seq val]
(let [i 0]
(if (some (fn [cur]
(if (= cur val)
val
(= (inc i) 0)))
seq)
i
nil)))
(defn indexof [seq val]
(letfn [(find-val [seq val count]
(cond (= seq ()) nil
(= (first seq) val) count
:else (find-val (rest seq) val (inc count))))]
(find-val seq val 0)))
(defn play [m1 m2]
(player-wins (indexof items m1) (indexof items m2)))
(play :rock :paper)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment