我正在尝试将Conway的生命游戏编程为Android应用程序。我创建了我可以绘制的初始网格,并设计了一个引擎。但是,当我尝试使用我的初始网格作为输入并运行引擎时,应用程序崩溃了。引擎的代码如下,如果有人能指出我如何创建工作引擎的话,我将不胜感激。
我看到的主要错误是“线程退出未捕获的异常”
LifeEngine.java
public class LifeEngine {
private Context context;
private NeighborCheck checker;
private MyGridView myGridView;
private int height = this.myGridView.getMyHeight();
private int width = this.myGridView.getMyWidth();
public LifeEngine(Context context) {
this.context = context;
}
public char[][] generateNextGeneration(char[][] world) {
//char[][] currentGeneration = world;
int neighbors;
char[][] nextGeneration = new char[this.myGridView.getMyHeight()][this.myGridView.getMyWidth()];
for (int c = 0; c < height/UpdateGridActivity.cell_size; c++) {
for (int r = 0; r < width/UpdateGridActivity.cell_size; r++) {
if(world[c][r] != 0) //handles live cells
{
neighbors = checker.check(world,r, c);
if (neighbors !=3 && neighbors !=2)
nextGeneration[c][r] = 0;
else
nextGeneration[c][r] = '*';
}
else //handles dead cells
{
neighbors = checker.check(world,r, c);
if(neighbors == 3)
nextGeneration[c][r] = '*';
else
nextGeneration[c][r] = 0;
}
}
}
return nextGeneration;
}
}
NeighborCheck.java
public class NeighborCheck {
private MyGridView myGridView;
public int Check(char[][] array, int r, int c)
{
int neighbor_total = 0;
int height = this.myGridView.getMyHeight();
int width = this.myGridView.getMyWidth();
//top left
try{
if (r != 0 && c != 0)
if(array[r-1][c-1] != 0)
neighbor_total +=1;}
catch (ArrayIndexOutOfBoundsException e) {}
//top center
try{
if (c != 0)
if(array[r-1][c] != 0)
neighbor_total +=1;}
catch (ArrayIndexOutOfBoundsException e) {}
//top right
try{
if (r != width && c != 0)
if(array[r-1][c+1] != 0)
neighbor_total +=1;}
catch (ArrayIndexOutOfBoundsException e) {}
//middle left
try{
if (r != 0)
if(array[r][c-1] != 0)
neighbor_total +=1;}
catch (ArrayIndexOutOfBoundsException e) {}
//middle right
try{
if (r != width - 1)
if(array[r][c+1] != 0)
neighbor_total +=1;}
catch (ArrayIndexOutOfBoundsException e) {}
//bottom left
try{
if (r != 0 && c != height - 1)
if(array[r+1][c-1] != 0)
neighbor_total +=1;}
catch (ArrayIndexOutOfBoundsException e) {}
//bottom middle
try{
if (c != height - 1)
if(array[r+1][c] != 0)
neighbor_total +=1;}
catch (ArrayIndexOutOfBoundsException e) {}
//bottom right
try{
if (r != width - 1 && c != height - 1)
if(array[r+1][c+1] != 0)
neighbor_total +=1;}
catch (ArrayIndexOutOfBoundsException e) {}
return neighbor_total;
}
}