I have been trying to write a program for the game of life and have been having some issues. The rules are:

  1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.
  2. Any live cell with two or three live neighbours lives on to the next generation.
  3. Any live cell with more than three live neighbours dies, as if by overcrowding.
  4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

I have been looking at my loop structure and functions for a while now but cant seem to find the problem. I need another set of eyes to look it oveer and see if they can't point out the problem to me. More than likely it is just a simple programming error but any help would be appreciated.

Here is my code:

#include "stdafx.h"
#include <iostream>
#include <time.h>

using namespace std;

int neighborCheck( char[][20], int, int ); 
void boardSwitch( char [][20], char[][20] );
void wait( int );
void printGrid( char[][20] );
void runRules( char [][20], char [][20], int, int );

const char ALIVE = 'X';
const char DEAD = ' ';


int _tmain(int argc, _TCHAR* argv[])
{
    char boardOne[70][20];
    char boardTwo[70][20];
    int seconds = 1;

    //Seed the boards as all dead cells
    for( int i = 0; i < 70; i++ )
            for( int j = 0; j < 20; j++ )
                boardOne[i][j] = DEAD;
    for( int i = 0; i < 70; i++ )
            for( int j = 0; j < 20; j++ )
                boardTwo[i][j] = DEAD;

    //Seed the board with alive cells
    boardOne[35][10] = ALIVE;
    boardOne[35][11] = ALIVE;
    boardOne[35][9] = ALIVE;

    //Print the game board
    printGrid( boardOne );
    wait( seconds );

    //Run the rules of the game
    for( int i = 0; i < 70; i++ )
    {
        for( int j = 0; j < 20; j++ )
        {
            runRules( boardOne, boardTwo, i, j );
        }
    }

    //Move the information in the second board to the first board and print the game board
    boardSwitch( boardOne, boardTwo );
    system("cls");
    printGrid( boardOne );

    system("pause");
    return 0;
}

void runRules( char gridOne[][20], char gridTwo[][20], int x, int y )
{
    //For each cell
    //Check how many neighbours it has
    //If it was alive, it needs X - Y neighbours to stay alive
    //If it was dead, it needs X neighbours to come to live
    //Otherwise kill the cell.
    int neighbors = neighborCheck( gridOne, x, y );

    //Rules if the cell is alive.
    if( gridOne[x][y] == ALIVE )
    {
        if( neighbors < 2 )
            gridTwo[x][y] = DEAD;
        if( neighbors == 2 || neighbors == 3 )
            gridTwo[x][y] = ALIVE;
        if( neighbors > 3 )
            gridTwo[x][y] = DEAD;
    }

    //Rules if the cell is dead
    else if( gridOne[x][y] == DEAD )
        if( neighbors == 3 )
            gridTwo[x][y] = ALIVE;
}

//For any given position in the grid, count how many neighbors it has and
//return that value.
int neighborCheck( char gridOne[][20], int x, int y )
{
    int checks = 0;

    if( gridOne[x-1][y-1] == ALIVE )
        checks++;
    else if( gridOne[x][y-1] == ALIVE )
        checks++;
    else if( gridOne[x+1][y-1] == ALIVE )
        checks++;
    else if( gridOne[x-1][y] == ALIVE )
        checks++;
    else if( gridOne[x+1][y] == ALIVE )
        checks++;
    else if( gridOne[x-1][y+1] == ALIVE )
        checks++;
    else if( gridOne[x][y+1] == ALIVE )
        checks++;
    else if( gridOne[x+1][y+1] == ALIVE )
        checks++;

    return checks;
}

//Switch the information in the first board with the second board
void boardSwitch( char gridOne[][20], char gridTwo[][20] )
{
    for( int i = 0; i < 20; i++ )
    {
        for( int j = 0; j < 70; j++ )
        {
            gridOne[i][j] = gridTwo[i][j];
        }
    }
}

//Timer
void wait( int ticks )
{
    clock_t endwait;
    endwait = clock () + ticks * CLK_TCK ;
    while (clock() < endwait) {}
}

//Print function
void printGrid( char grid[][20] )
{
    for( int i = 0; i < 20; i++ )
    {
        for( int j = 0; j < 70; j++ )
        {
            cout << grid[j][i];
        }
        cout << endl;
    }
}

I have been looking at my loop structure and functions for a while now but cant seem to find the problem. I need another set of eyes to look it oveer and see if they can't point out the problem to me. More than likely it is just a simple programming error but any help would be appreciated.

You didn't explain the problem so what can we say? We have no idea what we're looking for.

I can not imagine how your neighbour check function would work at sides of board where there is not all neighbours available.

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.