Can someone please help me with my program? If anyone is familair with conways game of life i would really appreciate it. Im making my function to count neighbours, but it doesnt seem to be working correctly.
The function is supposed to count the number of neighbours which are alive, but I dont think it is working correctly.
When I output the result of neighbours it outputs something like 250, when infact there are only 20 alive cells in the array
Any help? Thanks =)
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;
}
Tags: