【问题标题】:The game of life(Conway's game) - how to check for cell neighbours生命游戏(Conway's game)——如何检查小区邻居
【发布时间】:2011-12-03 00:13:28
【问题描述】:

大家好,我正在尝试计算二维数组中包含的对角线相邻单元格的数量。之后,我将运行使用生命游戏规则的程序,将填充我的新网格。但是我被 indexOutOfBoundsException 困住了,我无法弄清楚我在哪里做错了,我希望有人能帮助我,这里是代码:

import java.util.Scanner;
import java.io.*;

class LifeGrid
{
    private int[][] grid;
    private int generation;

    public LifeGrid(int x, int y, String filename) throws FileNotFoundException 
    { 
        grid = new int[x][y];
        int j = 0;

        Scanner scanner = new Scanner(new File(filename));

        while(scanner.hasNextLine() && j < x)
        {
            String line = scanner.nextLine();
            for(int i=0; i<line.length() && i<y; i++)
            {
                if(line.charAt(i) == '*')
                    grid[j][i] = 1;
                else
                    grid[j][i] = 0;
            }
            j++;
        }
        scanner.close();
    }

    public void show()
    {
        for(int i=0; i<grid.length; i++)
        {
            for(int j=0; j<grid[i].length; j++)
            {
                if(grid[i][j] == 1)
                    System.out.print("*");
                else
                    System.out.print(" "); 
            }
            System.out.println();
        }
        System.out.println("Generation:" + generation);
    }

    //Getter methods

    public int getWidth()             { return grid[0].length;  }
    public int getHeight()            { return grid.length;     }
    public int getGeneration()        { return this.generation; }
    public int getCell(int x, int y)  { return grid[x][y];      }


    public static void main(String[] args)throws FileNotFoundException 
    {
        LifeGrid life = new LifeGrid(6, 10, args[0]);
        life.run(); 
    }

    //Check neighbours

    public int neighbours(int x, int y)
    {
        int neighbours = 0;

        if(x >= 1 && y >= 1 && x < getHeight() && y < getWidth())
        {
            if(grid[x][y++] == 1)       {neighbours++;} 
            if(grid[x][y--] == 1)       {neighbours++;}
            if(grid[x++][y] == 1)       {neighbours++;}
            if(grid[x++][y++] == 1)     {neighbours++;} 
            if(grid[x++][y--] == 1)     {neighbours++;}
            if(grid[x--][y--] == 1)     {neighbours++;}
            if(grid[x--][y++] == 1)     {neighbours++;}
        }
        else if(x == 0 && y == 0)
        {
            if(grid[x][y++] == 1)       {neighbours++;} 
            if(grid[x++][y] == 1)       {neighbours++;}
            if(grid[x++][y++] == 1)     {neighbours++;}
        }
        else if(x == 0 && y >= 1 && y < getWidth() && x < getHeight())
        {
            if(grid[x][y++] == 1)       {neighbours++;} 
            if(grid[x][y--] == 1)       {neighbours++;}
            if(grid[x++][y] == 1)       {neighbours++;}
            if(grid[x++][y++] == 1)     {neighbours++;} 
            if(grid[x++][y--] == 1)     {neighbours++;}
        }
        else if(x >= 1 && x < getHeight() && y == 0 && y < getWidth())
        {
            if(grid[x][y++] == 1)       {neighbours++;} 
            if(grid[x++][y] == 1)       {neighbours++;}
            if(grid[x++][y++] == 1)     {neighbours++;} 
            if(grid[x--][y++] == 1)     {neighbours++;}
        }
        else if(x == getHeight() && y >= 1 && y < getWidth())
        {
            if(grid[x][y++] == 1)       {neighbours++;} 
            if(grid[x][y--] == 1)       {neighbours++;}
            if(grid[x--][y--] == 1)     {neighbours++;}
            if(grid[x--][y++] == 1)     {neighbours++;}
        }
        else if(x >=1 && x < getHeight() && y == getWidth())
        {
            if(grid[x][y--] == 1)       {neighbours++;}
            if(grid[x++][y] == 1)       {neighbours++;}
            if(grid[x++][y--] == 1)     {neighbours++;}
            if(grid[x--][y--] == 1)     {neighbours++;}
        }
        else if(x == 0 && y == getWidth())
        {   
            if(grid[x][y--] == 1)       {neighbours++;}
            if(grid[x++][y] == 1)       {neighbours++;}
            if(grid[x++][y--] == 1)     {neighbours++;}
        }
        else if(x == getHeight() && y == 0)
        {
            if(grid[x--][y] == 1)       {neighbours++;}
            if(grid[x][++y] == 1)       {neighbours++;}
            if(grid[x--][y++] == 1)     {neighbours++;}
        }
        else if(x == getHeight() && y == getWidth())
        {
            if(grid[x][y--] == 1)       {neighbours++;}
            if(grid[x--][y] == 1)       {neighbours++;}
            if(grid[x--][y--] == 1)     {neighbours++;}
        }
        return neighbours;
    }

