나는 학교 프로젝트를 위해 Conway의 Game of Life에서 일하고 있습니다. 나는 코드를 직접 찾고 있지 않다. 내 코드가 무엇이 잘못되었는지 알아 내려고합니다.JAVA - Conway의 Game of Life는 모든 살아있는 세포를 삭제합니까?
Conway의 Game of Life에서는 3 개의 살아있는 이웃이 있다면 세포가 죽은 것에서 살아있는 것으로 이동합니다. 두세 개의 살아있는 이웃이 있다면 그것은 살아 있습니다. 이들 중 어느 것도 사실이 아니라면 그것은 죽었다.
내 LifeView 클래스에는 셀 시뮬레이션을 표시하고 이후에 주어진 포인트 주변에 몇 개의 살아있는 셀이 있는지 표시하는 메서드가 있습니다. 두 번째 세대가 첫 번째 세대 살아있는 세포의 중심을 넘어 살아있는 세포의 수평 라인으로되어 있기 때문에
How many rows is your simulation?
5
How many columns is your simulation?
5
How many generations is your simulation?
3
xxxxx
xxxxx
xx0xx
xx0xx
xx0xx
00000
01110
02120
03230
02120
xxxxx
xxxxx
xxxxx
xxxxx
xxxxx
00000
00000
00000
00000
00000
xxxxx
xxxxx
xxxxx
xxxxx
xxxxx
00000
00000
00000
00000
00000
이 잘못 :
이
내가 무엇입니까 출력입니다. 그 중심을 가로 지르기보다는 모든 세포가 죽었습니다. 나는 왜 그것이 작동하지 않는가에 관해서 난처하게된다.Main 클래스 :
package gameOfLife;
import java.util.Scanner;
public class Main {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Scanner numReader = new Scanner(System.in);
System.out.println("How many rows is your simulation?");
int rows = numReader.nextInt();
System.out.println("How many columns is your simulation?");
int columns = numReader.nextInt();
System.out.println("How many generations is your simulation?");
int generations = numReader.nextInt();
LifeModel model = new LifeModel(rows,columns);
LifeView life = new LifeView(model);
for(int i=0; i<generations; i++)
{
life.displayLife();
model.nextGeneration();
}
}
LifeView 클래스 :
package gameOfLife;
import java.util.Scanner;
public class LifeView {
private LifeModel model;
public LifeView(LifeModel model)
{
this.model = model;
}
public void displayLife()
{
for(int i=0; i < model.getWorld().length; i++)
{
for(int j=0; j < model.getWorld()[0].length; j++)
{
if(model.getWorld()[i][j])
{
System.out.print("0");
}
else
{
System.out.print("x");
}
}
System.out.println("");
}
System.out.println("");
for(int i=0; i < model.getWorld().length; i++)
{
for(int j=0; j < model.getWorld()[0].length; j++)
{
System.out.print(model.numLivingNeighbors(i,j));
}
System.out.println("");
}
System.out.println("");
System.out.println("");
}
}
LifeModel 클래스 : 패키지 gameOfLife;
public class LifeModel
{
private boolean[][] world;
private int numRows;
private int numCols;
private boolean[][] tempWorld;
public LifeModel(int rows, int cols)
{
this.numRows=rows;
this.numCols=cols;
world = new boolean[rows][cols];
initWorld();
tempWorld = world;
}
private void initWorld()
{
boolean done = false;
while(!done)
{
int i = (int) (Math.random()*numRows);
int j = (int) (Math.random()*numCols);
if(j>0 && i>0 && i<numRows-1 && j<numCols-1)
{
/*
world[i-1][j-1] = true;
world[i-1][j] = true;
world[i-1][j+1] = true;
world[i][j+1] = true;
world[i+1][j] = true;
*/
world[i][j]=true;
world[i+1][j]=true;
world[i-1][j]=true;
done = true;
}
}
}
public void nextGeneration()
{
//tempWorld = new boolean[numRows+2][numCols+2];
int rows = world.length;
int columns = world[0].length;
for(int i=0; i < rows; i++)
{
for(int j = 0; j < columns; j++)
{
toggleCell(i,j);
}
}
world = tempWorld;
}
public void toggleCell(int r, int c)
{
int count = numLivingNeighbors(r,c);
if(!world[r][c] && count==3)
{
tempWorld[r][c] = true;
}
else if(world[r][c] && (count>=2 && count<=3))
{
tempWorld[r][c] = true;
}
else
{
tempWorld[r][c] = false;
}
}
public int numLivingNeighbors(int r, int c)
{
int count = 0;
boolean newCells[][] = world;
for(int i = -1; i<=1; i++)
{
for(int j = -1; j<=1; j++)
{
if(i!=0 || j!=0)
{
int row = r + i;
int column = c + j;
if(row>=0 && row < newCells.length && column>=0 && column<newCells[0].length && newCells[row][column])
{
count++;
}
}
}
}
return count;
}
public void userChange()
{
}
public boolean[][] getWorld()
{
return world;
}
}
어떤 도움을 주시면 감사하겠습니다!