diff clojure/com/aurellem/gb_driver.clj @ 88:65c2854c5875

can now save moves and states and am ready to continue past the title
author Robert McIntyre <rlm@mit.edu>
date Sat, 10 Mar 2012 15:36:26 -0600
parents e8855121f413
children 4c60ebca1a9d
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/gb_driver.clj	Sat Mar 10 14:48:17 2012 -0600
     1.2 +++ b/clojure/com/aurellem/gb_driver.clj	Sat Mar 10 15:36:26 2012 -0600
     1.3 @@ -13,16 +13,16 @@
     1.4  (defn frame->filename [frame]
     1.5    (File. *save-state-cache* (format "%07d.sav" frame)))
     1.6    
     1.7 -(defn write-save! [^SaveState save]
     1.8 +(defn write-state! [^SaveState save]
     1.9    (let [buf (:data save)
    1.10          bytes (byte-array (.limit buf))
    1.11          dest (frame->filename (:frame save))]
    1.12      (.get buf bytes)
    1.13      (FileUtils/writeByteArrayToFile dest bytes)
    1.14      (.rewind buf)
    1.15 -    save))
    1.16 +    dest))
    1.17  
    1.18 -(defn read-save [frame]
    1.19 +(defn read-state [frame]
    1.20    (let [save (frame->filename frame)]
    1.21      (if (.exists save)
    1.22        (let [buf (Gb/saveBuffer)
    1.23 @@ -54,11 +54,13 @@
    1.24  ;;; The first state!
    1.25  (defn gen-root! []
    1.26    (restart!)
    1.27 -  (write-save! (SaveState. 0  (Gb/saveState))))
    1.28 +  (let [state (SaveState. 0  (Gb/saveState))]
    1.29 +    (write-state! state)
    1.30 +    state))
    1.31  
    1.32  (defn root []
    1.33    (if (.exists (frame->filename 0))
    1.34 -    (read-save 0)
    1.35 +    (read-state 0)
    1.36      (gen-root!)))
    1.37  
    1.38  ;;;; Press Buttons
    1.39 @@ -88,7 +90,6 @@
    1.40  
    1.41  (def current-state (atom nil))
    1.42  
    1.43 -
    1.44  (defn set-state! [^SaveState state]
    1.45    (assert (:data state) "Not a valid state!")
    1.46    (if (not @on?) (restart!))
    1.47 @@ -112,7 +113,6 @@
    1.48    [^Move move buttons]
    1.49    (Move. (step (:state move) buttons) buttons))
    1.50  
    1.51 -
    1.52  (defn play
    1.53    ([^SaveState state n]
    1.54       (reduce (fn [s _] (step s)) state (range n)))