Question: For this C++ assignment, you will be creating a program to run Conway's Game of Life. The program should have the following features (what is
For this C++ assignment, you will be creating a program to run Conway's Game of Life.
The program should have the following features (what is required of the program):
- It follows Conway's Game of Life Rules.
- It uses a life 'board' that is 15 cells high by 40 cells wide. (This should be changeable in the code by changing two values, or you make allow the user to pick a size.)
- It allows the user to choose a random board configuration or to enter a configuration by entering the "row column" coordinates of the initial alive cells (entering "-1-1" to stop entering coordinates).
- It allows the user to choose how many iterations to run the game of life for.
- It includes a life class that manages the life grid. The class is defined in a separate file. The life class should hide the gird data and contain both public member functions and private helper member functions (e.g. a count_neighbors() type function).
Submission: You will need to write up 2 main files, one for the main file and the file that contains the classes.
- gameoflife.cpp - this is the main C++ file. It should be quite short.
- life.h - this is the C++ file that defines the life class. It will contain the majority of the code
--------------------------------------------------------------------------------------------------------------------------------------------------------------
gameoflife.cpp code:
#include
int main() { life board; // life is the class, board is the object board.initialize(); board.print(); board.run(); }
----------------------------------------------------------------------------------------------------------------------------------------------------------------
I just need help performing the above tasks in a separate life class. I need to make a 15 x 40 cell "board" using arrays
An example of the class would be as follows:
"life.h" class code (add to this code from the instructions above):
class life { public: void initialize(); void print(); void run(); private: int grid[rows + 2][columns + 2]; void update(); int neighbors(int, int); }; int life::neighbors(int row, int col) { int count = 0; for (int dr = -1; dr <= 1; dr++) { for (int dc = -1; dc <= 1; dc++) { count += grid[row + dr][col + dc]; } } count -= grid[row][col]; return count; } void life::update() { //cout << "update "; int count; int tempgrid[rows + 2][columns + 2]; for (int r = 1; r < rows + 1; r++) { for (int c = 1; c < columns + 1; c++) { count = neighbors(r, c); tempgrid[r][c] = 0; if (count == 3) { tempgrid[r][c] = 1; } if (count == 2) { tempgrid[r][c] = grid[r][c]; } } } for (int r = 1; r < rows + 1; r++) { for (int c = 1; c < columns + 1; c++) { grid[r][c] = tempgrid[r][c]; } } } void life::run() { int steps; do { cout << "How many steps? "; cin >> steps; for (int s = 0; s < steps; s++) { update(); print(); } } while (steps != 0); } void life::print() { for (int r = 1; r < rows + 1; r++) { for (int c = 1; c < columns + 1; c++) { cout << grid[r][c]; } cout << endl; } } void life::initialize() { for (int r = 0; r < rows + 2; r++) { for (int c = 0; c < columns + 2; c++) { grid[r][c] = 0; } } // fill in non-zero values, based on user choice grid[3][3] = 1; grid[2][3] = 1; grid[4][3] = 1;
}
--------------------------------------------------------------------------------------------------------------------------
(I need this done using C++)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
