My problem is this; I have the logic just right enough that certain shapes will stay untouched forever but I can tell something isn’t right. After studying the rules for the game of life and then studying my code thoroughly, I can’t seem to find why my game of life is acting differently than Conway’s.
Some of you who have built the game of life might be able to tell what’s up by just setting up a scenario on the board and watching what happens. The “check” button will complete one generation.
Thank you so much!
Here is my project - https://codepen.io/escottalexander/pen/oZRVLe?editors=0010
Here are some tests you can check under “Example Patterns”: http://www.math.cornell.edu/~lipa/mec/lesson6.html
Also, one reason they may not be working is because it doesn’t appear that you are using the rules correctly. These are the rules:
- Any live cell with fewer than two live neighbours dies (referred to as underpopulation or exposure).
- Any live cell with more than three live neighbours dies (referred to as overpopulation or overcrowding).
- Any live cell with two or three live neighbours lives, unchanged, to the next generation.
- Any dead cell with exactly three live neighbours will come to life.
but your switch case goes up to 8. You should write your rules as if/else if/else statements that take into account the life status of the cell being tested and all of its neighbors, but you won’t need 8 case statements.
I am guessing you are refreshing the board after every “square” you check? This way a square that “came to life” in this generation will also be counted as neighbour of a square that is checked after that (but in the same generation). So it will have a different number of neighbours when being checked, than when the generation started. Where a generation is one
But you shouldn’t take into account the changes that happened in the same generation. So, you would either have to run
neighbourCheck for all squares before you update anything. Or save a copy of the board (at the moment a new generation is started, which is at the start of
fullBoardNeighborCheck) for use in
Thanks, guys. dhcodes, You are right about not needing the 8 cases but why should I use if/else statements instead? Correct me if I’m wrong but it shouldn’t matter if a cell is alive or not. The quantity of neighbors decides whether a cell(dead or alive) will live or die in the next generation.
BenGitter, I bet you are right about what is happening. I will make a copy of the board that accepts the changes and then set the state from that when the checking process is finished.
Thanks for the input!
If cell is alive and has two neighbors it’ll live.
If cell is dead and has two neighbors it’ll stay dead.
Jenovs, I believe I have accounted for that by telling a square with two neighbors to stay as it is, whether dead or alive.
I have made the update that BenGitter suggested and I think it is working properly now with one exception. For some reason the very first time it does the neighborCheck and reRender it messes things up but after that first time it works fine (I think). I will look into it more tomorrow.
Thanks for all the help, everyone.
I have made it pretty far since the last issues I was having but ran into a snag with setting up setInterval with my “neighborChecking” function. I made a new thread since its a new problem.