CS 241 Data Structures- Lab 2, Due 2/2/2015 (demo by the end of lab)


To familiarize you with (or refresh your memory of), object-oriented design, 2-dimensional arrays, and the process of incremental implementation, you will implement Conway's Life Game on a torus.

Your GUI should have a TextArea (to display the current configuration), and a button (that goes to the next time step).

How to Proceed:

There are so many things which might go wrong that the process of programming can be difficult; but there are techniques that render it manageable. One of the most general and powerful is to employ the metaphor of "growing" your program instead of "building" it. The idea is to always have a working program, even though it doesn't do everything you want it to, yet. Then, repeatedly enhance (grow) the current version to gradually add functionality. Here's one sequence of prototypes (although step 3 could be done first, it's a matter of taste):
  1. An empty Frame with the title "Conway's Life"
  2. Add a TextArea, Button, and TextField (where the Button and TextField simply report that they would perform an action if the code were written).
  3. Create and test a Cell class
    1. Your Cell class should have two variables
      1. one that keeps track of the state (i.e. alive or dead)
      2. one that records the number of live neighbors (to decide next step alive or deadness)
    2. ...and at least the following methods
      1. a default constructor (that initializes the state to dead)
      2. a constructor that takes a parameter to set the state initially
      3. accessors for both variables
      4. public String toString() - which returns a String describing the complete state of the Cell
      5. char display() - returns a character: '*' for alive, '-' for dead
      6. update() - uses the values of the two instance variables to determine and set the new state
    3. To test your Cell class, create a live Cell, and a dead Cell, then display them in the TextArea (using both toString() and display()). Then write a bit of code to test the update method.
  4. After your Cell class is tested (and not until!)
    1. Create a Board class with a 10x10, 2-dimensional array of Cells (start with 10x10, but write your code so you can change the size effortlessly).
    2. Initialize all the Cells to dead, except say three in a row right in the middle, and display the Board to the TextArea (write a display method!).
    3. Set the font of the TextArea to monospaced (so things line up!) -- (in the Form Editor, right-click the TextArea, Properties/Font)
  5. Make the Button update the states of all Cells and redisplay them in the TextArea (as a grid).
  6. Add a Button to display the neighbor counts of each cell in a similar grid (this will simplify debugging!).
  7. etc...


Convert the grid square to a torus, by (logically) attaching the edges (as explained in class). If the grid is a 10x10 array, the neighbors of 0,0 are:

    9,9  9,0  9,1 
    0,9       0,1 
    1,9  1,0  1,1

To simulate simultaneous update of all the cells, first count the live neighbors for every Cell, then update every Cell. If instead, for each Cell, you count live neighbors and update immediately, bizarre effects will occur.

Hint: The update for Board (or Grid, or whatever you call it) might look like this (to get simultaneity):

update() { 
    for each cell { 
         count and store live nbrs    // thisCell.setLiveNbrCnt(countEm(i,j)); 

    for each cell { 

Minimal requirements:

  1. Base your code on a Cell class which includes the transition function (i.e. the update from one time step to another).
  2. Include a Button that steps your simulation (i.e. advances one time step for each button press).

And then...

Make the board variable sized (under the user's control)

Extra Credit:

  1. Invent an initial configuration of 5 or 6 live Cells that runs for many time steps without repeating
  2. Create a graphical interface.
  3. Read the initial state of all the Cells from a file.
  4. Allow the user to input the initial state with the mouse.