Question

I am really struggling with how to approach this program. Conway's Game of life, whereby, 1....

I am really struggling with how to approach this program. Conway's Game of life, whereby,

1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.

2. Any live cell with two or three live neighbours lives on to the next generation.

3. Any live cell with more than three live neighbours dies, as if by over-population.

4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

Submit 4 files, main.cpp, GameOfLife.cpp, GameOfLife.h, and a text file for implementation. Create two classes - one corresponding to the game itself (example: GameOfLife), and another one corresponding to a cell (example: Cell). The GameOfLife class will contain a private structure(s) of Cell. The entities will each have a set of responsibilities, reflected in the corresponding member functions/methods.

The GameOfLife would have to be able to:

Seed/initialize the game board with:

a predefined pattern (from a file to be created by the programmer)

a randomly generated pattern

The display of the board will be character based, simply accomplished with console output.

This implementation will use static two-dimensional arrays

Run the game

Execute the rules for every cell in order to generate the next generation of our cell population

Display the next generation matrix

Stop the game (ask a user up front how many generations to display, or ask a user whether to continue every step of the way)

The Cell would have to be able to:

Know, if it is alive or dead, and communicate so

Die

Come alive

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Executable Code

//GameOfLife.h

//Include the needed header files.

#include <iostream>

#ifndef GAMEOFLIFE_H

#define GAMEOFLIFE_H

//Declare rows.

const int NOOFROWS = 20;

//Declare columns

const int NOOFCOLS = 20;

//Class.

class gameOfLife

{

//Access specifier.

public:

//Constructor()

gameOfLife();

//Declare functions.

//Function get1().

bool get1(int ro, int co) const;

//Function set1().

void set1(int ro, int co, bool val);

//Function countRo().

int countRo(int ro) const;

//Function countCo().

int countCo(int co) const;

//Function countTot().

int countTot() const;

//Function display().

void display()const;

//Access specifier.

private:

  

//Matrix.

bool life[NOOFROWS][NOOFCOLS];

};

#endif

//GameOfLife.cpp

//Include the needed header files.

#include <iostream>

#include <iomanip>

#include "GameOfLife.h"

using namespace std;

//Class constructor.

gameOfLife::gameOfLife()

{

  

//For loop to iterate rows.

for (int ro = 0; ro < NOOFROWS; ro++)

{

  

//For loop to iterate columns.

for (int co = 0; co < NOOFCOLS; co++)

{

  

//Return.

life[ro][co] = false;

}

}

}

//Function definition get1().

bool gameOfLife::get1(int ro, int co) const

{

  

//Return.

return life[ro][co];

}

//Function definition set1()

void gameOfLife::set1(int ro, int co, bool val)

{

//Return.

life[ro][co] = val;

}

//Function definition countRo().

int gameOfLife::countRo(int ro) const

{

  

//Declare a variable.

int rowtotal = 0;

  

//For loop to iterate columns.

for (int co = 0; co < NOOFCOLS; co++)

{

  

//Condition check.

if ( life[ro][co] == true )

{

//Increment.

rowtotal++;

}

}

//Return.

return rowtotal;   

}

//Function definition countCo().

int gameOfLife::countCo(int co) const

{

//Declare a variable.

int colTotal = 0;

  

//For loop to iterate rows.

for (int ro = 0; ro < NOOFROWS; ro++)

{

//Condition check.

if ( life[ro][co] == true )

{

//Increment.

colTotal++;

}

}

//Return.

return colTotal;

}

//Function definition countTot().

int gameOfLife::countTot() const

{

//Declare a variable.

int total = 0;

  

//For loop to iterate rows.

for (int ro = 0; ro < NOOFROWS; ro++)

{

  

//For loop to iterate columns.

for (int co = 0; co < NOOFCOLS; co++)

{

//Condition check.

if ( life[ro][co] == true )

{

  

//Increment.

total++;

}

}   

}

//Return.

return total;   

}

//Function definition display().

void gameOfLife::display() const

