2013-02-28 52 views
0

我想編程生活的遊戲。當我試圖迭代世代時,我最終得到了垃圾。我嘗試了很多不同的東西,但我沒有看到我的錯誤,但顯然有一個。任何幫助將不勝感激。我幾乎可以肯定,當我嘗試將遊戲的邏輯應用於舊陣列,創建新陣列時,我做錯了什麼,但我不確定是什麼。這是四個功能之一,我已經測試了其他功能,並且我認爲這個功能中存在缺陷。最終目標是讓遊戲在每一代都隨着需要而擴大和縮小。麻煩與生命和動態數組遊戲,c + +

void iterateGeneration() 
{ 
ifstream fin; 
fin.open("tmp.txt"); 
ofstream fout; 

char **arr=new char * [ROWS]; 
for(int i=0; i<ROWS; i++) 
    arr[i] = new char [COLUMNS]; 

int lifecheck=0; 
//read in current generation from tmp.txt 
for(int i=0; i<ROWS; i++) 
    { 
    fin.getline(arr[i], COLUMNS); 
    } 
fin.close(); 

char **new_arr=new char * [ROWS]; 
for(int i=0; i<ROWS; i++) 
    new_arr[i]= new char [COLUMNS]; 

//count live neighbors, then determine if cell will be alive next generation 
for(int i=0; i<ROWS; i++) 
    { 
    for(int j=0; j<COLUMNS-1; j++) 
     { 
     lifecheck=0; 
     if((i==0 && j==0) || (i==0 && j==(COLUMNS-2)) || (i==(ROWS-1) && j==0) ||  (i==(ROWS-1) && j==(COLUMNS-2)))//corners always stay dead 
      { 
      lifecheck=0; 
      } 
     else if(i==0)//special check for first row, only three checks since neighbors  in first row is always dead 
      { 
      if(arr[i+1][j-1]=='1') 
       lifecheck+=1; 
      if(arr[i+1][j]=='1') 
       lifecheck+=1; 
      if(arr[i+1][j+1]=='1') 
       lifecheck+=1; 
      } 
     else if(i==(ROWS-1))//special check for last row 
      { 
      if(arr[i-1][j-1]=='1') 
       lifecheck+=1; 
      if(arr[i-1][j]=='1') 
       lifecheck+=1; 
      if(arr[i-1][j+1]=='1') 
       lifecheck+=1; 
      } 
     else if(j==0)//special check for first column 
      { 
      if(arr[i-1][j+1]=='1') 
       lifecheck+=1; 
      if(arr[i][j+1]=='1') 
       lifecheck+=1; 
      if(arr[i+1][j+1]=='1') 
       lifecheck+=1; 
      } 
     else if(j==(COLUMNS-2))//special check for last column 
      { 
      if(arr[i-1][j-1]=='1') 
       lifecheck+=1; 
      if(arr[i][j-1]=='1') 
       lifecheck+=1; 
      if(arr[i+1][j-1]=='1') 
       lifecheck+=1; 
      } 
     else 
      { 
      if(arr[i-1][j-1]=='1') 
       lifecheck+=1; 
      if(arr[i-1][j]=='1') 
       lifecheck+=1; 
      if(arr[i-1][j+1]=='1') 
       lifecheck+=1; 
      if(arr[i][j-1]=='1') 
       lifecheck+=1; 
      if(arr[i][j+1]=='1') 
       lifecheck+=1; 
      if(arr[i+1][j-1]=='1') 
       lifecheck+=1; 
      if(arr[i+1][j]=='1') 
       lifecheck+=1; 
      if(arr[i+1][j+1]=='1') 
       lifecheck+=1; 
      } 
     if(arr[i][j]=='0') 
      { 
      if(lifecheck==3) 
       new_arr[i][j]=='1'; 
      else 
       new_arr[i][j]=='0'; 
      } 
     else if(arr[i][j]=='1') 
      { 
      if(lifecheck==2) 
       new_arr[i][j]=='1'; 
      else if(lifecheck==3) 
       new_arr[i][j]=='1'; 
      else 
       new_arr[i][j]=='0'; 
      } 

     else 
      new_arr[i][j]=='0'; 
     }//2nd for 
    }//1st for 

fout.open("tmp.txt"); 

for(int i=0; i<ROWS; i++) 
    { 
    fout << new_arr[i]; 
    fout << endl; 
    } 

for(int p=0; p<ROWS; p++) 
    { 
    delete [] arr[p]; 
    delete [] new_arr[p]; 
    } 
delete [] arr; 
delete [] new_arr; 
} 


