이 게임 오브 라이프 예제 코드에서 배열을 조작하는 데 문제가 있습니다.void 메소드의 호출/호출 (Java Homework-Game of Life 예제)
상황 :
"Game of life"는 John Conway가 발명 한 자동화 장치입니다. 수학 규칙에 따라 살기/죽기/번식 할 수있는 세포 그리드로 구성됩니다. 이 그리드의 라이브 셀과 죽은 셀은 next()
메서드로 조작되며 nPals는 그리드의 초기 상태입니다.
질문 :
내 질문 - 내가 아는 오히려 초등학교입니다 - 나는 나에게 다음 단계를 제공하기 위해 nPals
에 next()
방법을 사용합니까 어떻게?
시도 :
내 시도는 지금까지 다음의 두 가지를 따르고 있습니다. 둘 다 비슷하게 뒤돌아 보입니다.
nPals.next();
int newNPALS[][] = nPals.next(); // and then printing the array newNPALS
모든 아이디어를 가장 극명하게 될 것이다!
코드 :
public class GameOfLife {
static int nPals[][] = {
{0,0,0,0,0,0,0},
{0,1,2,3,2,1,0},
{0,2,102,104,102,2,0},
{0,3,104,8,104,3,0},
{0,2,102,104,102,2,0},
{0,1,2,3,2,1,0},
{0,0,0,0,0,0,0}
};
public static void main(String[] args) {
//Initial Stage
System.out.println(" >>First Stage<<");
printMatrix(nPals);
//Second Stage
System.out.println("\n >>Second Stage<<");
printMatrix(nPals);
}//end main
static Stack<Integer>stk=new Stack<Integer>();
static final int LIVE=100;
static final int MAXGRIDSIZE=1024;
public static void next(){
for (int i=0;i<nPals.length;i++){
for(int j=0;j<nPals[i].length;j++){
switch(nPals[i][j]){
case LIVE+0:case LIVE+1:case LIVE+4:
case LIVE+5:case LIVE+6:case LIVE+7:
stk.push(-(i*MAXGRIDSIZE+j)); //death
nPals[i][j]-=LIVE;
break;
case 3:
stk.push(i*MAXGRIDSIZE+j); //life
nPals[i][j]+=LIVE;
break;
}//end switch
}//end for j
}//end for i
while(!stk.isEmpty()){
int k=stk.pop();
if(k>0)inc(k/MAXGRIDSIZE,k%MAXGRIDSIZE);
else{
k=-k;
dec(k/MAXGRIDSIZE,k%MAXGRIDSIZE);
}//end if
}//end while
}//end next
private static void inc(int i, int j) {
}
private static void dec(int i, int j){
if(i!=0){
//3 squares on top
if(j!=0) minus(i-1,j-1);
minus(i-1,j);
if(j!=nPals[i].length-1)minus(i-1,j+1);
}
//2 on either side
if(j!=0)minus(i,j-1);
if(j!=nPals[i].length-1)minus(i,j+1);
if(i!=nPals.length-1){
//3 squares on bottom
if(j!=0)minus(i+1,j-1);
minus(i+1,j);
if(j!=nPals[i].length-1)minus(i+1,j+1);
}
}
private static void minus(int i, int j){
if(nPals[i][j]>0)nPals[i][j]--;
}
private static void plus(int i, int j){
if(nPals[i][j]<=0)nPals[i][j]++;
}
//This is just for explaining printMatrix above, otherwise immaterial
public static <E> void printMatrix(int[][] m){
for(int[] rows:m){
System.out.println(Arrays.toString(rows));
}
}//end printMatrix
}//end GameOfLife
출력 :
public void doTurn(int [][]m) {
//manipulate matrix
}
그리고 각 단계에 대한 그 전화 :
>>First Stage<<
[0, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 2, 1, 0]
[0, 2, 102, 104, 102, 2, 0]
[0, 3, 104, 8, 104, 3, 0]
[0, 2, 102, 104, 102, 2, 0]
[0, 1, 2, 3, 2, 1, 0]
[0, 0, 0, 0, 0, 0, 0]
>>Second Stage<< /* currently unchanged */
[0, 0, 0, 0, 0, 0, 0]
[0, 1, 2, 3, 2, 1, 0]
[0, 2, 102, 104, 102, 2, 0]
[0, 3, 104, 8, 104, 3, 0]
[0, 2, 102, 104, 102, 2, 0]
[0, 1, 2, 3, 2, 1, 0]
[0, 0, 0, 0, 0, 0, 0]
삶의 게임을 설명합니다. 모든 사람들이 그것이 어떻게 작동 하는지를 기대하지 마십시오. –
GoL 구현의 일반적인 문제점 중 하나는 셀 생존에 대한 정보를 얻기 위해 단일 배열을 업데이트하고 사용하려고 시도하는 것입니다. 두 개의 배열을 유지하십시오. BTW - 숙제를'/ * 코드 작성 * /'명령으로 재현 한 것 같습니다. 그래서 그런 식으로 작동하지 않습니다. –
@AndrewThompson : 나는 당신의 코멘트의 후반을 이해하지 못한다. 학교 일이라는 사실을 숨기려고하지 않았습니다. 필자는 관련성이없는 코드를 사용자에게 포격하기를 원치 않았습니다. 나중에 참조 할 수 있도록 코드를 작성하는 더 좋은 방법은 무엇입니까? – kips15