[問題] 生命遊戲run不出來

看板C_and_CPP (C/C++)作者 (比格)時間16年前 (2009/06/14 00:34), 編輯推噓3(303)
留言6則, 3人參與, 最新討論串1/2 (看更多)
要寫生命遊戲的程式碼 規則: 1. 活細胞身邊八格若少於兩個鄰居會死亡 2. 活細胞身邊八格若多於三個鄰居會死亡 3. 活細胞身邊若剛好有二或三個鄰居可活到下一世代 4. 死細胞身邊若剛好有三個鄰居會復活 活細胞用"*"表示 死細胞用空格表示 如果身邊不足八個空格的 用它的對面補 例如3*3中的最上排中間那一格 它樓上的鄰居就最下排的中間那一格 以此類推 我的目前的構想是run出原始圖形後 建立一個和它一樣的圖形做修改用 可是compile過了以後 run出來的都只有一個星號 圖形也沒辦法按照規則產生變化 麻煩大家幫我看一下我的程式碼到底出了什麼問題呢?? 謝謝!! # include <iostream> # include <ctime> # include <cstdlib> using namespace std; void showArray(bool **a, int m, int n) //印出原始陣列a { for(int i=0; i<m; i++) { for(int j=0; j<n; j++) { if(a[i][j] == true) cout<<"*"; //活細胞 else cout<<" "; //死細胞 } } } void updateArray(bool**a, int m, int n) { int count = 0; bool **b; //製作一個和a相同的陣列b作為修改用 b = new bool*[m]; for(int i=0; i<m; i++) { b[i] = new bool[n]; for(int j=0; j<n; j++) { b[i][j] = rand()%10; if(b[i][j] % 2 == 0) b[i][j] = true; else b[i][j] = false; } cout<<endl; } for(int i=0; i<m; i++) { for(int j=0; j<n; j++) { if(b[i][j] == true) cout<<"*"; else cout<<" "; } } for(int i=0; i=m; i++) //將邊角做處理 { for(int j=0; j=n; j++) { for(int s=i-1; s=i+1; s++) { for(int t=j-1; t=j+1; t++) { if(i-1<0) s=i+1; if(j-1<0) t=j+1; if(i=m) s=0; if(j=n) t=0; if(b[s][t] % 2 == 0) count++; } } if(b[i][j] % 2 == 0) count--; //自己那一格如果是活細胞要扣掉 if(count == 2 || count == 3 && b[i][j] % 2 == 0) a[i][j] = true; //活細胞活到下一世代 if(count == 3 && b[i][j] % 2 != 0) a[i][j] = true; //死細胞復活 if(count < 1) a[i][j] = false; //活細胞死亡 if(count > 3) a[i][j] = false; //活細胞死亡 } } } int main() { int m; int n; cout<<"Please enter two numbers."; cin>>m; cin>>n; bool **a; a = new bool*[m]; for(int i=0; i<m; i++) { a[i] = new bool[n]; for(int j=0; j<n; j++) { a[i][j] = rand()%10; //陣列中每一個有一半機率為true if(a[i][j] % 2 == 0) a[i][j] = true; else a[i][j] = false; } cout<<endl; } showArray(a, m, n); char c; cout<<"Do you want to quit? (Y/N)"; cin>>c; if(c == 'Y'|| c == 'y') return 0; else do{ updateArray(a, m, n); showArray(a, m, n); cout<<"Do you want to quit? (Y/N)"; }while(c != 'Y'|| c != 'y'); } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 61.228.214.105

06/14 02:13, , 1F
//將邊角做處理 的地方弄出一個無窮迴圈了
06/14 02:13, 1F

06/14 02:53, , 2F
同一樓i=m?倒數第三行請給個回答的機會;while條件錯了
06/14 02:53, 2F

06/14 02:55, , 3F
建議你有new有相對應的delete、嘗試使用srand配合ctime
06/14 02:55, 3F

06/14 04:51, , 4F
這怎麼寫的阿 我Run的出來阿 囧
06/14 04:51, 4F

06/14 04:52, , 5F
你可能忘記加srand(time(NULL));了
06/14 04:52, 5F

06/14 06:13, , 6F
別逗了,這程式內容錯誤相當多,怎麼可能run出什麼
06/14 06:13, 6F
文章代碼(AID): #1ACzKV0r (C_and_CPP)
討論串 (同標題文章)
文章代碼(AID): #1ACzKV0r (C_and_CPP)