using namespace std; 

const int NUM_GENERATIONS = 1; //set to a smaller number for debugging 

int main() 
{ 
    populateWorld(FILE_NAME); 

    showWorld(); 

    for (int iteration = 0; iteration < NUM_GENERATIONS; iteration++) 
    { 

     if (WINDOWS) 
      system("cls"); //Windows only 
     else 
      system("clear"); //Linux only 

     iterateGeneration(); 

     showWorld(); 
    } 

    if (WINDOWS) 
     system("PAUSE"); 

    return 0; 
} 

0000000000000000000000000000000000000000000000000000000000000000000000000000 
0000000000000000000000000000000010000000000000000000000000000000000000000000 
0000000000000000000000000000001010000000000000000000000000000000000000000000 
0000000000000000000011000000110000000000001100000000000000000000000000000000 
0000000000000000000100010000110000000000001100000000000000000000000110000000 
0000000011000000001000001000110000000000000000000000000000000000000110000000 
0000000011000000001000101100001010000000000000000000000000000000000000000000 
0000000000000000001000001000000010000000000000000000000000000000000000000000 
0000000000000000000100010000000000000000000000000000000000000000000000000000 
0000000000000000000011000000000000000000000000000000000000000000000000000000 
0000000000000000000000000000000000000000000000000000000000000000000000000000 
0000000000000000000000000000000000000000000000000000000000000000000000000000 
0000000000000000000000000000000000000000000000000000000000000000000000000000 
0000000000000000000000000000000000000000000000000000000000000000000000000000 
0000000000000000000000000000000000000000000000000000000000000000001110000000 
0000000000000000000000000000000000000000000000000000000000000000010001000000 
0000000000000000000000000000000000000000000000000000000000000000100000100000 
0000000000000000000000000000000000000000000000000000000000000000100000100000 
0000000000000000000000000000000000000000000000000000000000000000000100000000 
0000000000000000000000000000000000000000000000000000000000000000010001000000 
0000000000000000000000000000000000000000000000000000000000000000001110000000 
0000011000000000000000111000000000000000000000000000000000000000000100000000 
0000011000000000000000101000000000000000000000000000000000000000000000000000 
0000000000000000000000110000000000000000000000000000000000000000000000000000 
0000000000000000000000000000000000000000000000000000000000000000000011100000 
0000000000000000000000000000000000000000000000000000000000000000000011100000 
0000000000000000000000000000000000000000000000000000000000000000000100010000 
0000000000000000000000000000000000000000000000000000000000000000000000000000 
0000000000000000000000000000000000000000000000000000000000000000001100011000 
0000000000000000000000000000000000000000000000000000000000000000000000000000 
0000000000000000000000000000000000000000000000000000000000000000000000000000 
0000000000000000000000000000000000000000000000000000000000000000000000000000 
0001100011000000000000000000000000000000000000000000000000000000000000000000 
0000000000000000000000000000000000000000000000000000000000000000000000000000 
0000100010000000000000000000000000000000000000000000000000000000000000000000 
0000011100000000000000000000000000000000000000000000000000000000000000000000 
0000011100000000000000000000000000000000000000000000000000000000000000000000 
0000000000000000000000000000000000000000000000000000110000000000000000000000 
0000000000000000000000000000000000000000000000000001010000000000000001100000 
0000000010000000000000000000000000000000000000000001110000000000000001100000 
0000000111000000000000000000000000000000000000000000000000000000000000000000 
0000001000100000000000000000000000000000000000000000000000000000000000000000 
0000000010000000000000000000000000000000000000000000000000000000000000000000 
0000010000010000000000000000000000000000000000000000000000000000000000000000 
0000010000010000000000000000000000000000000000000000000000000000000000000000 
0000001000100000000000000000000000000000000000000000000000000000000000000000 
0000000111000000000000000000000000000000000000000000000000000000000000000000 
0000000000000000000000000000000000000000000000000000000000000000000000000000 
0000000000000000000000000000000000000000000000000000000000000000000000000000 
0000000000000000000000000000000000000000000000000000000000000000000000000000 
0000000000000000000000000000000000000000000000000000000000000000000000000000 
0000000000000000000000000000000000000000000000000000001100000000000000000000 
0000000000000000000000000000000000000000000000000000100010000000000000000000 
0000000000000000000000000000000000000000000100000001000001000000001100000000 
0000000000000000000000000000000000000000000101000011010001000000001100000000 
0000000110000000000000000000000011000000000000110001000001000000000000000000 
0000000110000000000000000000000011000000000000110000100010000000000000000000 
0000000000000000000000000000000000000000000000110000001100000000000000000000 
0000000000000000000000000000000000000000000101000000000000000000000000000000 
0000000000000000000000000000000000000000000100000000000000000000000000000000 
0000000000000000000000000000000000000000000000000000000000000000000000000000 

x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00`ó!#\00\00 \00\00 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000000000 
00000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000 
00000000000000000000000000000010 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000110000001100000000000011000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000001100000000000011000000000000000000000001 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000000110000000 
00000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000 
00000000000000000010000010 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000001000100000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000110000000000000000000000000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000000000000000000000 
00 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000 
00000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000 
00000000000000000000000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000100010000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000100000100000 
00000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000100000 
00000000000000000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000011 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000000100000000 
00000110 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000 
00000000000000000000001100 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000000000000100010000 
00 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000 
00000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\001000 
00000000000000000000000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000000000 
00000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000 
00001000100000000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0011000000000000000000000000000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000110000000000000000000000 
00000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000001100000 
00000000100000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000001110000000000000000000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000000000000000000000 
00 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000 
00000100000100000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000 
00000010001000000000000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000000000 
00000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000 
00000000000000000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000001000100000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000100000001000001000000001100000000 
00000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000001100000000 
00000001100000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000001100000000000000000000000110000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\0000000000000000000000000000001100000011000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000101000000000000000000000000000000 
00 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000000000000000000000 
00000000000000000000 
x"#\00\00x"#\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\00\000000 
+0

請定義「垃圾」。你也使用了非常規的縮進風格。 – tadman 2013-02-28 22:51:26

+0

爲什麼每個人都堅持通過手動管理他們的記憶來讓他們的生活變得更加困難? – chris 2013-02-28 22:52:09

+0

「垃圾」× × xã_這就是我在複製後得到的輸出。對不起,奇怪的縮進。 – user1768079 2013-02-28 22:54:49

回答

0

您應該顯式輸出數組的每個字符而不是整個字符數組。

我相信fprintf會輸出一個你想要的數組,但從使用C++開始已經很長時間了。可能自從在高中寫一個非常非常類似的課程後,

0

在這裏你保存new_arr

for(int i=0; i<ROWS; i++) 
    { 
    fout << new_arr[i]; 
    fout << endl; 
    } 

在這裏,具體地說,就是錯誤:

fout << new_arr[i]; 

發送char *ostream將發送連續的字符,直到 「空終結者」找到字符。

你的代碼永遠不會設置空終止符,所以很難判斷寫了多少。根據你的垃圾結果,這太多了。

+0

我會嘗試通過字符輸出char,並讓你知道 – user1768079 2013-02-28 23:29:02

+0

@ user1768079應該完成這項工作,因爲你已經知道要輸出多少個字符。 – 2013-02-28 23:30:17

+0

發生了什麼事情,它正在寫入正確數量的行,只是它們的內容不好。輸入文件中有61行,輸出文件中有61行。輸出「tmp.txt」應該與輸入文件一樣是0和1,只是根據遊戲規則而改變。 – user1768079 2013-02-28 23:30:35