diff --git a/src/robotwar/core.clj b/src/robotwar/core.clj index 86ddff2..b9fd74b 100644 --- a/src/robotwar/core.clj +++ b/src/robotwar/core.clj @@ -20,13 +20,13 @@ (def world (init-world 30 30 (map #(assemble reg-names %) [src-code1 src-code2 src-code3]))) (def step (fn [initial-state n] - (nth (iterate tick-robot initial-state) n))) + (nth (iterate #(tick-robot % world) initial-state) n))) ; pretty-prints a robot-state with line numbers, ; and only the registers you want. Very convenient. (def ppt (fn [program n & reg-keys] - (let [state (step (init-robot-state program {}) n)] + (let [state (step (init-internal-state program {}) n)] (pprint (into (assoc-in state [:program :instrs] diff --git a/src/robotwar/robot.clj b/src/robotwar/robot.clj index 371c6cf..6bce686 100644 --- a/src/robotwar/robot.clj +++ b/src/robotwar/robot.clj @@ -1,5 +1,6 @@ (ns robotwar.robot (:use [clojure.string :only [join]] + [clojure.pprint :only [pprint]] (robotwar kernel-lexicon game-lexicon))) ; TODO: remove the game-lexicon dependency above, when it's no longer needed @@ -19,19 +20,28 @@ (Optionally, also pass in a hash-map of register names and values, which will override the defaults)." [program reg-names & [registers]] - {:registers (into {} (concat - (for [reg-name reg-names] - ; default values for :read, :write and :data. (TODO: move these into a higher-level module) - ; NOTE: the default version of the :read function does not need the world-state parameter. - [reg-name {:read (fn [data _] data) - :write (fn [robot data] - (assoc-in robot [:internal-state :registers reg-name] data)) - :data 0}]) - registers)) - :program program - :acc 0 - :instr-ptr 0 - :call-stack []}) + (pprint program) + (pprint reg-names) + (pprint registers) + (let [identity-with-throwaway-args (fn [x & args] x)] + {:registers (into {} (concat + (for [reg-name reg-names] + ; default values for :read, :write and :data. + ; (TODO: move these into a higher-level module) + ; NOTE: the default version of the :read function + ; does not need the world-state parameter. + [reg-name {:read identity-with-throwaway-args + :write (fn [robot data] + (assoc-in + robot + [:internal-state :registers reg-name] + data)) + :data 0}]) + registers)) + :program program + :acc 0 + :instr-ptr 0 + :call-stack []})) (defn read-register "returns a numeric value" @@ -44,11 +54,11 @@ [robot {write :write :as register} data] (write robot data)) -(defn resolve-register [registers reg] - (case reg - "RANDOM" (rand-int (registers reg)) - "DATA" (registers (reg-names (registers "INDEX"))) - (registers reg))) +;(defn resolve-register [registers reg] +; (case reg +; "RANDOM" (rand-int (registers reg)) +; "DATA" (registers (reg-names (registers "INDEX"))) +; (registers reg))) (defn resolve-arg [{arg-val :val arg-type :type} registers labels world] "resolves an instruction argument to a numeric value @@ -80,7 +90,7 @@ [robot world] (let [internal-state (:internal-state robot) {:keys [acc instr-ptr call-stack registers program]} internal-state - [{command :val} {unresolved-arg-val :val :as arg}] ((program :instrs) instr-ptr) + [{command :val} arg] ((program :instrs) instr-ptr) resolve #(resolve-arg % registers (program :labels) world) return-robot #(assoc robot :internal-state (into internal-state %))] (case command @@ -97,6 +107,6 @@ (return-robot {:instr-ptr (inc instr-ptr)}) (return-robot {:instr-ptr (+ instr-ptr 2)})) "TO" (write-register (return-robot {:instr-ptr (inc instr-ptr)}) - (registers unresolved-arg-val) + (registers (:val arg)) acc)))) diff --git a/src/robotwar/world.clj b/src/robotwar/world.clj index e6e295e..64773bb 100644 --- a/src/robotwar/world.clj +++ b/src/robotwar/world.clj @@ -1,6 +1,9 @@ (ns robotwar.world (:use [clojure.string :only [join]] - (robotwar foundry robot))) + (robotwar foundry robot game-lexicon))) + +; TODO: Write init-register function, in robot probably, and init those +; X and Y registers down below. (defn init-world "initialize all the variables for a robot world" @@ -8,12 +11,12 @@ {:width width :height height :shells [] - :robots (map-indexed (fn [idx program] - {:internal-state (init-robot-state program + :robots (vec (map-indexed (fn [idx program] + {:internal-state (init-internal-state program reg-names {"X" (rand-int width) "Y" (rand-int height)}) - :external-state {:icon (str idx)}}) - programs)}) + :icon (str idx)}) + programs))}) (defn tick-world "TODO" @@ -26,8 +29,7 @@ vert-border-char "+" header-footer (apply str (repeat (+ width 2) horiz-border-char)) field (for [y (range height), x (range width)] - (some (fn [{{{robot-x "X" robot-y "Y"} :registers} :internal-state - {icon :icon} :external-state}] + (some (fn [{{{robot-x "X" robot-y "Y"} :registers} :internal-state, icon :icon}] (if (= [x y] [robot-x robot-y]) icon " "))