       (> chance 0.8)  1
       :else 0)))
 
-(def red-test  (r/filter #(> 0  (:klingon %))))
-
-(defn get-starbases []
+(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 [] (gen/uniform 1 9))
-
-(defn quadrant-klingon-count [quadrants]
+(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 quadrant-starbase-count [quadrants]
+
+(defn remaining-starbase-count 
+  "Returns the number of starbases remaining in the game."
+  [quadrants]
   (reduce + (flatten (map #(map :bases %) quadrants))))
 
-(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)}))))
-
-(defn reset-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
-      (<= (quadrant-klingon-count quadrants) 0) (recur)
-      (<= (quadrant-starbase-count quadrants) 0) (recur)
+      (<= (remaining-klingon-count quadrants) 0) (recur)
+      (<= (remaining-starbase-count quadrants) 0) (recur)
       :else quadrants))))
 
-; reset enterprise state
-(defn reset-enterprise []
+(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

(ns startrek.core
  (:require [clojure.data.generators :as gen]
            [clojure.core.reducers :as r]))
; The global game datastructure
(def game-state (atom 0))

(defn fill-test [x]
  (+ 50 x))

(defn get-klingons []
  (let [chance (gen/double)]
    (cond
      (> chance 0.98) 3

   })

; fetch default game state
(defn new-game-state 
  "Create a new world game state. Resets the enterprise and the quadrants."
  []
  (defn gen-stardate [] (* 100 (+ 20 (gen/uniform 1 21))))

  (reset! game-state 
  {:enterprise (reset-enterprise)
  :quadrant {:x (gen-1-8) :y (gen-1-8)}
  :sector {:x (gen-1-8) :y (gen-1-8)}
  :quads (reset-quadrants)
  :current-sector {}
  :stardate {:start (gen-stardate) :end 30}}))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; All of these methods are used to check or update game state.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;


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