生命游戏(康威的游戏) - 如何检查细胞邻居 [英] The game of life(Conway's game) - how to check for cell neighbours

查看:86
本文介绍了生命游戏(康威的游戏) - 如何检查细胞邻居的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Hello everyone I am trying to count the number of neighbours cell, diagonal included in my 2 dimensional array. After that I will run the program which using the rule of The game of life, will fill my new grid. However I am stuck with an indexOutOfBoundsException and I cannot figure out where I am doing wrong, I hope someone can help me, here the code:

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()方法中的算法是完全错误的,因为我从生命游戏的规则中获得了不同的输出。
规则:


Thank you guys for your immediate answers, now the code works and I can see my output. However I have noticed that my algorithm in the run() method is completely wrong because I am getting different outputs from the rules of the Game of Life. Rules:

对于填充的空间:

每个单元格中有一个或没有邻居死,好像是孤独。
每个有四个或更多邻居的小区都会死亡,好像是人口过剩。
每个有两个或三个邻居的小区幸存。
对于空或未填充的空格:

Each cell with one or no neighbors dies, as if by loneliness. Each cell with four or more neighbors dies, as if by overpopulation. Each cell with two or three neighbors survives. For a space that is 'empty' or 'unpopulated':

每个包含三个邻居的单元格都会填充。

Each cell with three neighbors becomes populated.

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

The file which the program uses is designed like:

                                          * * *

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

Therefore following the rules I should have as output:

                                          *
                                          *
                                          *

这里我的代码:

  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;
                }

            }
        }
    }
}

提前谢谢你。

推荐答案

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

I think it's in the neighbours() method. Those x++ or y++ should be x+1 or y+1 (and similarly for the x--). Instead of checking 1 greater than x (or y) you're repeatedly incrementing it.

这篇关于生命游戏(康威的游戏) - 如何检查细胞邻居的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