Hi I've got most of the code finished but I can't for the life of me figure out how to correctly count neighbors for the edges and corners of the array.
These are the rules.
If the cell is currently empty:
If the cell has exactly three living neighbors, it will come to life in the next generation.
If the cell has any other number of living neighbors, it will remain empty.
If the cell is currently living:
If the cell has one or zero living neighbors, it will die of loneliness in the next generation.
If the cell has four or more living neighbors, it will die of overcrowding in the next generation.
If the cell has two or three neighbors, it will remain living.
All births and deaths occur simultaneously.
Any help would be greatly appreciated. I believe my problems are in the counting neighbors functions and maybe the next generation one as well.no
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
const int SIZE = 20;
void initGrid(bool life[][SIZE], bool life2[][SIZE]);
void readGrid(bool life[][SIZE], bool life2[][SIZE]);
void printGrid(bool life[][SIZE], bool life2[][SIZE]);
int LiveNeighbors(bool life[][SIZE], bool life2[][SIZE],int,int);
void DeadNeighbors(bool life[][SIZE], bool life2[][SIZE],int,int);
void NextGenerations(bool life[][SIZE], bool life2[][SIZE]);
int main()
{
bool life[SIZE][SIZE];
bool life2[SIZE][SIZE];
readGrid(life,life2);
printGrid(life,life2);
for (int count = 0; count < 5; count++)
{
NextGenerations(life,life2);
}
printGrid(life,life2);
system("PAUSE");
return 0;
}
void readGrid(bool life[][SIZE], bool life2[][SIZE])
{
ifstream infile("bacteria.txt");
int numBacteria, row, col;
initGrid(life,life2);
infile >> row >> col;
while (infile){
life[row][col] = true;
infile >> row >> col;
}
infile.close();
}
void initGrid(bool life[][SIZE], bool life2[][SIZE])
{
for (int row = 0; row < SIZE; row++){
for (int col = 0; col < SIZE; col++){
life[row][col] = false;
}
}
for(int row=0; row<SIZE;row++){
for(int col=0; col<SIZE; col++){
life2[row][col]=false;
}
}
}
void printGrid(bool life[][SIZE], bool life2[][SIZE])
{
cout << " 01234567890123456789" << endl;
for (int row = 0; row < SIZE; row++){
cout << setw(2) << row;
for (int col = 0; col < SIZE; col++){
if (life[row][col]){
cout << "*";
} else {
cout << " ";
}
}
cout << endl;
}
}
int LiveNeighbors(bool life[SIZE][SIZE], int row, int col)
{
int neighbors=0;
if(life[row][col]==true && life[row][col-1]==true && col-1>=0)
{
neighbors++;
}
if(life[row][col]==true && life[row+1][col-1]==true && row+1<SIZE && col-1>=0)
{
neighbors++;
}
if(life[row][col]==true && life[row+1][col]==true && row+1<SIZE)
{
neighbors++;
}
if(life[row][col]==true && life[row][col+1]==true && col+1<SIZE)
{
neighbors++;
}
if(life[row][col]==true && life[row-1][col-1]==true && row-1>=0 && col-1>=0)
{
neighbors++;
}
if(life[row][col]==true && life[row+1][col+1]==true && row+1<3 && col+1<SIZE)
{
neighbors++;
}
if(life[row][col]==true && life[row-1][col]==true && row-1>=0)
{
neighbors++;
}
if(life[row][col]==true && life[row-1][col+1]==true && row-1>=0 && col+1<SIZE)
{
neighbors++;
}
return neighbors;
}
int DeadNeighbors(bool life[SIZE][SIZE],int row,int col)
{
int neighbors2=0;
if(life[row][col]==true && life[row][col-1]==true && col-1>=0)
{
neighbors2++;
}
if(life[row][col]==true && life[row+1][col-1]==true && row+1<SIZE && col-1>=0)
{
neighbors2++;
}
if(life[row][col]==true && life[row+1][col]==true && row+1<SIZE)
{
neighbors2++;
}
if(life[row][col]==true && life[row][col+1]==true && col+1<SIZE)
{
neighbors2++;
}
if(life[row][col]==true && life[row-1][col-1]==true && row-1>=0 && col-1>=0)
{
neighbors2++;
}
if(life[row][col]==true && life[row+1][col+1]==true && row+1<3 && col+1<SIZE)
{
neighbors2++;
}
if(life[row][col]==true && life[row-1][col]==true && row-1>=0)
{
neighbors2++;
}
if(life[row][col]==true && life[row-1][col+1]==true && row-1>=0 && col+1<SIZE)
{
neighbors2++;
}
return neighbors2;
}
void NextGenerations(bool life[][SIZE], bool life2[][SIZE])
{
int neighbors=0;
int neighbors2=0;
for (int row=0;row<SIZE;row++)
{
for (int col=0;col<SIZE;col++)
{
neighbors=LiveNeighbors(life,row,col);
if (neighbors<=1)
life2[row][col]=false;
if (neighbors>=4)
life2[row][col]=false;
if (neighbors==2)
life2[row][col]=true;
if (neighbors==3)
life2[row][col]=true;
}
}
for (int row=0;row<SIZE;row++)
{
for (int col=0;col<SIZE;col++)
{
neighbors2=DeadNeighbors(life,row,col);
if (neighbors2==3)
life2[row][col]=true;
}
}
for(int row=0;row<SIZE;row++)
{
for(int col=0; col<SIZE;col++)
life[row][col]=life2[row][col];
}
}