Introduction to Computer Programming: C++
Assignment #1, Game of Life with Pointers, Vectors, and Dynamic Allocation
Due
Thursday, 9 October at noon
Overview
This is an assignment where you will gain experience using arrays,
pointers, and dynamic allocation. You'll have to write several functions to
play the Game of
Life and compile and run your code.
The Game of Life was developed by John Conway in 1970 to simulate
cellular automaton. The game board is a 2-dimensional grid. The game
has an initial state specified, then proceeds by evolving the grid
through "generations". Each cell in the grid can be either "live" or
"dead". Given an initial state of the board, the next generation of
the board is determined by the status of each cells' neighbors which
include cells directly horizontally, vertically, and diagonally
adjacent. An internal cell has 8 total neighbors. The evolution
of the board is created by applying the following rules to all cells
simultaneously:
- A live cell with fewer than 2 live neighbors dies.
- A live cell with more than 3 live neighbors dies.
- A live cell with 2 or 3 live neighbors lives on to the next
generation.
- A dead cell with exactly 3 neighbors becomes a live cell.
Here is an example initial 5 x 5 board, and the 1st generation where
"*" is live and "." is dead:
. . . . .
. * * * .
* * * . .
. . . . .
. . . . .
. . * . .
* . . * .
* . . * .
. * . . .
. . . . .
In this assignment, you will familiarize yourself with the connection between pointers and arrays, how vectors make things easier, and how to use arrays with dynamic allocation.
Make sure your code can compile and run before you turn it in.
Instructions
To get the initial board, you will take input from a file.
The initial board will be specified in a file, and you ask the user
for the name of the file using cin. You can test with two files: LifeZero.in and LifeZero2.in. You read input from the file. Each file
will first have the number of rows, then the number of columns
specified. These will be followed by a number of lines equal to the
number of rows specified. Each row will be filled with a number of
characters (separated by spaces) equal to the number of columns
specified. The characters will be either '*' or '.', meaning alive or
dead, respectfully. You can create your board initially using this
state (for each of the 3 parts of the assignment). At
the beginning of your program, you also use standard input (cin),
in main ONCE, to get
a number of generations, or number of times you will evolve the board for each of part 1, 2,
and 3 of the assignment. For each of the three
parts of the assignment, you first print the initial board state, then
print each evolution of the board for the given number of
user-specified generations. What I mean is that you should print all X
versions/evolutions of the board for part 1, then start over with the same
initial state and print all X versions of the board for part 2, and
then start over again for part 3. Printing of the board should go as follows: print each row to
standard out (cout), with characters separated by a space, and print a newline at the end of each row to start the next
row. At the end of each whole board printed (each generation), print a
blank line to clearly separate generations from each other.
For each of the 3 parts to this assignment, announce that you are now
in part X using standard out, and then print the initial board and
then the requested number of generations.
Start with gameOfLife.cpp. Make sure your name is at the top!
Write some code inside gameOfLife
that
plays the game of life:
- You will define 3 printLife functions that print the
2-dimensional gameboard of the game of life. For the first you can
use a 1D array, but you pass the array via pointers, and in the
print function you should use pointer arithmetic to go through your
array. For the second, you use vectors and use an iterator for the
print function. For the third, use dynamic allocation to create a 2d
array that is a pointer to a list of pointers, and print that 2d
array in the print function.
- You will define 3 playLife functions that play the game of life,
i.e. evolve the 2D gameboard from one generation to another. Notice
the different function prototypes - they will give you clues as to
how to call the play function and return an evolved game board back to main.
- You will also have to add some code to main to declare and
initialize arrays and test your game of life. You will get the name
of the input file from standard in, as well as the number of generations to print for each part. From the file, you will get the initial board state including dimensions. Then you have to evolve and print your board for each of the 3 parts of this assignment as specified above.
Compile and run your code in Visual Studio. Use Build Solution
and Start With Debugging or Start Without Debugging.
When you're happy with your code, submit your
gameOfLife_[yourLastName].cpp
file to the DropBox in Minerva.
You can turn in your file as many times as you
want - I will only take the last one submitted.
Submission Checklist
gameOfLife_[yourLastName].cpp