commented out most of these high-level files till the refactoring is done

This commit is contained in:
Richard Harrington 2013-08-03 03:38:01 -04:00
parent 67c599062c
commit 0c988d61f4
3 changed files with 133 additions and 136 deletions

View File

@ -2,34 +2,34 @@
(:use [clojure.pprint]
(robotwar foundry brain robot world game-lexicon)))
(def src-code1 " START
0 TO A
TEST
IF A > 2 GOTO START
GOSUB INCREMENT
GOTO TEST
100 TO A
INCREMENT
A + 1 TO A
ENDSUB
200 TO A ")
(def src-code2 "WAIT GOTO WAIT")
(def src-code3 "500 TO RANDOM RANDOM RANDOM RANDOM")
(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 % 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-internal-state program {}) n)]
(pprint (into (assoc-in
state
[:program :instrs]
(zipmap (range) (get-in state [:program :instrs])))
{:registers (select-keys (:registers state) reg-keys)})))))
;(def src-code1 " START
; 0 TO A
; TEST
; IF A > 2 GOTO START
; GOSUB INCREMENT
; GOTO TEST
; 100 TO A
; INCREMENT
; A + 1 TO A
; ENDSUB
; 200 TO A ")
;
;(def src-code2 "WAIT GOTO WAIT")
;(def src-code3 "500 TO RANDOM RANDOM RANDOM RANDOM")
;
;(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 % 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-internal-state program {}) n)]
; (pprint (into (assoc-in
; state
; [:program :instrs]
; (zipmap (range) (get-in state [:program :instrs])))
; {:registers (select-keys (:registers state) reg-keys)})))))
;

View File

@ -19,64 +19,61 @@
; FULLY-QUALIFIED NAMES. THAT MIGHT MAKE THINGS
; A BIT CLEARER. THE NAMES CAN BE SHORTENED QUITE A BIT,
; WHEN LOADED INTO THE MODULES.
(defn make-default-read [register]
"takes a register and returns the default version of its :read function,
which ignores the `world` parameter and just returns
the :val field from the register."
(fn [_]
(:val register)))
(defn make-default-write [robot-idx reg-name]
"takes a robot-idx and a reg-name to locate a register, and
returns the default version of that register's :write function,
which takes a world parameter and a data value and returns the
world with the data value assoc'd into it."
(fn [world data]
(assoc-in world [:robots robot-idx :registers reg-name :val] data)))
(def default-data 0)
(defn default-register [robot-idx reg-name]
(init-register
reg-name
(defn init-robot
[program x y]
{:pos-x x
:pos-y y
:veloc-x 0
:veloc-y 0
:accel-x 0
:accel-y 0
:damage 100
:
[let [special-registers
[init-register "X" default-
(defn init-world
"initialize all the variables for a robot world"
[width height programs]
{:width width
:height height
:shells []
:robots (vec (map-indexed (fn [idx program]
{:brain (init-brain
program
reg-names
{(init-register "X"
default-read
default-write
(rand-int width))
(init-register "Y"
default-read
default-write
(rand-int height))})
:icon (str idx)})
programs))})
(defn tick-robot
[robot world]
(let [ticked (tick-brain robot world)]
))
;
;(defn make-default-read [register]
; "takes a register and returns the default version of its :read function,
; which ignores the `world` parameter and just returns
; the :val field from the register."
; (fn [_]
; (:val register)))
;
;(defn make-default-write [robot-idx reg-name]
; "takes a robot-idx and a reg-name to locate a register, and
; returns the default version of that register's :write function,
; which takes a world parameter and a data value and returns the
; world with the data value assoc'd into it."
; (fn [world data]
; (assoc-in world [:robots robot-idx :registers reg-name :val] data)))
;
;(def default-data 0)
;
;(defn default-register [robot-idx reg-name]
; (init-register
; reg-name))
;
;
;(defn init-robot
; [program x y]
; {:pos-x x
; :pos-y y
; :veloc-x 0
; :veloc-y 0
; :accel-x 0
; :accel-y 0
; :damage 100})
;
;(defn init-world
; "initialize all the variables for a robot world"
; [width height programs]
; {:width width
; :height height
; :shells []
; :robots (vec (map-indexed (fn [idx program]
; {:brain (init-brain
; program
; reg-names
; {(init-register "X"
; default-read
; default-write
; (rand-int width))
; (init-register "Y"
; default-read
; default-write
; (rand-int height))})
; :icon (str idx)})
; programs))})
;
;(defn tick-robot
; [robot world]
; (let [ticked (tick-brain robot world)]
; ))

View File

@ -1,47 +1,47 @@
(ns robotwar.world
(:use [clojure.string :only [join]]
(robotwar foundry brain robot game-lexicon)))
(defn init-world
"initialize all the variables for a robot world"
[width height programs]
{:width width
:height height
:shells []
:robots (vec (map-indexed (fn [idx program]
{:brain (init-brain
program
reg-names
{(init-register "X"
default-read
default-write
(rand-int width))
(init-register "Y"
default-read
default-write
(rand-int height))})
:icon (str idx)})
programs))})
(defn tick-world
"TODO"
[world-state])
(defn arena-text-grid
"outputs the arena, with borders"
[{:keys [width height robots]}]
(let [horiz-border-char "-"
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}]
(if (= [x y] [robot-x robot-y])
icon
" "))
robots))]
(str header-footer
"\n"
(join "\n" (map #(join (apply str %) (repeat 2 vert-border-char))
(partition width field)))
"\n"
header-footer)))
;
;(defn init-world
; "initialize all the variables for a robot world"
; [width height programs]
; {:width width
; :height height
; :shells []
; :robots (vec (map-indexed (fn [idx program]
; {:brain (init-brain
; program
; reg-names
; {(init-register "X"
; default-read
; default-write
; (rand-int width))
; (init-register "Y"
; default-read
; default-write
; (rand-int height))})
; :icon (str idx)})
; programs))})
;
;(defn tick-world
; "TODO"
; [world-state])
;
;(defn arena-text-grid
; "outputs the arena, with borders"
; [{:keys [width height robots]}]
; (let [horiz-border-char "-"
; 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}]
; (if (= [x y] [robot-x robot-y])
; icon
; " "))
; robots))]
; (str header-footer
; "\n"
; (join "\n" (map #(join (apply str %) (repeat 2 vert-border-char))
; (partition width field)))
; "\n"
; header-footer)))