2013-06-12 3 views
1

Conway의 게임 Java 코드에서 작업 중이며 차세대 제작자라고도 불리는 업데이트 방법으로 어려움을 겪고 있습니다. 지금까지 작성한 코드를 게시하고 업데이트 방법을 수정하기 위해 내가 할 수있는 일을 알려주십시오.Conway 's Life of Life 업데이트 (차세대)

시간에 T 1이 없으면이 셀이 태어 났으며 이웃 세 명 중 정확히 세 명이 생존했습니다. 시간 T 1에서 중 두 개 또는 세 개의 이웃

시간에 T 1 미만이 이웃이 있다면 셀은 절연에서 사망이 있다면

기존의 세포는 살아 남아있다.

시간 T 1에 4 개 이상의 이웃이있는 경우 세포가 과밀 상태에서 사망합니다.

public class GameOfLife { 

    private char [][] grid; 
    private int rows; 
    private int columns; 

    public GameOfLife(int rows, int columns) { 
     grid=new char[rows][columns]; 
     for(int i=0;i<grid.length;i++) 
     { 
      for(int j=0;j<grid[i].length;j++) 
       grid[i][j]=' '; 
     } 

    } 

    public int numberOfRows() { 
     int countRows=0; 
      for(int i=0;i<grid.length;i++){ 
      countRows++; 
      rows=countRows; 
      } 
      return rows; 

    } 

    public int numberOfColumns() { 
     int countColumns=0; 
      for(int i=0;i<1;i++){ 
      for(int j=0;j<grid[i].length;j++) 
       countColumns++; 
       columns=countColumns; 
      } 
      return columns; 
    } 

    public void growCellAt(int row, int col) { 
     for(int i=0;i<grid.length;i++){ 
      for(int j=0;j<grid[i].length;j++) 
        grid[row][col]='O'; 
     } 
    } 

    public boolean cellAt(int row, int col) { 
     for(int i=0;i<grid.length;i++){ 
      for(int j=0;j<grid[i].length;j++) 
       if(grid[row][col]=='O') 
        return true; 
     } 
     return false; 
    } 

    public String toString() { 
     String result=""; 
     for(int i=0;i<rows;i++){ 
      for(int j=0;j<columns;j++) 
       result+=grid[i][j]; 
     } 
     return result; 
    } 

    public int neighborCount(int row, int col) { 
     int count=0; 
     int i=row; 
     int j=col; 
     int left; 
     int right; 
     int up; 
     int down; 
     if(i > 0) 
      up = i-1; 
     else 
      up = grid.length-1; 

     if(i < (grid.length-1)) 
      down = i+1; 
     else 
      down = 0; 

     if(j > 0) 
      left = j-1; 
     else 
      left = grid[i].length - 1; 

     if(j < (grid[i].length-1)) 
      right = j+1; 
     else 
      right = 0; 

     if(grid[up][left] == 'O') 
      count++; 

     if(grid[up][j] == 'O') 
      count++; 

     if(grid[up][right] == 'O') 
      count++; 

     if(grid[i][left] == 'O') 
      count++; 

     if(grid[i][right] == 'O') 
      count++; 

     if(grid[down][left] == 'O') 
      count++; 

     if(grid[down][j] == 'O') 
      count++; 

     if(grid[down][right] == 'O') 
      count++; 

     return count; 
    } 

    public void update() { 

     for(int i=0;i<grid.length;i++){ 
      for(int j=0;j<grid[i].length;j++){ 
       if(grid[i][j]==' ' && neighborCount(i,j)==3) 
        grid[i][j]='O'; 
       if(neighborCount(i,j)<2 || neighborCount(i,j)>3) 
        grid[i][j]= ' '; 
       if(grid[i][j]=='O' && neighborCount(i,j)==2 || neighborCount(i,j)==3) 
        grid[i][j]='O'; 
      } 
     } 
    } 
} 

업데이트 방법에서 새 배열을 만드는 것과 관련하여이 모든 작업을 수행해야합니까? 또한 업데이트 방법에 대한 어설 션 테스트를 수행하는 방법은 무엇입니까?

public void update() { 
    char[][] newGrid = new char[grid.length][grid[0].length]; 
    for(int i=0;i<grid.length;i++){ 
     for(int j=0;j<grid[i].length;j++){ 
      if(grid[i][j]==' ' && neighborCount(i,j)==3) 
       newGrid[i][j]='O'; 
      if(neighborCount(i,j)<2 || neighborCount(i,j)>3) 
       newGrid[i][j]= ' '; 
      if(grid[i][j]=='O' && neighborCount(i,j)==2 || neighborCount(i,j)==3) 
       newGrid[i][j]='O'; 
     } 
    } 
} 
+3

정확히 어떤 문제가 있습니까? – StarPilot

답변

4

반복하고있는 동일한 격자를 수정하려는 것처럼 보입니다. 그리드를 반복 할 때 이전 그리드 상태를 기반으로 변경해야합니다. 이전 그리드 대신 새로운 그리드를 작성하십시오.

0

그래서 내가 어떻게 할 것입니다. 이것은 C++ 11 구현입니다.

template<std::size_t X, std::size_t Y> 
class GameOfLife { 
private: 
    std::pair<int, int> neighbors[8]; 

public: 
    typedef std::array<std::array<uint16_t,Y>,X> Grid; 

private: 
    uint16_t getCellStatus(Grid const& conway, int x, int y) {   
    uint16_t liveCount = 0;  
    for(auto&& neighbor: neighbors) { 
     int nX = x + neighbor.first; 
     int nY = y + neighbor.second; 
     if(nX>=0 && nX<X && nY>=0 && nY<Y){ 
     if(conway[nX][nY]>0) liveCount++; 
     }  
    } 
    if(conway[x][y]>0){ 
     if(liveCount==2 ||liveCount == 3) return 1;   
    } 
    else { 
     if(liveCount==3) return 1;   
    } 
    return 0; 
    } 

public: 
    GameOfLife() { 
    size_t index = 0; 
    for(int i=-1; i<=1; ++i) { 
     for(int j=-1; j<=1; ++j){ 
     if((i|j)==0) continue; 
     neighbors[index].first = i; 
     neighbors[index++].second = j; 
     }    
    } 
    } 

    Grid getNextConway(Grid const& conway) { 
    Grid output; 
    for(size_t i=0; i<X; ++i) 
     for(size_t j=0; j<Y; ++j) output[i][j]=getCellStatus(conway,i,j); 
     return output; 
    } 

    Grid printGrid(Grid const& conway) { 
    for (int i = 0; i < X; ++i){ 
     for (int j = 0; j < Y; ++j) { 
     if(conway[i][j]==0) std::cout<<"0"; 
     else std::cout<<"1"; 
     } 
     std::cout<<std::endl; 
    } 
    std::cout<<std::endl; 
    } 

}; 


int main() { 
    size_t const DIM = 8; 
    size_t const NUM_GENS = 10; 
    typedef GameOfLife<DIM,DIM> Game; 
    typename Game::Grid gameGrid; 
    for (int i = 0; i < DIM; ++i) {  
    for (int j = 0; j < DIM; ++j) { 
     gameGrid[i][j] = rand()%2; 
    } 
    } 
    Game conway; 
    conway.printGrid(gameGrid); 
    for (int i = 0; i < NUM_GENS; ++i) { 
    gameGrid = conway.getNextConway(gameGrid); 
    conway.printGrid(gameGrid); 
    } 
    return 0; 
} 
관련 문제