New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

generated types #4

Open
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+70 −41
Diff settings

Always

Just for now

View
@@ -1,6 +1,6 @@
(ns startrek.core
{:lang :core.typed
:core.typed {:features #{:runtime-infer}}
;:core.typed {:features #{:runtime-infer}}
}
(:require [clojure.java.io :as io]
[clojure.edn :as edn]
@@ -74,31 +74,45 @@
clojure.edn/read-string
[t/Str :-> '[PageMap PageMap PageMap PageMap]])
(t/ann clojure.java.io/resource [t/Str :-> java.net.URL])
(t/ann -main [:-> nil])
(t/ann command-help AnyFunction)
(t/ann -main [t/Any * :-> nil])
(t/ann command-help [:-> nil])
(t/ann
game-over-destroyed
[(t/Atom1 CurrentKlingonsCurrentSectorEnterpriseMap) :-> Boolean])
(t/ann game-over-out-of-time AnyFunction)
(t/ann game-over-out-of-time
[(t/Atom1 CurrentKlingonsCurrentSectorEnterpriseMap) :-> Boolean])
(t/ann
game-over-powerless
[(t/Atom1 CurrentKlingonsCurrentSectorEnterpriseMap) :-> Boolean])
(t/ann game-over-success AnyFunction)
(t/ann game-over-success
[(t/Atom1 CurrentKlingonsCurrentSectorEnterpriseMap) :-> t/Any])
(t/ann
game-over?
[(t/Atom1 CurrentKlingonsCurrentSectorEnterpriseMap) :-> Boolean])
[(t/Atom1 CurrentKlingonsCurrentSectorEnterpriseMap) :-> t/Any])
(t/ann
game-state
(t/Atom1
(t/U
CurrentKlingonsCurrentSectorEnterpriseMap
(t/Map t/Nothing t/Nothing))))
(t/ann library-computer AnyFunction)
(t/ann pages (t/Coll t/Str))
(t/Atom1 CurrentKlingonsCurrentSectorEnterpriseMap))
(t/ann library-computer [-> t/Any])
(t/ann ^:no-check pages (t/Coll t/Str))
(t/ann parse-number [t/Str :-> t/Int])
(t/ann play-game [:-> nil])
(t/ann print-instructions [t/Int :-> nil])
(t/ann user-input [:-> t/Int])
(t/ann
startrek.computer/computer-command
[(t/Atom1 CurrentKlingonsCurrentSectorEnterpriseMap)
:->
t/Any])
(t/ann
startrek.enterprise/damage-control-report-command
[(t/Atom1 CurrentKlingonsCurrentSectorEnterpriseMap)
:->
t/Any])
(t/ann
startrek.enterprise/fire-phasers-command
[(t/Atom1 CurrentKlingonsCurrentSectorEnterpriseMap)
:->
t/Any])
(t/ann
startrek.enterprise/fire-torpedoes-command
[(t/Atom1 CurrentKlingonsCurrentSectorEnterpriseMap)
@@ -115,6 +129,11 @@
(t/Vec EnergySectorMap)
:->
EnergyIsDockedQuadrantMap])
(t/ann
startrek.nav/set-course-command
[(t/Atom1 CurrentKlingonsCurrentSectorEnterpriseMap)
:->
t/Any])
(t/ann
startrek.nav/enter-quadrant
[(t/Atom1 CurrentKlingonsCurrentSectorEnterpriseMap)
@@ -129,21 +148,23 @@
startrek.world/long-range-scan-command
[(t/Atom1 CurrentKlingonsCurrentSectorEnterpriseMap) :-> nil])
(t/ann
startrek.world/new-game-state
[(t/Atom1
(t/U
CurrentKlingonsCurrentSectorEnterpriseMap
(t/Map t/Nothing t/Nothing)))
startrek.world/reset-game-state!
[(t/Atom1 CurrentKlingonsCurrentSectorEnterpriseMap)
:->
CurrentKlingonsCurrentSectorEnterpriseMap])
(t/ann
startrek.world/new-game-state
[:-> (t/Atom1 CurrentKlingonsCurrentSectorEnterpriseMap)])
(t/ann
startrek.world/remaining-klingon-count
[(t/Vec BasesKlingonsQuadrantMap) :-> t/Int])
(t/ann
startrek.world/short-range-scan-command
[(t/Atom1 CurrentKlingonsCurrentSectorEnterpriseMap) :-> nil])
(t/ann
clojure.data.generators/*rnd* java.util.Random)
;; End: Generated by clojure.core.typed - DO NOT EDIT
(def game-state (atom {}))
(def game-state (w/new-game-state))
(def pages
(->> "instructions.edn"
@@ -155,6 +176,7 @@
(defn parse-number
"Reads a number from a string. Returns nil if not a number."
[s]
{:post [(integer? %)]}
(if (re-find #"^-?\d+\.?\d*$" s)
(read-string s)
(let [s2 (str "0" s)]
@@ -193,8 +215,8 @@
(defn game-over-powerless [game-state]
(u/message "THE ENTERPRISE IS DEAD IN SPACE. IF YOU SURVIVE ALL IMPENDING")
(u/message "ATTACK YOU WILL BE DEMOTED TO THE RANK OF PRIVATE")
(let [enterprise (k/klingon-turn (get-in @game-state [:enterprise]) (get-in @game-state [:current-klingons]))]
(if (neg? (get-in enterprise [:shields]))
(let [enterprise (k/klingon-turn (get @game-state :enterprise) (get @game-state :current-klingons))]
(if (neg? (get enterprise :shields))
(game-over-destroyed game-state)
(u/message (format "THERE ARE STILL %d KLINGON BATTLE CRUISERS"
(w/remaining-klingon-count (:quads @game-state))))))
@@ -205,20 +227,20 @@
(u/message "THE LAST KLINGON BATTLE CRUISER IN THE GALAXY HAS BEEN DESTROYED")
(u/message "THE FEDERATION HAS BEEN SAVED !!!")
(u/message)
(let [k (get-in @game-state [:starting-klingons])
start (get-in @game-state [:stardate :start])
current (get-in @game-state [:stardate :current])]
(let [k (get @game-state :starting-klingons)
start (-> @game-state :stardate :start)
current (-> @game-state :stardate :current)]
(u/message (format "YOUR EFFICIENCY RATING = %5.2f" (* 1000.0 (/ k (- current start)))))))
(defn game-over? [game-state]
(cond
(neg? (get-in @game-state [:enterprise :shields])) (game-over-destroyed game-state)
(and (<= (get-in @game-state [:enterprise :shields]) 1)
(zero? (get-in @game-state [:enterprise :energy]))) (game-over-powerless game-state)
(neg? (-> @game-state :enterprise :shields)) (game-over-destroyed game-state)
(and (<= (-> @game-state :enterprise :shields) 1)
(zero? (-> @game-state :enterprise :energy))) (game-over-powerless game-state)
(zero? (w/remaining-klingon-count (:quads @game-state))) (game-over-success game-state)
(> (get-in @game-state [:stardate :current])
(+ (get-in @game-state [:stardate :start])
(get-in @game-state [:stardate :end]))) (game-over-out-of-time game-state)
(> (-> @game-state :stardate :current)
(+ (-> @game-state :stardate :start)
(-> @game-state :stardate :end))) (game-over-out-of-time game-state)
:else false))
(defn command-help []
@@ -234,20 +256,20 @@
(println))
(defn user-input []
(parse-number (read-line)))
(parse-number (t/cast String (read-line))))
(defn play-game
"The main game loop. When you quit out, the game shuts down. Otherwise a new game starts
immediately after the old one was finished."
[]
(w/new-game-state game-state)
(w/reset-game-state! game-state)
(n/enter-quadrant game-state)
(let [stop-condition (ref false)]
(let [stop-condition (t/ref :- Boolean false)]
(while (not (deref stop-condition))
; check to see if the Enterprise is destroyed
(if (game-over? game-state)
(do
(w/new-game-state game-state)
(w/reset-game-state! game-state)
(n/enter-quadrant game-state))
(do
(w/short-range-scan-command game-state)
@@ -265,7 +287,7 @@
"7" (c/computer-command game-state)
"q" (dosync (alter stop-condition (t/ann-form
(fn [_] true)
[false :-> Boolean])
[Boolean :-> Boolean])
))
(command-help)
)))))))
@@ -278,17 +300,18 @@
(println)
(println)
(println "ENTER 1 OR 2 FOR INSTRUCTIONS (ENTER 2 TO PAGE) ")
(let [choice (parse-number (read-line))]
(let [choice (parse-number (t/cast String (read-line)))]
(if (or (= choice 1) (= choice 2))
(print-instructions choice)))
; This is unsafe if the user does not provide an int
; for a seed. Need to fix that.
(println)
(println "ENTER SEED NUMBER ")
(let [seed (parse-number (read-line))]
(binding [gen/*rnd* (java.util.Random. seed)])
(let [seed (parse-number (t/cast String (read-line)))]
(binding [gen/*rnd* (java.util.Random. (long seed))])
(t/tc-ignore ;; TODO waiting on core.typed support for with-redefs
(with-redefs [u/message println
n/pick-course user-input
n/pick-warp-factor user-input
@@ -298,7 +321,8 @@
c/pick-computer-command user-input
c/pick-coordinate user-input
]
(play-game))))
(play-game)))
nil))
(comment
(defn play-the-game []
@@ -315,4 +339,4 @@
"q\n")))]
(binding [*in* rdr]
(-main))))
)
)
View
@@ -19,8 +19,8 @@
;; Public commands intended to be exported by this namespace
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; fetch default game state
(defn new-game-state
"Create a new world game state. Resets the enterprise and the quadrants."
(defn reset-game-state!
"Resets the enterprise and the quadrants."
[game-state]
(let [stardate (* 100 (+ 20 (gen-uniform 1 21)))
quads (reset-quadrants)]
@@ -34,6 +34,11 @@
:starting-klingons (remaining-klingon-count quads)
:stardate {:start stardate :current stardate :end 30}})))
(defn new-game-state
"Create a new world game state. Resets the enterprise and the quadrants."
[]
(doto (atom {}) reset-game-state!))
(defn short-range-scan-command
"Creates most of the visible UI for the player. You get to see
where the enterprise is in relationship to everything else. This