diff --git a/resources/public/cljs/wordle.cljs b/resources/public/cljs/wordle.cljs new file mode 100644 index 0000000..5c1f0a9 --- /dev/null +++ b/resources/public/cljs/wordle.cljs @@ -0,0 +1,106 @@ +;; taken from https://github.com/oxalorg/wordle-clojurescript +(ns wordle.core2) + +(def board-state (atom [])) +(def counter (atom 0)) +(def attempt (atom 0)) +(def word-of-the-day (atom "hello")) + +(defn write-letter [cell letter] + (set! (.-textContent cell) letter)) + +(defn make-cell [] + (let [cell (js/document.createElement "div")] + (set! (.-className cell) "cell") + cell)) + +(defn make-board [n] + (let [board (js/document.createElement "div")] + (set! (.-className board) "board") + ;; adding cells + (doseq [_ (range n)] + (let [cell (make-cell)] + (swap! board-state conj cell) + (.appendChild board cell))) + board)) + +(defn get-letter [cell] + (.-textContent cell)) + +(defn color-cell [idx cell] + (let [color (fn [el color] + (set! (-> el .-style .-backgroundColor) + color)) + letter (get-letter cell)] + (cond + (= letter (nth @word-of-the-day idx)) + (color cell "green") + + (contains? (set @word-of-the-day) letter) + (color cell "aqua") + + :else + (color cell "#333333")))) + +(defn check-solution [cells] + (doseq [[idx cell] (map-indexed vector cells)] + (color-cell idx cell)) + (= (mapv get-letter cells) + (vec @word-of-the-day))) + +(defn user-input [key] + (let [start (* 5 @attempt) + end (* 5 (inc @attempt))] + (cond + (and (re-matches #"[a-z]" key) + (< @counter end)) + (do + (write-letter (nth @board-state @counter) key) + (swap! counter inc)) + + (and (= key "backspace") + (> @counter start)) + (do + (swap! counter dec) + (write-letter (nth @board-state @counter) "")) + + (and (= key "enter") + (zero? (mod @counter 5))) + (do + (when (check-solution (subvec @board-state start end)) + (js/alert "You won")) + (swap! attempt inc)) + + ))) + +(defonce listener (atom nil)) + +(defn ^:dev/before-load unmount [] + (js/document.removeEventListener "keydown" @listener) + (let [app (js/document.getElementById "app")] + (set! (.-innerHTML app) ""))) + +(defn mount [] + (let [app (js/document.getElementById "app") + board (make-board 30) + input-listener + (fn [e] + (user-input (.toLowerCase(.-key e))))] + (.appendChild app board) + (js/console.log board) + (reset! listener input-listener) + (js/document.addEventListener + "keydown" + input-listener))) + +(mount) + +(js/console.log "mounted!") + +(comment + (do + (def sim ["a" "a" "a" "a" "a" "enter" + "e" "h" "o" "l" "o" + "backspace" "k" "enter" + "h" "e" "l" "l" "o" "enter"]) + (run! user-input sim))) diff --git a/resources/public/index.html b/resources/public/index.html index 6533776..ad40432 100644 --- a/resources/public/index.html +++ b/resources/public/index.html @@ -129,6 +129,7 @@
Wordle by @oxalorg. + Play the game by typing letters. The source code is loaded from here. +
+ + + diff --git a/script/release.clj b/script/release.clj index 0046085..00a2396 100755 --- a/script/release.clj +++ b/script/release.clj @@ -15,6 +15,9 @@ (fs/copy "resources/public/bookmarklet.html" "gh-pages" {:replace-existing true}) +(fs/copy "resources/public/wordle.html" "gh-pages" + {:replace-existing true}) + (fs/copy "resources/public/disable_auto_eval.html" "gh-pages" {:replace-existing true})