Skip to content

Instantly share code, notes, and snippets.

@jacks205
Created March 9, 2015 06:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacks205/45360016ab238f6674e4 to your computer and use it in GitHub Desktop.
Save jacks205/45360016ab238f6674e4 to your computer and use it in GitHub Desktop.
Node.js script to simulate Cellular Automata
main();
//Create array of booleans
function main(){
var length = 64,
cellArray = new Array(length);
for(var i = 0; i < cellArray.length; ++i){
cellArray[i] = (i === 31);
}
//Run through 32 lines of cells
for(var i = 0; i < 32; ++i){
printCells(cellArray);
cellArray = evaluateCells(cellArray);
}
}
//Check right, left and then both of death case
function evaluateCells(cells){
var temp = cells.slice();
for(var i = 0; i < cells.length; ++i){
if(cells[i+1]) temp[i] = true; //right
if(cells[i-1]) temp[i] = true; //left
if(cells[i+1] && cells[i-1]) temp[i] = false; //both
}
return temp;
}
function printCells(cells){
for(var i = 0; i < cells.length; ++i){
if(!cells[i])
process.stdout.write('*');
else
process.stdout.write('-');
}
console.log();
}
// Output after 32 lines
// *******************************-********************************
// ******************************---*******************************
// *****************************--*--******************************
// ****************************---*---*****************************
// ***************************--*-*-*--****************************
// **************************---*-*-*---***************************
// *************************--*-*-*-*-*--**************************
// ************************---*-*-*-*-*---*************************
// ***********************--*-*-*-*-*-*-*--************************
// **********************---*-*-*-*-*-*-*---***********************
// *********************--*-*-*-*-*-*-*-*-*--**********************
// ********************---*-*-*-*-*-*-*-*-*---*********************
// *******************--*-*-*-*-*-*-*-*-*-*-*--********************
// ******************---*-*-*-*-*-*-*-*-*-*-*---*******************
// *****************--*-*-*-*-*-*-*-*-*-*-*-*-*--******************
// ****************---*-*-*-*-*-*-*-*-*-*-*-*-*---*****************
// ***************--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--****************
// **************---*-*-*-*-*-*-*-*-*-*-*-*-*-*-*---***************
// *************--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--**************
// ************---*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*---*************
// ***********--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--************
// **********---*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*---***********
// *********--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--**********
// ********---*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*---*********
// *******--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--********
// ******---*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*---*******
// *****--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--******
// ****---*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*---*****
// ***--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--****
// **---*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*---***
// *--*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*--**
// ---*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*---*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment