- C++ Basics: strings, lists, file I/O, and functions
We are going to implment Conway's well known Game of Life. You can find more than you need to know at, e.g., the Wikipedia entry. But all you really need is the rules. For convenience, I will repeat them here:
We are living in a two dimensional world. It consists of a "grid" of "cells". A cell is said to be either alive or dead. The world evolves from generation to generation. In each generation, we must determine, based in the prior generation, what is the new state of each cell. Is it alive or is it dead?
- Any live cell with fewer than two live neighbours dies, perhaps of loneliness.
- Any live cell with more than three live neighbours dies. Too crowded?
- Otherwise, a live cell will continue to live on to the next generation, i.e. if it had two or three live neighbours.
- Any dead cell with exactly three live neighbours becomes a live cell, otherwise it remains dead.
So what other details are there?
- Our program will read in a file depicting an initial configuration for the world.
- Defaults:
- The file's name will be "life.txt".
- The world will have 20 columns and 10 rows.
- We will display 10 generations after the initial input.
- The actual file is free to have more or fewer lines than the number of rows in our world. Similarly, individual lines in the file may be longer or shorter than the number of columns. This simplifies creating input files. Your program should ignore any extra data in the file (i.e. too much data on a line or too many lines) and if there is too little, then mark the unspecified cells as dead. (Note, you might prefer not to ignore this at first, providing your own input file that has just the right size.)
- The program will generate and display a "world" based on the input file.
- After displaying the initial state, the program will proceed to calculate some number of additional generations, displaying each in turn.
- Command line: from the command line we might modify the defaults. Please leave this till you have everything else working. If you don't know how to read from the command line, i.e. we haven't covered it, then don't bother with this at all. If you do, then being able to override the defaults from the command line can make debugging easier. (System arguments are mentioned in the documentation.
- If you do allow for command line arguments, please set the program up to run as follows:
./life [generations [filename [rows [columns]]]]- Note that all values are optional, so the program might be run as:
./life./life generations./life generations filename./life generations filename rows columns
- where rows, columns and generations are all numbers for the specified values to override the defaults and filename is similarly the name of a file to use instead of life.txt.
- An example input file is attached, along with the corresponding output that would appear on the console, assuming we used the defaults.
- All output should go to standard output.
It may make your life easier if your program creates an extra row at the top and bottom and an extra column to the left and the right, with all of the extra cells marked as dead. This hack can save you having to worry about edges or corners.