Description

In this assignment you will write a program that implements Conway's Game of Life. You will be expected to apply the principles of Design by Contract to your code.

The rules for the Game of Life are simple. The universe consists of a two-dimensional matrix of cells with each cell being alive or dead. For each generation every cell determines its next phase of life as follows:

  • If the cell is alive:
    • it dies if it has 0, 1, 4 or more living neighbours (starvation), or
    • it lives if it has 2 or 3 living neighbours (balance).
  • If the cell is dead:
    • it springs to life if it has exactly 3 neighbours (procreation).

A cycle occurs as soon as the state of the universe of the latest generation is the same as a previous generation. You should be able to see that once this occurs the Game of Life will repeat the intermediate generations forever.

Implementation Requirements

All of the following conditions must be met by your solution.

  • Your program must read the initial state from standard input (redirecting files on the command line is the way to go!). Here is a sample input file to use for testing. You will be expected to adequately test your program -- I will not be providing the files used by your marker. All input will conform to the following format:
    • The input will consist of starting universes for multiple games. Play each game completely, in the order they appear in the file.
    • The first line of the game starts with an asterisk and contains the game title.
    • The second line of the game contains two numbers, separated by a space, which indicate the number of rows and columns for the 2-D matrix.
    • Then there is one line for each row in the matrix:
    • A blank character represents a dead cell.
    • An 'X' represents an alive cell.
    • The next game starts on the next line, until EOF.
    • There are no errors in the input file format.
  • For each game, your program will first print the game title as read from the file. Print the starting universe as read in. Then, after each generation is calculated your program will print the current state of the universe. All printing goes to standard output, and each universe must be preceded by a label indicating the generation number (where the initial state is generation 0). Here is a sample of the output expected. You must include a border, use '.' for dead cells and '*' for alive cells.
  • Each game will run for 250 generations or until a cycle is detected, whichever comes first. If the game ends because of a cycle, print the numbers of the two generations that were duplicated.
  • Your program must include appropriate pre and post conditions.
  • Printing all generations may be great for debugging purposes (and should prove mesmerizing) but the release version should only print the initial universe, and the last 10 (or less) generations. When your program is compiled with -DNDEBUG, the behaviour must change from printing all generations to only printing the last 10. The sample output linked above is run with -DNDEBUG.

Sample Input


10 20



X
X
XXX



Sample Output

*Case #1
Generation 0:
+--------------------+
|....................|
|....................|
|....................|
|.......*............|
|........*...........|
|......***...........|
|....................|
|....................|
|....................|
|....................|
+--------------------+
Found a cycle between generation 19 and generation 20
Generation 11:
+--------------------+
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|.........*..........|
|..........**........|
|.........**.........|
|....................|
+--------------------+
Generation 12:
+--------------------+
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|..........*.........|
|...........*........|
|.........***........|
|....................|
+--------------------+
Generation 13:
+--------------------+
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|.........*.*........|
|..........**........|
|..........*.........|
+--------------------+
Generation 14:
+--------------------+
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|...........*........|
|.........*.*........|
|..........**........|
+--------------------+
Generation 15:
+--------------------+
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|..........*.........|
|...........**.......|
|..........**........|
+--------------------+
Generation 16:
+--------------------+
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|...........*........|
|............*.......|
|..........***.......|
+--------------------+
Generation 17:
+--------------------+
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|..........*.*.......|
|...........**.......|
+--------------------+
Generation 18:
+--------------------+
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|............*.......|
|...........**.......|
+--------------------+
Generation 19:
+--------------------+
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|...........**.......|
|...........**.......|
+--------------------+
Generation 20:
+--------------------+
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|....................|
|...........**.......|
|...........**.......|
+--------------------+
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.