 (def ai-players (reaction (get-in @game [:ai-players])))
 (def end-of-game (reaction (nil? @current-player)))
 
+(defn is-ai? [player]
+  (contains? @ai-players player))
+
+
+;; -----------------------------------------
+;; GAME LOOP
+;; -----------------------------------------
+
 (defn update-game! [f & args]
   (swap! app-state update-in [:games] conj (apply f @game args)))
 
-(defn new-game! []
-  (swap! app-state assoc-in [:games] (list (new-game))))
-
-(defn is-ai? [player]
-  (contains? @ai-players player))
-
-(defn restart-game! []
-  (swap! app-state update-in [:games] #(take-last 1 %)))
+(defn- handle-ai! []
+  (let [ai-algo (get @ai-players @current-player)
+        move (ai-algo @game @current-player)]
+    (update-game! play-move move)))
 
 (defn cancel-last-move! []
   (swap! app-state update-in [:games]

          new-list)))
    ))


;; -----------------------------------------
;; GAME LOOP
;; -----------------------------------------

(defn- handle-ai! []
  (let [ai-algo (get @ai-players @current-player)
        move (ai-algo @game @current-player)]
    (update-game! play-move move)))

(defn start-game-loop
  "Manage transitions between player moves, ai moves, and generic game events"

(defn show-top-panel
  [scores player]
  [:div.scores
   [top-panel-button #(send-game-event! :new-game) (special-char "&#9733;")]
   [top-panel-button #(swap! app-state update :help not) "?"]
   (show-scores scores player)
   [top-panel-button #(send-game-event! :restart) (special-char "&#x21bb;")]
   [top-panel-button #(send-game-event! :undo) (special-char "&#x21A9;")]])

(defn max-board-height []
  (- (-> (dom/getWindow) dom/getViewportSize .-height) 100))
