CS2- Lab 2b, Due 1/24 & 1/29/2013

Introduction

This lab assumes you have a working version of the Life lab to modify. If yours is not working by the end of lab today, please start with the one from class. It has been/will be compressed into xxx.zip, and is on the examplesFromClass directory. You should copy it to your own directory structure and unzip it (in OS-X double-click to expand where it is).

New features for the life simulation (1-5 for Thursday, the rest for next Tues)

  1. Add a TextField; when the user types a number in it and hits Enter, run for that many steps so they can watch life evolve
  2. Add a Reset button
  3. Add a button to run your simulation until either:
    1. the state of every Cell is identical with its previous state (i.e. the board has not changed), or,
    2. the current board is identical with what it was 2 time steps ago (i.e. it is oscillating).
  4. Make the board variable sized (under the user's control)
  5. Implement graphical output.
    1. Display the state of the life board by coloring squares of the Frame different colors for live and dead
    2. Make the speed variable, under the user's control
  6. Read the initial state of all the Cells from a file (See Chapter 10, in this PDF for how to use MyReader and StringTokenizer).
  7. Initialize each Cell to live with some probability; then run until the Board is quiescent or alternating (as above)
  8. Write an experiment loop to try 10 different initial densities and record/report the number of steps it runs with each; find a range where very different things happen (i.e. setting the probabilities to 0.4, 0.41, 0.42..., and 0.49 is not likely to generate very different results, whereas a range near 0 is!).
  9. Run 10 times on each density, average and report how long it runs on each.

How to Proceed:

Here's one way to proceed incrementally; whatever you do, don't try to write it all at once!
  1. Multiple steps
    1. Animation in Java must be implemented in a separate Thread
    2. Add an infinite loop at the end of the Frame constructor, which just delays unless the user has asked for some number of steps
    3. Making your program pause:
      final int DELAY_IN_MSEC = 100; // All caps means CONSTANT -- final enforces it!
      ...
      try {
          Thread.sleep(DELAY_IN_MSEC);
      } catch (Exception e) {}
                          
  2. The Reset button should reinitialize the Board and display
  3. Continuous run: complexify the expression controlling the infinite loop (Arrays.equals(array1, array2) does not work!
  4. Use a Slider to control the size of the board; use sizeSlider.getValue() in reset
  5. Implement graphical output.
    1. In Java, to control how a Component draws itself, override public void paint(Graphics g)
    2. See page 2 onward for basics of Graphics
    3. First simply fill one square in red, like
      
          public void paint(Graphics g) {
      	g.setColor(Color.red);
      	g.fillRect(100,100,40,40);
          }
      
    4. Define variables for the upper left corner of the display area, and its width, then modify paint to fill the entire thing with red and then draw a border around it in black (g.drawLine(x1, y1, x2, y2) is the method to draw a line).
    5. Define a variable for the width of an individual Cell and add code to draw the upper left Cell in black (on top of the red so you can see it easily)
    6. Write paint(Graphics) for the Board class, if you fill the display area with, say, white, in the Frame, you will only need to color in the live cells

Extra Credit:

  1. Allow the user to alter the current state with the mouse.
  2. Allow the board to be unlimitedly large (or at least 1,000,000 x 1,000,000).