Java 生活的游戏(康威的游戏)-如何检查手机邻居
大家好,我正在尝试计算相邻单元格的数量,对角线包含在我的二维数组中。之后,我将运行程序,使用游戏规则的生活,将填补我的新网格。然而,我被indexOutOfBoundsException所困扰,我无法找出我做错了什么,我希望有人能帮助我,代码如下:Java 生活的游戏(康威的游戏)-如何检查手机邻居,java,Java,大家好,我正在尝试计算相邻单元格的数量,对角线包含在我的二维数组中。之后,我将运行程序,使用游戏规则的生活,将填补我的新网格。然而,我被indexOutOfBoundsException所困扰,我无法找出我做错了什么,我希望有人能帮助我,代码如下: import java.util.Scanner; import java.io.*; class LifeGrid { private int[][] grid; private int generation; publ
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();
}
}
谢谢你们的即时回答,现在代码工作了,我可以看到我的输出了。然而,我注意到我在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;
}
}
}
}
}
import java.util.Scanner;
导入java.io.*;
类生命网格
{
私有int[][]网格;
私有整数生成;
公共LifeGrid(int x,int y,字符串文件名)引发FileNotFoundException
{
网格=新整数[x][y];
int j=0;
扫描仪=新扫描仪(新文件(文件名));
while(scanner.hasNextLine()&&j=1&&y=1&&x 对于(int i=0;i我认为这是在neights()方法中。那些x++或y++应该是x+1或y+1(对于x--)也是如此。不要检查大于x(或y)的1,而是重复递增它。要通过示例和演练重申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++;}
}
//...
/。。。
公共整数邻居(整数x,整数y)
{
int=0;
如果(x>=1&&y>=1&&x==1){neights++;}
if(grid[x++][y]==1){neights++;}
if(grid[x++][y++]==1){neights++;}
if(grid[x++][y--]==1){neights++;}
如果(网格[x-->[y-->==1){neights++;}
如果(网格[x--][y++]==1){neights++;}
}
//...
Let:getHeight()返回6
Let: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++;}
}
//...
/。。。
公共整数邻居(整数x,整数y)
{
int=0;
//如果5>=1&&9>=1&&5<6&&9<10…则为真
如果(x>=1&&y>=1&&x //如果网格[5][10],请发布异常详细信息,并告诉我们异常发生在代码中的什么位置。感谢您的回复,以下是详细信息:线程“main”java.lang.ArrayIndexOutOfBoundsException:10在LifeGrid.Neights(LifeGrid.java:87)在LifeGrid.run(LifeGrid.java:150)在LifeGrid.main(LifeGrid.java:59)中出现异常请用详细信息编辑问题。您可能会检查提示。我已按照您的建议将代码从x++更改为x+1,对x--和y也是如此。但是,如果出现相同的异常错误,它仍然不起作用。虽然我认为这是解决方案,但行号将非常有用。另一个错误可能是由于检查x和y针对getWidth()和getHeight()。我认为您应该针对getWidth()-1和getHeight()-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++;}
}
//...