mirror of
https://github.com/richardharrington/robotwar.git
synced 2024-07-15 16:29:35 +00:00
grinding along with the refactoring
This commit is contained in:
parent
4a8f83ad95
commit
6e04c076a6
@ -20,13 +20,13 @@
|
|||||||
(def world (init-world 30 30 (map #(assemble reg-names %) [src-code1 src-code2 src-code3])))
|
(def world (init-world 30 30 (map #(assemble reg-names %) [src-code1 src-code2 src-code3])))
|
||||||
|
|
||||||
(def step (fn [initial-state n]
|
(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,
|
; pretty-prints a robot-state with line numbers,
|
||||||
; and only the registers you want. Very convenient.
|
; and only the registers you want. Very convenient.
|
||||||
|
|
||||||
(def ppt (fn [program n & reg-keys]
|
(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
|
(pprint (into (assoc-in
|
||||||
state
|
state
|
||||||
[:program :instrs]
|
[:program :instrs]
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
(ns robotwar.robot
|
(ns robotwar.robot
|
||||||
(:use [clojure.string :only [join]]
|
(:use [clojure.string :only [join]]
|
||||||
|
[clojure.pprint :only [pprint]]
|
||||||
(robotwar kernel-lexicon game-lexicon)))
|
(robotwar kernel-lexicon game-lexicon)))
|
||||||
|
|
||||||
; TODO: remove the game-lexicon dependency above, when it's no longer needed
|
; 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,
|
(Optionally, also pass in a hash-map of register names and values,
|
||||||
which will override the defaults)."
|
which will override the defaults)."
|
||||||
[program reg-names & [registers]]
|
[program reg-names & [registers]]
|
||||||
|
(pprint program)
|
||||||
|
(pprint reg-names)
|
||||||
|
(pprint registers)
|
||||||
|
(let [identity-with-throwaway-args (fn [x & args] x)]
|
||||||
{:registers (into {} (concat
|
{:registers (into {} (concat
|
||||||
(for [reg-name reg-names]
|
(for [reg-name reg-names]
|
||||||
; default values for :read, :write and :data. (TODO: move these into a higher-level module)
|
; default values for :read, :write and :data.
|
||||||
; NOTE: the default version of the :read function does not need the world-state parameter.
|
; (TODO: move these into a higher-level module)
|
||||||
[reg-name {:read (fn [data _] data)
|
; 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]
|
:write (fn [robot data]
|
||||||
(assoc-in robot [:internal-state :registers reg-name] data))
|
(assoc-in
|
||||||
|
robot
|
||||||
|
[:internal-state :registers reg-name]
|
||||||
|
data))
|
||||||
:data 0}])
|
:data 0}])
|
||||||
registers))
|
registers))
|
||||||
:program program
|
:program program
|
||||||
:acc 0
|
:acc 0
|
||||||
:instr-ptr 0
|
:instr-ptr 0
|
||||||
:call-stack []})
|
:call-stack []}))
|
||||||
|
|
||||||
(defn read-register
|
(defn read-register
|
||||||
"returns a numeric value"
|
"returns a numeric value"
|
||||||
@ -44,11 +54,11 @@
|
|||||||
[robot {write :write :as register} data]
|
[robot {write :write :as register} data]
|
||||||
(write robot data))
|
(write robot data))
|
||||||
|
|
||||||
(defn resolve-register [registers reg]
|
;(defn resolve-register [registers reg]
|
||||||
(case reg
|
; (case reg
|
||||||
"RANDOM" (rand-int (registers reg))
|
; "RANDOM" (rand-int (registers reg))
|
||||||
"DATA" (registers (reg-names (registers "INDEX")))
|
; "DATA" (registers (reg-names (registers "INDEX")))
|
||||||
(registers reg)))
|
; (registers reg)))
|
||||||
|
|
||||||
(defn resolve-arg [{arg-val :val arg-type :type} registers labels world]
|
(defn resolve-arg [{arg-val :val arg-type :type} registers labels world]
|
||||||
"resolves an instruction argument to a numeric value
|
"resolves an instruction argument to a numeric value
|
||||||
@ -80,7 +90,7 @@
|
|||||||
[robot world]
|
[robot world]
|
||||||
(let [internal-state (:internal-state robot)
|
(let [internal-state (:internal-state robot)
|
||||||
{:keys [acc instr-ptr call-stack registers program]} internal-state
|
{: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)
|
resolve #(resolve-arg % registers (program :labels) world)
|
||||||
return-robot #(assoc robot :internal-state (into internal-state %))]
|
return-robot #(assoc robot :internal-state (into internal-state %))]
|
||||||
(case command
|
(case command
|
||||||
@ -97,6 +107,6 @@
|
|||||||
(return-robot {:instr-ptr (inc instr-ptr)})
|
(return-robot {:instr-ptr (inc instr-ptr)})
|
||||||
(return-robot {:instr-ptr (+ instr-ptr 2)}))
|
(return-robot {:instr-ptr (+ instr-ptr 2)}))
|
||||||
"TO" (write-register (return-robot {:instr-ptr (inc instr-ptr)})
|
"TO" (write-register (return-robot {:instr-ptr (inc instr-ptr)})
|
||||||
(registers unresolved-arg-val)
|
(registers (:val arg))
|
||||||
acc))))
|
acc))))
|
||||||
|
|
||||||
|
@ -1,6 +1,9 @@
|
|||||||
(ns robotwar.world
|
(ns robotwar.world
|
||||||
(:use [clojure.string :only [join]]
|
(: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
|
(defn init-world
|
||||||
"initialize all the variables for a robot world"
|
"initialize all the variables for a robot world"
|
||||||
@ -8,12 +11,12 @@
|
|||||||
{:width width
|
{:width width
|
||||||
:height height
|
:height height
|
||||||
:shells []
|
:shells []
|
||||||
:robots (map-indexed (fn [idx program]
|
:robots (vec (map-indexed (fn [idx program]
|
||||||
{:internal-state (init-robot-state program
|
{:internal-state (init-internal-state program reg-names
|
||||||
{"X" (rand-int width)
|
{"X" (rand-int width)
|
||||||
"Y" (rand-int height)})
|
"Y" (rand-int height)})
|
||||||
:external-state {:icon (str idx)}})
|
:icon (str idx)})
|
||||||
programs)})
|
programs))})
|
||||||
|
|
||||||
(defn tick-world
|
(defn tick-world
|
||||||
"TODO"
|
"TODO"
|
||||||
@ -26,8 +29,7 @@
|
|||||||
vert-border-char "+"
|
vert-border-char "+"
|
||||||
header-footer (apply str (repeat (+ width 2) horiz-border-char))
|
header-footer (apply str (repeat (+ width 2) horiz-border-char))
|
||||||
field (for [y (range height), x (range width)]
|
field (for [y (range height), x (range width)]
|
||||||
(some (fn [{{{robot-x "X" robot-y "Y"} :registers} :internal-state
|
(some (fn [{{{robot-x "X" robot-y "Y"} :registers} :internal-state, icon :icon}]
|
||||||
{icon :icon} :external-state}]
|
|
||||||
(if (= [x y] [robot-x robot-y])
|
(if (= [x y] [robot-x robot-y])
|
||||||
icon
|
icon
|
||||||
" "))
|
" "))
|
||||||
|
Loading…
Reference in New Issue
Block a user