CS105: Introduction to Computer Programming: C++
Assignment #2, Game of Life with Pointers, Vectors, and Dynamic Allocation
Tuesday, February 16th at NOON
This is an assignment that will help you further understand arrays. You will be coding the Game of
Life again, now that you are familiar with it. But this time, you get to 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.
See assignment2 for a general description on the Game of Life and it's rules.
Uou will take input from an input file for the initial board. (unlike previous 2 assignments). Using standard in, input the filename which you can then put in an fstream so you can read the board from the file. You can use as test files both LifeZero.in from assignment2 and another one LifeZeroA3.in. The 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). Each generation, you print your board in the same manner (you
do not have to print the dimensions each time). You 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, please print to cout the initial board. Then using standard input (cin), in main ONCE, get a number of generations to print the board for each of part 1, 2, and 3 of the assignment. So, for each part, you print your initial board fully, then use that number gathered from user input to determine the # of generations to print.
Start with gameOfLifeA3.cpp. Make sure your name is at the top!
Write some code inside
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.
- 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 get an evolved gameBoard back to main from where the function is called.
- 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 filename 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 like this:
computer% g++ -Wall -Werror -o life gameOfLifeA3.cpp
using the CS department linux machines To submit your
Eventually, you'll have to use the CS department machines to submit the assignment
. Make sure to test your
code on the department machines before turning it in!
gameOfLifeA3.cpp file, use
jbsartor as the grader and
the assignment name (if you choose to take mulligans, use
OR if late:
turnin --submit jbsartor assign3 gameOfLifeA3.cpp
You can turn in your file as many times as you
want - I will only take the last one submitted.
turnin --submit jbsartor assign3Mulligan gameOfLifeA3.cpp