2014-03-06 3 views
1

Game of Life 프로그램을 만들려고합니다. 루프 관련 문제점 및 게임 보드의 정확한 업데이트.Java Game of Life 구현 : 보드 업데이트 및 무한 루프 관련 문제점.

미안하지만 나는 초보자이므로이 점에 능숙하지 않습니다. 어떤 도움을 주셔서 감사합니다.

import java.util.Random; 
    public class Life { 
    public static void main(String[] args) { 
     int gridSize = 200; 
     int cellSize = 3; 
     Grid grid = new Grid(gridSize, cellSize, "The Game of Life"); 
     grid.setDelay(10); 

     int aliveCells; 
     int aliveColor = 1; 
     int deadColor = 0; 

     while (true) { 
      for (column = 0;; column++) { 
      for (row = 0;; row++) { 
       grid.getPos(row, column); 
       aliveCells = grid.matchingNeighbors(row, column, aliveColor); 
       if (aliveCells == 2 || aliveCells == 3) { 
       grid.setPos(row, column, aliveColor); 
       } else { 
       grid.setPos(row, column, deadColor); 
       } 
       grid.update(); 
      } 
      } 
     } 
+0

그리고 무엇을 달성하려고합니까? 즉 무한 루프가 필요한 이유는 무엇입니까? – ochi

+2

유용한 답변을 원한다면 좀 더 많은 정보를 제공해야 할 것입니다. 'while (true) {...} '를 사용하면 무한 루프가 생기고, 그 밖에 무엇을 원하는지 확실치 않습니다. –

+0

이미 무한 루프가 있습니다. –

답변

0

의견은 문제 영역을 과시합니다. 대문자를 적절한 변수/숫자로 바꿉니다.

import java.util.Random; 공용 클래스 수명 { public static void main (String [] args) { int gridSize = 200; int cellSize = 3; 그리드 눈금 = 새 눈금 (gridSize, cellSize, "삶의 게임"); grid.setDelay (10);

int aliveCells; 
    int aliveColor = 1; 
    int deadColor = 0; 

    //This creates a gridSizeXgridSize matrix, representing the board. 
    int bufferBoard[][] = new int[gridSize][gridSize]; 

    while (true) { 

     //Update the buffer board, represented by an matrix. 
     for (int column = 0; column < gridSize; ++column) { 
     for (int row = 0; row < gridSize; ++row) { 
      int isAlive = grid.getPos(row, column); //Assigning current cell's alive-ness to variable to be used later. 
      aliveCells = grid.matchingNeighbors(row, column, aliveColor); 

      if(isAlive == aliveColor){ //Make all the checks for a living cell. 
      if (aliveCells == 2 || aliveCells == 3) { 
       bufferBoard[row][column] = aliveColor; //This cell will be alive. 
      } else { 
       bufferBoard[row][column] = deadColor; //This cell will be dead. 
      } 
      } else { //The current cell is dead. 
      if(aliveCells == 3){ 
       bufferBoard[row][column] = aliveColor; 
      } 
      } 
     } 
     } 

     //Time to populate the actual board. 
     //Update the buffer board, represented by an matrix. 
     for (int column = 0; column < gridSize; ++column) { 
     for (int row = 0; row < gridSize; ++row) { 
      grid.getPos(row, column); 
      if (bufferBoard[row][column] == aliveColor) { //Check the location of the bufferBoard at the specified row and column. 
      grid.setPos(row, column, aliveColor); 
      } else { 
      grid.setPos(row, column, deadColor); 
      } 
      grid.update(); 
     } 
     } 
    } 
+0

나는 정적 TV처럼 보이는 화면을 얻고있다. 제 연구실이이 말이 옳지 않다고 말합니다. – MikeJ

+0

예. 그 말이 맞아. 보조 그리드가 없으면 이상한 일이 일어날 것입니다. update() 함수의 내용을 붙여 넣을 수 있습니까? 또한 주요 기능을 더 많이 붙여 넣으면 도움이 될 수 있습니다. 설명을 통해 수행해야 할 작업을 정확하게 처리 할 수 ​​있습니다. – SWPhantom

+1

나는 그것을 편집했다. 잘하면 그것은 당신이 무엇을 요구했는지입니다 – MikeJ

관련 문제