Advertisement
fosterbl

Conway's Game of Life in Processing - incomplete day 1

Jan 16th, 2020
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.12 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();  //to be completed day 2
  22.   if(keyCode == ENTER){
  23.     frameRate(5);
  24.     //update();  //to be completed day 2
  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.  
  46. }
  47.  
  48. //2. void drawLines()
  49. //* remember, the line function works like line(x1, y1, x2, y2);
  50. //a) draw all lines going across from left to right
  51. //b) draw all lines going vertically from top to bottom
  52. //c) each line should be separated by boxWidth or boxHeight in either direction
  53. //*there should be 50 lines in either direction
  54. //*height and width are variables that contain the height and width, respectively
  55. void drawLines() {
  56.  
  57. }
  58.  
  59. //3. int countNeighbors(int r, int c)
  60. //This method takes the row number and column number and returns the number of neighbors @ that location
  61. //a) initialize a counter
  62. //b)check all 8 neighbors and increment counter if they are populated ( true )
  63. //b1) check the 3 elements in the row above
  64. //b2) check the 2 elements in the same row to the left or right.
  65. //b3) check the 3 elements in the row below
  66. //*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
  67. //c) return the count
  68. int countNeighbors(int r, int c) {
  69.     //get rid of the line below and write the method
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement