Hey can someone help me finish my code? Im in a real stump here.
What Ive done so far is read and display the code from a 2d array, then I counted the neighbours that were dead and then did the process to make a new Generation.
However, I dont think Ive done it completely right. Like, how would I tell the function how many times I need to make the new generation, then display it?
Im so lost :s
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int SIZE = 20;
bool readGrid(string fileName, char grid[SIZE][SIZE]);
void displayGrid(const char grid[SIZE][SIZE]);
int countAlive(char grid[SIZE][SIZE]);
void newGen(char grid[SIZE][SIZE]);
// declare more functions if necessary
int main()
{
string fileName; // the name of the grid file
int nGenerations; // nGenerations is the number of generations
char grid[SIZE][SIZE]; // a char array consiting of '*' or '.'
// declare more variables if necessary
char nuevo[SIZE][SIZE];
cout << "Enter the filename of the grid: " << flush; // don't modify
cin >> fileName; // don't modify
cout << endl; // don't modify
readGrid(fileName, grid);
displayGrid(grid);
// add processing for reading and displaying the grid
cout << endl << "How many generations in total? " << flush; // don't modify
cin >> nGenerations; // don't modify
// add processing for new grid generations
if (nGenerations == 1) // don't modify
cout << "This is the grid after " << nGenerations << " generation:" << endl << endl; // don't modify
else // don't modify
cout << "This is the grid after " << nGenerations << " generations:" << endl << endl; // don't modify
// display the grid
cout << endl; // don't modify
system("pause"); // don't modify
return 0; // don't modify
}
// implement all declared functions
bool readGrid(string fileName, char grid[SIZE][SIZE])
{
ifstream inData;
inData.open(fileName.c_str());
if(inData)
{
for(int row = 0; row < SIZE; row++)
{
for(int col = 0; col < SIZE; col++)
{
inData >> grid[row][col];
}
}
return true; }
else
return false;
}
void displayGrid(const char grid[SIZE][SIZE])
{
for(int row = 0; row < SIZE; row++)
{
for(int col = 0; col < SIZE; col++)
{
cout << grid[row][col] << " ";
}
cout << endl;
}
}
int countAlive(char grid[SIZE][SIZE])
{
int neighbour = 0;
for(int row = 1; row < SIZE-1; row++)
{
for(int col = 1; col < SIZE-1; col++)
{
if(grid[row+1][col+1] == '*') neighbour++;
if(grid[row+1][col] == '*') neighbour++;
if(grid[row+1][col-1] == '*') neighbour++;
if(grid[row][col-1] == '*') neighbour++;
if(grid[row][col+1] == '*') neighbour++;
if(grid[row-1][col-1] == '*') neighbour++;
if(grid[row-1][col] == '*') neighbour++;
if(grid[row-1][col+1] == '*') neighbour++;
}
}
return neighbour;
}
void newGen(char grid[SIZE][SIZE])
{
int neighbour = countAlive(grid);
char nuevo[SIZE][SIZE];
grid[SIZE][SIZE] = nuevo[SIZE][SIZE];
for(int row = 0; row < SIZE; row++)
{
for(int col = 0; col < SIZE; col++)
{
grid[row][col] = nuevo[row][col];
if(grid[row][col] == '*' && neighbour == 1)
nuevo[row][col] == '*';
if(grid[row][col] == '.' && neighbour == 1)
nuevo[row][col] == '.';
if(grid[row][col] == '*' && neighbour == 2)
nuevo[row][col] == '*';
if(grid[row][col] == '.' && neighbour == 2)
nuevo[row][col] == '*';
if(grid[row][col] == '*' && neighbour > 2)
nuevo[row][col] == '.';
if(grid[row][col] == '.' && neighbour > 2)
nuevo[row][col] == '.';
}
}
}