Created
March 9, 2015 06:36
-
-
Save jacks205/45360016ab238f6674e4 to your computer and use it in GitHub Desktop.
Node.js script to simulate Cellular Automata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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