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:
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 XXThen 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.
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.
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
public void paintComponent(Graphics g)
super.paintComponent(g);//erases panel Contents
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:
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: