Advertisement
Guest User

Game Of Life Help

a guest
Jun 13th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. This is more of a stab in the dark than anything, but I think that using a for loop to set the states of the "cells" is causing this bug.
  2.  
  3. Let's say this is your grid:
  4.  
  5. ▓▓ - A living cell
  6. ░░ - A non-living cell
  7.  
  8. ░░░░░░░░░░
  9. ░░▓▓▓▓▓▓░░
  10. ░░▓▓░░▓▓░░
  11. ░░▓▓▓▓▓▓░░
  12. ░░░░░░░░░░
  13.  
  14. Here we have the 3x3 "donut" shape that causes the bug.
  15.  
  16. Your script runs through this top left to bottom right.
  17.  
  18. It does nothing until it reaches the top-middle non-living cell. It recognises that this has three neighbours, and sets it to living.
  19.  
  20. ░░░░▓▓░░░░
  21. ░░▓▓▓▓▓▓░░
  22. ░░▓▓░░▓▓░░
  23. ░░▓▓▓▓▓▓░░
  24. ░░░░░░░░░░
  25.  
  26. This means that the next cell scanned now has three neighbours, and thus:
  27.  
  28. ░░░░▓▓▓▓░░
  29. ░░▓▓▓▓▓▓░░
  30. ░░▓▓░░▓▓░░
  31. ░░▓▓▓▓▓▓░░
  32. ░░░░░░░░░░
  33.  
  34. The next two cells have nothing happen to them as they both have exactly two neighbours. However, the cell at (3, 4) is now overpopulated:
  35.  
  36. ░░░░▓▓▓▓░░
  37. ░░▓▓░░▓▓░░
  38. ░░▓▓░░▓▓░░
  39. ░░▓▓▓▓▓▓░░
  40. ░░░░░░░░░░
  41.  
  42. And the one at (5, 4) is now ready to populate:
  43.  
  44. ░░░░▓▓▓▓░░
  45. ░░▓▓░░▓▓▓▓
  46. ░░▓▓░░▓▓░░
  47. ░░▓▓▓▓▓▓░░
  48. ░░░░░░░░░░
  49.  
  50. This goes on as you'd expect it to.
  51.  
  52. Basically, by changing the state of the cell as you test its conditions, you affect the conditions of the surrounding cells. What you want to be doing is reading the conditions of all the cells and setting a seperate variable for their desired state.
  53.  
  54. Then, at the end of the update() function, you run a second for loop which sets the actual value to the desired value. This means that the cells are all read and dealt with before they change, and stops the distorting of cell states.
  55.  
  56. Hope this made sense and helps to fix the issue!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement