V22.0101 Homework Assignment 4 spring 2012
The Game of Life

Assigned: FRI NOV16
Due: MON, NOV 26 pm
Final Exam: December 18, 2012 10:00am - 11:50am

Consider a two dimensional array with NROW rows and NCOL columns. This represents the world in which some organisms live. Each of the NROW by NCOL cells in this array is either occupied by an organism (represented by an X) or is vacant (represented by a dot). Each cell, except those on the boundaries (the edge of the world), has exactly eight neighboring cells (above, below, left, right, and four diagonals).

Initially, there is a given population of organisms occupying certain of the cells. At each succeeding generation, the organisms reproduce and die as follows:

• Each organism of the current generation survives to the next generation (remains an X) if, and only if, it has 2 or 3 neighbors that are X's (a neighbor is an X that lives in a neighboring cell). Otherwise, it dies and its cell becomes empty (a dot) in the next generation.

• Each dot in the current generation becomes an X in the next generation if, and only if, it has exactly 3 neighbors that are X's. Otherwise, it remains a dot in the next generation.

For example, suppose the initial world (the ``zero"th generation) is as follows, using X to indicate the occupied cells and blanks for the vacant cells:

X X
XXX                XX       XXXX
X X       XX
Then the next generation is

X                           XX
X                 XX        XX
X                 XX        XX

On the input file, the blanks are represented by dots.

Write a program to play this Game of Life. Your program should read the initial world from a file (see below) and repeatedly generate new generations, as we did in Gui2.java onn NOV 15. Also, the program should terminate automatically if two succsessive worlds are identical, (use public boolean same(char[][] newGrid) for this) displaying a message accordingly (this will happen for life4.dat).

Use two-dimensional arrays of type char to store the old and new generations respectively. Set NROW=25 and NCOL=75, i.e. the world has 25 rows and 75 columns, and define these in a final static as we did in Gui2.java.

Data files for testing your program will be provided on the home page. You should make sure your program works correctly on all these files. Download these files simply by choosing save Target link as when the cursor is on the specific file on the The Data for the Game of Life page and the mouse right button is pressed. If you type .dat after the file name, it will be saved as dat file. Thus your program would read for instance, file2.dat. The way Gui2.java is setup, you select a file by double clicking its icon.

Programming Details

You need two 2-dimensional arrays---the new generation represented by newGrid should be created in generate() (see below) based on information from the old generation. The instance variable sameFlag, indicates if two consecutive worlds are the same. It is initially set to false.

Include at least three non-void methods.

• We have written one, public int neigh(int row, int col), in which the parameters are the coordinates of a cell and returns the number of nearest neighbors that the cell has.
• The second one, public boolean same(char[][] newGrid) should have as a parameter the array newGrid and return a boolean value that tells whether or not the contents of the array are the same as the previous one, represented by the instance variable grid. The variable sameFlag is set to true in isSame() if consecutive worlds are the same.
• The third one, public char[][] generate()
• declares a new grid, newGrid using new
• initializes it using init(newGrid), that's why init() now has a parameter in Gui2.java on NOV 15..
• fills in the cells of newGrid, using neigh(r,c) and
• finally returns the newGrid.

Write all generations to the screen (don't forget the generation numbers, so the heading for print is now public void print(Graphics g, int n).

You can see how the game, as described so far, works by connecting to the web page and running the program there on the Demo for different worlds link. However, your program should not be an applet; it should be a Java swing application, which Gui2.java is.

The only way to print on a panel is to use the instance g, e.g.,g.drawString(s, x, y) prints the string s at coordinates x, y. The method that prints data on a panel must be called from paintComponent() since g is required. Use the following for your paintComponent() method.

public void paintComponent(Graphics g)
{
super.paintComponent(g);//erases panel Contents
g.setColor(Color.yellow);
if(!sameFlag)//not isSame
{
print(g, n);//or whatever method you use to print the world
}
else if( sameFlag)
{
g.drawString("The worlds are repeating!", 10, 250);
}

}

You have to rewrite method begin() so that it now includes:

• A while loop beginning with while(!theSame),
• Increments n, the world number,
• gets newGrid from generate().

You call paintComponent() by executing repaint(); repaint() is incorported into the system. You do not write it!

At the risk of repeating infomation, we state the following:

1. paintComponent() does all the display.
2. You call paintComponent() using repaint().
3. setVisible(true) displays the panel.
4. You draw on a panel.
5. You must set the layout on a panel. This is done with setLayout( new GridLayout(1, 1) ).
6. A panel is a GUI and all GUIs must be placed on the ContentPane. This is done by setContentPane(panel).

Michael Overton & Samuel Marateck
FRI NOV 16 9:23:14 EST 2012