{

cout << " ";

//For loop to iterate rows.

for (int co = 0; co < NOOFCOLS; co++)

{

cout << co % 10;

}

cout << endl;

  

//For loop to iterate rows.

for (int ro = 0; ro < NOOFROWS; ro++)

{

  

//Display.

cout << setw(2) << ro % 100;

//For loop to iterate columns.

for (int co = 0; co < NOOFCOLS; co++)

{

  

//Condition.

if ( life[ro][co] == true )

{

//Display.

cout << "*";

}

else if ( life[ro][co] == false )

{

//Display.

cout << " ";

}

}

cout << endl;

}

}

//main.cpp

//Include the needed header files.

#include <iostream>

#include <fstream>

#include <cassert>

#include "GameOfLife.h"

using namespace std;

//Function declarations.

//Function for first generation.

void genOne(gameOfLife& gen);

//Function to get the next generation.

void getGenNext(gameOfLife& gen,int liveNeighbours,int ro,int co);

//Function for the fate of dead cell.

void deadCell(gameOfLife& gen,int liveNeighbours,int ro,int co);

//Function for the fate of live cell.

void liveCell(gameOfLife& gen,int liveNeighbours,int ro,int co);

//Function to check boundary.

void checkBound(int ro, int co, int& liveNeighbours);

//Function to display result.

void displayOut(gameOfLife& gen);

//Function to tally the living neighbours.

int tallyLiveNeighbours(const gameOfLife& gen, int& liveNeighbours,int ro,int co);

//Function main().

int main()

{

  

//Object for class.

gameOfLife gen;

//Declaration of variables

int noOfGen, liveNeighbours=0, ro=0, co=0;

  

//Prompt for user.

cout << "How many generations in total?: " << endl;

//Get the input.

cin >> noOfGen;

//Call the function genOne().

genOne(gen);

//Call the function display().

gen.display();

//Display the output.

cout << "Total alive in row 10 = " << gen.countRo(10) << endl;

cout << "Total alive in column 10 = " << gen.countCo(10) << endl;

cout << "Total alive = " << gen.countTot() << endl << endl;

  

//Loop to iterate

for(int count = 1; count < noOfGen; count++)

{

  

//Call the function getGenNext().

getGenNext(gen,liveNeighbours,ro,co);

//Call the function displayOut().

displayOut(gen);

}

//To pause.

system("pause");

return 0;

}

//Function definition genOne().

void genOne(gameOfLife& gen)

{

//Input file.

ifstream infile("input.txt");

assert(infile);

//Declare row and column.

int ro, co;

//Get row and column.

infile >> ro >> co;

//While true.

while (infile)

{

  

//Set row and column.

gen.set1(ro, co, true);

  

//Get row and column

infile >> ro >> co;

}

//Close file.

infile.close();

}

//Function definition getGenNext().

void getGenNext(gameOfLife& gen,int liveNeighbours,int ro,int co)

{

  

//Declare a variable for live neighbours.

liveNeighbours = 0;

//For loop to iterate rows.

for(int ro = 0; ro < NOOFROWS; ro++)

{

  

//For loop to iterate columns.

for(int co = 0; co < NOOFCOLS; co++)

{

  

//Condition check.

if(gen.get1(ro,co) == false)

{

  

//Function call to deadCell().

deadCell(gen,liveNeighbours,ro,co);

}

//Condition check.

else if(gen.get1(ro,co) == true)

{

  

//Function call to liveCell.

liveCell(gen,liveNeighbours,ro,co);

}

}

}

}

//Function definition tallyLiveNeighbours().

int tallyLiveNeighbours(gameOfLife& gen,int& liveNeighbours,int ro,int co)

