Commits

catseye  committed 2fcadf2

Use yoob.Playfield.map() to evolve the playfield.

  • Participants
  • Parent commits 79e4816

Comments (0)

Files changed (2)

File demo/circute.html

   var c = new yoob.Controller();
   var pf;
   
-  // monkeypatchish
-  var evolvePlayfield = function(pf, newPf) {
-      for (var y = pf.minY - 1; y <= pf.maxY - -1; y++) {
-        for (var x = pf.minX - 1; x <= pf.maxX - -1; x++) {
-          newPf.put(x, y, evalState(pf, x, y));
-        }
-      }
-      newPf.recalculateBounds();
-  };
-
   var dumper = function(state) {
       if (state === 'Space') return ' ';
       if (state === 'NAND') return 'N';
   c.step = function() {
       var newPf = new yoob.Playfield();
       newPf.setDefault('Space');
-
-      evolvePlayfield(pf, newPf);
-
+      pf.map(newPf, evalState, -1, -1, +1, +1);
       pf = newPf;
       var output = document.getElementById('output');
       output.innerHTML = pf.dump(dumper);

File demo/yoob/playfield.js

     };
 
     /*
+     * Analogous to (monoid) map in functional languages,
+     * iterate over this Playfield, transform each value using
+     * a supplied function, and write the transformed value into
+     * a destination Playfield.
+     *
+     * Supplied function should take a Playfield (this Playfield),
+     * x, and y, and return a value.
+     *
+     * The map source may extend beyond the internal bounds of
+     * the Playfield, by giving the min/max Dx/Dy arguments
+     * (which work like margin offsets.)
+     *
+     * Useful for evolving a cellular automaton playfield.  In this
+     * case, min/max Dx/Dy should be computed from the neighbourhood.
+     */
+    this.map = function(destPf, fun, minDx, minDy, maxDx, maxDy) {
+        if (minDx === undefined) minDx = 0;
+        if (minDy === undefined) minDy = 0;
+        if (maxDx === undefined) maxDx = 0;
+        if (maxDy === undefined) maxDy = 0;
+        for (var y = this.minY + minDy; y <= this.maxY + maxDy; y++) {
+            for (var x = this.minX + minDx; x <= this.maxX + maxDx; x++) {
+                destPf.putDirty(x, y, fun(pf, x, y));
+            }
+        }
+        destPf.recalculateBounds();
+    };
+
+    /*
      * Draws elements of the Playfield in a drawing context.
      * x and y are canvas coordinates, and width and height
      * are canvas units of measure.