Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

For this C++ assignment, you will be creating a program to run Conway's Game of Life. The program should have the following features: It follows

For this C++ assignment, you will be creating a program to run Conway's Game of Life.

The program should have the following features:

  • 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.

  1. gameoflife.cpp - this is the main C++ file. Following the approach in the text and in class, it should be quite short.
  2. life.h - this is the C++ file that defines the life class. It will contain the majority of the code.

Sample Code:

gameoflife.cpp: (Main file)

#include using namespace std; #include"life.h" int main(){ life board; // life is the class, board is the object board.initialize(); board.print(); // board.run(); }

-----------------------------------------------------------------------------------------------------------

life.h: (File to define class)

const int rows = 15; const int columns = 40; 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; }

------------------------------------------------------------------------------------------------

The above code is what we've been working on in class combined with my own personal work on the assignment. I'm just having trouble adding some of the assigned functions for the code. If you could help me out that would be much appreciated. Thanks!

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Flash XML Applications Use AS2 And AS3 To Create Photo Galleries Menus And Databases

Authors: Joachim Schnier

1st Edition

0240809173, 978-0240809175

More Books

Students also viewed these Databases questions

Question

1. What are the peculiarities of viruses ?

Answered: 1 week ago

Question

Describe the menstrual cycle in a woman.

Answered: 1 week ago