c++multidimensional-arrayconways-game-of-life

A function for counting neighbors in a multidimensional array for the Game of Life game


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


Solution

  • There are a few issues in your code:

    Then there are some other things that should be improved:

    Updated code

    int main() {
        // Initialise a map with a "glider" pattern:
        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,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,1,1,0,0},
                                     {0,0,0,0,0,0,1,0,1,0},
                                     {0,0,0,0,0,0,0,0,0,0},
                                     {0,0,0,0,0,0,0,0,0,0},}; 
        while (true) { 
            showMap(map);
            nextGeneration(map);
            Sleep(1000);
        }
    };
    
    void showMap(const int map[mapSize][mapSize]) {
        cout<< endl;
        for (int i = 0; i < mapSize; i++) {
            for (int j = 0; j < mapSize; j++) {
                cout << map[i][j] << " ";
            }
            cout << endl;
        }
        cout << endl;
    }
    
    void updateRow(int map[mapSize][mapSize], int newRows[2][mapSize], int i) {
        if (i--) { // Update previous row if it exists:
            for (int j = 0; j < mapSize; j++) {
                map[i][j] = newRows[i % 2][j];
            }
        }
    }
    
    void nextGeneration(int map[mapSize][mapSize]) {
        int newRows[2][mapSize]; // Buffer to store new cell values
        for (int i = 0; i < mapSize; i++) {
            for (int j = 0; j < mapSize; j++) {
                newRows[i % 2][j] = neighborhood(map, i, j);
            }
            // Commit the values of the previous(!) row in the matrix
            updateRow(map, newRows, i);
        }
        updateRow(map, newRows, mapSize);
    }
    
    int neighborhood(const int map[mapSize][mapSize], int x, int y) {
        // Use a local variable, and already subtract current cell so
        //    to simplify the loop logic:
        int numSys = -map[x][y]; 
        for (int i = -1; i <= 1; i++) {
            int newX = x + i; // earlier calculation and range-check:
            if (newX >= 0 && newX < mapSize) {
                for (int j = -1; j <= 1; j++) {
                    int newY = y + j;
                    if (newY >= 0 && newY < mapSize) {
                        numSys += map[newX][newY];
                    }
                }
            }
        }
        // Simplified calculation of cell's new value:
        return numSys == 2 ? map[x][y] : numSys == 3;
    }