        (dom/td nil))))))
 
 (defn find-piece [position]
-  (first (filter #(and
-                 (= (:x position) (:x %))
-                 (= (:y position) (:y %)))
-               (:cells (om/root-cursor app-state)))))
+  (let [{:keys [:blue-team :red-team :balls] :as units} (:cells (om/root-cursor app-state))]
+    (first (filter #(and
+                   (= (:x position) (:x %))
+                   (= (:y position) (:y %)))
+                 (flatten (vals units))))))
+
+(defn coords [m]
+  (dissoc m :move))
+
+(defn all-units [cells]
+  (let [{:keys [:blue-team :red-team :balls] :as units} (:cells (om/root-cursor app-state))]
+  units))
+
+(defn red-unit? [x y]
+  (let [{:keys [:red-team]} (:cells (om/root-cursor app-state))]
+    (first (filter #(and
+             (= x (:x %))
+             (= y (:y %)))
+           red-team))))
+
+(defn blue-unit? [x y]
+  (let [{:keys [:blue-team]} (:cells (om/root-cursor app-state))]
+    (first (filter #(and
+             (= x (:x %))
+             (= y (:y %)))
+           blue-team))))
+
+(defn ball? [x y]
+  (let [{:keys [:balls]} (:cells (om/root-cursor app-state))]
+    (first (filter #(and
+             (= x (:x %))
+             (= y (:y %)))
+           balls))))
 
 (defn highlight-tile [e]
  (set! (.. e -target -style -background) "gray"))
-
-(defn update-piece-position [e state]
-  (om/transact! (om/root-cursor app-state) :cells #(conj {:type :my-team :x 1 :y 8})))
 
 (defn player-view [cursor owner]
   (reify

    (render [_]
      (dom/div #js {:className "blue-team"}))))

(defn tile-view [cursor owner]
  (reify
    om/IRenderState
    (render-state [this {:keys [x y move] :as position}]
      (let [found (find-piece position)]
        (dom/td #js {:onClick #(put! move position)}
          (cond
            (= (:type found) :my-team)
              (om/build player-view found {:state {:move move}})
            (= (:type found) :opponent)
               (om/build opponent-view cursor)
            (= (:type found) :loose-ball)
               (om/build ball-view cursor)))))))

(defn ball-view [cursor owner]
  (reify
    om/IRender
    (render [_]
          (dom/div #js {:className "ball"}))))

(defn board-view [app owner]
  (reify
     om/IInitState
     (init-state [_]
       {:move (chan)})
    om/IWillMount
    (will-mount [_]
      (let [move (om/get-state owner :move)]
        (go (loop []
          (let [player-to-move (find-piece (<! move))]
            (let [{:keys [x y] :as position} (<! move)]
              (if (find-piece position)
                (println "CAN'T MOVE THERE")
                (om/transact! app :cells
                  (fn [cs]
                    (vec (map
                           #(if (= player-to-move %)
                                  (assoc % :x x :y y)
                                  %)
                         cs)))))))
            (recur)))))
    om/IRenderState
    (render-state [this {:keys [move]}]
      (dom/div #js {:className "container"}

          (vec (for [y board-size]
            (apply dom/tr #js {:className (border-top y)}
              (vec (for [x board-size]
                (om/build tile-view all-units
                  {:state {:move move :x x :y y}})))))))
        (bench)))))

