Compare commits
	
		
			1 commit
		
	
	
		
			master
			...
			beforetheb
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
|  | 0716b517cb | 
|  | @ -1,11 +1,11 @@ | ||||||
| (ns ^:figwheel-always mw3.core | (ns ^:figwheel-always mw3.core | ||||||
|  |   (:use mw3.utils) | ||||||
|   (:use-macros [dommy.template :only [node deftemplate]]) |   (:use-macros [dommy.template :only [node deftemplate]]) | ||||||
|   (:require-macros [cljs.core.async.macros :refer [go]]) |   (:require-macros [cljs.core.async.macros :refer [go]]) | ||||||
|   (:require |   (:require | ||||||
|     [mw3.rulesets :as rulesets] |     [mw3.rulesets :as rulesets] | ||||||
|     [dommy.core :as dommy :refer-macros [sel sel1]] |     [dommy.core :as dommy :refer-macros [sel sel1]] | ||||||
|     [dommy.template :as temp] |     [dommy.template :as temp])) | ||||||
|     )) |  | ||||||
| 
 | 
 | ||||||
| 
 | 
 | ||||||
| ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; | ||||||
|  | @ -101,17 +101,6 @@ | ||||||
|    [:pre {:id (str "rule-feedback-" index) :class "rule-feedback"}] |    [:pre {:id (str "rule-feedback-" index) :class "rule-feedback"}] | ||||||
|    ]) |    ]) | ||||||
| 
 | 
 | ||||||
| ;; (deftemplate rule-editors |  | ||||||
| ;;   ;; Constructs, as a `div`, a set of rule editors for the rules in the ruleset with |  | ||||||
| ;;   ;; this `ruleset-name`. |  | ||||||
| ;;   [ruleset-name] |  | ||||||
| ;;   [:div |  | ||||||
| ;;    (vec |  | ||||||
| ;;      (map |  | ||||||
| ;;        #(rule-editor % %) |  | ||||||
| ;;        (rulesets/rulesets ruleset-name) |  | ||||||
| ;;        (range)))]) |  | ||||||
| 
 |  | ||||||
| (defn load-ruleset | (defn load-ruleset | ||||||
|   "Loads the ruleset with the specified `name` into a set of rule editors" |   "Loads the ruleset with the specified `name` into a set of rule editors" | ||||||
|   [name] |   [name] | ||||||
|  |  | ||||||
|  | @ -1,15 +1,11 @@ | ||||||
| (ns ^:figwheel-always mw3.core | (ns ^:figwheel-always mw3.core | ||||||
|   (:use [mw3.utils :only [error member?]] |   (:use mw3.utils | ||||||
|         [clojure.string :only [split trim triml]] |     [clojure.string :only [split trim triml]]) | ||||||
|         #?(:cljs |   (:require [instaparse.core :as insta])) | ||||||
|             [cljs.reader :only [read-string]])) |  | ||||||
|   (:require [instaparse.core :as insta]) |  | ||||||
|   ) |  | ||||||
| 
 | 
 | ||||||
| ;; error thrown when an attempt is made to set a reserved property | ;; error thrown when an attempt is made to set a reserved property | ||||||
| (def reserved-properties-error | (def reserved-properties-error | ||||||
|   "The properties 'x' and 'y' of a cell are reserved and should not be set in rule actions") |   "The properties 'x' and 'y' of a cell are reserved and should not be set in rule actions") | ||||||
| 
 |  | ||||||
| ;; error thrown when a rule cannot be parsed. Slots are for | ;; error thrown when a rule cannot be parsed. Slots are for | ||||||
| ;; (1) rule text | ;; (1) rule text | ||||||
| ;; (2) cursor showing where in the rule text the error occurred | ;; (2) cursor showing where in the rule text the error occurred | ||||||
|  | @ -204,6 +200,39 @@ | ||||||
|   ([comp1 quantity property-condition] |   ([comp1 quantity property-condition] | ||||||
|    (generate-neighbours-condition comp1 quantity property-condition 1))) |    (generate-neighbours-condition comp1 quantity property-condition 1))) | ||||||
| 
 | 
 | ||||||
