Java:生命游戏 - 与邻居的问题

时间:2014-04-01 12:53:32

标签: java

我是这个论坛的新手所以我希望我能以正确的方式发布这个问题 - 否则,请告诉我。

我正在尝试用Java编写一个简单的生命游戏动画的代码,而且大部分代码似乎按预期工作。 然而,如果一个邻居“规则”中的一个“规则”被认为是“杀死”一个单元,如果它有少于2个邻居,这意味着单元可以有一个或没有邻居并且仍然存活 - 这可以在这里看到:{ {3}}。所有其他规则似乎都运行良好。

任何人都可以帮我弄清楚为什么它会忽略规则(活细胞等于'1',死为'0'。)

if (this.state[i][j] == 1 &&
    liveNeighbours(i, j) > 3 || liveNeighbours(i, j) < 2) {
                    this.tempState[i][j] = 0;

提前感谢!

代码如下:


import java.util.*;

public class GameOfLife {
    private int[][] state;
    private int[][] tempState;

    //Constructor that creates n x n grid of randomly placed live cells
    public GameOfLife(int n) {
        this.state = new int[n+2][n+2];
        this.tempState = new int[n+2][n+2];
        createRandomBoard(n);
    }

    //Sets up the initial state
    public void createRandomBoard(int n) {
        //Creates (n+2) x (n+2) grid of dead cells
        for (int i = 0; i < n+2; i++) {
            for (int j = 0; j < n+2; j++) {
                this.state[i][j] = 0;
            }
        }
        //Creates n x n grid of randomly placed live cells. Live cells are equal to '1', dead to '0'.
        for (int i = 1; i < n + 1; i++) {
            for (int j = 1; j < n + 1; j++) {
                int a = (int)Math.round(Math.random());
                this.state[i][j] = a;
            }
        }
        drawBoard();
    }

    //Draws the live cells as dots based on current state
    public void drawBoard() {
        StdDraw.show(50);
        StdDraw.clear();
        for (int i = 1; i < this.state.length-2; i++) {
            for (int j = 1; j < this.state.length-2; j++) {
                StdDraw.setXscale(1,this.state.length-3);
                StdDraw.setYscale(1,this.state.length-3);
                StdDraw.setPenRadius((double)1/this.state.length);
                if (this.state[i][j] == 1) {
                    StdDraw.point(i, j);
                }
            }
        }
        StdDraw.show(50);
    }

    //Determines which cells live or die in the next state
    public void nextState() {

        //Copies the state array to a temporary state(array) during killing/reviving
        for (int i = 1; i < this.state.length-2; i++) {
            for (int j = 1; j < this.state.length-2; j++) {
                this.tempState[i][j] = this.state[i][j];
            }
        }

        //Kills cells with more than 3 or less than 2 neighbours and revives dead cells with 3 neighbours
        for (int i = 1; i < this.state.length-2; i++) {
            for (int j = 1; j < this.state.length-2; j++) {
                if (this.state[i][j] == 1 && liveNeighbours(i, j) > 3 || liveNeighbours(i, j) < 2) {
                    this.tempState[i][j] = 0;
                }
                else if (this.state[i][j] == 0 && liveNeighbours(i, j) == 3) {
                    this.tempState[i][j] = 1;
                }
            }
        }

        //Copies the modified temporary state array to the original state(array) again
        for (int i = 1; i < this.state.length-2; i++) {
            for (int j = 1; j < this.state.length-2; j++) {
                this.state[i][j] = this.tempState[i][j];
            }
        }

        drawBoard();
    }

    //Counts the number of live neighbours to a cell
    private int liveNeighbours(int x, int y) {
        int numLiveNeighbours = 0;
        for (int i = x-1; i < x+2; i++) {
            for (int j = y-1; j < y+2; j++) {
                if (this.state[i][j] == 1) {
                    numLiveNeighbours++;
                }
            }
        }
        numLiveNeighbours --;
        return numLiveNeighbours;
    }



    public String toString() {
        return Arrays.deepToString(this.state);
    }

    public int[][] getState() {
        return this.state;
    }

}

1 个答案:

答案 0 :(得分:4)

您的情况似乎没有表达您想要的内容。您需要将其更改为:

if (this.state[i][j] == 1 && (liveNeighbours(i, j) > 3 || liveNeighbours(i, j) < 2))

检查operators precedence rules in Java。你正在杀死那些活着的细胞,并且有超过3个邻居或那些细胞少于两个。

通过这种改变,你将杀死那些活着的细胞,并且有超过3个或少于2个邻居。

我认为你的liveNeighbours也有一点缺陷:你在计算(x,y)邻居和(x,y)本身。您应该将其更改为:

private int liveNeighbours(int x, int y) {
        int numLiveNeighbours = 0;
        for (int i = x-1; i < x+2; i++) {
            for (int j = y-1; j < y+2; j++) {
                if (this.state[i][j] == 1 && (x != i || j != y)) {
                    numLiveNeighbours++;
                }
            }
        }
        return numLiveNeighbours;
    }