       (draw-cell ctx x y board))
     nil))
 
+;;------------------------------------------------------------
+;; STATE OF THE GAME
+;;------------------------------------------------------------
+
+(def state (atom {:piece (get-rand-piece)
+                  :position start-position
+                  :board empty-board}))
+
+;;------------------------------------------------------------
+;; STATE MONITOR
+;;------------------------------------------------------------
+
 (defn create-drawable-board
   "Creates a new drawable board, by combining the current piece with the current board."
   []

   :O {:name :O :coords [[0 0] [-1 0] [-1 1] [0 1]]}
   :T {:name :T :coords [[0 0] [-1 0] [1 0] [0 1]]}})

(def empty-board [[0 0 0 0 0 0 0 0 0 0]
                  [0 0 0 0 0 0 0 0 0 0]
                  [0 0 0 0 0 0 0 0 0 0]
                  [0 0 0 0 0 0 0 0 0 0]
                  [0 0 0 0 0 0 0 0 0 0]
                  [0 0 0 0 0 0 0 0 0 0]
                  [0 0 0 0 0 0 0 0 0 0]
                  [0 0 0 0 0 0 0 0 0 0]
                  [0 0 0 0 0 0 0 0 0 0]
                  [0 0 0 0 0 0 0 0 0 0]
                  [0 0 0 0 0 0 0 0 0 0]
                  [0 0 0 0 0 0 0 0 0 0]
                  [0 0 0 0 0 0 0 0 0 0]
                  [0 0 0 0 0 0 0 0 0 0]
                  [0 0 0 0 0 0 0 0 0 0]
                  [0 0 0 0 0 0 0 0 0 0]
                  [0 0 0 0 0 0 0 0 0 0]
                  [0 0 0 0 0 0 0 0 0 0]
                  [0 0 0 0 0 0 0 0 0 0]
                  [0 0 0 0 0 0 0 0 0 0]
                  [0 0 0 0 0 0 0 0 0 0]
                  [0 0 0 0 0 0 0 0 0 0]])

(defn get-rand-piece
  "Return a random piece."
  []
  (pieces (rand-nth (keys pieces))))

; The starting position of all pieces.
(def start-position [4 2])

(def state (atom {:piece (get-rand-piece)
                  :position start-position
                  :board empty-board}))

(defn get-cell-color
  [cell]
  (if (= 0 cell) "#EEE" "#F0F"))

(defn read-board
  "Get the current value from the given board position."
  [x y board]
  (get-in board [y x]))

(defn coord-inside?
  [x y]
  (and (<= 0 x 9) (<= 0 y 21)))

(defn write-to-board
  "Returns a new board with a value written to the given position."

        socket (.connect js/io url)]
    (.on socket "refresh" #(.reload js/location))))

;;------------------------------------------------------------
;; Entry Point
;;------------------------------------------------------------

(defn init []
  (size-canvas)
  (spawn-piece!)

  (connect-repl)
  (auto-refresh)
  )

