Java : GameOfLife : 我如何计算周围所有的活细胞?

Java : GameOfLife : How could I count all the alive cells around?

我正在尝试在 java 中创建一个家庭作业游戏。

首先,我必须从接口 class 创建一个数组。

然后,我用活细胞和空细胞填充这个数组,例如:

public World(int n, int p) {

    numberOfRow = n;
    numberOfCol = p;

    myWorld = new Cell[numberOfRow][numberOfCol];
    AliveCell cellNew = new AliveCell(1, "X");
    DeadCell cellDead = new DeadCell(0,"O");
    EmptyCell emptyCell = new EmptyCell("");
    int gameTurn = 0;
    int countAgeCell = 0;
    boolean isOccupied = false;
    Random numberRand = new Random();
    Random numberXRand = new Random();
    Random numberYRand = new Random();
    numberRandomCellAlive = numberRand.nextInt((2500 - 1 + 1) +1);
    compteurCellAliveGenerated = 0;

for(int i = 0; i < this.myWorld.length; i++) {
        for(int j = 0; j < this.myWorld.length; j ++) {
            myWorld[i][j] = emptyCell;
        }
    }

for(int i =0; i < this.myWorld.length -1; i ++) {
        for(int j = 0; j < this.myWorld.length -1; j++) {
            if(compteurCellAliveGenerated < numberRandomCellAlive) {

            int posXRandomCellAlive = numberXRand.nextInt((50 - 1));
            int posYRandomCellAlive = numberYRand.nextInt((50 - 1));

            myWorld[posXRandomCellAlive][posYRandomCellAlive] = cellNew;

            isOccupied = true;
            compteurCellAliveGenerated++;
        }

在这里,我可以打印我的数组,一切正常。我的意思是,数组中充满了随机位置为 array[i][j].

的 aliveCells

然后,我制作了这个函数来计算一个细胞周围的每个活细胞。

public void afficherUpdate(){

        AliveCell cellNew = new AliveCell(1,"X");
        AliveCell cellBorn = new AliveCell(1, "B");
        EmptyCell emptyCell = new EmptyCell("");
        int count = 0;
    for(int i = 0; i < this.myWorld.length -1; i++) {
            for(int j = 0; j < this.myWorld.length -1; j++) {

                 if(this.myWorld[i][j] == cellNew && i > 0 && i < 50 && j > 0 && j < 50) {
                        if(this.myWorld[i][j] == cellNew){
                            count++;
                        }
                        if(this.myWorld[i][j-1] == cellNew) {
                            count++;
                        }
                        if(this.myWorld[i-1][j] == cellNew) {
                            count++;
                        }
                        if(this.myWorld[i-1][j+1] == cellNew) {
                            count++;
                        }
                        if(this.myWorld[i-1][j-1] == cellNew) {
                            count++;
                        }
                        if(this.myWorld[i+1][j+1] == cellNew) {
                            count++;
                        }
                        if(this.myWorld[i+1][j-1] == cellNew) {
                            count++;
                        }
                        if(this.myWorld[i+1][j] == cellNew) {
                            count++;
                        }
                 }
                        System.out.println(count);

                if(this.myWorld[i][j] == emptyCell && i > 0 && i < 50 && j > 0 && j < 50) {
                    if(this.myWorld[i][j+1] == cellNew){
                        count++;
                    }
                    if(this.myWorld[i][j-1] == cellNew) {
                        count++;
                    }
                    if(this.myWorld[i-1][j] == cellNew) {
                        count++;
                    }
                    if(this.myWorld[i-1][j+1] == cellNew) {
                        count++;
                    }
                    if(this.myWorld[i-1][j-1] == cellNew) {
                        count++;
                    }
                    if(this.myWorld[i+1][j+1] == cellNew) {
                        count++;
                    }
                    if(this.myWorld[i+1][j-1] == cellNew) {
                        count++;
                    }
                    if(this.myWorld[i+1][j] == cellNew) {
                        count++;
                    }
                    System.out.println(count);
                }
                        if(myWorld[i][j] == cellNew  && count == 2 || count ==  3){
                            this.myWorld[i][j] = cellBorn;      
                        }
                        if(myWorld[i][j] == emptyCell && count== 3) {
                        this.myWorld[i][j] = cellBorn;

                        }
                 }
            }

但是,计数变量returns只有0;它无法计算周围的活细胞。我真的不明白为什么这不起作用。

感谢您的回复。

您要检查的是 myWorld[i][j] instanceOf AliveCell

java 中的

== 正在检查对象是否 相同 实例不 相等