 (ns startrek.core
   (:require [clojure.data.generators :as gen]
             [clojure.core.reducers :as r]))
-; The global game datastructure
+
+; The global game data structure
 (def game-state (atom 0))
 
-(defn fill-test [x]
-  (+ 50 x))
-
-(defn get-klingons []
+(def enterprise-id 1)
+(def klingon-id 2)
+(def base-id 3)
+(def star-id 4)
+
+(defn gen-1-8 [] (gen/uniform 1 9))
+
+(defn init-sector-item
+  "Assign an item in a sector to a location."
+  [sector value]
+  (loop []
+    (let [x (gen-1-8) y (gen-1-8)]
+      (if (> (aget sector x y ) 0)
+        (recur)
+       (aset sector x y value)))))
+
+(defn get-klingons
+ "Returns 0-3 klingons in a sector based on a uniform distribution."
+ []
   (let [chance (gen/double)]
     (cond
       (> chance 0.98) 3

   })

; fetch default game state
(defn new-game-state []
  (defn gen-0-8 [] (gen/uniform 0 8))
  (defn gen-stardate [] (* 100 (+ 20 (gen/uniform 1 21))))

  {:enterprise (reset-enterprise)
  :quadrant (reset-quadrants)
  :sector {:x (gen-0-8) :y (gen-0-8)}
  :stardate {:start (gen-stardate) :end 30}}
  )

(defn print-instructions []
  (println "INSTRUCTIONS"))

      (> chance 0.8)  1
      :else 0)))

(defn get-starbases
 "Returns 0-1 starbases in a sector based on a uniform distribution."
  []
  (let [chance (gen/double)]
    (cond
      (> chance 0.96) 1
      :else 0))) 

(defn get-stars 
  "Returns 1-9 stars for each sector based on a uniform distribution."
  [] (gen/uniform 1 9))

(defn remaining-klingon-count 
  "Returns the number of klingon ships left in the game."
  [quadrants]
  (reduce + (flatten (map #(map :klingons %) quadrants))))

(defn remaining-starbase-count 
  "Returns the number of starbases remaining in the game."
  [quadrants]
  (reduce + (flatten (map #(map :bases %) quadrants))))

(defn reset-quadrants 
  "Returns a new quadrants 2d-array that is guaranteed to have at least 1 klingon and 1 starbase." 
  []
  (defn create-quadrants []
    (to-array-2d 
      (partition  9
                 (for  [y  (range 1 10) x  (range 1 10)]  
                   {:x x :y y :klingons (get-klingons) :stars (get-stars) :bases (get-starbases)}))))

  (loop []
  (let [quadrants (create-quadrants)]
    (cond
      (<= (remaining-klingon-count quadrants) 0) (recur)
      (<= (remaining-starbase-count quadrants) 0) (recur)
      :else quadrants))))

(defn init-sector 
  "Initialize a sector with the right amount of game related items."
  []
  (defn create-empty-sector []
    (to-array-2d 
      (partition  9
                 (for  [y  (range 1 10) x  (range 1 10)]  0))))
  (def quad (aget (:quads @game-state) (:x (:quadrant @game-state)) (:y (:quadrant @game-state))))

  (let [sector (create-empty-sector)]
    (aset sector (:x (:sector @game-state)) (:y (:sector @game-state)) enterprise-id)
    (loop [k (:klingons quad)]
      (when (> k 0)
        (init-sector-item sector klingon-id)
        (recur (- k 1))))
    (loop [k (:bases quad)]
      (when (> k 0)
        (init-sector-item sector base-id)
        (recur (- k 1))))
    (loop [k (:stars quad)]
      (when (> k 0)
        (init-sector-item sector star-id)
        (recur (- k 1))))
    (swap! game-state assoc-in [:current-sector] sector)))


(defn reset-enterprise
 "Returns a new instance of the enterprise in the default (repaired) state."
 []
  {:damage 
   {:short_range_sensors 0
    :computer_display 0
