Skip to content

Instantly share code, notes, and snippets.

@minikomi
Created October 15, 2021 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minikomi/8072790b0b928d0a27fd235bb05e5d5c to your computer and use it in GitHub Desktop.
Save minikomi/8072790b0b928d0a27fd235bb05e5d5c to your computer and use it in GitHub Desktop.
lightbulbs guessing
(ns lightbulbs.app.core
(:require [reagent.dom :as rdom]
[reagent.core :as r]))
(defn gen-default-state [n]
{:lightbulbs (mapv #(vector % false) (vec (shuffle (range n))))
:snapshots []})
(def state
(r/atom (gen-default-state 8)))
(defn adjust []
[:div
[:h2 "lightbulbs"]
[:p
{:style {:margin "10px"}}
"Adjust number of lightbulbs. Resets state."]
[:input {:type :range :min 0 :max 20 :value (-> @state :lightbulbs count)
:on-change #(reset! state
(gen-default-state (js/parseInt (.. % -target -value) 10)))}]
[:span (str (-> @state :lightbulbs count))]])
(defn switches []
[:div
[:h2 "switches"]
[:p
{:style {:margin "10px"}}
"Use the switches to turn on / off lights. "
[:br]
"Press " [:strong "Check Lights"] " to visit the light room"]
[:ul
(for [[i [n checked]] (map-indexed vector (-> @state :lightbulbs))]
[:li {:key n :style {:display 'inline}}
[:input
{:type :checkbox
:checked (if checked "checked" "")
:on-change #(swap! state update-in [:lightbulbs i 1] not)}]])]])
(defn guesses []
[:div
[:h2
"Visits"]
[:ul
(for [[n s] (map-indexed vector (@state :snapshots))]
[:li {:key n}
[:hr]
[:div
[:h3 (str "visit: " n)]
[:h4 "Switches"]
[:ul
(for [[n checked] s]
[:li {:key n :style {:display 'inline}}
[:input
{:type :checkbox :readOnly true :checked (if checked "checked" "")}]])]
[:h4 "Lights"]
[:ul
(for [[n checked] (sort-by first s)]
[:li {:key n :style {:display 'inline}}
(if checked "💡" "⚫ ")])]]])]
[:h3 [:button {:on-click #(swap! state update :snapshots conj (@state :lightbulbs))} "Check Lights"]]])
(defn app []
[:div
[adjust]
[:hr]
[switches]
[:hr]
[guesses]])
(defn render []
(rdom/render [app] (.getElementById js/document "root")))
(defn ^:export main []
(render))
(defn ^:dev/after-load reload! []
(render))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment