 (def scores (reagent/cursor app-state [:scores]))
 (def current-player (reagent/cursor app-state [:player]))
 
+(def player-is-blue (reaction (= @current-player :blue)))
+(def end-of-game (reaction (nil? @current-player)))
+
+;; -----------------------------------------
+;; GAME LOOP
+;; -----------------------------------------
+
+(defn start-game-loop
+  "Attempt at creating a game loop to manage the animation and game state"
+  []
+  (let [is-human (fn [_] @player-is-blue)
+        is-ai (fn [_] (and (not @end-of-game) (not @player-is-blue)))
+        ai-events (chan 1 (filter is-ai))
+        player-events (chan 1 (filter is-human))
+        game-events (chan 1)]
+    
+    (go-loop []
+      (alt!
+        player-events
+        ([coord]
+          (swap! app-state play-move coord)
+          (put! ai-events :start-ai))
+        
+        game-events
+        ([msg]
+          (when (= msg :new-game)
+            (reset! app-state (new-game))
+            (put! ai-events :start-ai)))
+        
+        ai-events
+        ([msg]
+          (condp = msg
+            :start-ai (let [auto-move (time (best-move @app-state (:player @app-state)))]
+                        (swap! app-state play-move auto-move)
+                        (put! ai-events :start-ai))
+            )))
+      
+      (recur))
+    {:game-events game-events
+     :player-events player-events}))
+
+(defonce game-loop (start-game-loop))
+
 
 ;; -----------------------------------------
 ;; DISPLAY

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


(defn take-empty-cell-move
  "Create a move to take an empty cell" 
  [player point]
  {:move point
   :winner player
   :looser :empty
   :taken [point]})

