【问题标题】:Trouble with Game of Life and dynamic arrays, c++生命游戏和动态数组的问题,c++
【发布时间】:2013-02-15 07:58:02
【问题描述】:

我正在尝试编写人生游戏。当我尝试迭代几代时,我最终得到了垃圾。我已经尝试了很多不同的东西,但我没有看到我的错误,但显然有一个。任何帮助将不胜感激。当我尝试将游戏的逻辑应用于旧数组以创建新数组时,我几乎可以肯定我做错了什么,但我不确定是什么。这是四个功能之一,我已经测试了其他功能,我很肯定这个缺陷存在于这个功能中。最终目标是让游戏在每一代都根据需要扩展和缩小。

void iterateGeneration ()
{
ifstream fin;
fin.open("tmp.txt");
ofstream fout;

char **arr=new char * [ROWS];
for(int i=0; i<ROWS; i++)
    arr[i] = new char [COLUMNS];

int lifecheck=0;
//read in current generation from tmp.txt
for(int i=0; i<ROWS; i++)
    {
    fin.getline(arr[i], COLUMNS);
    }
fin.close();

char **new_arr=new char * [ROWS];
for(int i=0; i<ROWS; i++)
    new_arr[i]= new char [COLUMNS];

//count live neighbors, then determine if cell will be alive next generation
for(int i=0; i<ROWS; i++)
    {
    for(int j=0; j<COLUMNS-1; j++)
        {
        lifecheck=0;
        if((i==0 && j==0) || (i==0 && j==(COLUMNS-2)) || (i==(ROWS-1) && j==0) ||     (i==(ROWS-1) && j==(COLUMNS-2)))//corners always stay dead
            {
            lifecheck=0;
            }
        else if(i==0)//special check for first row, only three checks since neighbors     in first row is always dead
            {
            if(arr[i+1][j-1]=='1')
                lifecheck+=1;
            if(arr[i+1][j]=='1')
                lifecheck+=1;
            if(arr[i+1][j+1]=='1')
                lifecheck+=1;
            }
        else if(i==(ROWS-1))//special check for last row
            {
            if(arr[i-1][j-1]=='1')
                lifecheck+=1;
            if(arr[i-1][j]=='1')
                lifecheck+=1;
            if(arr[i-1][j+1]=='1')
                lifecheck+=1;
            }
        else if(j==0)//special check for first column
            {
            if(arr[i-1][j+1]=='1')
                lifecheck+=1;
            if(arr[i][j+1]=='1')
                lifecheck+=1;
            if(arr[i+1][j+1]=='1')
                lifecheck+=1;
            }
        else if(j==(COLUMNS-2))//special check for last column
            {
            if(arr[i-1][j-1]=='1')
                lifecheck+=1;
            if(arr[i][j-1]=='1')
                lifecheck+=1;
            if(arr[i+1][j-1]=='1')
                lifecheck+=1;
            }
        else
            {
            if(arr[i-1][j-1]=='1')
                lifecheck+=1;
            if(arr[i-1][j]=='1')
                lifecheck+=1;
            if(arr[i-1][j+1]=='1')
                lifecheck+=1;
            if(arr[i][j-1]=='1')
                lifecheck+=1;
            if(arr[i][j+1]=='1')
                lifecheck+=1;
            if(arr[i+1][j-1]=='1')
                lifecheck+=1;
            if(arr[i+1][j]=='1')
                lifecheck+=1;
            if(arr[i+1][j+1]=='1')
                lifecheck+=1;
            }
        if(arr[i][j]=='0')
            {
            if(lifecheck==3)
                new_arr[i][j]=='1';
            else
                new_arr[i][j]=='0';
            }
        else if(arr[i][j]=='1')
            {
            if(lifecheck==2)
                new_arr[i][j]=='1';
            else if(lifecheck==3)
                new_arr[i][j]=='1';
            else
                new_arr[i][j]=='0';
            }

        else
            new_arr[i][j]=='0';
        }//2nd for
    }//1st for

fout.open("tmp.txt");

for(int i=0; i<ROWS; i++)
    {
    fout << new_arr[i];
    fout << endl;
    }

for(int p=0; p<ROWS; p++)
    {
    delete [] arr[p];
    delete [] new_arr[p];
    }
delete [] arr;
delete [] new_arr;
}


using namespace std;

const int NUM_GENERATIONS = 1; //set to a smaller number for debugging

int main() 
{
    populateWorld(FILE_NAME);

    showWorld();

    for (int iteration = 0; iteration < NUM_GENERATIONS; iteration++) 
    {

        if (WINDOWS)
           system("cls"); //Windows only
        else
           system("clear"); //Linux only

        iterateGeneration();

        showWorld();
    } 

    if (WINDOWS)
        system("PAUSE");

    return 0;
}

