2013-12-17 1 views
1

나는 학교 프로젝트를 위해 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; 
} 


} 

어떤 도움을 주시면 감사하겠습니다!

답변

4

LifeModel 클래스에는 몇 가지 작은 문제가 있습니다.

생성자에서 실제 게임 세계와 동일한 배열을 참조하도록 tempWorld를 설정합니다. 이로 인해 tempWorld를 수정하면 gameWorld에도 영향을줍니다.

public LifeModel(int rows, int cols) 
{ 
    this.numRows=rows; 
    this.numCols=cols; 
    world = new boolean[rows][cols]; 
    initWorld(); 
    //tempWorld = world; // You can remove this line. 
} 

그런 다음 다음 세대에 당신은 라인 "// tempWorld = 새로운 부울 [numRows의 + 2] [NUMCOLS + 2]"이 주석 처리했다. 여기에 새로운 임시 배열을 만들어야 만 게임 보드를 읽으면서 게임 보드를 변경하지 않아도됩니다. 그러나, 나는 +2가 무엇을해야하는지 잘 모르겠다. 그래서 나는 그것을 제거했다. 당신은 가지고 있어야합니다 :

public void nextGeneration() 
{ 
    tempWorld = new boolean[numRows][numCols]; // Keep it the same size 

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

내가 변경 한 후에는 완벽하게 작동했습니다. 내 컴퓨터에서 사용했던 전체 LifeModel 클래스를 아래에 포함했습니다.

package 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(); 
} 

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][numCols]; 

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


} 
0

numLivingNeighbors가 세계의 각 셀에 적절한 값을 반환하는지 확인하십시오.

또한이 LifeModel의 생성자 코드

public LifeModel(int rows, int cols) 
{ 
    this.numRows=rows; 
    this.numCols=cols; 
    world = new boolean[rows][cols]; 
    initWorld(); 
    tempWorld = world; 
} 

에서 살아

0

이봐 당신이 행한 단순한 실수 체류하는 단계를 확인합니다. 이 생성자에서 tempworld도 초기화해야합니다. 당신은 당신의 세계를 tempworld에 할당해서는 안됩니다. 수정 후이 코드 블록은 다음과 같이됩니다.

public LifeModel(int rows, int cols) 
{ 
this.numRows=rows; 
this.numCols=cols; 
world = new boolean[rows][cols]; 
tempWorld = new boolean[rows][cols]; 
initWorld(); 
} 

출력이 올바를 것입니다.

관련 문제