|  | ;; (def s1 "if 3 neighbours have state equal to forest then state should be forest") | ||||||
|  | ;; (def s2 "if some neighbours have state equal to forest then state should be forest") | ||||||
|  | ;; (def s3 "if more than 3 neighbours have state equal to forest then state should be forest") | ||||||
|  | ;; (def s4 "if fewer than 3 neighbours have state equal to forest then state should be forest") | ||||||
|  | ;; (def s5 "if all neighbours have state equal to forest then state should be forest") | ||||||
|  | ;; (def s6 "if more than 3 neighbours within 2 have state equal to forest then state should be forest") | ||||||
|  | 
 | ||||||
|  | ;; (nth (simplify (parse-rule s1)) 2) | ||||||
|  | ;; (second (nth (simplify (parse-rule s1)) 2)) | ||||||
|  | ;; (nth (simplify (parse-rule s2)) 2) | ||||||
|  | ;; (map simplify (nth (simplify (parse-rule s2)) 2)) | ||||||
|  | ;; ;; (second (nth (simplify (parse-rule s2)) 2)) | ||||||
|  | ;; ;; (nth (simplify (parse-rule s3)) 2) | ||||||
|  | ;; (second (nth (simplify (parse-rule s3)) 2)) | ||||||
|  | ;; (map simplify (second (nth (simplify (parse-rule s3)) 2))) | ||||||
|  | ;; ;; (nth (simplify (parse-rule s4)) 2) | ||||||
|  | ;; ;; (second (nth (simplify (parse-rule s4)) 2)) | ||||||
|  | ;; ;; (nth (simplify (parse-rule s5)) 2) | ||||||
|  | ;; ;; (second (nth (simplify (parse-rule s5)) 2)) | ||||||
|  | ;; ;; (nth (simplify (parse-rule s6)) 2) | ||||||
|  | ;; ;; (second (nth (simplify (parse-rule s6)) 2)) | ||||||
|  | 
 | ||||||
|  | ;; ;; (generate (nth (nth (simplify (parse-rule s5)) 2) 4)) | ||||||
|  | ;; ;; (generate (nth (simplify (parse-rule s2)) 2)) | ||||||
|  | ;; ;; (generate (nth (simplify (parse-rule s1)) 2)) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
|  | ;; (generate-neighbours-condition '= 3 '(= (:state cell) :forest) 1) | ||||||
|  | ;; (generate-neighbours-condition (nth (simplify (parse-rule s3)) 2)) | ||||||
|  | ;; (generate-neighbours-condition (nth (simplify (parse-rule s2)) 2)) | ||||||
|  | ;; (generate-neighbours-condition (nth (simplify (parse-rule s1)) 2)) | ||||||
|  | 
 | ||||||
|  | 
 | ||||||
| (defn generate | (defn generate | ||||||
|   "Generate code for this (fragment of a) parse tree" |   "Generate code for this (fragment of a) parse tree" | ||||||
|   [tree] |   [tree] | ||||||
|  | @ -323,7 +352,5 @@ | ||||||
|   [rule] |   [rule] | ||||||
|   (assert (string? rule)) |   (assert (string? rule)) | ||||||
|   (let [tree (simplify (parse-rule rule))] |   (let [tree (simplify (parse-rule rule))] | ||||||
|     (if (rule? tree) #?(:clj (eval (generate tree)) |     (if (rule? tree) (eval (generate tree)) | ||||||
|                              :cljs (generate tree)) |  | ||||||
|       (throw-parse-exception tree)))) |       (throw-parse-exception tree)))) | ||||||
| 
 |  | ||||||
