2014-02-21 35 views
1

我正在努力寫康威的生命遊戲。遊戲生活C++,檢查鄰居

不幸的是,當我去檢查一個塊的鄰居時,我總是會在我的數組中的某些元素上收到錯誤。具體來說,在grid[0][11]它給了我一個鄰居,但是,我已經設置好了,只有當它周圍的塊不是空格,或者我的代碼中有' '時,我纔會將值添加到名爲neighbors的變量中。

我相信,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

這裏來檢查,如果細胞的鄰居是活的簡單方法。將下面的代碼放在一個函數中,並傳入想要檢查的單元格的行和列。

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

注意bool inBounds(int, int)是檢查,以確保你不會關閉陣列的功能。如果你是一個40×30格的工作,inBounds()將這個簡單的一行

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

我把它作爲一個練習OP應用於生活的基礎上,live_cell_count

值的規則
0

75最好由HEIGHT代替,22代替WIDTH

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