Created
April 28, 2015 16:33
-
-
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...
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
(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