Extension 7.1: Conways Game of Life (6 points)¶
Sample Patterns¶
Block¶
Beehive¶
Loaf¶
Boat¶
Blinker¶
Toad¶
Beacon¶
Pulsar¶
Glider¶
Lightweight Spaceship¶
Gosper Glider Gun¶
Block-Laying Switch Engine¶
Directions¶
In this extension you will be responsible for building the simulator portion of Conway’s Game of Life (henceforth known as Conway, or Life). You can then run the game on your own patterns or on patterns that we provide.
The code for this work can be found conway package. The Conway class is where you will be doing all of your work. ConwayTestSuite is the tester for Conway and ConwayApp is what you will run when your code is finished to actually see your work happen. The ConwayApp class creates a GUI, Graphical User Interface, which allows you to see cells dying and coming back to life. Open Conway. You will complete the following methods:
A
public Conway(int rows, int cols)constructor that specifies the dimensions of the Conway board.A
public int getRows()method, that is an accessor.A
public int getColumns()method, that is an accessorA
public void setAlive(boolean isAlive, int row, int col)method that takes in a row and a column, and whether that cell should be currently alive or deadYou must come up with a data type that stores values in rows an columns to represent all of the cells. There are multiple ways to store this information, but think carefully about which one you choose. Some choices will make remaining work easier than others. (Hint: This method’s signature may suggest an appropriate representation)
An
public boolean isAlive(int row, int col)method, which returns whether the cell at that specific row and column is alive or dead. If the row and column are out of the bounds of that Conway object, then returnfalse.Run
ConwayTestSuite. Your code should now pass theAliveTestSuiteportion of theConwayTestSuite.A
public void clear()method, which sets every cell in the Conway object to dead.Run
ConwayTestSuite. Your code should now pass theClearTestSuiteportion of theConwayTestSuite.A
public int countLivingNeighbors(int row, int col)method, which considers the cell at a certain row and column, and returns the number of living neighbors that it has.The neighbors of a certain cell are considered to be the eight cells that are surrounding it. Your
isAlive()should help you with this.Run
ConwayTestSuite. Your code should now pass theCountTestSuiteportion of theConwayTestSuite.A
public void step()method, which executes a generation of life (one step of the simulation of life). Take all of the current cells and determine whether or not they will be alive in the next generation using the rules given above.You need to be careful how you update. The updates should not impact the outcome of another update. For example, when you figure out if the cell in row 0 and column 0 will be alive, this new value should not impact the computation for the surrounding rows. They should be based on the original value of row 0 column 0 before it’s next value was computed. (Hint: It may be helpful to use an additional array or an entirely new
Conwayobject)For instance, say cell A and cell B both alive and are neighbors. If you determine that A will be dead in the next generation, and you kill it, when you go to count the number of living neighbors of B, it will have fewer living neighbors now than it should.
Run
ConwayTestSuite. Your code should now pass theStepTestSuiteportion of theConwayTestSuite.- Once you have completed all the methods, you can run
ConwayDebugApp.javato run a simulation. You’ll want to select an item to simulate from the drop down menu on the right of the window.
Then click on
Startto run a simulation. (Notice that each item in the menu corresponds to methods that were done for you already or that you haven’t finished, likeblinker()andyourDesignOne(). You can explore this more in another Extension)
- Once you have completed all the methods, you can run
Note: you can click on the cells to change the state of the board in the ConwayDebugApp to create interesting patterns.
Hint: If you need to debug your code the visual interface allows you to take one step at a time. If the game is not working, use the debugger or print information helpful to diagnosing the problems you see.
You will be working with “code generation” that will create code that could be pasted into the
yourDesignOne()andyourDesignTwo()methods inPatterns.java. This approach allows you to stop a particularly interesting simulation and “save” the values at the instant you stopped the simulation. You can then use this “saved” data to restore the simulation and run it from that point.
For example, the Toad code is captured already:
but if you were to generate code for it using toJavaCode() the result would look something like this:
conway.clear();
conway.setAlive(true, 2, 2);
conway.setAlive(true, 2, 3);
conway.setAlive(true, 2, 4);
conway.setAlive(true, 3, 1);
conway.setAlive(true, 3, 2);
conway.setAlive(true, 3, 3);
When you run ConwayDebugApp.java and click on the To Java Code button the JavaCodeUtils.toJavaCode(conway) method will be called. You will need to implement this method. You should return a String with enough data to represent the method calls needed to re-build the current board.
Note: the contents of the String returned from toJavaCode(conway) will both be printed to the console and placed on the system clipboard for easy pasting into myDesignOne() or myDesignTwo().
The code text you return can then be pasted into either myDesignOne() or myDesignTwo() in Patterns.java. When you re-run ConwayDebugApp.java and select the corresponding item from the drop-down menu (for example: Your Design One) it should reproduce the board as it looked when generated and pasted the code previously.
Once you have toJavaCode() working, use this new tool to capture your own Conway patterns in myDesignOne() and myDesignTwo(). For credit for this extension, these patterns should be both intriguing and potentially time-consuming to generate by hand. When done, toJavaCode(conway) should work and both myDesignOne() and myDesignTwo() should create interesting patterns.
Testing¶
ConwayTestSuite
To Demo¶
Your code must pass all of the unit tests, and the GUI should work, and be able to display cells interacting with each other.