2014-02-21 4 views
1

나는 Conway의 Game of Life를 쓰려고합니다.Game of Life C++, 이웃 확인하기

불행히도 블록의 이웃을 검사 할 때 항상 배열의 특정 요소에서 오류가 발생합니다. 특히 grid[0][11]에서 나에게 하나의 이웃을 제공하지만, 주변 블록이 공간이 아니거나, 내 코드에 ' ' 인 이웃이라고하는 변수에 값을 추가하도록 설정했습니다.

if 문에서의 조건은 배열 외부로 나오지 않기도합니다.

배열 전체가 ' '으로 채워져 있는데도 불구하고 이웃에도 여전히 값을 얻습니다.

저는 몇 시간 동안이 문제를 해결해주었습니다. 내 코드는 여전히 테스트 시도로 가득 차 있으며, 나는 그 깔끔함의 부족에 대해 사과드립니다. 제 문제를 해결할 수있는 도움을 주신 모든 분들께 미리 감사드립니다. 당신이 인덱스을 확인 할 할 때

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    bool infloop = true; 
    //Create the playing grid. 
    char grid[HEIGHT][WIDTH]; 
    //Comment out later. Used for testing. 
    for(int i=0; i<75; i++) 
     for(int j=0; j<22; j++) 
      grid[i][j] = ' '; 

    //Create initial seed here. grid [x coordinate] [y coordinate]. 
    //grid [1][1] = '+'; grid [2][1] = '+'; grid [3][1] = '+'; 

    //Key. * is going to live. + is alive currently. 
    //- is going to die, and negative space is dead. 

    //As Conway's Game of Life runs infinitely, create an infinite loop. 
    while (infloop) 
     generation(grid); 


    cout << endl; 
    system("pause"); 
    return 0; 
} 

void generation(char grid[][WIDTH]) { 

    int neighbors; 

    /*Check each point on the grid for alive or dead. If it is, check the 
    surrounding neighbors and apply the game's rules.*/ 
    for(int x=0; x<75; x++) { 
     for(int y=0; y<22; y++) 
     { 

      neighbors = 0; 
      /*check all eight neighbors except for when outside of the 
      array.*/ 
      if((grid[x+1][y] != ' ') && (grid[x+1][y] < grid[HEIGHT][y])){ 
       neighbors++; cout << "A"; 
      } 

      if((grid[x-1][y] != ' ') && (grid[x-1][y] > grid[-1][y])){ 
       neighbors++; cout << "E"; 
      } 

      if((grid[x][y+1] != ' ') && (grid[x][y+1] < grid[x][WIDTH])){ 
       neighbors++; cout << "C"; 
      } 

      if((grid[x][y-1] != ' ') && (grid[x][y-1] > grid[x][-1])){ 
       neighbors++; cout << "G"; 
      } 

      if((grid[x+1][y+1] != ' ') && (grid[x+1][y+1] < grid[HEIGHT][WIDTH])){ 
       neighbors++; cout << "B"; 
      } 

      grid[0][11] = ' '; grid[11][0] = ' '; 
      if((grid[x-1][y-1] != ' ') && (grid[x-1][y-1] > grid[-1][-1])){ 
       neighbors++; cout << "F"; 
      } 

      if((grid[x+1][y-1] != ' ') && (grid[x+1][y] < grid[HEIGHT][y]) && 
       (grid[x][y-1] > grid[x][-1])){ 
        neighbors++; cout << "H"; 
      } 

      if((grid[x-1][y+1] != ' ') && (grid[x-1][y] > grid[-1][y]) 
       && (grid[x][y+1] < grid[x][WIDTH])){ 
        neighbors++; cout << "D"; 
      } 

      system("pause"); 

      cout << neighbors; 

      //Set a marker for each point according to neighbor amounts and key. 
      if(grid[x][y] == '+' && neighbors < 2) 
       grid[x][y] = '-'; 

      if(grid[x][y] == '+' && (neighbors == 2 || neighbors == 3)) 
       grid[x][y] = '*'; 

      if(grid[x][y] == '+' && neighbors > 3) 
       grid[x][y] = '-'; 

      if(grid[x][y] == ' ' && (neighbors == 3)) 
       grid[x][y] = '*'; 
     } 
    } 
    for(int x=0; x<75; x++){ 
     for(int y=0; y<22; y++) 
     { 
      if(grid[x][y] == '*') 
       grid[x][y] = '+'; 

      if(grid[x][y] == '-') 
       grid[x][y] = ' '; 
     } 
    } 
    system("pause"); 
    display(grid); 
} 

답변

1

이웃 검사는 현재 내용을 확인하고 있습니다. 또한 의 내용을 확인하기 전에을 확인하십시오. 그렇지 않으면 범위를 벗어난 배열을 얻을 수 있습니다. 여기

if ((x+1 < HEIGHT) && (grid[x+1][y] != ' ')) { // Swap and index instead of contents. 
    neighbors++; cout << "A"; 
} 
2

셀의 이웃이 살아 있는지 확인하는 쉬운 방법입니다

여기에 고정 처음이다. 함수에 다음 코드를 삽입하고 검사 할 셀의 행과 열을 전달하십시오. bool inBounds(int, int) 당신이 배열을하지 않을 있는지 확인하기 위해 확인하는 기능입니다

int live_cell_count = 0; 
for (int i = -1; i <= 1; i++) 
{ 
    for (int j = -1; j <= 1; j++) 
    { 
     //disregard grid[row][col] 
     if (!(i == 0 && j == 0) && inBounds(row+i,col+j) 
       && grid[row+i][col+j] == '+') 
       live_cell_count++; 
    } 
} 

참고. 당신이 40 X 30 그리드로 작업하는 경우, inBounds() 내가 live_cell_count

의 값에 따라 삶의 규칙을 적용 할 수있는 영업 이익 연습으로두고이 간단한 한 줄

return ((row >= 0 && row < 40) && (col >= 0 && col < 30));

0

75HEIGHT으로, 22WIDTH으로 바뀝니다.

if((grid[x+1][y] != ' ') && (x+1 < HEIGHT)) { 
     neighbors++; cout<<"A"; 
} 

해야

if((x+1 < HEIGHT) && (grid[x+1][y] != ' ')) { // The range check should be done firstly 
     neighbors++; cout<<"A"; 
} 
관련 문제