Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm having trouble implementing a world for Conway's game of life from a file rather than making a vector with a world inside (manual input).

I'm having trouble implementing a world for Conway's game of life from a file rather than making a vector with a world inside (manual input). So far this code works if I remove the file input portion and use my own world to make a new generation (Like just typing the contents of world.txt into a vector). Unfortunately I can't figure out how to connet the file connects and the game of life rules to output into a file. This is what I got so far:

#include

#include

#include

#include

#include

#include

#include

using namespace std;

void computeNextGeneration();

vector > world;

ifstream fin;

ofstream fout;

int main() {

char c = ' ';

vector > world;

fin.open("world.txt");

fout.open("Report.txt");

world.push_back(vector ());

size_t row = 0;

cout << "Original Generation:" << endl;

for (int i = 0; i

for (int j = 0; j

cout << world[i][j] << " ";

}

cout << endl;

}

cout << endl;

computeNextGeneration();

system("pause");

return 0;

}

void computeNextGeneration() {

int m = world.size();

int n = world[0].size();

vector< vector >newWorld;

for (int i = 0; i

std::vectorrowVector(n, false);

newWorld.push_back(rowVector);

}

for (int row = 1; row

for (int colum = 1; colum < world[row].size() - 1; colum++) {

int aliveCell = 0;

for (int k = -1; k <= 1; k++)

for (int l = -1; l <= 1; l++)

aliveCell += world[row + k][colum + l];

aliveCell -= world[row][colum];

if ((world[row][colum] == true) && (aliveCell < 2)) {

newWorld[row][colum] = false;

}

else

if ((world[row][colum] == true) && (aliveCell > 3)) {

newWorld[row][colum] = false;

}

else

if ((world[row][colum] == false) && (aliveCell == 3)) {

newWorld[row][colum] = true;

}

else

if ((world[row][colum] == true) && (aliveCell == 3)) {

newWorld[row][colum] = true;

}

else {

newWorld[row][colum] = world[row][colum];

}

}

}

cout << "The next generation:" << endl;

for (int i = 0; i

for (int j = 0; j

cout << newWorld[i][j] << " ";

fout << newWorld[i][j] << " ";

}

cout << endl;

}

}

world.txt

{ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0 }, { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 }

The idea is that given the world from the file, we can apply the game of life rules and continously allow the generations to breed more. (Must be vectors, not arrays)

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

What is DDL?

Answered: 1 week ago