diff clojure/com/aurellem/run/util.clj @ 318:9a4d3f801c89

fixing runs to use new util functions.
author Robert McIntyre <rlm@mit.edu>
date Mon, 02 Apr 2012 23:13:49 -0500
parents 3c5bf2221ea0
children 92c47a9cdaea
line wrap: on
line diff
     1.1 --- a/clojure/com/aurellem/run/util.clj	Mon Apr 02 21:25:24 2012 -0500
     1.2 +++ b/clojure/com/aurellem/run/util.clj	Mon Apr 02 23:13:49 2012 -0500
     1.3 @@ -7,6 +7,8 @@
     1.4  (def ← [:l])
     1.5  (def → [:r])
     1.6  
     1.7 +[↑ ↓ ← →]
     1.8 +
     1.9  (defn first-difference
    1.10    [base alt difference-metric [moves root :as script]]
    1.11    (loop [branch-point root
    1.12 @@ -145,6 +147,25 @@
    1.13         (play-moves
    1.14          (repeat n []))))
    1.15  
    1.16 +(defn delayed-improbability-search
    1.17 +  "insert blank frames before calling script-fn until
    1.18 +   metric returns true."
    1.19 +  [delay metric script-fn script]
    1.20 +  (loop [blanks 0]
    1.21 +    (let [new-script
    1.22 +          (->> script
    1.23 +               (play-moves
    1.24 +                (concat (repeat blanks [])))
    1.25 +               script-fn)
    1.26 +          future-state
    1.27 +          (run-moves (second new-script)
    1.28 +                     (repeat delay []))
    1.29 +          result (metric future-state)]
    1.30 +      (if result
    1.31 +        (do
    1.32 +          (println "improbability factor:" blanks)
    1.33 +          new-script)
    1.34 +        (recur (inc blanks))))))
    1.35  
    1.36  (defn critical-hit
    1.37    "Put the cursor over the desired attack. This program will
    1.38 @@ -152,46 +173,19 @@
    1.39     insert before pressing [:a] to ensure that the attack is
    1.40     a critical hit."
    1.41    [script]
    1.42 -  (loop [blanks 6]
    1.43 -    (let [new-script
    1.44 -          (->> script
    1.45 -               (play-moves
    1.46 -                (concat (repeat blanks [])
    1.47 -                        [[:a][]])))]
    1.48 -      (if (let [future-state
    1.49 -                (run-moves (second new-script)
    1.50 -                           (repeat 400 []))
    1.51 -
    1.52 -                result (search-string (memory future-state)
    1.53 -                                      "Critical")]
    1.54 -            (if result
    1.55 -              (println "critical hit with" blanks "blank frames"))
    1.56 -            result) 
    1.57 -        new-script
    1.58 -        (recur (inc blanks))))))  
    1.59 +  (delayed-improbability-search
    1.60 +   400
    1.61 +   #(search-string  % "Critical")
    1.62 +   (partial play-moves [[:a][]])
    1.63 +   script))
    1.64  
    1.65  (defn move-thru-grass
    1.66    [direction script]
    1.67 -  (loop [blanks 0]
    1.68 -    (let [new-script
    1.69 -          (->> script
    1.70 -               (play-moves (repeat blanks []))
    1.71 -               (move direction))
    1.72 -
    1.73 -          future-state
    1.74 -          (run-moves (second new-script)
    1.75 -                     (repeat 600 []))
    1.76 -          
    1.77 -          result (search-string (memory future-state)
    1.78 -                                "Wild")]
    1.79 -      (if (nil? result)
    1.80 -        (do
    1.81 -          (if (< 0 blanks)
    1.82 -            (do
    1.83 -              (println "avoided pokemon with"
    1.84 -                       blanks "blank frames")))
    1.85 -             new-script)
    1.86 -        (recur (inc blanks))))))
    1.87 +  (delayed-improbability-search
    1.88 +   600
    1.89 +   #(nil? (search-string % "Wild"))
    1.90 +   (partial move direction)
    1.91 +   script))
    1.92  
    1.93  (defn walk-thru-grass
    1.94    [directions script]