Advertisement
Guest User

Untitled

a guest
Apr 17th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. boolean thisGen[][] = new boolean[100][100];
  2. boolean nextGen[][] = new boolean[100][100];
  3.  
  4. void setup() {
  5. size(800, 800);
  6. strokeWeight(1);
  7. frameRate(100);
  8. for (int i = 0; i < thisGen.length; i++) {
  9. for (int j = 0; j < thisGen.length; j++) {
  10. nextGen[i][j] = false;
  11. int rand = int(random(5));
  12. if (rand == 0) {
  13. thisGen[i][j] = true;
  14. }
  15. }
  16. }
  17. //glider
  18. //thisGen[5][5] = true;
  19. //thisGen[6][5] = true;
  20. //thisGen[6][3] = true;
  21. //thisGen[7][4] = true;
  22. //thisGen[7][5] = true;
  23. }
  24.  
  25. void draw() {
  26. for (int i = 0; i < thisGen.length; i++) {
  27. for (int j = 0; j < thisGen.length; j++) {
  28. if (thisGen[i][j]) {
  29. fill(0);
  30. } else fill(255);
  31. rect(i*8, j*8, 8, 8);
  32. }
  33. }
  34. for (int i = 1; i < thisGen.length - 1; i++) {
  35. for (int j = 1; j < thisGen.length - 1; j++) {
  36.  
  37. int count = 0;
  38. if (thisGen[i-1][j-1]) count++; // top left
  39. if (thisGen[i][j-1]) count++; //top middle
  40. if (thisGen[i+1][j-1]) count++; // top right
  41. if (thisGen[i+1][j]) count++; // middle right
  42. if (thisGen[i+1][j+1]) count++; //bottom right
  43. if (thisGen[i][j+1]) count++; //bottom middle
  44. if (thisGen[i-1][j+1]) count++; //bottom left
  45. if (thisGen[i-1][j]) count++; // left middle
  46.  
  47. if (thisGen[i][j] && count < 2) nextGen[i][j] = false;
  48. if (thisGen[i][j] && count > 3) nextGen[i][j] = false;
  49. if (thisGen[i][j] && count == 2) nextGen[i][j] = true;
  50. if (thisGen[i][j] && count == 3) nextGen[i][j] = true;
  51. if (!thisGen[i][j] && count == 3) nextGen[i][j] = true;
  52. }
  53. }
  54.  
  55. for (int i = 0; i < thisGen.length; i++) {
  56. for (int j = 0; j < thisGen.length; j++) {
  57. thisGen[i][j] = nextGen[i][j];
  58. }
  59. }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement