Reputation: 1
I have built the Conways game of life but the algorithm is not working properly. I used a mix of Js and Jquery for this. What my program does is go through the entire board cell by cell, check the cells neighbours and apply rules of the game to each cell by checking it's neighbours. Here is the code for that:
function checkSquares() {
generation++;
document.getElementById('gen').innerHTML=generation;
for (var i = 100; i <= 6220; i++) {
if (squareArray[i][1] === 1) {
var total = squareArray[i + 81][1] + squareArray[i + 80][1] + squareArray[i + 79][1] + squareArray[i - 81][1] + squareArray[i - 80][1] + squareArray[i - 79][1] + squareArray[i + 1][1] + squareArray[i - 1][1];
switch (total) {
case 0:
case 1:
squareArray[i][1] = 0;
$('#square' + i).css("background-image", "url('http://www.fg-a.com/wallpapers/geo-shapes-black-1280.jpg')");
break;
case 4:
case 5:
case 6:
case 7:
case 8:
squareArray[i][1] = 0;
$('#square' + i).css("background-image", "url('http://www.fg-a.com/wallpapers/geo-shapes-black-1280.jpg')");
break;
}
}else{
var total = squareArray[i + 81][1] + squareArray[i + 80][1] + squareArray[i + 79][1] + squareArray[i - 81][1] + squareArray[i - 80][1] + squareArray[i - 79][1] + squareArray[i + 1][1] + squareArray[i - 1][1];
switch(total){
case 3:
squareArray[i][1] = 1;
$('#square' + i).css("background-image", "url('https://c1.staticflickr.com/3/2942/15323841455_6c64757dbd_b.jpg')");
break;
}
}
}
eliminate();
}
In short what the above code does is take a square, check it's neighbouring cells and use if-else statements to decide whether the cell lives or dies.
Now I do know what the problem is; for example take a simple pattern such as this:
cell here dies ----> []
new cell born here --> [] <-- new cell born here
cell here dies ----> []
What happens in my code is:
checks this cell, only one neighbour so it dies ---> []
When it comes to this cell it has only one neighbour because above -> []
neighbour died. Therefore it dies.
No neighbours, so this dies -----------------------> []
So the obvious solution is to somehow check the entire pattern and then decide on whether the cell is to live or die. But how can I check several cells at once?
Also, if it helps, here is the codepen link to the full program:
http://codepen.io/Phantom-Intruder/pen/BLaBPG/
Upvotes: 0
Views: 85
Reputation: 386604
You need to keep the old board and generate a new board (called generation) with the rules.
Then skip the old board an use the new one.
With only one board Conway's Game of Life does not work, because you destroy the actual state of the board while interacting with the neibours.
The initial pattern constitutes the seed of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed—births and deaths occur simultaneously, and the discrete moment at which this happens is sometimes called a tick (in other words, each generation is a pure function of the preceding one). The rules continue to be applied repeatedly to create further generations.
Upvotes: 2