 (defn flip [board]
   (map (comp vec reverse) board))
 
+(defn coordinates []
+  (for [x (range 4) y (range 4)] [x y]))
+
+(defn place [board]
+  (let [index (first (shuffle (filter #(not (get-in board %)) (coordinates))))]
+    (print index)
+    (assoc-in board index 2)))
+
+(defn update [board]
+  (print board)
+  (let [new-board (pack board)]
+    (cond
+      (= board new-board) new-board
+      :else (place new-board))))
+
 (defmulti next-board (fn [d b] d))
 
 (defmethod next-board :left [_ board]
-  (pack board))
+  (-> board update))
 
 (defmethod next-board :right [_ board]
-  (-> board flip pack flip))
+  (-> board flip update flip))
 
 (defmethod next-board :up [_ board]
-  (-> board rotate pack rotate))
+  (-> board rotate update rotate))
 
 (defmethod next-board :down [_ board]
-  (-> board rotate flip pack flip rotate))
+  (-> board rotate flip update flip rotate))
 
 (defmethod next-board nil [_ board]
   board)

  (take 4 (concat row (repeat nil))))

(defn collapse [row]
  row)

(defn pack [board]
  (map #(-> % strip collapse pad) board))

(defn rotate [board]
  (apply map vector board))


(def app-state (atom {:state :playing
                      :board [[nil nil nil nil]
                              [  2   2   2   2]
                              [nil nil nil   2]
                              [nil nil nil nil]]}))

