Advertisement
Guest User

YOU SHALL NOT STEAL

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