1

我正在为学校写康威的人生游戏。在程序中,我遇到了数组获取我分配给它们的值的问题。在程序的某一时刻,他们打印出分配给他们的值 (1),但在程序结束时,当我需要打印数组以显示游戏的迭代时,它显示的数字非常低。另一个麻烦是我在输入一个循环时遇到了困难,该循环会询问它是否希望您运行另一个迭代。所以我删除了它,直到以前的错误得到修复。我用 C++ 写这个

#include <stdio.h> 

int main (void)
{
int currentarray [12][12];
int futurearray [12][12];

char c;
char check = 'y';
int neighbors = 0;

int x = 0; // row
int y = 0; //column

printf("Birth an organism will be born in each empty location that has exactly  three neighbors.\n");
printf("Death an organism with four or more organisms as neighbors will die from overcrowding.\n"); 
printf("An organism with fewer than two neighbors will die from loneliness.\n");
printf("Survival an organism with two or three neighbors will survive to the next generation.\n");
printf( "To create life input x, y coordinates.\n");

while ( check == 'y' )
{
    printf("Enter x coordinate.\n");
    scanf("%d", &x ); while((c = getchar()) != '\n' && c != EOF);
    printf("Enter y coordinate.\n");
    scanf("%d", &y ); while((c = getchar()) != '\n' && c != EOF);
    currentarray [x][y] = 1;
    printf ("%d\n", currentarray[x][y]);
    printf( "Do you wish to enter more input? y/n.\n");
    scanf("%c", &check); while((c = getchar()) != '\n' && c != EOF);
}

// Note - Need to add a printf statement showing the array before changes are made after input added.

// check for neighbors
while(check == 'y')
{
 for(y = 0; y <= 12; y++)
 {
     for(x = 0; x <= 12; x++)
     {
         //Begin counting number of neighbors:
         if(currentarray[x-1][y-1] == 1) neighbors += 1;
         if(currentarray[x-1][y] == 1) neighbors += 1;
         if(currentarray[x-1][y+1] == 1) neighbors += 1;
         if(currentarray[x][y-1] == 1) neighbors += 1;
         if(currentarray[x][y+1] == 1) neighbors += 1;
         if(currentarray[x+1][y-1] == 1) neighbors += 1;
         if(currentarray[x+1][y] == 1) neighbors += 1;
         if(currentarray[x+1][y+1] == 1) neighbors += 1;

         //Apply rules to the cell:
         if(currentarray[x][y] == 1 && neighbors < 2)
            futurearray[x][y] = 0;
         else if(currentarray[x][y] == 1 && neighbors > 3)
            futurearray[x][y] = 0;
         else if(currentarray[x][y] == 1 && (neighbors == 2 || neighbors == 3))
            futurearray[x][y] = 1;
         else if(currentarray[x][y] == 0 && neighbors == 3)
            futurearray[x][y] = 1;
     }
 }
}

// Set the current array to the future and change the future to 0

{
 for(y = 0; y < 12; y++)
 {
     for(x = 0; x < 12; x++)

     {
    //Begin the process
    currentarray [x][y] = futurearray [x][y];
    futurearray [x][y] = 0;
}
 }
}
{
 for(y = 0; y < 12; y++)
 {
     for(x = 0; x < 12; x++)

     {
//print the current life board
         printf("%d ", currentarray[x][y]);
}
}
}


// Have gone through one iteration of Life
//Ask to do another iteration
printf("Do you wish to continue y/n?\n");
scanf("%c", &check); while((c = getchar()) != '\n' && c != EOF);

return 0;
}
4

4 回答 4

3

您将数组定义为 [12][12]。

在您的生成循环中,您从 i = 0 走到 i <= 12,这是 13 步而不是数组的 12 步。此外,您正在尝试访问 x-1 和 y-1,它们可能低至 -1。再次不在您的阵列内。

有时您会从数组中获得半有用的值,但在某些边界上您只是在访问随机数据。

尝试纠正你的边界。

于 2010-01-16T23:12:16.020 回答
2

在计算它们之前,您忘记设置neighbors为 0。

由于这是 C++(不是 C),您不妨neighbors在循环体中声明。也使这些问题更容易被发现。

另外,是我,还是那个while循环永远不会完成?一般来说,你的牙套是一团糟,你的缩进也是如此。你可以通过清理这些来帮自己和我们一个忙。

于 2010-01-16T23:12:49.357 回答
1

显然同意以上所有建议。你可能想用 Life 实现的一个好技巧是在你的区域周围创建一个额外的边界。因此,如果用户想要一个 12x12 的网格(并且您应该允许指定宽度/高度并动态分配内存),那么您可以在内部保存一个 14x14 的网格,该网格对应于实际网格周围的边框。在运行计算之前,将顶行复制到下边框,将底行复制到顶边框等。现在您可以在内部 12x12 网格上运行主算法,而不必担心边缘情况。如果图案从边缘脱落,这将使您的图案重新出现在另一侧。

于 2013-01-08T14:19:39.953 回答
0

您还忘记将两个数组的值都设置为零。这将解决您遇到的荒谬数字问题。你可以通过复制这个 for 循环来做到这一点:

for(y = 0; y < 12; y++)
{
    for(x = 0; x < 12; x++)
    {
        //Begin the process
        currentarray [x][y] = futurearray [x][y];
        futurearray [x][y] = 0;
    }
}

并将其粘贴在 while 循环之前,但不是设置 currentarray[x][y] = futurearray[x][y],而是将其设置为 0。此外,如果坐标是可视位置而不是数组坐标,您将想改变这个:

printf ("%d\n", currentarray[x][y]);

对此:

printf ("%d\n", currentarray[x-1][y-1]);

我还建议在打印每一行后放置一个带有换行符 (\n) 的 printf,并在每个项目后放置一个制表符 (\t),以便格式看起来更清晰。

于 2010-01-17T00:09:05.007 回答