Question
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
ifstream fin;
ofstream fout;
int main() {
char c = ' ';
vector
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 for (int i = 0; i std::vector 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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started