The Game of Life probably isn't what you think, it's a program that simulates how cells might grow or die based on a small set of rules. It was created by a British mathematician named John Conway and is sometimes called "Conway's Game of Life". John Conway was a very gifted Mathematician and universally admired for his unvarnished opinions. He unfortunately passed away April 11th 2020 due to complication with COVID - a tremendous loss to all of us. The following description and the image above are from Wikipedia:
Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:
- Any live cell with fewer than two live neighbours dies, as if by underpopulation.
- Any live cell with two or three live neighbours lives on to the next generation.
- Any live cell with more than three live neighbours dies, as if by overpopulation.
- Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
These rules, which compare the behavior of the automaton to real life, can be condensed into the following:
- Any live cell with two or three live neighbours survives.
- Any dead cell with three live neighbours becomes a live cell.
- All other live cells die in the next generation. Similarly, all other dead cells stay dead.
You can find examples and descriptions of the Game of Life at the following websites:
Play the Game of Life online
What is the Game of Life?
Conway's Game of Life on Wikipedia
- Your Game of Life will be a two dimensional array of buttons
- If the user clicks on a button it should turn that cell on and off
- All class member variables and methods will be labeled appropriately as either
publicorprivate - The finished program should correctly implement the rules of the Game of Life described above (or an interesting variation, see extensions below)
- The finished program should be able to be paused on and off so that the player can modify the arrangement of living cells
-
First make sure that you are not running the Processing 4 version. If so quit Processing 4.
-
You will need an OLDER version of processing for Guido GUI to work. Download it here for your Operating System. You will use this older version to write your code and run it.
-
Now Open Processing 3 you just downloaded. Then click
Sketch->Import Library. Please note that theGuidolibrary should already be available as shown below
-
If not let me know, I will come over to help.
- Fork and clone down this repository.
- Once downloaded, Open Processing 3.5.4 making sure Processing 4.x is NOT also running at simultaneously!!. Then use
File->Open...to open theGameOfLife.pdethat you just downloaded into Processing 3.5.4. - Under the comment
//Declare and initialize constants NUM_ROWS and NUM_COLS = 20create two integer constantsNUM_ROWSandNUM_COLSand initialize them each with the value 20 (using constants will make things much easier if later you want to change the size of the grid). - Under the comment
//your code to initialize buttons goes hereuse the constantsNUM_ROWSandNUM_COLSto initialize the 2d arraybuttonsto hold 20 rows and 20 columns ofLifeobjects. - Just under that, use nested loops to create a
new Lifefor each row column pair. Be sure to use the constants in your loop. - Locate and move all of the code under
class Lifeinto its own tab namedLife. Do not use the.javaextension to name this tab as we weill be invoking Processing functions from this class! - Uncomment the first two lines in the Life constructor
public Life (int row, int col) - In
drawmethod of theLifeclass under the comment//use nested loops to draw the buttons herewrite nested loops (again using the constants) to draw each button to the screen. - Run the program, you should now see a random grid of buttons similar to the picture below. If you click on the button it should turn off and on.

- Now that we have a grid of buttons, we'll start to implement the rules of the Game of life. You will need a thorough understanding of the members and functions of
class Lifeand what it actually does! - Next, find the 2 functions
getLife()andsetLife(boolean living)in theclass Life. Modify them by following instructions in comment. This should be obvious and simple to do if you have understood what theclass Lifeis doing! - Next, find the function
public boolean isValid(int r, int c)from themaintab and complete it using the constants so that it returnstrueif (r,c) is valid. In this case valid means it exists, it's a row column pair that is on the grid and not outside of the edges. If we have 20 rows and 20 columns, valid positions have row and column numbers from 0 to 19 inclusive. You should have already completed writing and testing the codingbat problem isValidOn5x5. You should be able to simply incorporate theisValidOn5x5code here! - Next, find the function
public int countNeighbors(int row, int col)from themaintab. Complete it so that it returns the number of neighboring life cells that are alive ortrue. While it is possible to write this function using nested loops, I find it much easier to code with 8ifstatements, one for each possible neighbor. You will first have to make sure that neighbor is valid before you check to see if it istrue, otherwise you may go out of bounds. As an example you could check if the neighbor above (r,c) was true with code likebuttons[r-1][c].getLife()==true. You should have already completed writing and testing the codingbat problem countNeighborTrues. You should be able to simply incorporate thecountNeighborTruescode here! - In the
maintab, undersetup()you'll need a "buffer", a place to store the current state of the game. Insetupuse the constants to initialize the previously defined variablebufferto be a new 2d array of typeboolean. This will be very similar to initializing 2d arraybuttons. Hint: in this case you are initializing a 2d array of typebooleaninstead of a 2d array of typeLife! - Complete the functions
copyFromBufferToButtons()andcopyFromButtonsToBuffer(). They will use the constants and nested loops to copy alltrues andfalses from a 2d array of booleans to thebuttonsgrid and vice versa. - You are just about done! Now go back to the nested loops you wrote on step 6. Inside the loops, just before you draw the button check:
- If the
buttonhas exactly 3 living neighbors, set the corresponding row and column in the buffer totrue - If the
buttonhas exactly 2 living neighbors and thebuttonis alive, set the corresponding row and column in the buffer totrue - In all other circumstances, set the corresponding row and column in the buffer to
false
- If the
- The game is more interesting when you can pause it and create new arrangements. Write in code in the
keypressedfunction so that every key press switches theboolean runningvariable on and off (i.e.trueorfalse)
If you have implemented the rules correctly, you should notice as you run your program it produces some classic stable and repeating patterns. To test your program, try pausing it and entering some of the following arrangements:

illustration from pi.math.cornell.edu
If you don't see those patterns emerge in your program, double check that isValid and countNeighbors are correct. You might want to go back and compare those functions with the codingbat problems isValidOn5x5 and countNeighborTrues. Those two functions are typically the most bug prone.
If you have extra time, you might consider adding different key presses to:
- erase the screen
- increase or decrease the number of rows and columns
- speed up or slow down the frame rate
- load particular patterns.
There are also many variations on the rules of the game of life. This website lists some interesting ones.
