Advertisement
Guest User

Untitled

a guest
Oct 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1.  
  2. public class Runner {
  3.  
  4. static int scale = 10;
  5. static boolean grid[][] = new boolean[500 / scale][500 / scale];
  6.  
  7. public static void main(String[] args) {
  8. Window.size(500, 500);
  9. Window.out.background("white");
  10.  
  11. // Assign a random value to every place in the grid
  12. // either a living cell or not, completely random
  13. for (int row = 0; row < grid.length; row++) {
  14. for (int col = 0; col < grid[row].length; col++) {
  15. if (Math.random() < 0.5) {
  16. grid[row][col] = false;
  17. } else {
  18. grid[row][col] = true;
  19. }
  20. }
  21. }
  22.  
  23. while (true) {
  24. // NEW: the method to add cells on mouse click
  25. addCell();
  26. // make it draw
  27. draw();
  28. // make it move update everything in our game
  29. update();
  30. // Refreshing
  31. Window.frame();
  32.  
  33. }
  34.  
  35. }
  36.  
  37. public static void addCell() {
  38. // if the mouse if clicked
  39. if (Window.mouse.clicked()) {
  40.  
  41. // get the coordinates of the click
  42. int x = Window.mouse.getX();
  43. int y = Window.mouse.getY();
  44.  
  45. // translate the coordinates into scaled rows and columns; how we will find it
  46. // in our grid
  47. int col = x / scale;
  48. int row = y / scale;
  49.  
  50. grid[col][row] = true;
  51.  
  52. }
  53. // this is for the block structure/pattern
  54. if (Window.key.pressed("b")) {
  55. //change the actual grid to make the desired pattern
  56. // in this case a 2x2 grid
  57.  
  58. int x = Window.mouse.getX();
  59. int y = Window.mouse.getY();
  60.  
  61. // translate the coordinates into scaled rows and columns; how we will find it
  62. // in our grid
  63. int col = x / scale;
  64. int row = y / scale;
  65.  
  66. grid[col][row] = true;
  67. grid[col + 1][row] = true;
  68. grid[col][row + 1] = true;
  69. grid[col + 1][row + 1] = true;
  70. }
  71. }
  72.  
  73. public static void draw() {
  74. // cells and dots
  75. for (int row = 0; row < grid.length; row++) {
  76. for (int col = 0; col < grid[row].length; col++) {
  77. if (grid[row][col] == true) {
  78. // draw it
  79. Window.out.color("white");
  80. Window.out.square(row * scale, col * scale, scale);
  81. } else {
  82. // don't want to draw it
  83. }
  84. }
  85. }
  86. }
  87.  
  88. public static void update() {
  89.  
  90. // play out the simulation
  91. // go through each cell and if dead or alive
  92. // follow the four rules
  93.  
  94. // 1 is create a copy of the grid
  95. boolean copy[][] = new boolean[500 / scale][500 / scale];
  96.  
  97. for (int row = 0; row < grid.length; row++) {
  98. for (int col = 0; col < grid.length; col++) {
  99.  
  100. copy[row][col] = grid[row][col];
  101. }
  102. }
  103.  
  104. // 2 determine the number of neighbors (max of 8)
  105. for (int row = 0; row < grid.length; row++) {
  106. for (int col = 0; col < grid.length; col++) {
  107.  
  108. int neighbors = 0;
  109.  
  110. // up and to the left
  111. if (row != 0 && col != 0 && grid[row - 1][col - 1] == true) {
  112. neighbors = neighbors + 1;
  113. }
  114. // directly up
  115. if (row != 0 && grid[row - 1][col] == true) {
  116. neighbors = neighbors + 1;
  117. }
  118. // up and to the right
  119. if (row != 0 && col < grid.length - 1 && grid[row - 1][col + 1] == true) {
  120. neighbors = neighbors + 1;
  121. }
  122. // directly right
  123. if (col < grid.length - 1 && grid[row][col + 1] == true) {
  124. neighbors = neighbors + 1;
  125. }
  126. // down and to the right
  127. if (col < grid.length - 1 && row < grid.length - 1 && grid[row + 1][col + 1] == true) {
  128. neighbors = neighbors + 1;
  129. }
  130. // directly down
  131. if (row < grid.length - 1 && grid[row + 1][col] == true) {
  132. neighbors = neighbors + 1;
  133. }
  134. // down and to the left
  135. if (row < grid.length - 1 && col != 0 && grid[row + 1][col - 1] == true) {
  136. neighbors = neighbors + 1;
  137. }
  138. // directly left
  139. if (col != 0 && grid[row][col - 1] == true) {
  140. neighbors = neighbors + 1;
  141. }
  142.  
  143. // 3 test the number of neighbors with the rules
  144. // rule 1
  145. if (neighbors < 2) {
  146. copy[row][col] = false;
  147. }
  148. // rule 3
  149. else if (neighbors > 3) {
  150. copy[row][col] = false;
  151. }
  152. // rule 4
  153. else if (neighbors == 3) {
  154. copy[row][col] = true;
  155. }
  156. }
  157. }
  158.  
  159. grid = copy;
  160.  
  161. // 4 update the cell in the next generation (alive or dead)
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement