 (defn init []
   (connect-repl)
   (size-canvas)
-  (draw-board)
+  (draw-board (:board @state))
 
   (auto-refresh)
   )


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

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

(defn write-to-board!
  "Writes a given value to the x,y position on the board."
  [ x y value ]
  (if (coord-inside? x y)
    (swap! state assoc-in [:board y x] value)))

(defn write-coord-to-board!
  [[cx cy] x y]
    (write-to-board! (+ cx x) (+ cy y) 1))

(defn write-piece-to-board!
  "Writes a given piece to the board."
  [piece x y]
  (doall (map #(write-coord-to-board! % x y) piece)))

(defn clear-board!
  "Clears the board."
  []
  (swap! state assoc :board empty-board))

(defn rotate-piece
  "Create a new piece by rotating the given piece clockwise."


(defn coord-empty?
  "Determines if the given coordinate on the board is empty."
  [x y board]
  (zero? (read-board x y board)))

(defn coord-fits?
  "Determines if the given relative coordinate fits at the position on the board."
  [[cx cy] x y board]
  (let [abs-x (+ x cx)
        abs-y (+ y cy)]
    (and (coord-inside? abs-x abs-y)
         (coord-empty? abs-x abs-y board))))

(defn piece-fits?
  "Determines if the given piece will collide with anything in the current board."
  [piece x y board]
  (every? #(coord-fits? % x y board) piece))

; ------------------------------------------------------------
; DRAWING FUNCTIONS
