生命游戏(错误) [英] Game of Life (Error)

查看:81
本文介绍了生命游戏(错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我刚刚写了一个生活游戏,我早些时候在书中读过.虽然,我非常仔细地尝试过,但是该程序中仍然留有一些错误,因此该错误不会超出初始阶段.这个小型玩具程序的作用将从main.cpp中清楚了解,或者在这里阅读有关生活游戏的信息.
http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life [

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

#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




life.h

#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




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;
}

推荐答案

您的问题在这里:

your problem is here:

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


life::initialize中.

要检查是否相等,您需要两个等号,如下所示:


in life::initialize.

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

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


这篇关于生命游戏(错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