Forked from shawki2/gist:044fd11cf4625788acf3e625125df181
Last active
July 3, 2018 09:29
-
-
Save textbook/2105272d6de7cedf0813a341cf87062c to your computer and use it in GitHub Desktop.
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
var cell = new Array(3); | |
for (i = 0; i < 3; i++) { | |
cell[i] = new Array(3); | |
} | |
var newCell = new Array(3); | |
for (i = 0; i < 3; i++) { | |
newCell[i] = new Array(3); | |
} | |
// you do the same thing twice above; it would be worth extracting that to a function | |
var i = 0, j = 0; // these are re-defined in the for loops anyway, so this line is redundant | |
function checkArray(cell) { | |
console.log("check center call"); | |
for (var i = 0; i <= 2; i++) { | |
console.log("check center i= ", i); | |
for (var j = 0; j <= 2; j++) { | |
console.log("check center j= ", j); | |
if (cell[i][j] === 1) { | |
cell[i][j] = 1; | |
console.log("check entering one"); | |
} else { | |
cell[i][j] = 0; | |
console.log("check entering zeros"); | |
} | |
} | |
} | |
console.log("check end call", cell); | |
return cell; | |
} | |
function neighbour(returnCell) { | |
var totalCells = 0; var cell2 = returnCell; // this *won't* create a copy of the cell! | |
console.log("check center call second function"); | |
// why f and k here rather than i and j? | |
for (var f = 1; f < 2; f++) { //iterate through rows | |
console.log("check center second function f= ", f); | |
for (var k = 1; k < 2; k++) { //iterate through columns | |
console.log("check center second function j= ", k); | |
//add up the total values for the surrounding cells | |
totalCells += cell2[f - 1][k - 1]; //top left | |
totalCells += cell2[f - 1][k]; //top center | |
totalCells += cell2[f - 1][k + 1]; //top right | |
totalCells += cell2[f][k - 1]; //middle left | |
//totalCells += cell2[f][k]; //middle center | |
totalCells += cell2[f][k + 1]; //middle right | |
totalCells += cell2[f + 1][k - 1]; //bottom left | |
totalCells += cell2[f + 1][k]; //bottom center | |
totalCells += cell2[f + 1][k + 1]; //bottom right | |
console.log("check totalCells second function totalCells= ", totalCells); | |
//apply the rules to each cell | |
if (cell2[f][k] === 0) { | |
console.log("check if second function totalCells= ", totalCells); | |
switch (totalCells) { | |
case 3: | |
newCell[f][k] = 1; //if cell is dead and has 3 neighbours, switch it on | |
break; | |
default: | |
newCell[f][k] = 0; //otherwise leave it dead | |
} | |
// no need for else if - if it's not 0 it must be 1 | |
} else if (cell2[f][k] === 1) { //apply rules to living cell | |
console.log("check if second function totalCells= ", totalCells); | |
switch (totalCells) { | |
case 0: | |
case 1: | |
newCell[f][k] = 0; //die of lonelines | |
console.log("check case 1 = ", totalCells); | |
console.log("die of lonelines "); | |
break; | |
case 2: console.log("die of lonelines "); break; | |
case 3: | |
newCell[f][k] = 1; //carry on living | |
console.log("carry on living "); | |
break; | |
case 4: console.log("carry on living "); break; | |
case 5: console.log("carry on living "); break; | |
case 6: console.log("carry on living "); break; | |
case 7: console.log("carry on living "); break; | |
case 8: | |
newCell[f][k] = 0; //die of overcrowding | |
console.log("die of lonelines "); | |
break; | |
default: | |
newCell[f][k] = 0; // | |
} | |
} | |
} | |
for (var f = 0; f <= 2; f++) { //iterate through rows | |
// console.log("check lastcall For = "); | |
for (var k = 0; k <= 2; k++) { //iterate through columns | |
newCell[f][k] = cell2[f][k]; | |
} | |
} | |
} | |
return newCell; // if you return the new cell, you should create it inside this function | |
} | |
var array = [[0, 0, 0], [0, 1, 1], [0, 0, 0]]; | |
var retChec = checkArray(array); | |
console.log("retChec = ", retChec); | |
console.log("checking the arrary = ", retChec); // this is the same thing you just logged, is that right? | |
console.log("checking the neighbours of cells ", neighbour(retChec)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment