cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Looking for quick-add? Try the external resource search, it's quicker and gives you access to the most recent version of thousands of libraries. ☝️

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              var lexicon = "FFFFffffff+++--->>><<<rw|=.".split("");
var code = "";
var steplen = 1/40;
var turnangle1 = Math.PI / 2;
var turnangle2 = Math.PI / 2;
var genome_size = 25;
var pop_grid_size = 3;
var pop_size = pop_grid_size*pop_grid_size;
var pop = [];
var t0 = now;

function make_genome() {
  var g = [];
  for (var i = 0; i < genome_size; i++) {
    var gene = lexicon[random(lexicon.length)];
    g.push(gene);
  }
  return g.join("");
}

function reset() {
  for (var i=0; i<pop_size; i++) {
    var o = {
      genome: make_genome()
    }
    write(i, o.genome);
    pop.push(o);
  }
  
  
  code = make_genome()
  console.log(code);
}
reset();

var which = 0;
function mouse(e, p) {
  var c = Math.floor( p[0] * pop_grid_size );
  var r = Math.floor( p[1] * pop_grid_size );
  var i = c + pop_grid_size * r;
  which = i;
  if (e == "down") {
    //reset();
    t0 = now;
    
    var parent = pop[i];
    
    for (var j=0; j<pop_size; j++) {
      if (i == j) continue;
      
      // get parent genomes:
      var parent1_genome = parent.genome;
      var parent2_genome = pop[j].genome;
      // 50% chance of swapping parents around:
      if (random(2) == 1) {
        var tmp = parent1_genome;
        parent1_genome = parent2_genome;
        parent2_genome = tmp;
      }
      // insert a segment of parent 2 into parent 1:
      // pick two cut points
      var cut1 = random(parent1_genome.length);
      var cut2 = random(parent2_genome.length);
      // ensure cut1 <= cut2
      if (cut1 > cut2) {
        var tmp = cut1;
        cut1 = cut2;
        cut2 = tmp;
      }
      // get the cut sections
      var sec1 = parent2_genome.substring(0, cut1);
      var sec2 = parent1_genome.substring(cut1, cut2);
      var sec3 = parent2_genome.substring(cut2);
      // and join:
      child = sec1 + sec2 + sec3;
      
      // chance of randomizing genome:
      if (random() < 1/pop_size) {
        var list = child.split("");
        list = shuffle(list);
        child = list.join("");
      }
      
      // mutate a gene:
      var mutation_rate = 1/child.length;
      var mutated = [];
      for (k=0; k<child.length; k++) {
        if (random() < mutation_rate) {
          var gene = lexicon[random(lexicon.length)];
          mutated.push(gene);
        } else {
          mutated.push(child[k]);
        }
      }
      child = mutated.join("");
      
      // replace in population:
      pop[j].genome = child;
    }
  }
}

function update() {
  turnangle1 = Math.PI * (1.5+Math.cos(now)) / 8;
  turnangle2 = Math.PI * (1.5+Math.sin(now * 4)) / 16;
}

function interpret(turtle, code) {
  for (var i = 0; i < code.length; i++) {
    var c = code[i];
    switch (c) {
      case "=":
        // fork another turtle with flipped chirality
        var t1 = {
          pos: turtle.pos.clone(),
          dir: turtle.dir.clone().negate(),
          chirality: turtle.chirality,
          color: turtle.color,
        };
        // run the rest of the string on it too:
        var remain = code.substring(i + 1);
        // run the second turtle:
        interpret(t1, remain);
        break;
      case "|":
        // fork another turtle with flipped chirality
        var t1 = {
          pos: turtle.pos.clone(),
          dir: turtle.dir.clone(),
          chirality: -turtle.chirality,
          color: turtle.color,
        };
        // run the rest of the string on it too:
        var remain = code.substring(i + 1);
        // run the second turtle:
        interpret(t1, remain);
        break;
      case "r":
        turtle.color = "red";
        break;
      case "w":
        turtle.color = "white";
        break;
      case ".":
        draw2D.color(turtle.color);
        draw2D.rect(turtle.pos, steplen);
        break;
      case "F":
        // move forward drawing a line
        var p2 = turtle.dir.clone().mul(steplen).add(turtle.pos);
        draw2D.color(turtle.color);
        draw2D.line(turtle.pos, p2);
        turtle.pos = p2;
        break;
      case "f":
        // move forward drawing a line
        var p2 = turtle.dir.clone().mul(steplen * 0.5).add(turtle.pos);
        draw2D.color(turtle.color);
        draw2D.line(turtle.pos, p2);
        turtle.pos = p2;
        break;
      case "+":
        // turn positively
        turtle.dir.rotate(turtle.chirality * turnangle1);
        break;
      case "-":
        // turn the other way
        turtle.dir.rotate(turtle.chirality * -turnangle1);
        break;
      case ">":
        // turn positively
        turtle.dir.rotate(turtle.chirality * turnangle2);
        break;
      case "<":
        // turn the other way
        turtle.dir.rotate(turtle.chirality * -turnangle2);
        break;

    }
  }
}

function draw() {
  
  var age = Math.floor((now - t0) * 10);
  
  for (var i=0; i<pop_size; i++) {
    var o = pop[i];
    write(i, o.genome);
    
    draw2D.push()
      .scale(1/pop_grid_size)
      .translate(i%pop_grid_size, Math.floor(i/pop_grid_size))
      .translate(0.5, 0.5);
    
    if (i == which) {
      draw2D.color(0.2, 0.2, 0.2);
      draw2D.rect();
    }
    
    var turtle = {
      pos: new vec2(),
      dir: new vec2(0, 1),
      chirality: 1,
      color: "white",
    };
    interpret(turtle, o.genome.substring(0, age));

    draw2D.pop();
   }
}
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console