Hey everyone, I've got a problem with my program and don't really know how to fix it. The program is a simulation Game of Life and has these rules:
- A new cell is born on an empty square if is surrounded by exactly three neighboring cells
- A cell dies of overcrowding if it has four or more neighbors
- A cell dies of loneliness if it has zero or one neighbor
- Any live cell with two or three live neighbors lives, unchanged, to the next generation.
The program is run in a project so I have a driver, cpp and h file. So, I think the problem is with my "countNeighbors" function because every time I try to compile it I get a grid of 2's and 3's instead of a grid with 1's and 0's in it. This is what is in my cpp file:
#include <iostream>
#include <fstream>
#include "life.h"
using namespace std;
//global data structure
string *world;
int numRows;
int numCols;
//reads a text file into an array of strings
//will allocate more memory as necessary
//assumes each line is same length
void populateWorld (string FILE_NAME){
const int SIZE_INCREMENT = 3;
char line[80];
numRows = 0;
numCols = 0;
ifstream inFile;
inFile.open(FILE_NAME.data());
world = new string[SIZE_INCREMENT];
while ( inFile.getline(line,80) ) {
world [numRows] = line;
numCols = world[numRows].length();
numRows++;
if (numRows % SIZE_INCREMENT == 0) { //time to resize
string *tempWorld = new string[numRows + SIZE_INCREMENT];
for (int i = 0; i < numRows; i++) {
tempWorld[i] = world[i];
}
//free the world memory
delete [] world;
world = tempWorld;
}
}
}
//Prints out structure as if it were 2D array of characters
void showWorld() {
//show the the grid or txt file
for(int row=0; row < numRows; row++) {
for (int col=0; col < numCols; col++) {
cout << world[row][col];
}
cout << endl;
}
cout << endl;
}
// creates a duplicate of the space in the Life program
void iterateGeneration() {
string *newworld = new string[numRows];
for(int row=0; row < numRows; row++) {
string temp = ""; //create a storage place for the next line
for (int col=0; col < numCols; col++) {
temp += (char) (world[row][col] + 1);
}
newworld[row] = temp;
}
delete [] world;
world = newworld;
return;
}
// counts number of neighbors in grid
int countNeighbors(bool ** newworld, int row, int col){
int neighbors = 0;
if ( newworld[row - 1][col - 1] == true )
neighbors++;
if ( newworld[row - 1][col] == true )
neighbors++;
if ( newworld[row - 1][col + 1] == true )
neighbors++;
if ( newworld[row ][col - 1] == true )
neighbors++;
if ( newworld[row ][col + 1] == true )
neighbors++;
if ( newworld[row + 1][col - 1] == true )
neighbors++;
if ( newworld[row + 1][col] == true )
neighbors++;
if ( newworld[row + 1][col + 1] == true )
neighbors++;
return neighbors;
}
So, a couple things I forgot to mention: 1. I am only computing the first and second generations. In my driver file I declared it as: "const int NUM_GENERATIONS = 2." 2. I am reading the grid from a txt file declared as this: const string FILE_NAME = "starting_grid.txt." The txt file contains the following:
00000000100000001010
00000000010000001001
11100000010100000010
10100100101010101010
00101010010010101000
Can anyone help me?