core.cljs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. :rule (automata.rules.string-to-rule (automata.rules.preset-rules :110))
  12. :board [(automata.init.init-random 15)]
  13. :interv 500
  14. :go false
  15. :style {
  16. :cell-size 40
  17. :border-thickness 0
  18. :active-color "red"
  19. :inactive-color "blue"
  20. }
  21. }))
  22. ;(swap! app-state assoc :board [(automata.init.init-random 55)])
  23. (defn Cell [row ind value]
  24. [:td
  25. {
  26. :style {:background-color (if value "red" "blue")}
  27. :key (str "r" row "c" ind)
  28. }])
  29. (defn Row [ind row]
  30. [:tr
  31. {:key (str "row" ind)}
  32. (map-indexed (partial Cell ind) row)])
  33. (defn Board []
  34. [:table
  35. (map-indexed Row (@app-state :board))])
  36. (defn step []
  37. (swap! app-state update-in [:board] conj (automata.rules.proc (last (@app-state :board)) (@app-state :rule))))
  38. ;(go (dotimes [n 100]
  39. ; (<! (timeout 1000))
  40. ; (println (str "beat" n))
  41. ; (step)))
  42. (defn App []
  43. [Board])
  44. (reagent/render-component [App]
  45. (. js/document (getElementById "app")))
  46. (defn on-js-reload []
  47. ;; optionally touch your app-state to force rerendering depending on
  48. ;; your application
  49. ;; (swap! app-state update-in [:__figwheel_counter] inc)
  50. )