生命问题的游戏

时间:2013-02-20 21:19:46

标签: java

我不确定我是否以正确的方式进行此操作,我已经创建了适用于所有“静物”单元格安排的代码,即。蜂巢,座,船和面包。然而,对于被认为是被操纵并涉及细胞死亡和分娩的安排,我有问题。

现在我首先创建单元格,然后杀掉任何符合规则的人(如果周围的邻居总数小于或等于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

1 个答案:

答案 0 :(得分:4)

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

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

这是一个相当常见的模拟错误,涉及像Game of Life这样的游戏板 - 所以现在你知道要注意它:)