123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- (ns automata.core
- (:require [reagent.core :as reagent :refer [atom]]
- [automata.rules]
- [automata.init]
- [cljs.core.async :as async :refer [chan <! >! timeout put!]])
- (:require-macros [cljs.core.async.macros :refer [go alt!]]))
- (enable-console-print!)
- ;; define your app data so that it doesn't get over-written on reload
- (defonce app-state (atom {
- :text "Hello world!"
- :binary-rule (automata.rules.preset-rules :110)
- :rule (automata.rules.string-to-rule (automata.rules.preset-rules :110))
- :board [(automata.init.init-random 15)]
- :interv 500
- :go false
- :style {
- :cell-size 40
- :border-thickness 0
- :active-color "red"
- :inactive-color "blue"
- }
- :desired-columns 15
- }))
- ;(swap! app-state assoc :board [(automata.init.init-random 55)])
- (defn Cell [row ind value]
- [:td
- {
- :style {:background-color (if value "blue" "lightblue")}
- :key (str "r" row "c" ind)
- }])
- (defn Row [ind row]
- [:tr
- {:key (str "row" ind)}
- (map-indexed (partial Cell ind) row)])
- (defn Board []
- [:table
- {:id "board"}
- [:tbody
- (map-indexed Row (@app-state :board))]])
- (defn step []
- (swap! app-state update-in [:board] conj (automata.rules.proc (last (@app-state :board)) (@app-state :rule))))
- (defn start-stepping []
- (go
- (while (@app-state :go)
- (step)
- (<! (timeout 100)))))
- (defn Controls []
- [:div
- {:class "controls"}
- [:button {:on-click (fn [] (swap! app-state update-in [:go] not) (start-stepping))} (if (@app-state :go) "stop" "GO")]
- [:button {:on-click (fn [] (step))} "step"]
- [:button {:on-click (fn [] (swap! app-state assoc :board [(automata.init.init-random 55)]))} "reset"]
- [:hr]
- "columns"
- [:input {:type "number" :min 1 :value (@app-state :desired-columns) :on-change (fn [e] (swap! app-state assoc :desired-columns e.target.value))}]
- [:button {:on-click (fn [] (println "change number of columns to" (@app-state :desired-columns)))} "change"]
- [:hr]
- "rules"
- ;(println (interleave (automata.rules.rule-order) (@app-state :binary-rule)))
- (println "hi" (@app-state :binary-rule) automata.rules.rule-order)
- ])
- (defn App []
- [:div
- [Controls]
- [Board]])
- (reagent/render-component [App]
- (. js/document (getElementById "app")))
- (defn on-js-reload []
- ;; optionally touch your app-state to force rerendering depending on
- ;; your application
- ;; (swap! app-state update-in [:__figwheel_counter] inc)
- )
|