Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
"All work and no play makes Jack a dull boy" - Cellular Automata in p5.js
function Grid(cells, generation, cell_scale){
this.text = " all work and no play makes jack a dull boy ";
// cells
this.cells = cells;
this.generation = generation;
// scale
this.cell_scale = cell_scale;
// create next generation
this.next_gen = function(ruleset){
//return new Grid(this.cells, this.generation + 1);
// next gen cells
var n_cells = [];
n_cells.push(cells[0]);
// iterate through cells
for(var i=1;i<cells.length -1;i++){
// convert bit arrays to decimal
neighborhood = cells[i-1] << 2 | cells[i] << 1 | cells[i+1];
// fill in next gen
n_cells.push(ruleset[ruleset.length - neighborhood -1]);
}
n_cells.push(cells[cells.length -1]);
return new Grid(n_cells, this.generation +1, this.cell_scale);
}
this.show = function(){
stroke(190);
for(var i=0;i<cells.length;i++){
if(cells[i]){
rect(5*i,5*generation,5,5)
}
}
}
this.render_text = function(){
push();
stroke(190);
fill(190);
textSize(this.cell_scale);
textFont("Courier New");
var text_idx = 0;
for(var i=0;i<cells.length;i++){
if(cells[i]){
//rect(5*i,5*generation,5,5)
if(text_idx >= this.text.length){
text_idx = 0;
}
else{
text(str(this.text[text_idx]), this.cell_scale*i, this.cell_scale*generation);
text_idx++;
}
}
}
pop();
}
}
var grid = [];
var grid_steps = 50;
var t = 0.0;
var t_1 = -1.0; // memory
// assign rule set here
var ruleset_dec = 30;
var cell_scale = 12;
// decimal to array of bits in boolean
function dec2array(dec_num){
// array of bits in boolean
var arr = [];
// to string
var base2 = (dec_num).toString(2);
// iterate string
for(var j=0;j<base2.length;j++){
arr.push(boolean(int(base2[j])));
}
arr_len = arr.length;
// prepend zeros
for(var j=0;j<8-arr_len;j++){
arr.unshift(false);
}
return arr;
}
function array2dec(arr){
var dec = 0;
for(var i=arr.length-1;i>=0;i--){
dec += int(arr[i]) * pow(2,arr.length-i-1);
}
return dec;
}
function array2string(arr){
var arr_str = '';
for(var i=0;i<arr.length;i++){
arr_str += str(int(arr[i]));
}
return arr_str;
}
function setup(){
//cell_scale = 10;
createCanvas(70*cell_scale, 55*cell_scale);
background(50);
//renderRule(70);
}
function renderRule(rulenum){
ruleset = dec2array(rulenum);
background(50);
translate(50,10);
// the parent row
grid = new Grid(init_cells(grid_steps), 1, cell_scale);
grid.render_text();
// create 100 generations
for(var i=0;i<grid_steps;i++){
// get next gen
grid = grid.next_gen(ruleset);
// display
//grid.show();
grid.render_text();
}
}
function random_cells(len){
var cells = [];
for(var i=0;i<len;i++){
cells.push(Math.random() >= 0.5);
}
return cells;
}
function init_cells(len){
var cells = new Array(len).fill(false);
cells[len/2] = true;
return cells;
}
function draw(){
// reset time
if(t>255){
t=0;
}
if(int(t_1) != int(t)){
renderRule(t);
t_1 = t;
}
// time increment
t+=0.01;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment