0

我不确定我是否以正确的方式进行此操作,我创建了适用于所有“静物”单元格排列的代码,即。蜂巢、块、船和面包。但是,对于应该被操纵并涉及细胞死亡和出生的安排,我遇到了问题。

现在我首先创建单元格,然后杀死任何符合规则的单元格(如果周围的邻居总数小于或等于 1,则等于或大于 4)。

我现在要做的事情,并没有按照它应该的方式工作。我将在我的代码下方包含一些图像:

public class Untitled {
    public static void main(String[] params){
        Life life = new Life();
        String[][] list = life.readGame();

        life.print(list);
        System.out.println();

        for(int i = 1; i <= 5; i++){
            list = life.run(list);
            life.print(list);
            System.out.println();
        }
    }
}

class Life {
    public String[][] readGame(){
        String[][] array = {
                { "-","-", "-", "-", "-", "-","-"},
                { "-","-", "-", "-", "-", "-","-"},
                { "-","-", "-", "x", "-", "-","-"},
                { "-","-", "-", "x", "-", "-","-"},
                { "-","-", "-", "x", "-", "-","-"},
                { "-","-", "-", "-", "-", "-","-"},
                { "-","-", "-", "-", "-", "-","-"}
        };
        return array;
    }

    public void print(String[][] array){
        for(int x = 0; x < array.length; x++){
            for(int y = 0; y < array[x].length; y++){
                System.out.print(array[x][y]);
            }
            System.out.println();
        }
    }

    public String[][] run(String[][] array){
        populateCells(array);
        System.out.println("Populated:\n");
        print(array);
        killCells(array);
        System.out.println("Killed:\n");
        print(array);
        return array;
    }

    public int amountOfNeighbors(String[][] array, int x, int y){
        int amount = 0;

        for(int x1 = -1; x1 <= 1; x1++){
            if(x + x1 >= 0 && x + x1 < array.length){
                for(int y1 = -1; y1 <= 1; y1++){
                    if(y + y1 >= 0 && y + y1 < array[x1 + x].length){
                        if(!(x1 == 0 && y1 == 0) && !array[x1 + x][y1 + y].equals("-")){ 
                            amount++;
                        }
                    }
                }
            }
        }
        return amount;
    }

    public void populateCells(String[][] array){
        int neighbours;

        for(int x = 0; x < array.length; x++){
            for(int y = 0; y < array[x].length; y++){
                neighbours = amountOfNeighbors(array,x,y);

                if(neighbours == 3){
                    array[x][y] = "x";
                }
            }
        }
    }

    public void killCells(String[][] array){
        int neighbours;

        for(int x = 0; x < array.length; x++){
            for(int y = 0; y < array[x].length; y++){
                neighbours = amountOfNeighbors(array,x,y);

                if(neighbours <= 1 || neighbours >= 4){
                    array[x][y] = "-";
                }
            }
        }
    }
}

至于图像,这是一个简单的闪烁结构,以及在填充和删除细胞后会发生什么:

1

2

3

4

1 回答 1

4

每次调用时,run()您都需要创建原始电路板的副本,并在修改原始电路板中的单元格时使用该副本检查每个单元格的状态。

否则,每次更新单元格时,您都会无意中更改其周围单元格的状态。

这是涉及游戏板(如 Game of Life)的模拟中相当常见的错误 - 所以现在您知道要提防它 :)

于 2013-02-20T21:27:45.397 回答