{

  

//Condition check.

if(gen.get1(ro-1,co-1) == true)

{

  

//Increment live neighbours.

liveNeighbours++;

//Function call checkBound().

checkBound(ro, co, liveNeighbours);

}

//Condition check.

else if(gen.get1(ro-1,co) == true)

{

  

//Increment live neighbours.

liveNeighbours++;

  

//Function call checkBound().

checkBound(ro, co, liveNeighbours);

}

//Condition check.

else if(gen.get1(ro-1,co+1) == true)

{

//Increment live neighbours.

liveNeighbours++;

  

//Function call checkBound().

checkBound(ro, co, liveNeighbours);

}

//Condition check.

else if(gen.get1(ro,co-1) == true)

{

  

//Increment live neighbours.

liveNeighbours++;

//Function call checkBound().

checkBound(ro, co, liveNeighbours);

}

//Condition check.

else if(gen.get1(ro,co+1) == true)

{

  

//Increment live neighbours.

liveNeighbours++;

  

//Function call checkBound().

checkBound(ro, co, liveNeighbours);

}

//Condition check.

else if(gen.get1(ro+1,co-1) == true)

{

  

//Increment live neighbours.

liveNeighbours++;

  

//Function call checkBound().

checkBound(ro, co, liveNeighbours);

}

//Condition check.

else if(gen.get1(ro+1,co)== true)

{

  

//Increment live neighbours.

liveNeighbours++;

  

//Function call checkBound().

checkBound(ro, co, liveNeighbours);

}

//Condition check.

else if(gen.get1(ro+1,co+1) == true)

{

  

//Increment live neighbours.

liveNeighbours++;

//Function call checkBound().

checkBound(ro, co, liveNeighbours);

}

return 1;

}

//Function definition deadCell().

void deadCell(gameOfLife& gen,int liveNeighbours,int ro,int co)

{

//Function call.

tallyLiveNeighbours(gen,liveNeighbours,ro,co);

//Condition check.

if(liveNeighbours == 3)

{

  

//Function call.

gen.set1(ro,co,true);

}

//Condition check.

else if ((liveNeighbours < 3) || (liveNeighbours > 3))

{

  

//Function call.

gen.set1(ro,co,false);

}

}

//Function definition liveCell().

void liveCell(gameOfLife& gen,int liveNeighbours,int ro,int co)

{

  

//Function call.

tallyLiveNeighbours(gen,liveNeighbours,ro,co);

//Condition check.

if((liveNeighbours <= 1) || (liveNeighbours >= 4))

{

  

//Function call.

gen.set1(ro,co,false);

}

//Condition check.

else if((liveNeighbours == 2) || (liveNeighbours == 3))

{

  

//Function call.

gen.set1(ro,co,true);

}

}

//Function definition checkBound().

void checkBound(int ro, int co, int& liveNeighbours)

{

  

//Condition check.

if(((ro-1) < 0) || ((ro+1) > NOOFROWS) || ((co-1)< 0) || ((co+1) < NOOFCOLS))

{

  

//Decrement.

liveNeighbours--;

}

}

////Function definition displayOut().

void displayOut(gameOfLife& gen)

{

  

//Function call.

gen.display();

//Output.

cout << "Total alive in row 10 = " << gen.countRo(10) << endl;

cout << "Total alive in column 10 = " << gen.countCo(10) << endl;

cout << "Total alive = " << gen.countTot() << endl << endl;

}

Input File

Output

Add a comment
Know the answer?
Add Answer to:
I am really struggling with how to approach this program. Conway's Game of life, whereby, 1....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Option 2: Conwav's Game of Life Your task is to write a program that plays Conway's...

    Option 2: Conwav's Game of Life Your task is to write a program that plays Conway's Game of Life (for details and ex amples, see https://en.vikipedia.org/wiki/Convay's.Game of Life). The basic gam ts played on each generation, each cell changes according to the following instructions: nal grid of square cells, each of which is either alive or dead. With 1. Any dead cell with exactly three live neighbors (out of its eight nearest neighbors) comes to life [reproduction]. 2. Any live...

  • Program in C++ Implement Conway's Game of Life using 2-dimensional arrays. All the tips, tricks, techniques...

    Program in C++ Implement Conway's Game of Life using 2-dimensional arrays. All the tips, tricks, techniques we have been using in class are allowed. Nothing else. The program should read the initial state of the board by reading in "alive" cells from a user input data file. Meaning your program should ask the user the name of the data file. Assume all the other cells are "dead." Make sure to use modular coding techniques. The main program should be pretty...

  • You are going to be implementing the classic computer science simulation, Conway's Game of Life. Conway's...

    You are going to be implementing the classic computer science simulation, Conway's Game of Life. Conway's Life is played on a matrix of cells, kind of like a chess board but theoretically extending infinitely in every direction. Each individual cell in the matrix can either be alive or dead. A live cell in the matrix is shown in our simulation by printing an asterisk (*) to the screen. A dead cell is shown by leaving that area of the matrix...

  • 1. The Pentagonal Game of Life is a variant of the Game of Life played on a grid in which each cell has 5 neighbours. L...

    1. The Pentagonal Game of Life is a variant of the Game of Life played on a grid in which each cell has 5 neighbours. Live cells are shown in grey and dead cells are shown in white. The rules are as follows Two cells are considered to be neighbours if they share an edge. A dead cell comes to life it if has exactly 2 living neighbours. .A live cell remains alive if it has 2 or 3 living...

  • JAVA. Threading the Game of Life Write a Java program to implement the Game of Life,...

    JAVA. Threading the Game of Life Write a Java program to implement the Game of Life, as defined by John Conway: Any live cell with fewer than two live neighbors dies, as if caused by under-population. Any live cell with two or three live neighbors lives on to the next generation. Any live cell with more than three live neighbors dies, as if by overpopulation. Any dead cell with exactly three live neighbors becomes a live cell, as if by...

  • Hi, PLEASE I need the code in C programming, (USING THE SAME INPUT AND OUTPUT FORMAT...

    Hi, PLEASE I need the code in C programming, (USING THE SAME INPUT AND OUTPUT FORMAT DESCRIBED BELOW). Title: Game of Life Description In this assignment, you will code a simulation called "Game of Life". It is a very famous 'game' and the formed the basis of an entire field of simulation models called "cellular automata". Before continuing reading this assignment, I suggest you read a bit more about Game of Life at: https://en.wikipedia.org/wiki/Conway's_Game_of_Life or http://www.math.com/students/wonders/life/life.html (the latter also has...

  • Write this Game of Life program in Java. The Game of Life is a well-known mathematical...

    Write this Game of Life program in Java. The Game of Life is a well-known mathematical game that gives rise to amazingly complex behavior, although it can be specified by a few simple rules. Here are the rules. The game is played on a rectangular board. Each square can be either empty or occupied. At the beginning, you can specify empty and occupied cells using 1's and 0's; then the game runs automatically. In each generation, the next generation is...

  • Write this Game of Life program in Java. The Game of Life is a well-known mathematical...

    Write this Game of Life program in Java. The Game of Life is a well-known mathematical game that gives rise to amazingly complex behavior, although it can be specified by a few simple rules. Here are the rules. The game is played on a rectangular board. Each square can be either empty or occupied. At the beginning, you can specify empty and occupied cells using 1's and 0's; then the game runs automatically. In each generation, the next generation is...

  • I have a question about my assignment. Could you please make program and give me some...

    I have a question about my assignment. Could you please make program and give me some explanation of codes? I need c++ codes. Conway's Game of Life is a game invented by mathematician John Conway in 1970. The rules are as follows: Each cell lives in a cell in a square (make it 10x10) grid. A cell can either be dead or alive. Before you start the game, you need to provide an initial state. You can do this by...

  • I have a question about my assignment. Could you please make program and give me some...

    I have a question about my assignment. Could you please make program and give me some explanation of codes? Conway's Game of Life is a game invented by mathematician John Conway in 1970. The rules are as follows: Each cell lives in a cell in a square (make it 10x10) grid. A cell can either be dead or alive. Before you start the game, you need to provide an initial state. You can do this by randomly setting 20 of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT