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

View File

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

View File

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