Mid I: Multi-threaded Game of Life

Instructions

Game Of Life

The following description is taken from Wikipedia. The universe of the Game of Life is an infinite two-dimensional orthogonal 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:

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.

In principle, the Life field is infinite, but computers have finite memory. This leads to problems when the active area encroaches on the border of the array. Programmers have used several strategies to address these problems. The simplest strategy is simply to assume that every cell outside the array is dead. This is the strategy we will used in this exam.

Here is a running example:

Game of Life Animation

Input/Output

You are to write a Java program GameOfLife that is executed like this:

java GameOfLife N G F

N defines the size of NxN game area. G is the number of generations to simulate. F is the name of a file containing the initial seed configuration. The file should contain N lines with N characters each. Valid characters are a dot “.” (representing dead cells) and character “x” (representing alive cells). Don’t forget the rules of defensive programming.

The only output of the program is the game configuration after all G generations have been calculated in the same format as seed configuration in the given file.

Here are some sample input data sets to try. This initial population dies out.

.....
.x...
..x..
...x.
.....

This initial population dies more slowly.

.....
.x...
.x...
..x..
.....

This initial population reaches a stable state.

.....
.xx..
.x...
.....
.....

This initial population oscillates.

.....
..x..
..x..
..x..
.....

Version Control

Your work should be in a new local git repository (no git push) from the start with commits when you reach a working milestone. There should be a serial branch and a master branch where the serial branch has code that solves the problem without multi-threading and the master branch uses threads as described below. When and where to branch the code is your choice. If both branches are working correctly, then merge both and add an extra command line argument to choose between the serial version and the parallel version.

Multi-threading

Each cell should be updated by a separate thread. The cell value is shared between the threads corresponding to this cell and neighbors’ cells. There are two issues to handle:

The above two should be the only two dependencies. In particular, you should not have a global dependency that generation G+1 cannot be calculated until all cells have been updated with generation G values. Consider, for example, a cell in a corner. It can be at generation G+2, its three neighbors at generation G+1 with calculated but not updated values for generation G+2 and their five other neighbors at generation G with calculated but not updated values for generation G+1.

Design and code quality

Multithreading, correct synchronization, git usage, top-down design, and coding quality (as discussed in Code Complete 2) are the prime focus. Performance is not a focus of this assignment except that you should not overly synchronize as discussed above. I recommend that you give a thought to your overall design for 20-30 minutes with a pen and paper before actually starting to code.