~phl/qttt

d690e6b9da3ec6674742835f13465777d1908180 — Filip Miletic 1 year, 10 months ago 48f77d9 master
Showing info on which player made the mark
M src/cljs/qttt/db.cljs => src/cljs/qttt/db.cljs +17 -4
@@ 1,15 1,28 @@
(ns qttt.db)

(goog-define *version* "development")

;; TODO: add schema
(def default-value
  {:game {:name "QTTT"
          :version "0.0.1"}
          :version *version*}
   
   :board {

           


           }
   
   :current-selection {
                       {:cell 1}
                       {:cell 2}
                       }

   :players {}
   :players {
             :ids [0 1]
             :current 0
             }
  
   :turn 0

   :state {} })
   :state {}})

M src/cljs/qttt/events/board.cljs => src/cljs/qttt/events/board.cljs +18 -5
@@ 7,10 7,23 @@
      false
      true)))

;; If entaglement happens we collapse. Collapsing is when player chooses an
;; outcome on the board. THis is done by observing the board by the selection
;; of a cell.

(re-frame/reg-event-db
 :toggle-cell
 (fn [db [_ cell subcell]]
   (if (valid-cell? db cell subcell)
     (assoc-in db [:board cell subcell] {:toggle true})
     db)))
 :switch-player
 (fn [db]
   (update-in db [:players :current] (fn [current]
                                       (if (even? (inc current))
                                         0
                                         1)))))

(re-frame/reg-event-fx
 :toggle-cell
 (fn [{:keys [db]} [_ cell subcell]]
   {:db (if (valid-cell? db cell subcell)
          (assoc-in db [:board cell subcell] {:toggle true
                                              :player (get-in db [:players :current])})
          db)
    :dispatch [:switch-player]}))

M src/cljs/qttt/subs/board.cljs => src/cljs/qttt/subs/board.cljs +5 -0
@@ 2,6 2,11 @@
  (:require [re-frame.core :as re-frame]))

(re-frame/reg-sub
 :players
 (fn [db]
   (:players db)))

(re-frame/reg-sub
 :cell-meta
 (fn [db [_ cell subcell]]
   (get-in db [:board cell subcell])))

M src/cljs/qttt/views/board.cljs => src/cljs/qttt/views/board.cljs +8 -4
@@ 30,9 30,13 @@
(defmethod create-cell :open [{:keys [parent cell]}]
  (let [data (re-frame/subscribe [:cell-meta parent cell])]
    [:td {:on-click #(re-frame/dispatch [:toggle-cell parent cell])
          :class (if (:toggle @data) "mark" "open")}]))
          :class (if (:toggle @data) "mark" "open")}
     [:span (:player @data)]]))

(defn board []
  [:table.board
   [:tbody
    (generate-cells :superposition nil)]])
  (let [players (re-frame/subscribe [:players])]
    [:div
     [:table.board
      [:tbody
       (generate-cells :superposition nil)]]
     [:div (str @players)]]))