Created
July 3, 2018 09:00
-
-
Save shawki2/044fd11cf4625788acf3e625125df181 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); | |
} | |
var i = 0, j = 0; | |
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; | |
console.log("check center call second function"); | |
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 | |
} | |
} 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; | |
} | |
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); | |
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