Advertisement
fosterbl

COMPLETE Conway's Game of Life in Processing

Jan 17th, 2020
202
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.48 KB | None | 0 0
  1. //static variables
  2. color yellow = color(239, 255, 23);
  3. color gray = color(195, 196, 192);
  4.  
  5. //"instance" variables
  6. boolean[][] grid;
  7. int boxWidth, boxHeight;
  8.  
  9. void setup() {
  10.   size(500, 500);
  11.   background(gray);
  12.   initGrid();
  13.   drawLines();
  14. }
  15.  
  16. void draw() {
  17.   if (mousePressed == true) {
  18.     changeElement();
  19.     delay(100);
  20.   }
  21.   drawGrid();
  22.   if(keyCode == ENTER){
  23.     frameRate(5);
  24.     update();
  25.   }
  26.   else if(keyCode == CONTROL){
  27.     keyCode = SHIFT;
  28.     initGrid();
  29.     frameRate(60);
  30.   }
  31. }
  32.  
  33. void changeElement() {
  34.   grid[mouseX/boxWidth][mouseY/boxHeight] = !grid[mouseX/boxWidth][mouseY/boxHeight];
  35. }
  36.  
  37. //*********************//
  38. //      DAY 1          //
  39. //*********************//
  40.  
  41. //1. void initGrid()
  42. //a) instantiate the grid to be 50 by 50. *Note: This will also initialize all elements to false, which is what we want.
  43. //b) initialize the boxWidth and boxHeight to be the width and height divided by the number of boxes in each row
  44. void initGrid() {
  45.   grid = new boolean[50][50];
  46.   boxWidth = width/50;
  47.   boxHeight = height/50;
  48. }
  49.  
  50. //2. void drawLines()
  51. //* remember, the line function works like line(x1, y1, x2, y2);
  52. //a) draw all lines going across from left to right
  53. //b) draw all lines going vertically from top to bottom
  54. //c) each line should be separated by boxWidth or boxHeight in either direction
  55. //*there should be 50 lines in either direction
  56. //*height and width are variables that contain the height and width, respectively
  57. void drawLines() {
  58.   for (int r = 0; r < height; r += boxHeight) {
  59.     line(0, r, width, r);
  60.   }
  61.   for (int c = 0; c < width; c += boxWidth) {
  62.     line(c, 0, c, height);
  63.   }
  64. }
  65.  
  66. //3. int countNeighbors(int r, int c)
  67. //This method takes the row number and column number and returns the number of neighbors @ that location
  68. //a) initialize a counter
  69. //b)check all 8 neighbors and increment counter if they are populated ( true )
  70. //b1) check the 3 elements in the row above
  71. //b2) check the 2 elements in the same row to the left or right.
  72. //b3) check the 3 elements in the row below
  73. //*be careful about bounds issues. For example, if r = 0, the row above would be -1, which does not exist and will cause an error
  74. //c) return the count
  75. int countNeighbors(int r, int c) {
  76.   int count = 0;
  77.   //row above
  78.   if ( r - 1 >= 0 && c - 1 >= 0 && grid[r-1][c-1] == true ) count++;
  79.   if ( r - 1 >= 0 && grid[r-1][c] == true ) count++;
  80.   if ( r - 1 >= 0 && c + 1 < grid[r].length && grid[r-1][c+1] == true ) count++;
  81.  
  82.   //same row
  83.   if ( c - 1 >= 0 && grid[r][c-1] == true ) count++;
  84.   if ( c + 1 < grid[r].length && grid[r][c+1] == true ) count++;
  85.  
  86.   //row below
  87.   if ( r + 1 < grid.length && c - 1 >= 0 && grid[r+1][c-1] == true ) count++;
  88.   if ( r + 1 < grid.length && grid[r+1][c] == true ) count++;
  89.   if ( r + 1 < grid.length && c + 1 < grid[r].length && grid[r+1][c+1] == true ) count++;
  90.  
  91.   return count;
  92. }
  93.  
  94. //*********************//
  95. //      DAY 2          //
  96. //*********************//
  97.  
  98. //4. void drawGrid()
  99. //a)loop over all the row indices
  100. //b)make a nested loop inside that loops over all the column indices
  101. //c)if the grid is occupied, change fill color to yellow
  102. //d)otherwise, change fill color to gray
  103. //e)draw the rectangle as in rect(r * boxHeight, c * boxWidth, boxWidth, boxHeight);
  104. void drawGrid() {
  105.   for (int r = 0; r < grid.length; r++) {
  106.     for (int c = 0; c < grid[r].length; c++) {
  107.       if (grid[r][c] == true) {
  108.         fill( yellow );
  109.       } else {
  110.         fill( gray );
  111.       }
  112.       rect(r * boxHeight, c * boxWidth, boxWidth, boxHeight);
  113.     }
  114.   }
  115. }
  116.  
  117. //5. void update()
  118. //a) make a new boolean 2D array that holds the same amount of elements as grid
  119. //b) loop over all the elements in grid using a nested loop
  120. //c) call count neighbors to determine how many neighbors the location r,c has
  121. //d) use the conway's game of life rules to set the value for the corresponding possition of the new grid
  122. //e) at the end of the method, point grid at the new grid or do a deep copy
  123. void update() {
  124.   boolean[][] newGrid = new boolean[grid.length][grid[0].length];
  125.   for (int r = 0; r < grid.length; r++) {
  126.     for (int c = 0; c < grid[r].length; c++) {
  127.       int numNeighbors = countNeighbors(r, c);
  128.       if ( grid[r][c] == true ) {
  129.         if ( numNeighbors <= 1 || numNeighbors >= 4) newGrid[r][c] = false;
  130.         else if ( numNeighbors == 2 || numNeighbors == 3 ) newGrid[r][c] = true;
  131.       } else {
  132.         if ( numNeighbors == 3 ) newGrid[r][c] = true;
  133.       }
  134.     }
  135.   }
  136.   grid = newGrid;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement