Created
April 26, 2024 15:10
-
-
Save joinr/9dc617cea715374e13ecc094f2e6dae9 to your computer and use it in GitHub Desktop.
port of demo hangman project to maps
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 first-cloj.core) | |
(def list-of-words ["horse" "dog" "bird"]) | |
(def init {:guesses [] :incorrect-guesses 0}) | |
(def hang-vec | |
[" ______" | |
" | |" | |
" O |" | |
"/|\\ |" | |
" /\\ |" | |
" _____|"]) | |
(defn print-hang-map [{:keys [incorrect-guesses] :as ctx}] | |
(println "[THE END IS NEAR]") | |
(let [lines (subvec hang-vec 0 (inc incorrect-guesses))] | |
(doseq [line lines] | |
(println line))) | |
(println "You have " (- 5 incorrect-guesses) " turns remaining")) | |
;prints the initial message upon running the program | |
(defn print-welcome-message [] | |
(println "Welcome to [REDACTED]'s first clojure project, a hangman game.\n | |
Please enter your name to begin")) | |
;takes the user's name as input to pass in the print-message function | |
(defn newfunc [] | |
(println "Enter your name I guess:") | |
(->> (read-line) (println "Your name is: "))) | |
(defn read-single-char [] | |
(let [input (-> (read-line) .trim)] | |
(if (= (count input) 1) | |
(first input) | |
(do | |
(println "Please enter only one character.") | |
(recur)))));somehow this isn't a standard function | |
(defn take-a-guess [] | |
(println "what letter would you like to guess?") | |
(let [char (read-single-char)] | |
char)) | |
(defn game-over? [{:keys [hint target-word incorrect-guesses]}] | |
(or (= hint target-word) | |
(= incorrect-guesses 5))) | |
(defn add-guess [{:keys [target-word known guesses incorrect-guesses hint] :as ctx} letter] | |
(let [new-known (conj known letter) | |
new-hint (->> target-word | |
(map (fn [c] (get new-known c \_))) | |
(apply str))] | |
(assoc ctx :hint new-hint | |
:known new-known | |
:incorrect-guesses (if (= new-hint hint) | |
(inc incorrect-guesses) | |
incorrect-guesses) | |
:guesses (conj guesses letter)))) | |
(defn guess-handling | |
[{:keys [incorrect-guesses] :as ctx} guessed-letter] | |
(let [nxt (add-guess ctx guessed-letter)] | |
(if (= (nxt :incorrect-guesses) incorrect-guesses) | |
(println "The guessed letter matches a letter in the word!") | |
(println "The guessed letter does not match any letter in the word.")) | |
(print-hang-map nxt) | |
nxt)) | |
(defn handle-end-game [{:keys [hint target-word]}] | |
(if (= hint target-word) | |
(println "Congratulations! You've guessed the word correctly!") | |
(println "Sorry, you've run out of turns. The word was:" target-word))) | |
(defn turn-sequence | |
[{:keys [target-word hint guesses] :as ctx}] | |
(let [_ (println "This is the hint:" hint) | |
_ (println "Turns:" (count guesses)) | |
nxt (guess-handling ctx (take-a-guess))] | |
(if (game-over? nxt) | |
(handle-end-game nxt) | |
(recur nxt)))) | |
(defn run-game [] | |
(let [word (nth list-of-words (rand-int 3)) | |
ctx {:incorrect-guesses 0 | |
:guesses [] | |
:known #{} | |
:target-word word | |
:hint (apply str (repeat (count word) \_))}] | |
(print-welcome-message) | |
(newfunc) | |
(turn-sequence ctx))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment