Advertisement
Guest User

Untitled

a guest
May 11th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (ns quick-chess.core
  2.   (:require [reagent.core :as r]))
  3.  
  4. (enable-console-print!)
  5.  
  6. (println (str "Reloaded: " (.toLocaleTimeString (js/Date.))))
  7.  
  8. (def init-pieces
  9.   [[\u265C \u265E \u265D \u265B \u265A \u265D \u265E \u265C]
  10.    [\u265F \u265F \u265F \u265F \u265F \u265F \u265F \u265F]
  11.    ["" "" "" "" "" "" "" ""]
  12.    ["" "" "" "" "" "" "" ""]
  13.    ["" "" "" "" "" "" "" ""]
  14.    ["" "" "" "" "" "" "" ""]
  15.    [\u2659 \u2659 \u2659 \u2659 \u2659 \u2659 \u2659 \u2659]
  16.    [\u2656 \u2658 \u2657 \u2655 \u2654 \u2657 \u2658 \u2656]])
  17.  
  18. (def init-colors
  19.   (vec
  20.    (take 8 (cycle [(vec (take 8 (cycle '("#a3854b" "#d3d2cf"))))
  21.                    (vec (take 8 (cycle '("#d3d2cf" "#a3854b"))))]))))
  22.  
  23. (defonce init-state
  24.   {:width 500
  25.    :height 500
  26.    :board
  27.    (mapv
  28.     (fn [row-pieces row-colors]
  29.       (mapv
  30.        (fn [piece color]
  31.          {:piece piece :color color})
  32.        row-pieces row-colors))
  33.     init-pieces init-colors)})
  34.  
  35. (defonce app-state (r/atom init-state))
  36. (swap! app-state assoc-in [:board 7 4 :color] "#4286f4")
  37. #_(reset! app-state init-state)
  38.  
  39. (defn board-style
  40.   [data]
  41.   {:border "1px solid black"
  42.    :width (:width data)
  43.    :height (:height data)})
  44.  
  45. (defn tr-style
  46.   [data]
  47.   {:width (:width data)
  48.    :height (/ (:height data) (count (:board data)))})
  49.  
  50. (defn td-style
  51.   [data color]
  52.   {:border "1px solid black"
  53.    :width (/ (:width data) (count (first (:board data))))
  54.    :height (/ (:height data) (count (:board data)))
  55.    :background color})
  56.  
  57. (defn piece-style
  58.   [data]
  59.   {:text-align "center"
  60.    :font-size (*
  61.                (min (:height data) (:width data))
  62.                0.07)})
  63.  
  64. (defn board-view
  65.   "the board"
  66.   [state]
  67.   [:table {:style (board-style state)}
  68.    (map-indexed
  69.     (fn [row-idx row]
  70.       ^{:key row-idx} [:tr {:style (tr-style state)}
  71.                    (map-indexed
  72.                     (fn [col-idx elem]
  73.                       ^{:key col-idx} [:td {:style (td-style state (:color elem))}
  74.                                        [:div {:style (piece-style state)} (:piece elem)]])
  75.                     row)])
  76.     (:board state))])
  77.  
  78. (defn app-view
  79.   "main component of our app"
  80.   []
  81.   [:div
  82.    [board-view @app-state]])
  83.  
  84. ;; -------------------------
  85. ;; Initialize app
  86.  
  87. (defn mount-root []
  88.   (r/render [app-view] (.getElementById js/document "app")))
  89.  
  90. (defn init! []
  91.   (mount-root))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement