Project 2: Spatial Simulation: Game of Life Assigned: Sun Sep 15 2013 Due: 11:59:59 PM on Mon Sep 23 2013 Team Size: 1 Language: Java This week begins a multi-week project during which we'll explore the simulation of entities on a 2D grid. The entities will interact with the grid and with each other, simulating various social phenomena.
The inspiration for this series of projects comes from both research on cullular automata, such as Conway's Game of Life, and work at the Brookings Institute by Epstein and Axtell on modeling human social dynamics. There are a number of newer publications and white papers on the Brookings Institute web site with more recent results, including significant models of the spread of disease.
Resources:
Setup: - Create a new project folder for this week's project.
Assignment: This week you'll first develop three classes: a Cell, a Landscape, and a LifeSimulation. The Cell will represent a location on the Landscape. The Landscape will represent a 2D grid of Cells, and a LifeSimulation will control the rules and actions of the simulation. The end result will be a text-based simulaton of Conway's Game of Life.
For each class, be sure to create a unit test (main method) that tests all of it's capabilities.
Also, for each class and (public) method, be sure to include appropriate JavaDoc. Tasks: Cell - a Cell object represents one location on a regular grid. The Cell class should store its own location in the grid, store whether it is alive or not, and implement the following methods: - public Cell(int row, int column, boolean alive) - constructor method.
- public int getRow() - returns the row value of the Cell's position.
- public int getColumn()
- public boolean isAlive() - returns whether the cell is alive.
- public String toString() - returns a string that indicates the alive state of the Cell as a one-character string. For example, you could use a "0" for alive and " " for dead. This will override the default toString method in the Object class.
Landscape - create a java class called Landscape, which will hold a 2D grid of Cell object references. The Landscape class should have a field to hold the array of Cell object references and implement the following methods: - public Landscape(int rows, int columns) - sets the number of rows and columns to the specified values and allocates the grid of Cell references. Then it should allocate a Cell for each location in the Grid.
- public int getRows() - returns the number of rows in the Landscape.
- public int getColumns()
- public Cell getCell(int row, int column) - returns a reference to the Cell located at position (row, column).
- public void reset() - resets all the cells to be dead.
- public String toString() - converts the Landscape into a text-based string representation. At the end of each row, put a carriage return ("\n").
- public ArrayList getNeighbors(int row, int column) - returns a list of references to the neighbors of the Cell at location (row, column). Pay attention to the boundaries of the Landscape when writing this function.
LifeSimulation - create a Java class called LifeSimulation, which will hold a Landscape and drives a simulation of the Game of Life. You may also want to keep track of the time state (number of iterations) the simulation has executed. The LifeSimulation should implement the following methods: - public LifeSimulation(int rows, int columns) - initialize the Landscape to the given size. Use the Landscape reset method to set the initial values.
- public void initializeRandom(double density) - repopulate the Landscape with cells that are alive with the probability given by density, where density is in the range [0.0, 1.0].
- public String toString() - converts the LifeSimulation to a text-based string representation. This should at least return the result of calling toString on the Landscape, but you can add more to make it more useful.
Test your code throroughly and make sure it is working before continuing! (You should be doing that at each step, but the next steps will get the simulation running.) Add a method to the Cell class: public Cell nextState(Landscape scape) . This method should look at the Cell's neighbors on the provided Landscape and return a new Cell that represents the next state. The default rule should be that if a live Cell has either two or three live neighbors, then it will be alive in the next round. If a dead Cell has exactly three neighbors, it will be alive next time. Otherwise, the Cell will be dead. Add a method to the Landscape class: public void advance() . The advance method should move all Cells forward one generation. Note that the rules for the Game of Life require all Cells to be updated simultaneously. Therefore, you cannot update the Cells in place one at a time (why not?).
Instead, create a temporary Landscape of the same size. Then duplicate the alive status of the original Landscape with the temporary Landscape (you need to loop over the original and set the temporary Cell alive values). One design option is to make a new constructor that takes a Landscape as its argument and creates a true copy.
Then go through each Cell in the temporary Landscape and call updateState, passing the original Landscape to the Cell to evaluate. When the code has updated all of the Cells, you need to transfer the information back. You can just assign the grid of the temporary Landscape to the grid of the original one. Add a method to the LifeSimulation class: simulate() . Since this can throw an exception, we need to add some extra JavaDoc tags:
/**
* Runs the simulation.
*
* @param rounds The number of rounds to simulate.
* @throws InterruptedException if any thread interrupts the thread calling this.
*/
public void simulate(int n) throws InterruptedException {
The simulate method should run the simulation n times, printing out the current Landscape at each step. You can use Thread.sleep() to slow down the simulation. Thread.sleep takes milliseconds as its argument. If you call simulate from any other function (e.g. main) then you will also have to add the 'throws InterruptedException' to that function's header as well. Finally, update your LifeSimulation main method to create a LifeSimulation, initialize it, and then run the simulation for some number of rounds (perhaps using an argument from the command line). Start with small Landscapes for testing.
If you want to test your program using a known pattern, go to the Wikipedia page and look up an example. Then create a new initialization method that sets the Landscape to the specified pattern.
Extensions: Each assignment will have a set of suggested extensions. The required tasks constitute about 85% of the assignment, and if you do only the required tasks and do them well you will earn a B+. To earn a higher grade, you need to undertake at least one extension. The difficulty and quality of the extension or extensions will determine your final grade for the assignment. One significant extension, or 2-3 smaller ones, done well, is typical. - Modify the main method in LifeSimulation to make use of command line parameters. For example, it would be nice to control the size of the Landscape and the number of iterations used in the simulation.
- Try modifying the Cell class to implement different rules for determining the next state. Think about how to do this using good design rather than having many copies of the Cell class or commenting/un-commenting blocks of code.
- Create more than one type of Cell and give each type different rules.
- Be creative with the visual representation.
Hand-In: Make your writeup for the project a wiki page in your personal space. If you have questions about making a wiki page, stop by my office or ask in lab.
Your writeup should have a simple format: - A brief description of the overall task, in your own words.
- An explanation of your solution, focusing on the interesting bits. The interesting bits here are things like the toString functions, initializing the Landscape, and creating the simulation rules.
Printouts, pictures, or results to show what you did. You can do screen captures of your terminal to show the initial and final landscapes. You can also dump your terminal output to a file.
$ java LifeSimulation > output.txt
- Other results to demonstrate extensions you undertook. If you tried different update rules, for example, show how those affected the overall simulation results.
- A brief conclusion and description of what you learned.
Once you have written up your assignment, give the page the label: cs231f13project2 . You can give any wiki page a label using the label field at the bottom of the page. (The label is different from the title.)
Do not put code on your writeup page or anywhere it can be publicly accessed. To hand in code, put it on the Courses fileserver. Create a directory for each project inside the private folder inside your username folder.
|