こんにちは皆私の2次元配列に含まれる隣接セルの数を数えようとしています。その後、私は、ゲームのルールを使用して、私の新しいグリッドを埋めるプログラムを実行します。しかし、私はは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()メソッドのアルゴリズムが完全に間違っていることに気付きました。これは、Game of Lifeのルールとは異なる出力を得ているからです。 ルール:「移入」されている空間の
:
一つまたは無隣人と各セルはあたかも孤独によって、ダイ。 4つ以上の隣接セルを有する各セルは、過剰人口によって死ぬ。 2つまたは3つの隣接セルが存続します。 「空」または「空白」の空白の場合:
3つの隣接セルがあるセルには、それぞれ、空白が入ります。
プログラムが使用するファイルは同じように設計されています
*
*
*
ここに私のコード:
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;
}
}
}
}
}
感謝をので、私は出力として持つべきルールに次
* * *
あなたは事前に。
例外の詳細を掲載して、例外が発生したコードの場所を教えてください。 – MByD
は、返信いただきありがとうございます、これらは詳細です:LifeGrid.run(LifeGrid.java:150)で10 LifeGrid.neighboursで \t(LifeGrid.java:87) \t:スレッド "メイン" java.lang.ArrayIndexOutOfBoundsExceptionの例外 \t at LifeGrid.main(LifeGrid.java:59) – user1078406
詳細を編集してください。 – MByD