Conway's Game of Life is an early example of a cellular automaton which can be easily simulated on a computer. A cellular automaton consists of a large number of cells, each operating according to some simple local rules. Given a large enough number of cells and an interesting initial configuration, non-trivial emergent behavior can result.

See the Wikipedia article on Life (first link above) and carefully read the "Rules" section and look over some of the examples of cellular automata.

We will use the simplified rules where: a dead cell with exactly 3 neighbors becomes live; and only a live cell with 2 or 3 neighbors survives, all others die.

Note incidentally that this simulation has no relation to the board game "Life".

The program: play Life

Write a program that simulates the playing or animation of Conway's Game of Life on a 40-by-40 board, displayed as Xs (live cells) and blank spaces (dead cells). You may make the board size (40) a global variable.

Your files will consist of: lifefunc.h, lifefunc.c, and playlife.c (resp., the header, functions file, and main driver), as well as a Makefile that will generate an executable named playlife (you may use this Makefile; or edit it in case you need to add more flags, or more files).

Your program should be designed to run in two possible modes: interactive and batch.

The mode chosen will be determined by how the program is run:

1) interactive mode: this mode will be triggered when the program is executed with no command line arguments

Initially, the program should set up a completely blank board; it should then ask the user for input:

If the user enters:

  • a , followed by two integers: they will represent the coordinates where to add a new live cell (no action needed if cell is already alive).
  • r , followed by two integers: remove a cell from the cell with those coordinates (no action needed if cell is already dead).
  • n : advance the simulation to the next iteration by applying the rules of the game.
  • q : quit the program.
  • p : play the game continuously (forever, without asking for more input; you can press control-C to stop the program).

2) batch mode: this mode will be triggered if the executable is followed by a data file.

The data file will consist of a sequence of commands (see above) followed by a "p". Lines starting with an "a" or an "r" will be followed by two numbers (representing a cell's location). A typical data file (scene) will most likely consist of just "a" lines (an a followed by two integers), ending with a solo "p" line at the end.

In this batch mode, the program will play the scene continuously.

You may assume that all data files will definitely have a "p" as the last line; and that all scene files are always valid and correct.

For instance: here is an example of a simple glider scene, glider.txt, which creates cells that move across the screen diagonally.

Example usage:

playlife --> Game of Life in interactive mode
playlife glider.txt --> Game of Life in batch mode, using that scene

Note that the sides of the board are fixed sides, and the board does not 'loop around'.

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.