V22.0101 Homework Assignment 4 Fall 2001
The Game of Life
Assigned: MON NOV 12
Due: Sun NOV 25, 11:59 pm
Final Exam:TUE DEC 18, 2:00-3:50 pm.
Consider a two dimensional array with M rows and N columns. This represents the world in which some organisms live. Each of the M by N cells in this array is either occupied (if an organism lives there) or is vacant. No more than one organism can live in any one cell at any time. Each cell, except those on the boundaries (the edge of the world), has exactly eight neighboring cells (above, below, left, right, and four diagonals). The cells on the boundaries have less than eight neighboring cells.
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, prompting the user each time to see if he or she wants to see the next generation or terminate the program. Also, the program should terminate automatically if the world becomes empty, displaying a message accordingly (this will happen for life3.dat).
Use two-dimensional arrays of type char to store the old and new generations respectively. To keep things simple, assume that M=25 and N=75, i.e. the world has 25 rows and 75 columns, and define these in a final statement before declaring your array variables.
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, and you should also try your own test data. Download these files simply by choosing save 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. They will automatically be saved as DAT files. Thus your program would read for instance, file0.dat. The program should prompt the user for the name of the input file (use a String variable).
Empty cells are represented with dots and occupied cells with X's. Read the data and assign the data to a two-dimensional array using two nested for loops. The outer loop reads a string consisting of the data on a given line of the input file. The inner loop assigns the data to a given row of the array. In order to read the file, you will have to use FileStringReader (it is posted directly under the link for this assignment) which has the string indicating the file you are reading as the parameter of the constructor. Thus to read the first file you would have FileStringReader kbd = new FileStringReader( "c:/file1.dat");; and then String line = kbd.readLine() to read a string. Of course, in your program you would have a string variable representing "c:/file1.dat" whose value ypu type as input to your program . If you have saved the data files in the same directory as your program, simply type, for instance, file1.dat as your input.
You need two 2-dimensional arrays---the new generation should be created based on information from the old generation. If you use only one array, you will find that your old generation is overwritten by the new one before you have finished using all the information you need from it.
You should create a ``border'' of cells that always stay empty by declaring your arrays from 0 to M+1 and from 0 to N+1. This way, the cells on the edge will also have eight neighboring cells and won't need special treatment.
Include at least two non-void methods. One should take a world and the coordinates of a cell and return the number of neighbors (organisms in neighboring cells) that the cell has. The other should take a generation array and return a Boolean value that tells whether or not the world represented by the array is empty.
Write all generations to the screen (don't forget the generation numbers), with a prompt to the user to type a key when ready to see the next one.
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 application.
Once your program is working,
also terminate if
the world is the same as the previous one (this will happen for