Advertisement
Guest User

Untitled

a guest
May 9th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.48 KB | None | 0 0
  1. boolean thisGen[][] = new boolean[100][100];
  2. boolean nextGen[][] = new boolean[100][100];
  3. int state;
  4. boolean underPop, overPop, alive, revive;
  5.  
  6. void setup() {
  7. underPop = false;
  8. overPop = false;
  9. alive = true;
  10. revive = true;
  11. size(800, 800);
  12. strokeWeight(1);
  13. frameRate(10);
  14. state = 0;
  15. for (int i = 0; i < thisGen.length; i++) {
  16. for (int j = 0; j < thisGen.length; j++) {
  17. nextGen[i][j] = false;
  18. int rand = int(random(4));
  19. if (rand == 0) {
  20. thisGen[i][j] = true;
  21. }
  22. }
  23. }
  24. //glider
  25. //thisGen[5][5] = true;
  26. //thisGen[6][5] = true;
  27. //thisGen[6][3] = true;
  28. //thisGen[7][4] = true;
  29. //thisGen[7][5] = true;
  30. }
  31.  
  32. void draw() {
  33. fill(0);
  34. textAlign(CENTER);
  35. textSize(50);
  36. if (state == 0) {
  37. text("Rules", width/2, 100);
  38. text("Press Enter to start", width/2, height-100);
  39. textSize(25);
  40. text("Black = true", width-100, 75);
  41. text("White = false", width-100, 100);
  42. text("If population is less than 2", width/2, 200);
  43. if (underPop == true) fill(0);
  44. else fill(255);
  45. rect(width/2, 250, 25, 25);
  46. if ((mouseX > width/2) && (mouseX < width/2 + 25) && (mouseY > 250) && (mouseY < 250 + 25) && (mousePressed)) {
  47. if (underPop == false) underPop = true;
  48. else if (underPop == true) underPop = false;
  49. }
  50. fill(0);
  51. text("If population bigger than 3", width/2, 300);
  52. if (overPop == true) fill(0);
  53. else fill(255);
  54. rect(width/2, 350, 25, 25);
  55. if ((mouseX > width/2) && (mouseX < width/2 + 25) && (mouseY > 350) && (mouseY < 350 + 25) && (mousePressed)) {
  56. if (overPop == false) overPop = true;
  57. else if (overPop == true) overPop = false;
  58. }
  59.  
  60. fill(0);
  61. text("If population is 2 or 3", width/2, 400);
  62. if (alive == true) fill(0);
  63. else fill(255);
  64. rect(width/2, 450, 25, 25);
  65. if ((mouseX > width/2) && (mouseX < width/2 + 25) && (mouseY > 450) && (mouseY < 450 + 25) && (mousePressed)) {
  66. if (alive == false) alive = true;
  67. else if (alive == true) alive = false;
  68. }
  69.  
  70. fill(0);
  71. text("If it is dead and 3 neighbours are beside it", width/2, 500);
  72. if (revive == true) fill(0);
  73. else fill(255);
  74. rect(width/2, 550, 25, 25);
  75. if ((mouseX > width/2) && (mouseX < width/2 + 25) && (mouseY > 550) && (mouseY < 550 + 25) && (mousePressed)) {
  76. if (revive == false) revive = true;
  77. else if (revive == true) revive = false;
  78. }
  79. if (keyCode == ENTER) {
  80. state = 1;
  81. }
  82. }
  83.  
  84.  
  85. if (state == 1) {
  86. for (int i = 0; i < thisGen.length; i++) {
  87. for (int j = 0; j < thisGen.length; j++) {
  88. if (thisGen[i][j] == true) {
  89. fill(0);
  90. } else fill(255, 190);
  91. rect(i*8, j*8, 8, 8);
  92. }
  93. }
  94. for (int i = 1; i < thisGen.length - 1; i++) {
  95. for (int j = 1; j < thisGen.length - 1; j++) {
  96.  
  97. int count = 0;
  98. //The middle
  99. if (thisGen[i-1][j-1]) count++; // top left
  100. if (thisGen[i][j-1]) count++; //top middle
  101. if (thisGen[i+1][j-1]) count++; // top right
  102. if (thisGen[i+1][j]) count++; // middle right
  103. if (thisGen[i+1][j+1]) count++; //bottom right
  104. if (thisGen[i][j+1]) count++; //bottom middle
  105. if (thisGen[i-1][j+1]) count++; //bottom left
  106. if (thisGen[i-1][j]) count++; // left middle
  107.  
  108. //Rules
  109. if (thisGen[i][j] && count < 2) nextGen[i][j] = underPop; //popuation less than 2 = die
  110. if (thisGen[i][j] && count > 3) nextGen[i][j] = overPop; //population bigger than 3 = die
  111. if (thisGen[i][j] && count == 2) nextGen[i][j] = alive; //population is 2 = alive
  112. if (thisGen[i][j] && count == 3) nextGen[i][j] = alive; //population is 3 = alive
  113. if (!thisGen[i][j] && count == 3) nextGen[i][j] = revive; //if it's dead and there is exatopLefty 3 beside it = alive
  114. }
  115. }
  116.  
  117. //TOP
  118. for (int i = 1; i < thisGen.length - 1; i++) {
  119. int count = 0;
  120. if (thisGen[i-1][99]) count++;
  121. if (thisGen[i][99]) count++;
  122. if (thisGen[i+1][99]) count++;
  123. if (thisGen[i+1][0]) count++;
  124. if (thisGen[i+1][1]) count++;
  125. if (thisGen[i][1]) count++;
  126. if (thisGen[i-1][1]) count++;
  127. if (thisGen[i-1][0]) count++;
  128.  
  129. if (thisGen[i][0]) {
  130. if (count < 2 || count > 3) nextGen[i][0] = false;
  131. else nextGen[i][0] = true;
  132. } else if (!thisGen[i][0] && count == 3) nextGen[i][0] = true;
  133. }
  134.  
  135. for (int i = 0; i < thisGen.length; i++) {
  136. for (int j = 0; j < thisGen.length; j++) {
  137. thisGen[i][j] = nextGen[i][j];
  138. }
  139. }
  140.  
  141. //RIGHT
  142. for (int j = 1; j < thisGen.length - 1; j++) {
  143. int count = 0;
  144. if (thisGen[98][j-1]) count++;
  145. if (thisGen[99][j-1]) count++;
  146. if (thisGen[0][j-1]) count++;
  147. if (thisGen[0][j]) count++;
  148. if (thisGen[0][j+1]) count++;
  149. if (thisGen[99][j+1]) count++;
  150. if (thisGen[98][j+1]) count++;
  151. if (thisGen[98][j]) count++;
  152.  
  153. if (thisGen[99][j]) {
  154. if (count < 2 ||count > 3) nextGen[99][j] = false;
  155. else nextGen[99][j] = true;
  156. } else if (!thisGen[99][j] && count == 3) nextGen[99][j] = true;
  157. }
  158.  
  159. //BOTTOM
  160. for (int i = 1; i < thisGen.length - 1; i++) {
  161. int count = 0;
  162. if (thisGen[i-1][98]) count++;
  163. if (thisGen[i][98]) count++;
  164. if (thisGen[i+1][98]) count++;
  165. if (thisGen[i+1][99]) count++;
  166. if (thisGen[i+1][0]) count++;
  167. if (thisGen[i][0]) count++;
  168. if (thisGen[i-1][0]) count++;
  169. if (thisGen[i-1][99]) count++;
  170.  
  171. if (thisGen[i][99]) {
  172. if (count < 2 || count > 3) nextGen[i][99] = false;
  173. else nextGen[i][99] = true;
  174. } else if (!thisGen[i][99] && count == 3) nextGen[i][99] = true;
  175. }
  176.  
  177. //LEFT
  178. for (int j = 1; j < thisGen.length - 1; j++) {
  179. int count = 0;
  180. if (thisGen[99][j-1]) count++;
  181. if (thisGen[0][j-1]) count++;
  182. if (thisGen[1][j-1]) count++;
  183. if (thisGen[1][j]) count++;
  184. if (thisGen[1][j+1]) count++;
  185. if (thisGen[0][j+1]) count++;
  186. if (thisGen[99][j+1]) count++;
  187. if (thisGen[99][j]) count++;
  188.  
  189. if (thisGen[0][j]) {
  190. if (count < 2 || count > 3) nextGen[0][j] = false;
  191. else nextGen[0][j] = true;
  192. } else if (!thisGen[0][j] && count == 3) nextGen[0][j] = true;
  193. }
  194.  
  195. // Top left
  196. int topLeft = 0;
  197. if (thisGen[99][99]) topLeft++;
  198. if (thisGen[0][99]) topLeft++;
  199. if (thisGen[1][99]) topLeft++;
  200. if (thisGen[1][0]) topLeft++;
  201. if (thisGen[1][1]) topLeft++;
  202. if (thisGen[0][1]) topLeft++;
  203. if (thisGen[99][1]) topLeft++;
  204. if (thisGen[99][0]) topLeft++;
  205.  
  206. if (thisGen[0][0]) {
  207. if (topLeft < 2 || topLeft > 3) nextGen[0][0] = false;
  208. else nextGen[0][0] = true;
  209. } else if (!thisGen[0][0] && topLeft == 3) nextGen[0][0] = true;
  210.  
  211. // Top right
  212. int topRight = 0;
  213. if (thisGen[98][99]) topRight++;
  214. if (thisGen[99][99]) topRight++;
  215. if (thisGen[0][99]) topRight++;
  216. if (thisGen[0][0]) topRight++;
  217. if (thisGen[0][1]) topRight++;
  218. if (thisGen[99][1]) topRight++;
  219. if (thisGen[98][1]) topRight++;
  220. if (thisGen[98][0]) topRight++;
  221.  
  222. if (thisGen[99][0]) {
  223. if (topRight < 2 || topRight > 3) nextGen[99][0] = false;
  224. else nextGen[99][0] = true;
  225. } else if (!thisGen[99][0] && topRight == 3) nextGen[99][0] = true;
  226.  
  227. // Bottom left
  228. int bottomLeft = 0;
  229. if (thisGen[99][98]) bottomLeft++;
  230. if (thisGen[0][98]) bottomLeft++;
  231. if (thisGen[1][98]) bottomLeft++;
  232. if (thisGen[1][99]) bottomLeft++;
  233. if (thisGen[1][0]) bottomLeft++;
  234. if (thisGen[0][0]) bottomLeft++;
  235. if (thisGen[99][0]) bottomLeft++;
  236. if (thisGen[99][99]) bottomLeft++;
  237.  
  238. if (thisGen[0][99]) {
  239. if (bottomLeft < 2 || bottomLeft > 3) nextGen[0][99] = false;
  240. else nextGen[0][99] = true;
  241. } else if (!thisGen[0][99] && bottomLeft == 3) nextGen[0][99] = true;
  242.  
  243. // Bottom right
  244. int bottomRight = 0;
  245. if (thisGen[98][98]) bottomRight++;
  246. if (thisGen[99][98]) bottomRight++;
  247. if (thisGen[0][98]) bottomRight++;
  248. if (thisGen[0][99]) bottomRight++;
  249. if (thisGen[0][0]) bottomRight++;
  250. if (thisGen[99][0]) bottomRight++;
  251. if (thisGen[98][0]) bottomRight++;
  252. if (thisGen[98][99]) bottomRight++;
  253.  
  254. if (thisGen[99][99]) {
  255. if (bottomRight < 2 || bottomRight > 3) nextGen[99][99] = false;
  256. else nextGen[99][99] = true;
  257. } else if (!thisGen[99][99] && bottomRight == 3) nextGen[99][99] = true;
  258. }
  259. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement