Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

//main.cpp file #include #include GameOfLife.h using namespace std; int main() { gameOfLife gol; gol.run(); std::cin.get(); return 0; } //GameOfLife.h file #ifndef GAME_OF_LIFE_H #define GAME_OF_LIFE_H #include

//main.cpp file

#include

#include "GameOfLife.h"

using namespace std;

int main()

{

gameOfLife gol;

gol.run();

std::cin.get();

return 0;

}

//GameOfLife.h file

#ifndef GAME_OF_LIFE_H

#define GAME_OF_LIFE_H

#include

#include

class gameOfLife

{

public:

typedef std::vector< std::vector> gridVector;

private:

int gridSize;

gridVector mainGrid;

gridVector *gridCopy;

public:

gameOfLife();

public:

void run();

void initGrid();

void drawGrid();

void updateGrid();

void copyGrid ();

int checkNeighbors(gridVector *grid, int x, int y);

void nextGeneration(int x, int y, int lod);

int randGenerate(int start, int end);

private:

std::random_device randDevice;

std::mt19937 gen;

};

//GameOfLife.cpp

#include

#include "GameOfLife.h"

//Define gameOfLife

gameOfLife::gameOfLife() : gridSize(15), gen(randDevice())

{

}

void gameOfLife::initGrid()

{

for(int i = 0; i < gridSize; i++)

{

for(int j = 0; j < gridSize; j++)

{

mainGrid[i].push_back(0);

}

}

for(int i = 1; i < gridSize ; i++)

{

for(int j = 1; j < gridSize ; j++)

{

int randSize = randGenerate(2 , gridSize);

int state = randGenerate(0 , 2);

int indexRow = randGenerate(1, randSize );

int indexColumn = randGenerate(1, randSize );

mainGrid[indexColumn][indexRow] = state;

}

}

}

int gameOfLife::randGenerate(int start, int end)

{

std::uniform_int_distribution genenNum(start, end - 1);

return genenNum(gen);

}

void gameOfLife::drawGrid()

{

char ch = 100;

for(int i = 1; i < gridSize - 1; i++)

{

for(int j = 1; j < gridSize - 1; j++)

{

if(mainGrid[i][j])

{

std::cout << " "<

}

else

{

std::cout << " ";

}

}

std::cout<

}

}

void gameOfLife::copyGrid()

{

(*gridCopy).clear();

(*gridCopy).resize(gridSize);

for(int i = 0; i < gridSize; i++)

{

for(int j = 0; j< gridSize; j++)

{

(*gridCopy)[i].push_back((mainGrid)[i][j]);

}

}

}

void gameOfLife::updateGrid()

{

copyGrid();

for(int i = 1; i < gridSize - 1 ; i++)

{

for(int j = 1; j < gridSize - 1; j++)

{

int lod = checkNeighbors(gridCopy, i, j);

nextGeneration( i, j, lod);

}

}

}

int gameOfLife::checkNeighbors(gridVector *gridCopy, int x, int y)

{

int lod = 0;

for(int i = -1; i < 2; i++)

{

for(int j = -1; j < 2; j++)

{

if(!(i == 0 && j == 0))

{

if((*gridCopy)[ x + i ][ y + j ])

{

lod++;

}

}

}

}

return lod;

}

void gameOfLife::nextGeneration(int x, int y, int lod)

{

if(lod < 2)

{

mainGrid[x][y] = 0;

}

else if(lod == 3)

{

mainGrid[x][y] = 1;

}

else if(lod > 3)

{

mainGrid[x][y] = 0;

}

}

void gameOfLife::run()

{

drawGrid();

updateGrid();

}

I got one problem when i ran the program, but could not able to figure out.

I need to summit it in 4 hours

Can someone able to help on this problem please?

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

Oracle Database 19c DBA By Examples Installation And Administration

Authors: Ravinder Gupta

1st Edition

B09FC7TQJ6, 979-8469226970

More Books

Students also viewed these Databases questions

Question

Brief the importance of span of control and its concepts.

Answered: 1 week ago

Question

What is meant by decentralisation?

Answered: 1 week ago

Question

Write down the Limitation of Beer - Lamberts law?

Answered: 1 week ago