    public void run()
    {
        int[][] newGrid = grid;
        int[][] swapGrid = grid;;
        for(int i=0; i<grid.length; i++)
        {
            for(int j=0; j<grid[i].length; j++)
            {
                if(grid[i][j] == 1)
                {
                    if(neighbours(i,j) < 2)     generation = 0;
                    if(neighbours(i,j) > 3)     generation = 0;
                    if(neighbours(i,j) == 2)    generation = 1;
                }
                if(neighbours(i,j) == 3)        generation = 1;
                if(generation == 1)
                {
                    swapGrid[i][j] = 1;
                    newGrid = swapGrid;     
                }
                else
                {
                    swapGrid[i][j] = 0;
                    newGrid = swapGrid;
                }
            }
        }
        grid = newGrid;
        show();
    }
}       

异常详情:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at   
LifeGrid.neighbours(LifeGrid.java:87) at LifeGrid.run(LifeGrid.java:150) at 
LifeGrid.main(LifeGrid.java:59)

感谢你们的即时回答,现在代码可以正常工作,我可以看到我的输出了。但是我注意到我在 run() 方法中的算法是完全错误的,因为我从生命游戏的规则中得到了不同的输出。 规则:

对于“填充”的空间:

每个有邻居或没有邻居的细胞都会死去,就像孤独一样。 每个有四个或更多邻居的细胞都会死亡,就像人口过剩一样。 每个有两个或三个邻居的细胞都存活下来。 对于“空”或“未填充”的空间:

每个具有三个邻居的单元格都会被填充。

程序使用的文件设计如下:

                                          * * *

因此遵循我应该作为输出的规则:

                                          *
                                          *
                                          *

