This is my first attempt at Clojure! Apart from all game related issues, how is my code? Is there anything I can do to make it more idiomatic?
(ns hangman.core
(:import (java.net ServerSocket Socket SocketException)
(java.io PrintWriter InputStreamReader BufferedReader))
(:gen-class))
(defn guess [guess word]
(apply str (map #(let [x (str %)] (if (.contains guess x) x "*")) word)))
(defn listen [port]
(new ServerSocket port))
(def word "clojure")
(defn conn-handler [conn]
(let [in (:in @conn)
out (:out @conn)]
(.println out "Welcome to this simple hangman game.")
(def the-guess (atom ""))
(loop [conn conn]
(.println out (str "Guess the word: " (guess @the-guess word)))
(.flush out)
(let [g (str @the-guess (.readLine in))
res (guess g word)]
(if (= word res)
(do
(.println out res)
(.println out "Correct!")
(.flush out))
(do
(reset! the-guess g)
(recur conn)))))))
(defn -main [& args]
(println "Server running…")
(with-open [server (listen 3456)]
(loop []
(let [client (.accept server)
in (BufferedReader.(InputStreamReader.(.getInputStream client)))
out (PrintWriter. (.getOutputStream client))
conn (ref {:in in :out out})]
(println "Client connected")
(doto (Thread. #(conn-handler conn)) (.start))
(recur)))))