core.cljs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. (ns automata.core
  2. (:require [reagent.core :as reagent :refer [atom]]
  3. [automata.rules]
  4. [automata.init]
  5. [cljs.core.async :as async :refer [chan <! >! timeout put!]])
  6. (:require-macros [cljs.core.async.macros :refer [go alt!]]))
  7. (enable-console-print!)
  8. ;; define your app data so that it doesn't get over-written on reload
  9. (defonce app-state (atom {
  10. :text "Hello world!"
  11. :binary-rule (automata.rules.preset-rules :110)
  12. :rule (automata.rules.string-to-rule (automata.rules.preset-rules :110))
  13. :board [(automata.init.init-random 15)]
  14. :interv 500
  15. :go false
  16. :style {
  17. :cell-size 40
  18. :border-thickness 0
  19. :active-color "red"
  20. :inactive-color "blue"
  21. }
  22. :desired-columns 15
  23. }))
  24. ;(swap! app-state assoc :board [(automata.init.init-random 55)])
  25. (defn Cell [row ind value]
  26. [:td
  27. {
  28. :style {:background-color (if value "blue" "lightblue")}
  29. :key (str "r" row "c" ind)
  30. }])
  31. (defn Row [ind row]
  32. [:tr
  33. {:key (str "row" ind)}
  34. (map-indexed (partial Cell ind) row)])
  35. (defn Board []
  36. [:table
  37. {:id "board"}
  38. [:tbody
  39. (map-indexed Row (@app-state :board))]])
  40. (defn step []
  41. (swap! app-state update-in [:board] conj (automata.rules.proc (last (@app-state :board)) (@app-state :rule))))
  42. (defn start-stepping []
  43. (go
  44. (while (@app-state :go)
  45. (step)
  46. (<! (timeout 100)))))
  47. (defn Controls []
  48. [:div
  49. {:class "controls"}
  50. [:button {:on-click (fn [] (swap! app-state update-in [:go] not) (start-stepping))} (if (@app-state :go) "stop" "GO")]
  51. [:button {:on-click (fn [] (step))} "step"]
  52. [:button {:on-click (fn [] (swap! app-state assoc :board [(automata.init.init-random 55)]))} "reset"]
  53. [:hr]
  54. "columns"
  55. [:input {:type "number" :min 1 :value (@app-state :desired-columns) :on-change (fn [e] (swap! app-state assoc :desired-columns e.target.value))}]
  56. [:button {:on-click (fn [] (println "change number of columns to" (@app-state :desired-columns)))} "change"]
  57. [:hr]
  58. "rules"
  59. ;(println (interleave (automata.rules.rule-order) (@app-state :binary-rule)))
  60. (println "hi" (@app-state :binary-rule) automata.rules.rule-order)
  61. ])
  62. (defn App []
  63. [:div
  64. [Controls]
  65. [Board]])
  66. (reagent/render-component [App]
  67. (. js/document (getElementById "app")))
  68. (defn on-js-reload []
  69. ;; optionally touch your app-state to force rerendering depending on
  70. ;; your application
  71. ;; (swap! app-state update-in [:__figwheel_counter] inc)
  72. )