【问题标题】:Conways Game of Life trouble [java]康威人生游戏的麻烦 [java]
【发布时间】:2016-01-21 02:25:31
【问题描述】:
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);
        }

    }


}

我已经测试了打印方法,所以那里没有问题。我只是不明白为什么会这样。感谢所有帮助!谢谢!

【问题讨论】:

  • = 运算符测试相等性。

标签: java conways-game-of-life


【解决方案1】:

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

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

【讨论】:

  • 天哪,我怎么没看到!非常感谢!但问题仍然存在。
【解决方案2】:
if(cellState[x][y] = true) 

您忘记使用 == 进行此操作。否则,您的代码看起来不错。

【讨论】:

  • 把=改成==购买问题依旧
  • 请更新您的帖子以说明 ==。我看到你把它们都改成了 == 除了我指定的那个。
猜你喜欢
  • 2012-01-19
  • 1970-01-01
  • 1970-01-01
  • 2018-01-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-21
相关资源
最近更新 更多