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." 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
public
orprivate
- 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
If you are running Processing on your PC or Mac, you will need to install the Guido GUI library. (You can skip this step if you are working online on repl.it) To install it, start Processing and choose Sketch | Import Library | Add Library. Type Guido in the search box, the click on Guido by Florian Jenett and then click Install.
- Fork and clone down this repository (or if you are working online, fork this repl.it).
- You may find slides 46-73 of the 2d Arrays slide presentation helpful in completing the following steps.
- Under the comment
//Declare and initialize constants NUM_ROWS and NUM_COLS = 20
create two integer constantsNUM_ROWS
andNUM_COLS
and 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 here
use the constantsNUM_ROWS
andNUM_COLS
to initialize the 2d arraybuttons
to have 20 rows and 20 columns - Just under that, use nested loops to create a
new Life
for each row column pair. Be sure to use the constants in your loop. - Uncomment the first two lines in the Life constructor
public Life (int row, int col)
- In
draw
under the comment//use nested loops to draw the buttons here
write 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. Find the function
public boolean isValid(int r, int c)
and complete it using the constants so that it returnstrue
if (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 may find first completing the codingbat problems isValidOn5x5 and isValidOnNxN to be helpful. - Next, find the function
public int countNeighbors(int row, int col)
. Complete it so that it returns the number of neighboring life cells that are alive ortrue
. You may find first completing the codingbat problem countNeighborTrues to be helpful. While it is possible to write this function using nested loops, I find it much easier to code with 8if
statements, 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
. - We'll need a "buffer", a place to store the current state of the game. In
setup
use the constants to initialize the variablebuffer
to be a new 2d array of typeboolean
- Write the functions
copyFromBufferToButtons()
andcopyFromButtonsToBuffer()
. They will use the constants and nested loops to copy alltrue
s andfalse
s from a 2d array of booleans to thebuttons
grid 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
button
has exactly 3 living neighbors, set the corresponding row and column in the buffer totrue
- If the
button
has exactly 2 living neighbors and thebutton
is 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
keypressed
function so that every key press switches theboolean running
variable on and off (i.e.true
orfalse
)
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, isValidOnNxN and countNeighborTrues. isValid
and countNeighbors
are typically the most bug prone functions in this assignment.
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.
none yet!