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

(ns triboard.core
  (:require
    [reagent.core :as reagent :refer [atom]]
    [clojure.string :as str]))

(enable-console-print!)
(set! *assert* false) ;; Set to true for the debug mode

(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
