Click here to Skip to main content
15,905,971 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I just wrote a game of life which I earlier read in some book. Although, I tried very carefully, some bug is still left within this program due to which it doesn't go beyond the initial stage. What this small toy program does will be clear from the main.cpp or else read about game of life here.
http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life[^]

main.cpp
C#
#include<iostream>
#include"life.h"
using namespace std;
void instructions();
bool user_says_yes();
using namespace std;
int main()
{
    life configuration;
    instructions();
    configuration.initialize();
    configuration.print();
    //cout<<"Continue viewing generation? "<<endl;
    //while(user_says_yes())
    //{
    //    configuration.update();
    //    configuration.print();
    //    cout<<"Continue viewing generations? "<<endl;
    //}
    return 0;
}
void instructions()
{
cout<<"Welcome to Conway's game of life"<<endl;
cout<<"The game uses a grid of size "<<maxrow<<" by "<<maxcol<<endl;
cout<<"in which each cell can be occupied by the organism or not"<<endl;
cout<<"The occupied cells chnage from generation to generation"<<endl;
cout<<"according to the number of neighbouring cells which are alive."<<endl;
}
bool user_says_yes()
{
    int c;
    bool initial_response=true;
    do
    { //Loop until an appropriate input is received
        if(initial_response)
        {
            cout<<"(y/n?)"<<flush;
        }
        else
        {
            cout<<"Respsond with either y or n: "<<flush;
        }
           do
            { //Ignore white space
            c = cin.get();
            //cout<<c;
            }while(c=='\n'|| c==' '|| c=='\t');
            initial_response=false;
     }while(c!='y'&&c!='Y'&&c!='n'&&c!='N');
     return (c=='y'||c=='Y');
}



life.h
C#
#include<iostream>
const int maxrow =20, maxcol =60; //Grid dimensions
class life
{
    public:
    void initialize();
    void print();
    void update();
    private:
    int grid[maxrow + 2][maxcol+2];
    int neighbour_count(int row, int col);
};



life.cpp
#include"life.h"
#include<iostream>
using namespace std;

void life::initialize()
{
    int row, col;
    for(row=0;row=maxrow+1;row++)
        {
        for(col=0;col=maxcol+1;col++)
            {
            grid[row][col]=0;
            }
        }
    cout<<"List the coordinates of the living cell."<<endl;
    cout<<"Terminate the list with a special pair -1 -1"<<endl;
    cin>>row>>col;
    while(row!=-1 || col!=-1)
    {
        if(row>=1 && row<=maxrow)
        if(col>=1 && col<=maxcol)
            grid[row][col]=1;
        else
            cout<<"Column "<<col<<" is out of range";
        else
        cout<<"Row "<<row<<" is out of range";
        cin>>row>>col;
    }
}
void life::print()
{
    int i,j;
    cout<<"The current life configuration is: "<<endl;
    for(i=1;i<=maxrow;i++)
    {
        for(j=1;j<=maxcol;j++)
        {
            if(grid[i][j]==1)
            {
                cout<<"*";
            }
            else
            {
                cout<<" ";
                //cout<<endl pending here
            }
        }
        cout<<endl;
    }
}
void life::update()
{
    /*Pre: The life object contains a configuration*/
    /*Post: The updated configuration is now ready*/
    int i,j;
    int new_grid[maxrow+2][maxrow+2];

    for(i=1;i<=maxrow;i++)
    for(j=1;j<=maxcol;j++)
    switch(neighbour_count(i, j)){
    case 2:
    new_grid[i][j]=grid[i][j];
    break;
    case 3:
    new_grid[i][j]=1;
    break;
    default:
    new_grid[i][j]=0;
    }

    for(i=1;i<=maxrow;i++)
    {
        for(j=1;j<=maxcol;j++)
        {
            grid[i][j]= new_grid[i][j];
        }
    }

}
int life::neighbour_count(int row, int col)
{
    /*Pre: The life configuration exiss and the row and column of the living cell are passed to the function*/
    /*Post: The number of living neigbours of the cells is returned*/
    int count=0, i, j;
    for(i=row-1;i=row+1;i++)
    {
        for(j=col-1;j<=col+1;j++)
        {
        count += grid[i][j]; //Increase the count if the neighbour is alive
        }
    }
    count -= grid[row][col]; //Reduce the count by 1 since the cell is not a neighbour of itself
    return count;
}
Posted
Comments
William Winner 22-Dec-10 14:06pm    
the chances of someone reading through your code to find out where you've gone wrong are pretty slim. You need to step through your code and figure out exactly where it goes wrong. Saying "it doesn't go beyond the initial stage" doesn't tell us anything. What is the initial stage? How far does it get? Does it give you an error message?
johannesnestler 22-Dec-10 14:07pm    
what is the bug? you uncommented the configuration.update(); part. the bug happens there?
optimus_prime1 22-Dec-10 14:20pm    
It doesn't even take the input at the beginning and neither give any error message.
I uncommented a part in the main to see the working of only configuration::initialize(). But no input is accepted and no output is given.
If required, here's the downloadable project. (271 kB) http://cid-7cfad5624f27df6f.office.live.com/self.aspx/.Public/GameofLife.zip

1 solution

your problem is here:

C#
for(row=0;row=maxrow+1;row++)
{
   for(col=0;col=maxcol+1;col++)
   {
       grid[row][col]=0;
   }
}

in life::initialize.

to check for equality, you need two equal signs as in:

C#
for(row=0;row==maxrow+1;row++)
{
   for(col=0;col==maxcol+1;col++)
   {
       grid[row][col]=0;
   }
}
 
Share this answer
 
Comments
optimus_prime1 22-Dec-10 14:46pm    
:O
Does that mean
for(i=0;i=5;i++) - Invalid
for(i=0;i==5;i++) - Invalid
for(i=0;i<=5;i++) - Valid
William Winner 22-Dec-10 14:49pm    
no, the second statement is valid.
optimus_prime1 24-Dec-10 4:33am    
But
for(int i=0;i==5;i++)
{
cout<<i;
}
doesn't give me anything in the output.
William Winner 24-Dec-10 13:18pm    
sorry...went a bit quickly with this last time. The for loop's second part is the statement while statement. So, while that part is true, the for loop will continue, so you really want
for(i=0;i<=5;i++)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900