0000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000010000000000000000000000000000000000000000000
0000000000000000000000000000001010000000000000000000000000000000000000000000
0000000000000000000011000000110000000000001100000000000000000000000000000000
0000000000000000000100010000110000000000001100000000000000000000000110000000
0000000011000000001000001000110000000000000000000000000000000000000110000000
0000000011000000001000101100001010000000000000000000000000000000000000000000
0000000000000000001000001000000010000000000000000000000000000000000000000000
0000000000000000000100010000000000000000000000000000000000000000000000000000
0000000000000000000011000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000001110000000
0000000000000000000000000000000000000000000000000000000000000000010001000000
0000000000000000000000000000000000000000000000000000000000000000100000100000
0000000000000000000000000000000000000000000000000000000000000000100000100000
0000000000000000000000000000000000000000000000000000000000000000000100000000
0000000000000000000000000000000000000000000000000000000000000000010001000000
0000000000000000000000000000000000000000000000000000000000000000001110000000
0000011000000000000000111000000000000000000000000000000000000000000100000000
0000011000000000000000101000000000000000000000000000000000000000000000000000
0000000000000000000000110000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000011100000
0000000000000000000000000000000000000000000000000000000000000000000011100000
0000000000000000000000000000000000000000000000000000000000000000000100010000
0000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000001100011000
0000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000
0001100011000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000
0000100010000000000000000000000000000000000000000000000000000000000000000000
0000011100000000000000000000000000000000000000000000000000000000000000000000
0000011100000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000110000000000000000000000
0000000000000000000000000000000000000000000000000001010000000000000001100000
0000000010000000000000000000000000000000000000000001110000000000000001100000
0000000111000000000000000000000000000000000000000000000000000000000000000000
0000001000100000000000000000000000000000000000000000000000000000000000000000
0000000010000000000000000000000000000000000000000000000000000000000000000000
0000010000010000000000000000000000000000000000000000000000000000000000000000
0000010000010000000000000000000000000000000000000000000000000000000000000000
0000001000100000000000000000000000000000000000000000000000000000000000000000
0000000111000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000001100000000000000000000
0000000000000000000000000000000000000000000000000000100010000000000000000000
0000000000000000000000000000000000000000000100000001000001000000001100000000
0000000000000000000000000000000000000000000101000011010001000000001100000000
0000000110000000000000000000000011000000000000110001000001000000000000000000
0000000110000000000000000000000011000000000000110000100010000000000000000000
0000000000000000000000000000000000000000000000110000001100000000000000000000
0000000000000000000000000000000000000000000101000000000000000000000000000000
0000000000000000000000000000000000000000000100000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000

x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00`ó!#\00\00 \00\00
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000000000
00000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000
00000000000000000000000000000010
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000110000001100000000000011000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000001100000000000011000000000000000000000001
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000000110000000
00000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000
00000000000000000010000010
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000001000100000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000110000000000000000000000000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000000000000000000000
00
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000
00000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000
00000000000000000000000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000100010000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000100000100000
00000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000100000
00000000000000000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000011
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000000100000000
00000110
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000
00000000000000000000001100
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000000000000100010000
00
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000
00000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\001000
00000000000000000000000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000000000
00000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000
00001000100000000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0011000000000000000000000000000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000110000000000000000000000
00000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000001100000
00000000100000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000001110000000000000000000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000000000000000000000
00
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000
00000100000100000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000
00000010001000000000000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000000000
00000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000
00000000000000000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000001000100000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000100000001000001000000001100000000
00000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000001100000000
00000001100000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000001100000000000000000000000110000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000001100000011000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000101000000000000000000000000000000
00
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000
00000000000000000000
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000

【问题讨论】:

  • 请定义“垃圾”。您还使用了一种非常不正统的缩进样式。
  • 为什么每个人都坚持通过手动管理记忆来让生活变得更加困难?
  • "Garbage" xã_ xã_ xã_ 这就是我在复制后得到的输出。很抱歉出现奇怪的缩进。
  • 请提供您的 main()
  • 请提供tmp.txt

标签: c++ arrays memory dynamic conways-game-of-life


【解决方案1】:

这里是你保存new_arr的地方:

for(int i=0; i<ROWS; i++)
    {
    fout << new_arr[i];
    fout << endl;
    }

这里,特别是,是错误:

    fout << new_arr[i];

char * 发送到ostream 将发送连续字符,直到找到“空终止符”'\0' 字符。

您的代码从不设置空终止符,因此很难判断写入了多少。根据你的垃圾结果,太多了。

【讨论】:

  • 我会尝试逐个字符输出并告诉你
  • @user1768079 应该可以完成这项工作,因为您已经知道要输出多少个字符。
  • 所发生的一切都是写正确的行数,只是它们的内容很糟糕。输入文件有 61 行,输出文件有 61 行。输出“tmp.txt”应该是0和1,就像输入文件一样,只是根据游戏规则改变。
  • 现在有点用了。我得到一些1和0。我将 tmp.txt 复制到 OP 最底部的输入文件下方。
  • 我唯一能想到的是,有时它会将数组留空,因为 if 语句中的逻辑不好,所以当它工作时,我会得到我想要的应该,但剩下的时间我会得到内存中已经存在的东西。
【解决方案2】:

您应该显式输出数组的每个字符,而不是整个字符数组。

我相信 fprintf 会像你想要的那样输出一个数组,但是自从我使用 c++ 以来已经很长时间了。可能不是因为在高中写了一个非常相似的程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-01
    相关资源
    最近更新 更多