【问题标题】:Conway's Game of Life-Cells Dying When They're Not Supposed to? (Java)康威的生命细胞游戏在他们不应该死亡的情况下死亡? (爪哇)
【发布时间】:2015-08-08 23:08:46
【问题描述】:

我正在尝试编写康威生命游戏的一个非常基本的版本。我的程序几乎可以工作,但 1(代表活细胞)即使有两三个活的邻居,也会继续死亡。这很奇怪,因为闪光灯工作但当我尝试信标模式时它不再工作。

这是一个维基百科链接,其中包含我正在谈论的模式。 http://en.wikipedia.org/wiki/Conway's_Game_of_Life。

这是我的代码:

public class GameofLife {
public static void main(String[] args){
    int [][] array = new int [12][12];
    array[8][8]=1;
    array[8][9]=1;
    array[8][7]=1;
    int [][] end = new int [12][12];
    printArray(array);
    System.out.println("");
    System.out.println("");
    int a=0;
    while(a<30){
    for(int i=1; i<=10; i++)
    {
        for(int j=1; j<=10; j++)
        {
            int counter = surround(array,i,j);
            if(array[i][j]==1 && counter<2)
            {
                end[i][j]=0;
            }
            if(array[i][j]==1 && counter==2)
            {
                array[i][j]=1;
            }
            if(array[i][j]==1 && counter>4)
            {
                end[i][j]=0;
            }
            if(array[i][j]==0 && counter==3)
            {
                end[i][j]=1;
            }
            if(array[i][j]==1 && counter==3)
            {
                end[i][j]=1;
            }
        }
    }
    printArray(end);    
    a++;
    for(int i=0; i<12; i++)
    {
        for(int j=0; j<12; j++)
        {
            array[i][j]=end[i][j];
            end[i][j]=0;
        }
    }
    System.out.println("");
    System.out.println("");
    }

}

    public static int surround(int[][] initial, int i, int j){
    int[][] surrounding = {{initial[i-1][j-1],initial[i-1][j],initial[i-1][j+1]},
            {initial[i][j-1],initial[i][j],initial[i][j+1]},
            {initial[i+1][j-1],initial[i+1][j],initial[i+1][j+1]}};
    int counter = 0;
    for(int a=0; a<=2; a++)
    {
        for(int b=0; b<=2; b++)
        {
            if(surrounding[a][b]==1)
            {
                counter ++;
            }
        }
    }
    return counter;
}
public static void printArray(int[][] input)
{
    for(int x =0; x<input.length; x++)
    {
        for(int y=0; y< input[0].length; y++)
        {
            System.out.print(input[x][y]);
        }
        System.out.println("");
    }
}

}

感谢您的帮助!

【问题讨论】:

  • 这看起来不对:if(array[i][j]==1 &amp;&amp; counter==2) { array[i][j]=1; } - 你不应该像其他检查一样更新end 数组吗?
  • 非常感谢!我一直在寻找我的错误一段时间
  • 有什么奖励或我可以在这个网站上给你的东西
  • 您可以通过点击旁边的勾号将魏子尧的答案标记为正确。我懒得发布答案,所以我什么也没得到 - 但你的感谢是很多的:)

标签: java algorithm debugging if-statement multidimensional-array


【解决方案1】:

这里的逻辑:

for(int a=0; a<=2; a++)
{
    for(int b=0; b<=2; b++)
    {
        if(surrounding[a][b]==1)
        {
            counter ++;
        }
    }
}

有点错误——你不需要计算单元格本身,因此a == 1 &amp;&amp; b == 1时需要跳过。

...当然,@Blorgbeard 评论了什么 :-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多