这是我的代码:

  import java.util.Scanner;
  import java.io.*;

  class LifeGrid
  {
    private int[][] grid;
    private int generation;

    public LifeGrid(int x, int y, String filename) throws FileNotFoundException 
    {  
        grid = new int[x][y];
        int j = 0;

        Scanner scanner = new Scanner(new File(filename));

        while(scanner.hasNextLine() && j < x)
        {
                String line = scanner.nextLine();

            for(int i=0; i<line.length() && i<y; i++)
            {
                if(line.charAt(i) == '*')
                    grid[j][i] = 1;
                else
                    grid[j][i] = 0;
            }
            j++;
        }
        scanner.close();
    }

    public void show()
    {
        for(int i=0; i<grid.length; i++)
        {
            for(int j=0; j<grid[i].length; j++)
            {
                if(grid[i][j] == 1)
                    System.out.print("*");
                else
                    System.out.print(" "); 
            }
            System.out.println();
        }
        System.out.println("Generation:" + generation);
    }

//Getter methods

    public int getWidth()             { return grid[0].length;  }
    public int getHeight()            { return grid.length;     }
    public int getGeneration()        { return this.generation; }
    public int getCell(int x, int y)  { return grid[x][y];      }


    public static void main(String[] args)throws FileNotFoundException 
    {
        LifeGrid life = new LifeGrid(6, 10, args[0]);
        life.run(); 
        }

//Check neighbours

    public int neighbours(int x, int y)
    {
        int neighbours = 0;

        if(x >= 1 && y >= 1 && x < getHeight() -1 && y < getWidth() -1)
        {
            if(grid[x][y+1] == 1)       {neighbours++;} 
            if(grid[x][y-1] == 1)       {neighbours++;}
            if(grid[x+1][y] == 1)       {neighbours++;}
            if(grid[x+1][y+1] == 1)     {neighbours++;} 
            if(grid[x+1][y-1] == 1)     {neighbours++;}
            if(grid[x-1][y-1] == 1)     {neighbours++;}
            if(grid[x-1][y+1] == 1)     {neighbours++;}
        }
        else if(x == 0 && y == 0)
        {
            if(grid[x][y+1] == 1)       {neighbours++;} 
            if(grid[x+1][y] == 1)       {neighbours++;}
            if(grid[x+1][y+1] == 1)     {neighbours++;}
        }
        else if(x == 0 && y >= 1 && y < getWidth() -1 && x < getHeight() -1)
        {
            if(grid[x][y+1] == 1)       {neighbours++;} 
            if(grid[x][y-1] == 1)       {neighbours++;}
            if(grid[x+1][y] == 1)       {neighbours++;}
            if(grid[x+1][y+1] == 1)     {neighbours++;} 
            if(grid[x+1][y-1] == 1)     {neighbours++;}
        }
        else if(x >= 1 && x < getHeight() -1 && y == 0 && y < getWidth() -1)
        {
            if(grid[x][y+1] == 1)       {neighbours++;} 
            if(grid[x+1][y] == 1)       {neighbours++;}
            if(grid[x+1][y+1] == 1)     {neighbours++;} 
            if(grid[x-1][y+1] == 1)     {neighbours++;}
        }
        else if(x == getHeight() && y >= 1 && y < getWidth() - 1)
        {
            if(grid[x][y+1] == 1)       {neighbours++;} 
            if(grid[x][y-1] == 1)       {neighbours++;}
            if(grid[x-1][y-1] == 1)     {neighbours++;}
            if(grid[x-1][y+1] == 1)     {neighbours++;}
        }
        else if(x >=1 && x < getHeight() - 1 && y == getWidth() )
        {
            if(grid[x][y-1] == 1)       {neighbours++;}
            if(grid[x+1][y] == 1)       {neighbours++;}
            if(grid[x+1][y-1] == 1)     {neighbours++;}
            if(grid[x-1][y-1] == 1)     {neighbours++;}
        }
        else if(x == 0 && y == getWidth() )
        {   
            if(grid[x][y-1] == 1)       {neighbours++;}
            if(grid[x+1][y] == 1)       {neighbours++;}
            if(grid[x+1][y-1] == 1)     {neighbours++;}
        }
        else if(x == getHeight()  && y == 0)
        {
            if(grid[x-1][y] == 1)       {neighbours++;}
            if(grid[x][y+1] == 1)       {neighbours++;}
            if(grid[x-1][y+1] == 1)     {neighbours++;}
        }   
        else if(x == getHeight()  && y == getWidth() )
        {
            if(grid[x][y-1] == 1)       {neighbours++;}
            if(grid[x-1][y] == 1)       {neighbours++;}
            if(grid[x-1][y-1] == 1)     {neighbours++;}
        }
        return neighbours;
    }

    public void run()
    {
        int[][] newGrid;
        int[][] swap, old, New;
        int n;

        for(int i=0; i<grid.length; i++)
        {
            for(int j=0; j<grid[i].length; j++)
            {
                n = neighbours(i,j);
                old = grid;

                if(grid[i][j] == 1)
                {
                        if(n < 2)       {generation = 0;}
                    else if(n > 3)      {generation = 0;}
                    else if(n == 2)     {generation = 1; }
                }
                else
                {
                    if(n == 3)      {generation = 1;}
                    else
                            {generation = 0;}
                }

                if(generation == 1)
                {
                    New = old;
                    New[i][j] = 1;
                    swap = New;
                    newGrid = swap;
                    grid = newGrid;

                    show();

                    grid = old;
                }
                else
                {
                    New = old;
                    New[i][j] = 0; 
                                        swap = New;
                    newGrid = swap;
                                        grid = newGrid;

                                        show();

                                        grid = old;
                }

            }
        }
    }
}

