Skip to content

Instantly share code, notes, and snippets.

@ihercowitz
Created April 28, 2015 16:33
Show Gist options
  • Save ihercowitz/29e2335dfa6e3a1a7bff to your computer and use it in GitHub Desktop.
Save ihercowitz/29e2335dfa6e3a1a7bff to your computer and use it in GitHub Desktop.
A mastermind board game (http://en.m.wikipedia.org/wiki/Mastermind_(board_game)) implementation in clojure...
(ns mastermind.core
(:gen-class))
(defn generate-numbers []
(let [game [1 2 3 4 5 6]]
(->> game shuffle (take 4) vec)))
(defn guess [user answer]
(map #(= %1 %2) user answer))
(defn user-game []
(print "(mastermind) Do your guess: ")
(flush)
(let [u (str "[" (read-line) "]")]
(-> u read-string)))
(defn play []
(let [game (generate-numbers)]
(loop [u (user-game)
g game]
(let [result (guess u g)]
(cond
(= u "quit") (println "Bye...")
(every? true? result) (println "You won!")
:default
(do
(println result)
(recur (user-game) g)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment