Game of Life

Instructions:

You are to write an object-oriented program that simulates a game called Game of Life. The game simulation is a top view of a 2-D grid of cell objects: live cells printed as 'O' and dead cells printed as '-'. To mimic the cell objects in OOP, you will define two classes: Square and Cell. Class Square (considered as a general living thing) is the parent class of Class Cell. The rules of the game are given on the wiki page :

The Game of Life is an infinite two-dimensional grid of square cells, each of which is in one of two possible states, alive or dead. Every cell interacts with its eight neighbours, which are the cells that are horizontally, vertically, or diagonally adjacent. At each step in time, the following transitions occur:

  1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  2. Any live cell with two or three live neighbours lives on to the next generation.
  3. Any live cell with more than three live neighbours dies, as if by overcrowding.
  4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

The initial pattern constitutes the seed of the system. The first generation is created by applying the above rules simultaneously to every cell in the seed—births and deaths occur simultaneously, and the discrete moment at which this happens is sometimes called a tick (in other words, each generation is a pure function of the preceding one). The rules continue to be applied repeatedly to create further generations.

Once your program works correctly, you should be able to observe some common patterns of cells as given in the wikipedia reference page.

There are many ways to simulate the game. But, as an object-oriented programming assignment, you are required to use the classes provided in the shared drive to simulate the game: \\copernicus\Public\csc2340\GameOfLife

First, you need to study three C++ files: A client program containing the main() function (main.cpp), a header file with class declarations (grid.h), and a class implementation file (grid.cpp). The files main.cpp and grid.h have been completed. Use these files as reference and implement the Game of Life Java.

To generate initial states of the cells on a grid, you may use the web page here . Below are some sample input and outputs:

Below are some sample Java skeleton programs:

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;

public class GameOfLife
{	
	public static void main (String [] args)
	{
		int [][]initStates = null;	//Initial cell states

		String inputFileName = args[0];		//The file name containing initial states

		initStates = getInitialStates(inputFileName);
		
		Grid gd = new Grid(initStates);
		
		gd.display();		//Display cells
		gd.getStats();		//Get stats of cells on a grid
		gd.run();			//Start running the simulation
	}

	//The first parameter "fileName" is the input file name containing initial states of cells. 
	//The first line in the file contains rows and columns of a grid. The remaining lines contain the states represented by 
	//1s meaning live and 0s dead.
	//This method takes an input file name and returns the initial states of cells.
	public static int [][] getInitialStates(String fileName)
	{
		File file = new File(fileName);
		
		//Create a 2D array for the initial states of cells
		int [][] states = null;
			
		try {
			Scanner input = new Scanner(file);
	
			int rows = input.nextInt();
			int cols = input.nextInt();

			states = new  int  [rows][];
			for( int i=0; i < rows; i++)
			{
				states[i] = new int[cols];
			}

			//Read initial states from the input file
			for ( int i = 0; i < rows; i++ )
				for ( int j = 0; j < cols; j++ )
				{
					states[i][j] = input.nextInt();
				}	
		} 
		catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		return states;	
	}
}

abstract class Square
{
	private Grid _grid;			//The grid where this square lives on 
	private Coordinate _coord;	//Coordinates of this square 

	public Square( Grid aGrid, Coordinate aCoord)
	{
	}

	public Grid getGrid()
	{
	}
	
	public Coordinate getCoordinate()
	{
	}
	
	//abstract methods for dynamic binding
        abstract boolean getLiveInfo();
	abstract char getImage();
	abstract void setImage(char img);
	abstract void print();
	abstract void update();
}

class Grid
{
	final int NUM_RUNS = 15	;			//The number of runs (generation) to run the simulation
	final int NUM_DISPLAY = 1 ;	                //How frequently to print the grid with cells
	final char LIVE_IMAGE = 'O';			//Image for live cells
	final char DEAD_IMAGE = '-';			//Image for dead cells
	
	private int rows;			  //The number of rows of this grid
	private int cols;			  //The number of columns of this grid
	private Square [][]_squares;  //A 2D array of pointers to squares (parent class of cells)
}

class Cell extends Square
{
	final char LIVE_IMAGE = 'O';			//Image for live cells
	final char DEAD_IMAGE = '-';			//Image for dead cells

	private char _image;	//For example: cell: O, square: -
	private int _lifeSpan;  //The number of generations this cell has lived
	private boolean _toBeLive; //True if this cell will be live next generation
}

A sample input pattern for initial states:
20 20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Sample Output for a grid of 20x20 with a pattern of live cells initially:
Initial states of cells on the grid (20x20)
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - O O O -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - O O O - - - - - - - - -
- - - - - - - - O - O - - - - - - - - -
- - - - - - - - O O O - - - - - - - - -
- - - - - - - - O O O - - - - - - - - -
- - - - - - - - O O O - - - - - - - - -
- - - - - - - - O O O - - - - - - - - -
- - - - - - - - O - O - - - - - - - - -
- - - - - - - - O O O - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- O O - - - - - - - - - - - - - - - - -
- O O - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
The number of live cells = 29
The number of dead cells = 371
Total cells = 400

Runs = 1
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - O - -
- - - - - - - - - - - - - - - - - O - -
- - - - - - - - - - - - - - - - - O - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - O - - - - - - - - - -
- - - - - - - - O - O - - - - - - - - -
- - - - - - - O - - - O - - - - - - - -
- - - - - - - O - - - O - - - - - - - -
- - - - - - - O - - - O - - - - - - - -
- - - - - - - O - - - O - - - - - - - -
- - - - - - - O - - - O - - - - - - - -
- - - - - - - O - - - O - - - - - - - -
- - - - - - - - O - O - - - - - - - - -
- - - - - - - - - O - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- O O - - - - - - - - - - - - - - - - -
- O O - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
The number of live cells = 25
The number of dead cells = 375
Total cells = 400


Runs = 2
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - O O O -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - O - - - - - - - - - -
- - - - - - - - O O O - - - - - - - - -
- - - - - - - O O - O O - - - - - - - -
- - - - - - O O O - O O O - - - - - - -
- - - - - - O O O - O O O - - - - - - -
- - - - - - O O O - O O O - - - - - - -
- - - - - - O O O - O O O - - - - - - -
- - - - - - - O O - O O - - - - - - - -
- - - - - - - - O O O - - - - - - - - -
- - - - - - - - - O - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- O O - - - - - - - - - - - - - - - - -
- O O - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
The number of live cells = 47
The number of dead cells = 353
Total cells = 400


Runs = 3
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - O - -
- - - - - - - - - - - - - - - - - O - -
- - - - - - - - - - - - - - - - - O - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - O O O - - - - - - - - -
- - - - - - - O - - - O - - - - - - - -
- - - - - - O - - - - - O - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - O - - - - - - - O - - - - - -
- - - - - O - - - - - - - O - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - O - - - - - O - - - - - - -
- - - - - - - O - - - O - - - - - - - -
- - - - - - - - O O O - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- O O - - - - - - - - - - - - - - - - -
- O O - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
The number of live cells = 25
The number of dead cells = 375
Total cells = 400
- - - - - - - - O O O - - - - - - - - -
- - - - - - - - O O O - - - - - - - - -
- - - - - - - - - O - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - O - - - - - - - - - -
- - - - - - - - O O O - - - - - - - - -
- - - - - - - - O O O - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - O - O - - - - - - - - -
- O O - - - - - - O - - - - - - - - - -
- O O - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
The number of live cells = 27
The number of dead cells = 373
Total cells = 400


Runs = 13
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - O - -
- - - - - - - - - O - - - - - - - O - -
- - - - - - - - - O - - - - - - - O - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - O - - - - - - - - - -
- - - - - - - - O - O - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - O O O - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - O O O - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - O - O - - - - - - - - -
- - - - - - - - - O - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - O - - - - - - - - - -
- O O - - - - - - O - - - - - - - - - -
- O O - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
The number of live cells = 23
The number of dead cells = 377
Total cells = 400


Runs = 14
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - O O O -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - O - - - - - - - - - -
- - - - - - - - - O - - - - - - - - - -
- - - - - - - - O - O - - - - - - - - -
- - - - - - - - - O - - - - - - - - - -
- - - - - - - - - O - - - - - - - - - -
- - - - - - - - - O - - - - - - - - - -
- - - - - - - - - O - - - - - - - - - -
- - - - - - - - O - O - - - - - - - - -
- - - - - - - - - O - - - - - - - - - -
- - - - - - - - - O - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- O O - - - - - - - - - - - - - - - - -
- O O - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
The number of live cells = 19
The number of dead cells = 381
Total cells = 400


Runs = 15
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - O - -
- - - - - - - - - - - - - - - - - O - -
- - - - - - - - - - - - - - - - - O - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - O O O - - - - - - - - -
- - - - - - - - O - O - - - - - - - - -
- - - - - - - - O O O - - - - - - - - -
- - - - - - - - O O O - - - - - - - - -
- - - - - - - - O O O - - - - - - - - -
- - - - - - - - O O O - - - - - - - - -
- - - - - - - - O - O - - - - - - - - -
- - - - - - - - O O O - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
- O O - - - - - - - - - - - - - - - - -
- O O - - - - - - - - - - - - - - - - -
- - - - - - - - - - - - - - - - - - - -
The number of live cells = 29
The number of dead cells = 371
Total cells = 400

Press any key to continue . . .

Submissions

Zip all your program files into a single zip file and submit to the Bb by the deadline.