提前谢谢你。

【问题讨论】:

  • 请发布异常详细信息并告诉我们异常发生在代码中的哪个位置。
  • 感谢您的回复,这些是详细信息:线程“main”中的异常 java.lang.ArrayIndexOutOfBoundsException: 10 at LifeGrid.neighbours(LifeGrid.java:87) at LifeGrid.run(LifeGrid.java :150) 在 LifeGrid.main(LifeGrid.java:59)
  • 请用详细信息编辑问题。
  • 您可以查看this example 以获取提示。

标签: java


【解决方案1】:

我认为它在 neighbours() 方法中。那些 x++ 或 y++ 应该是 x+1 或 y+1 (对于 x-- 也是如此)。不是检查 1 是否大于 x(或 y),而是重复递增它。

【讨论】:

  • 我已按照您的建议更改了代码,因此从 x++ 更改为 x+1,x-- 和 y 相同。但是它仍然无法给我同样的异常错误。
  • 虽然我认为这是解决方案,但行号会有很大帮助。
  • 另一个错误可能是由于根据 getWidth() 和 getHeight() 检查 x 和 y。我认为您应该检查 getWidth() - 1 和 getHeight() - 1 因为它们返回实际长度但索引少一个。
  • 非常感谢!现在我没有从异常中得到任何错误,但它不计算任何代...
【解决方案2】:

通过示例和演练重申dgunderson

//...

public int neighbours(int x, int y)
{
int neighbours = 0;

if(x >= 1 && y >= 1 && x < getHeight() && y < getWidth())
{
    if(grid[x][y++] == 1)       {neighbours++;} 
    if(grid[x][y--] == 1)       {neighbours++;}
    if(grid[x++][y] == 1)       {neighbours++;}
    if(grid[x++][y++] == 1)     {neighbours++;} 
    if(grid[x++][y--] == 1)     {neighbours++;}
    if(grid[x--][y--] == 1)     {neighbours++;}
    if(grid[x--][y++] == 1)     {neighbours++;}
}
//...

让:getHeight() 返回 6。

让:getWidth() 返回 10。

让:x 等于 getHeight() - 1。

让:y 等于 getWidth() - 1。

因此:

//...

public int neighbours(int x, int y)
{
int neighbours = 0;

//if 5 >= 1 && 9 >= 1 && 5 < 6 && 9 < 10...TRUE
if(x >= 1 && y >= 1 && x < getHeight() && y < getWidth())
{
    //if grid[5][9] == 1)  "increment y"  { "increment neighbors" };
    if(grid[x][y++] == 1)       {neighbours++;}
    //if grid[5][10] <-- WHUPS! out of bounds.
    if(grid[x][y--] == 1)       {neighbours++;}
    if(grid[x++][y] == 1)       {neighbours++;}
    if(grid[x++][y++] == 1)     {neighbours++;} 
    if(grid[x++][y--] == 1)     {neighbours++;}
    if(grid[x--][y--] == 1)     {neighbours++;}
    if(grid[x--][y++] == 1)     {neighbours++;}
}
//...

此外,调用堆栈表示该错误专门发生在邻居中,而您在邻居中所做的唯一事情就是操作数组索引。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-18
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多