Conways 生命游戏麻烦 [java]

Conways Game of Life trouble [java]

public static boolean[][] calculate(boolean cellState[][])//imports cell patern(in the form of a graph)
{
    int x = 0, y = 0;
    int alive = 0;
    boolean newCellState[][] = new boolean[50][50];//what the next generation is stored in

    while(x < 50 && y < 50)//calculation loop
    {
        try//each adjacent cell
        {
            if(cellState[x-1][y] == true)
                alive++; //if the  adjacent cell is alive the alive variable gets + 1
        }catch(IndexOutOfBoundsException e) {}

        try
        {
            if (cellState[x-1][y-1] == true)
                alive++;
        } catch(IndexOutOfBoundsException e) { }

        try
        {
            if(cellState[x-1][y+1] == true)
                alive++;
        } catch(IndexOutOfBoundsException e) { }

        try
        {
            if (cellState[x][y+1] == true)
                alive++;
        } catch(IndexOutOfBoundsException e) { }

        try
        {
            if (cellState[x][y-1] == true)
                alive++;
        } catch(IndexOutOfBoundsException e) { }

        try
        {
            if(cellState[x+1][y] == true)
                alive++;
        } catch(IndexOutOfBoundsException e) { }

        try
        {
            if(cellState[x+1][y-1] == true)
                alive++;
        } catch(IndexOutOfBoundsException e) { }

        try
        {
            if(cellState[x+1][y+1] == true)
                alive++;
        } catch(IndexOutOfBoundsException e) { }

        if(cellState[x][y] = true) //determines if the cell should be alive in the next generation
        {
            if(alive < 2)
                newCellState[x][y] = false;
            else if(alive > 3)
                newCellState[x][y] = false;
            else if(alive == 2)
                newCellState[x][y] = true;//stores in new cell state for next gen
            else
                newCellState[x][y] = true;
        } else
        {
            newCellState[x][y] = false;
        }

        alive = 0; //resets alive

        if(x == 49)//counter for graph
        {
            y++;
            x=0;
        }else
        {
            x++;
        }
    }

    return newCellState;
}

这是我用来计算下一代康威生命游戏的方法。每次都是 运行,不管模式是什么,它都会打印出除网格边缘之外的每个单元格都死了。

这是主要方法:

public static void main(String Args[])
{
    int x = 0;
    int y = 0;
    boolean cellState[][] = new boolean[50][50];
    String answer;
    Scanner input = new Scanner(System.in);

    while(true)
    {
        System.out.print("\n Type anything for next generation, 'new' for new grid, or 'stop' to end>> ");
        answer = input.nextLine();
        if(answer.equals("new"))
        {
            cellState = newCells();
        }else if(answer.equals("stop"))
        {
            break;
        }else
        {
            cellState = calculate(cellState);
            print(cellState);
        }

    }


}

打印方式我测试过,没有问题。我只是不明白为什么要这样做。感谢所有帮助!谢谢!

使用 == 进行测试而不是 =

= 在 if 条件下将无法达到您的目的。

if(cellState[x][y] = true) 

您忘记为这个使用 ==。否则,您的代码看起来不错。