Hi, I'm having to write Conway's game of life for school and I'm having a little trouble with it. Whenever it runs through the code, all of the 'cells' move to the left a bunch and warp around the screen.. I'm not sure why.. Any help would be great! Thanks!

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime>

using namespace std;

void seedGrid();
void runTick();
void outputGrid();
void setCellState();

char lifeGrid[80][22]={'*'};

int main(int argc, char *argv[])
{
    srand( time(NULL) );
    seedGrid();
    outputGrid();
    system("PAUSE");
    
    do
    {
    system("CLS");
    runTick();
    outputGrid();
    setCellState();
    outputGrid();
    system("PAUSE");
    }while(true);
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


//Runs rules and sets cell's status accordingly. 
void seedGrid()
{
    int randNum = 0;
    
    for( int i=0; i < 80; i++)
    {
        for( int j=0; j < 22; j++)
        {
             randNum = rand() % 100;
             
             switch( randNum)
             {
                 case 1:
                      lifeGrid[i][j] = '*';
                      break;
                 default: 
                      lifeGrid[i][j] = ' ';
             } 
        }
    }
}
    
void runTick()
{
    int numSurround = 0;
     
    for( int i=0; i < 80; i++)
    {
        for( int j=0; j < 22; j++)
        {
             numSurround = 0;
             
             if( lifeGrid[i+1][j] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[i-1][j] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[i][j+1] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[i+1][j-1] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[i+1][j+1] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[i-1][j-1] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[i+1][j-1] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[i-1][j+1] == '*')
             {
                 numSurround +=1;
             } 
             
             cout << numSurround << endl;
             
             if( numSurround >= 2 && numSurround <= 3 && lifeGrid[i][j]== ' ' )
             {
                lifeGrid[i][j] = 'a';
             }  
             else if(( numSurround < 2) || ( numSurround > 3))
             {
                 lifeGrid[i][j] = 'd';   
             }     
        }
    }
}

void setCellState()
{
    for( int i=0; i < 80; i++)
    {
        for( int j=0; j < 22; j++)
        {
             if( lifeGrid[i][j] == 'a')
             {
                 lifeGrid[i][j] = '*';
             }
             else if( lifeGrid[i][j] == 'd')
             {
                 lifeGrid[i][j] = ' ';
             }
        }
    }
}

void outputGrid()
{
    for( int i=0; i < 80; i++)
    {
        for( int j=0; j < 22; j++)
        {
             cout << setw(1) << lifeGrid[i][j];
        }
    }
}

Dani AI

Generated

Good call by to watch the edges, and nice troubleshooting by finding a duplicated neighbor check — those two problems are the usual causes of the “shift/warp” behaviour. Two more reliable practices will make the implementation robust and easier to debug: (1) count neighbors with a compact, bounds-checked loop instead of enumerating eight checks by hand, and (2) update into a separate buffer so reads never see partially-updated state.

A safe neighbor-count pattern (rows = y, cols = x) is straightforward and reduces copy/paste mistakes:

int count = 0;
for (int dy = -1; dy <= 1; ++dy)
  for (int dx = -1; dx <= 1; ++dx)
    if (dy || dx) {
      int ny = y + dy, nx = x + dx;
      if (ny >= 0 && ny < gridH && nx >= 0 && nx < gridW)
        count += (grid[ny][nx] == '*');
    }

Use a second array next to hold the computed next generation, then swap or copy it back after the full pass. The update rule is simple and unambiguous:

bool alive = (grid[y][x] == '*');
next[y][x] = (alive && (count == 2 || count == 3)) || (!alive && count == 3);

Quick debugging tips: test with tiny, known patterns (block, blinker, glider) to verify rules and orientation; add asserts to catch any index outside [0,gridH) or [0,gridW); temporarily print neighbor counts for a single cell when results look wrong. Also keep your row/column naming consistent: index 0..gridH-1 = rows, 0..gridW-1 = columns, and print a newline after each row. These practices catch both display-index swaps and memory-corruption-style artifacts quickly.

Recommended Answers

All 4 Replies

It won't let me edit my original post, so here is an update with where I am now. I still get the same problem, however I think it is something wrong with my display..

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <ctime>

using namespace std;

void seedGrid();
void runTick();
void outputGrid();
void setCellState();

const int gridH = 22;
const int gridW = 40;

char lifeGrid[gridH][gridW]={'*'};

int main(int argc, char *argv[])
{
    srand( time(NULL) );
    seedGrid();
    outputGrid();
    system("PAUSE");
    
    do
    {
    system("CLS");
    runTick();
    setCellState();
    outputGrid();
    system("PAUSE");
    }while(true);
    
    
    
    system("PAUSE");
    return EXIT_SUCCESS;
}


//Runs rules and sets cell's status accordingly. 
void seedGrid()
{
    int randNum = 0;
    
    for( int y=0; y < gridH; y++)
    {
        for( int x=0; x < gridW; x++)
        {
             randNum = rand() % 100;
             
             switch( randNum)
             {
                 case 1:
                      lifeGrid[y][x] = '*';
                      break;
                 default: 
                      lifeGrid[y][x] = ' ';
             } 
        }
    }
}
    
void runTick()
{
    int numSurround = 0;
     
    for( int y=0; y < gridH; y++)
    {
        for( int x=0; x < gridW; x++)
        {
             numSurround = 0;
             
             if( lifeGrid[y+1][x] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[y-1][x] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[y][x+1] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[y+1][x-1] == '*')
             {
                 numSurround +=1;    
             }
             
             
             if( lifeGrid[y+1][x+1] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[y-1][x-1] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[y+1][x-1] == '*')
             {
                 numSurround +=1;
             }
             
             if( lifeGrid[y-1][x+1] == '*')
             {
                 numSurround +=1;
             } 
             
             //cout << numSurround << endl;
             
             if( numSurround >= 2 && numSurround <= 3 && lifeGrid[y][x]== ' ' )
             {
                lifeGrid[y][x] = 'a';
             }  
             if(( numSurround < 2) || ( numSurround > 3))
             {
                 lifeGrid[y][x] = 'd';   
             }     
        }
    }
}

void setCellState()
{
    for( int y=0; y < gridH; y++)
    {
        for( int x=0; x < gridW; x++)
        {
             if( lifeGrid[y][x] == 'a')
             {
                 lifeGrid[y][x] = '*';
             }
             if( lifeGrid[y][x] == 'd')
             {
                 lifeGrid[y][x] = ' ';
             }
        }
    }
}

void outputGrid()
{
    for( int y=0; y < gridH; y++)
    {
        for( int x=0; x < gridW; x++)
        {
             cout << lifeGrid[y][x];
        }
        cout << endl;
    }
}

One thing is, you need to treat the outer border (column 0 and 21, row 0 and 21)as special so you don't go out of bound on your array. For example on line 100, in cell (0,0) it tries to read lifeGrid[-1][-1]. You may be lucking out and there might be junk at that address, but usually it should just crash. Cells in the top row should only check the row below and each side, etc.

Thank you! That and the fact that I was forgetting to check one of the cells and instead checking one twice were my problems! :) Thanks!

<Mods Please Delete>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.