0

I'm new here so I apologize if I formatted something incorrectly. I cannot seem to get an output for the game of life. The output should be in 0 and 1 in a 2d grid and it doesnt not seem to be outputting anything. Can someone please help me with this?

EDIT: I changed the code and got this error:

#include <vector>
using namespace std;
#include <iostream>

vector<vector<bool> > world = {
{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,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,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,0,0,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,1,1,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}
};
vector<vector<bool> > world2 = world;
const int ROW_NUMBER = world.size();
const int COL_NUMBER = world[0].size();
unsigned int i;
unsigned int j;
int countLivingNeightbors(vector<vector<bool> > world,unsigned int i,unsigned int j) {
    int livingNeighbors = 0;
    for (int row = i - 1; row <= i + 1; row++) {
        for (int col = j - 1; col <= j + 1; col++) {
            if (row == i && col == j) {
                continue; // skipping this iteration
            } else {
                int optimizedRow = (row + ROW_NUMBER) % ROW_NUMBER;
                int optimizedCol = (col + COL_NUMBER) % COL_NUMBER;
                if (world[optimizedRow][optimizedCol] == true) {
                    livingNeighbors++;
                } else {
                    continue;
                }
            }
        }
    }
    return livingNeighbors;
}

int main()
{
for (int i = 0; i < world.size(); i++) {
        for (int j = 0; j < world[i].size(); j++) {
            int livingNeighbors = countLivingNeightbors(world, i, j);
            if((world[i][j]==1)&&(livingNeighbors>3)){
                world2 [i][j]=='.';
            }
            if((world[i][j]==1)&&(livingNeighbors<2)){
                world2 [i][j]=='.';
            }
            if((world[i][j]==0)&&(livingNeighbors==3)){
                world2 [i][j]=='O';
            }
            if((world[i][j]==1)&&(livingNeighbors==2||livingNeighbors==3)){
                world2 [i][j]=='O';
            }

        cout << world2;

        }
        cout << endl;
    }
    return 0;
}
Andy Zhou
  • 1
  • 1
  • `int countLivingNeightbors(vector > world,unsigned int i,unsigned int j) {` you may want to pass world by const reference instead of by value for performance reasons. – drescherjm Oct 07 '19 at 01:07
  • Welcome to Stack Overflow! Stack Overflow is a place for specific questions. Please debug your code and isolate the problematic code. Then, post the isolated code in the form of a [mre]. Thank you! – L. F. Oct 07 '19 at 01:12
  • Incidentally, please try to avoid `using namespace std;` because it is considered bad practice. See [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/q/1452721) – L. F. Oct 07 '19 at 01:24

0 Answers0