|  |  | ||||||
|  | @ -5,11 +5,21 @@ | ||||||
|   #?(:cljs (js/Error. message) |   #?(:cljs (js/Error. message) | ||||||
|            :clj (Exception. message))) |            :clj (Exception. message))) | ||||||
| 
 | 
 | ||||||
|  | (defn nth | ||||||
|  |   "I'm getting a compilation error saying `nth` isn't defined; so I'm defining it." | ||||||
|  |   [collection index] | ||||||
|  |   {:pre [(and (coll? collection) (integer? index) (or (zero? index) (pos? index)))]} | ||||||
|  |   (cond | ||||||
|  |     (empty? collection) nil | ||||||
|  |     (zero? index) (first collection) | ||||||
|  |     :true (nth (rest collection) (dec index)))) | ||||||
|  | 
 | ||||||
| (defn abs | (defn abs | ||||||
|   "Surprisingly, Clojure doesn't seem to have an abs function, or else I've |   "Surprisingly, Clojure doesn't seem to have an abs function, or else I've | ||||||
|    missed it. So here's one of my own. Maps natural numbers onto themselves, |    missed it. So here's one of my own. Maps natural numbers onto themselves, | ||||||
|    and negative integers onto natural numbers. Also maps negative real numbers |    and negative integers onto natural numbers. Also maps negative real numbers | ||||||
|    onto positive real numbers. |    onto positive real numbers. | ||||||
|  | 
 | ||||||
|    * `n` a number, on the set of real numbers." |    * `n` a number, on the set of real numbers." | ||||||
|   [n] |   [n] | ||||||
|   (if (neg? n) (- 0 n) n)) |   (if (neg? n) (- 0 n) n)) | ||||||
|  | @ -33,9 +43,11 @@ | ||||||
|   [world cell] |   [world cell] | ||||||
|   (merge cell {:generation (get-int-or-zero cell :generation)})) |   (merge cell {:generation (get-int-or-zero cell :generation)})) | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
| (defn in-bounds | (defn in-bounds | ||||||
|   "True if x, y are in bounds for this world (i.e., there is a cell at x, y) |   "True if x, y are in bounds for this world (i.e., there is a cell at x, y) | ||||||
|    else false. |    else false. | ||||||
|  | 
 | ||||||
|   * `world` a world as defined above; |   * `world` a world as defined above; | ||||||
|   * `x` a number which may or may not be a valid x coordinate within that world; |   * `x` a number which may or may not be a valid x coordinate within that world; | ||||||
|   * `y` a number which may or may not be a valid y coordinate within that world." |   * `y` a number which may or may not be a valid y coordinate within that world." | ||||||
|  | @ -77,6 +89,7 @@ | ||||||
| 
 | 
 | ||||||
| (defn get-cell | (defn get-cell | ||||||
|   "Return the cell a x, y in this world, if any. |   "Return the cell a x, y in this world, if any. | ||||||
|  | 
 | ||||||
|   * `world` a world as defined above; |   * `world` a world as defined above; | ||||||
|   * `x` a number which may or may not be a valid x coordinate within that world; |   * `x` a number which may or may not be a valid x coordinate within that world; | ||||||
|   * `y` a number which may or may not be a valid y coordinate within that world." |   * `y` a number which may or may not be a valid y coordinate within that world." | ||||||
|  | @ -86,6 +99,7 @@ | ||||||
| 
 | 
 | ||||||
| (defn get-int | (defn get-int | ||||||
|   "Get the value of a property expected to be an integer from a map; if not present (or not an integer) return 0. |   "Get the value of a property expected to be an integer from a map; if not present (or not an integer) return 0. | ||||||
|  | 
 | ||||||
|   * `map` a map; |   * `map` a map; | ||||||
|   * `key` a symbol or keyword, presumed to be a key into the `map`." |   * `key` a symbol or keyword, presumed to be a key into the `map`." | ||||||
|   [map key] |   [map key] | ||||||
|  | @ -99,6 +113,7 @@ | ||||||
|   "Return the population of this species in this cell. Currently a synonym for |   "Return the population of this species in this cell. Currently a synonym for | ||||||
|    `get-int`, but may not always be (depending whether species are later |    `get-int`, but may not always be (depending whether species are later | ||||||
|    implemented as actors) |    implemented as actors) | ||||||
|  | 
 | ||||||
|   * `cell` a map; |   * `cell` a map; | ||||||
|   * `species` a keyword representing a species which may populate that cell." |   * `species` a keyword representing a species which may populate that cell." | ||||||
|   [cell species] |   [cell species] | ||||||
|  | @ -124,15 +139,18 @@ | ||||||
| 
 | 
 | ||||||
| (defn get-neighbours | (defn get-neighbours | ||||||
|   "Get the neighbours to distance depth of a cell in this world. |   "Get the neighbours to distance depth of a cell in this world. | ||||||
|  | 
 | ||||||
|   Several overloads: |   Several overloads: | ||||||
|   * `world` a world, as described in world.clj; |   * `world` a world, as described in world.clj; | ||||||
|   * `cell` a cell within that world |   * `cell` a cell within that world | ||||||
|   Gets immediate neighbours of the specified cell. |   Gets immediate neighbours of the specified cell. | ||||||
|  | 
 | ||||||
|   * `world` a world, as described in world.clj; |   * `world` a world, as described in world.clj; | ||||||
|   * `cell` a cell within that world |   * `cell` a cell within that world | ||||||
|   * `depth` an integer representing the depth to search from the |   * `depth` an integer representing the depth to search from the | ||||||
|   `cell` |   `cell` | ||||||
|   Gets neighbours within the specified distance of the cell. |   Gets neighbours within the specified distance of the cell. | ||||||
|  | 
 | ||||||
|   * `world` a world, as described in world.clj; |   * `world` a world, as described in world.clj; | ||||||
|   * `x` an integer representing an x coordinate in that world; |   * `x` an integer representing an x coordinate in that world; | ||||||
|   * `y` an integer representing an y coordinate in that world; |   * `y` an integer representing an y coordinate in that world; | ||||||
|  | @ -184,6 +202,7 @@ | ||||||
| (defn get-neighbours-with-state | (defn get-neighbours-with-state | ||||||
|   "Get the neighbours to distance depth of the cell at x, y in this world which |   "Get the neighbours to distance depth of the cell at x, y in this world which | ||||||
|    have this state. |    have this state. | ||||||
|  | 
 | ||||||
|     * `world` a world, as described in `world.clj`; |     * `world` a world, as described in `world.clj`; | ||||||
|     * `cell` a cell within that world; |     * `cell` a cell within that world; | ||||||
|     * `depth` an integer representing the distance from [x,y] that |     * `depth` an integer representing the distance from [x,y] that | ||||||
|  | @ -212,6 +231,7 @@ | ||||||
|    (get-least-cell cells property #?(:cljs 900719925474099 |    (get-least-cell cells property #?(:cljs 900719925474099 | ||||||
|                                      :clj (Integer/MAX_VALUE))))) |                                      :clj (Integer/MAX_VALUE))))) | ||||||
| 
 | 
 | ||||||
|  | 
 | ||||||
| (defn- set-cell-property | (defn- set-cell-property | ||||||
|   "If this `cell`s x and y properties are equal to these `x` and `y` values, |   "If this `cell`s x and y properties are equal to these `x` and `y` values, | ||||||
|    return a cell like this cell but with the value of this `property` set to |    return a cell like this cell but with the value of this `property` set to | ||||||
|  | @ -252,4 +272,3 @@ | ||||||
|                   (merge %2 cell) |                   (merge %2 cell) | ||||||
|                   %2)) |                   %2)) | ||||||
|     world)) |     world)) | ||||||
| 
 |  | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue