i writing Game of Life using c++ and multidimensional array for this. its code:
#include <iostream>
#include <windows.h>
#include <cstdlib>
using namespace std;
const int mapSize = 10;
int neighborhoods = 0;
int isLife = 0;
int numSys = 0;
void ShowMap(int map[mapSize][mapSize], int size);
void Check(int map[mapSize][mapSize], int size);
int neighborhood(int map[mapSize][mapSize], int x, int y);
int main() {
int map[mapSize][mapSize] = {{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,1,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,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0,0},};
for(int i = 0;i > mapSize; i++) {
map[i][i+2] = 1;
}
int userRow, userColumn;
while(1) {
ShowMap(map, mapSize);
Check(map, mapSize);
Sleep(1000);
}
};
void ShowMap(int map[mapSize][mapSize], int size) {
cout<< endl;
for(int i = 0;i < size; i++) {
for(int j = 0; j < size; j++) {
cout<< map[i][j] << " ";
};
cout<< endl;
}
cout<< endl;
}
void Check(int map[mapSize][mapSize], int size) {
isLife = 0;
for(int i = 0;i < size; i++) {
for(int j = 0; j < size; j++) {
if(map[i][j] == 1) {
neighborhood(map, i, j);
++isLife;
} else {
cout << "nope" << " ";
neighborhood(map, i, j);
}
};
}
if(isLife <= 0) {
} else {
cout<< "Life: " << ifLife;
}
}
int neighborhood(int map[mapSize][mapSize], int x, int y) {
numSys = 0;
for (int i = -1; i <= 1; i++) {
for (int j = -1; j <= 1; j++) {
int newX = x + i;
int newY = y + j;
if(newX == x && newY == y) {
continue;
}
if (newX >= 0 && newX < mapSize && newY >= 0 && newY < mapSize) {
if (i == 0 && j == 0) {
continue;
}
if(map[newX][newY] == 1) {
++numSys;
} else {
continue;
}
}
}
}
if(map[x][y] == 1 && (numSys == 2 || numSys == 3)) {
cout<<endl<< "stay" << " " << numSys;
}
if(map[x][y] == 1 && numSys < 2) {
cout<<endl<< "deppression" << " " << numSys;
map[x][y] = 0;
}
if(map[x][y] == 1 && numSys > 3) {
cout<<endl<< "overpopulation"<< " " << numSys;
map[x][y] = 0;
}
if(map[x][y] == 0 && (numSys == 2 || numSys == 3)) {
cout<<endl<<"New life"<< " "<< numSys;
map[x][y] = 1;
}
But neighborhood function does not work as I need, it does not correctly count neighboring living cells, how can I fix this?
P.S:Sorry for my english
I thought that this function will count the neighboring 8 cells, but it works in a completely different way