首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >生命的游戏(康威的游戏)-如何检查细胞邻居

生命的游戏(康威的游戏)-如何检查细胞邻居
EN

Stack Overflow用户
提问于 2011-12-03 00:13:28
回答 2查看 4.5K关注 0票数 0

大家好,大家好,我试着数数相邻单元格的数目,对角线包括在我的二维数组中。之后,我将运行的程序,使用游戏的生活规则,将填补我的新网格。然而,我被一个indexOutOfBoundsException卡住了,我不知道我在哪里做错了,我希望有人能帮我,这里的代码:

代码语言:javascript
复制
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();
    }
}       

例外细节:

代码语言:javascript
复制
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()方法中的算法是完全错误的,因为我得到的输出与“生命游戏规则”不同。规则:

对于“已填充”的空间:

每个有一个或没有邻居的牢房都会死,就像孤独一样。每一个有四个或更多邻居的细胞都会死亡,好像是因为人口过多。每个有两三个邻居的牢房都能存活下来。对于“空”或“未填充”的空格:

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

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

代码语言:javascript
复制
                                          * * *

因此,按照我应该作为输出的规则:

代码语言:javascript
复制
                                          *
                                          *
                                          *

这里我的代码:

代码语言:javascript
复制
  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;
                }

            }
        }
    }
}

提前谢谢你。

EN

回答 2

Stack Overflow用户

发布于 2011-12-03 00:18:13

我想是在邻居()的方法里。这些x++或y++应该是x+1或y+1 (与x-类似)。不是检查大于x(或y)的值,而是重复递增。

票数 2
EN

Stack Overflow用户

发布于 2011-12-03 00:46:49

为了重申dgunderson,请举一个示例和一个演练:

代码语言:javascript
复制
//...

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++;}
}
//...

Let: getHeight()返回6。

Let: getWidth()返回10。

设:x等于getHeight() - 1。

设:y等于getWidth() - 1。

因此:

代码语言:javascript
复制
//...

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++;}
}
//...

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8364301

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档