Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //static variables
- color yellow = color(239, 255, 23);
- color gray = color(195, 196, 192);
- //"instance" variables
- boolean[][] grid;
- int boxWidth, boxHeight;
- void setup() {
- size(500, 500);
- background(gray);
- initGrid();
- drawLines();
- }
- void draw() {
- if (mousePressed == true) {
- changeElement();
- delay(100);
- }
- drawGrid();
- if(keyCode == ENTER){
- frameRate(5);
- update();
- }
- else if(keyCode == CONTROL){
- keyCode = SHIFT;
- initGrid();
- frameRate(60);
- }
- }
- void changeElement() {
- grid[mouseX/boxWidth][mouseY/boxHeight] = !grid[mouseX/boxWidth][mouseY/boxHeight];
- }
- //*********************//
- // DAY 1 //
- //*********************//
- //1. void initGrid()
- //a) instantiate the grid to be 50 by 50. *Note: This will also initialize all elements to false, which is what we want.
- //b) initialize the boxWidth and boxHeight to be the width and height divided by the number of boxes in each row
- void initGrid() {
- grid = new boolean[50][50];
- boxWidth = width/50;
- boxHeight = height/50;
- }
- //2. void drawLines()
- //* remember, the line function works like line(x1, y1, x2, y2);
- //a) draw all lines going across from left to right
- //b) draw all lines going vertically from top to bottom
- //c) each line should be separated by boxWidth or boxHeight in either direction
- //*there should be 50 lines in either direction
- //*height and width are variables that contain the height and width, respectively
- void drawLines() {
- for (int r = 0; r < height; r += boxHeight) {
- line(0, r, width, r);
- }
- for (int c = 0; c < width; c += boxWidth) {
- line(c, 0, c, height);
- }
- }
- //3. int countNeighbors(int r, int c)
- //This method takes the row number and column number and returns the number of neighbors @ that location
- //a) initialize a counter
- //b)check all 8 neighbors and increment counter if they are populated ( true )
- //b1) check the 3 elements in the row above
- //b2) check the 2 elements in the same row to the left or right.
- //b3) check the 3 elements in the row below
- //*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
- //c) return the count
- int countNeighbors(int r, int c) {
- int count = 0;
- //row above
- if ( r - 1 >= 0 && c - 1 >= 0 && grid[r-1][c-1] == true ) count++;
- if ( r - 1 >= 0 && grid[r-1][c] == true ) count++;
- if ( r - 1 >= 0 && c + 1 < grid[r].length && grid[r-1][c+1] == true ) count++;
- //same row
- if ( c - 1 >= 0 && grid[r][c-1] == true ) count++;
- if ( c + 1 < grid[r].length && grid[r][c+1] == true ) count++;
- //row below
- if ( r + 1 < grid.length && c - 1 >= 0 && grid[r+1][c-1] == true ) count++;
- if ( r + 1 < grid.length && grid[r+1][c] == true ) count++;
- if ( r + 1 < grid.length && c + 1 < grid[r].length && grid[r+1][c+1] == true ) count++;
- return count;
- }
- //*********************//
- // DAY 2 //
- //*********************//
- //4. void drawGrid()
- //a)loop over all the row indices
- //b)make a nested loop inside that loops over all the column indices
- //c)if the grid is occupied, change fill color to yellow
- //d)otherwise, change fill color to gray
- //e)draw the rectangle as in rect(r * boxHeight, c * boxWidth, boxWidth, boxHeight);
- void drawGrid() {
- for (int r = 0; r < grid.length; r++) {
- for (int c = 0; c < grid[r].length; c++) {
- if (grid[r][c] == true) {
- fill( yellow );
- } else {
- fill( gray );
- }
- rect(r * boxHeight, c * boxWidth, boxWidth, boxHeight);
- }
- }
- }
- //5. void update()
- //a) make a new boolean 2D array that holds the same amount of elements as grid
- //b) loop over all the elements in grid using a nested loop
- //c) call count neighbors to determine how many neighbors the location r,c has
- //d) use the conway's game of life rules to set the value for the corresponding possition of the new grid
- //e) at the end of the method, point grid at the new grid or do a deep copy
- void update() {
- boolean[][] newGrid = new boolean[grid.length][grid[0].length];
- for (int r = 0; r < grid.length; r++) {
- for (int c = 0; c < grid[r].length; c++) {
- int numNeighbors = countNeighbors(r, c);
- if ( grid[r][c] == true ) {
- if ( numNeighbors <= 1 || numNeighbors >= 4) newGrid[r][c] = false;
- else if ( numNeighbors == 2 || numNeighbors == 3 ) newGrid[r][c] = true;
- } else {
- if ( numNeighbors == 3 ) newGrid[r][c] = true;
- }
- }
- }
- grid = newGrid;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement