CS105: Introduction to Computer Programming: C++
Assignment #2: Introduction to the Game of Life
Due
Tuesday, February 9th at NOON
Overview
This is an assignment where you will dive
into using arrays. 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:
. . . . .
. * * * .
* * * . .
. . . . .
. . . . .
. . * . .
* . . * .
* . . * .
. * . . .
. . . . .
To get the initial board, you will take input from standard input
(cin) . The initial board will be specified in a file, which you
will pipe into your program, just as in assignment 1, so you can use
standard in. The file, LifeZero.in,
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. 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.
Instructions
These instructions assume that you're doing
development on the CS department linux machines.
I recommend
using the CS department linux machines, but if you feel more
comfortable using another development platform, that's fine.
Eventually, you'll have to use the CS department machines to
submit the assignment, but you can develop
on any machine you're comfortable with. Just make sure to test your
code on the department machines before turning it in!
- If you don't have a CS department account, you should request a new
account.
- Log on to a CS
department linux machine using your CS department login. Make
sure you log into a Linux machine, not a Sun machine!
- Start up XWindows by typing
startx
If you're new to linux, you might want to get a friend to help you or
come by office hours. Getting used to a new operating system can take
some time.
- Open up a terminal window and make a new directory for this
assignment called
assignment2:
mkdir assignment2
And change to that directory:
cd assignment2
Now use your favorite text editor to
edit this file I created to get you started called gameOfLife.cpp.
- First, make sure your name is in the top of the file! Write some code inside
gameOfLife.cpp (without using dynamic allocation or pointers - no "*"!) that
plays the game of life, taking input from a file such as LifeZero.in (You can create others to test with and I will use other files to grade with):
- You will define 2 printLife functions that print the
2-dimensional gameboard of the game of life. One will use an array declared to be 2D (with fixed 2nd dimension) and the other will store the 2D board in a 1 dimensional array and use that.
- You will define 2 playLife functions that play the game of life,
i.e. evolve the 2D gameboard from one generation to another. These take input arrays similarly to the 2 printLife functions above.
- You will also have to add some code to main to declare and
initialize arrays and test your game of life. Notice the constant default 2nd dimension of your 2D arrays is specified at the top of the cpp file, and thus can be used in function prototypes. Although the input file specifies the number of columns, just assume (or print an error if untrue) that the number of columns specified in your input file equals that defaultNumColumns. Also, for "part 2" of the assignment, you can initialize your board with the same contents of the board initialized in "part 1", taken from cin. Also notice that you should print the initial board once (followed by a blank line). Then you should go through 3 iterations of calling your playLife function to evolve the board, then calling your printLife function to print it post evolution. Make sure there is a blank line in between each board generation.
- Compile and run your code:
computer% g++ -Wall -Werror -o life gameOfLife.cpp
computer% ./life < LifeZero.in
- When you're happy with your code, use the
turnin program to submit your
gameOfLife.cpp file. Use
jbsartor as the grader and assign2 as
the assignment name (if you choose to take mulligans, use assign2Mulligan).
turnin --submit jbsartor assign2 gameOfLife.cpp
OR if late: turnin --submit jbsartor assign2Mulligan gameOfLife.cpp
You can turn in your file as many times as you
want - I will only take the last one submitted.