Re: [問題] 生命遊戲

看板C_and_CPP (C/C++)作者 (猴子)時間16年前 (2009/06/10 15:54), 編輯推噓1(1011)
留言12則, 2人參與, 最新討論串1/1
※ 引述《leslieha (哈)》之銘言: 我忘了我改了那些了 @@ 有印像的用綠色 #include <iostream> using namespace std; const int row = 3, column = 3; void input(int init[row][column], int &generation); void rule_set(int init[row][column], int next[row][column]); void output(int a[row][column]); int main(int argc, char* argv[]) { int init[row][column]; int next[row][column]; int generation; /*設定兩個陣列,一為起始陣列,一為演變後的陣列*/ input(init, generation); cout << "(0)" << endl; output(init); for(int i = 0; i < generation; i++) /*以世代數決定要執行幾次*/ //for(int i = 0; i <= generation; i++) 原本是這個 { cout << "(" << i+1 << ")" << endl; rule_set(init, next); output(next); for(int x = 0; x < row; x++) { for(int y = 0; y < column; y++) { init[x][y] = next[x][y]; } } } system("pause"); return 0; } void input(int init[row][column], int &generation) { int i, j; cout << "請輸入一" << row << "×" << column << "的陣列作為初始值。"<< endl << "以0代表無細胞,1代表有細胞。" << endl; for(i = 0; i < row; i++) /*輸入起始陣列*/ { for(j = 0; j < column; j++) { cin >> init[i][j]; } } cout << "請輸入要計算的世代數:"; cin >> generation; } void rule_set(int init[row][column], int next[row][column]) { int x, y, c, r, count = 0; for(x = 0; x < row; x++) /*計算每個細胞周圍的細胞個數*/ { for(y = 0; y < column; y++) { for(r = (x - 1); r <= (x + 1); r++) { for(c = (y - 1); c <= (y + 1); c++) { if((r < 0) || (r >= row) || (c < 0) || (c >= column)) continue; if(init[r][c] == 1) count++; } } if(init[x][y] == 1) count--; switch (count) { case 0: case 1: case 4: case 5: case 6: case 7: case 8: next[x][y] = 0; break; case 2: case 3: next[x][y] = 1; break; } count = 0; } } } void output(int a[row][column]) { int i, j; for(i = 0; i < row; i++) { for(j = 0; j < column; j++) { cout << a[i][j]; } cout << endl; } cout << endl; } 因為我們這次的作業也是生命遊戲 不過規則比較不一樣 想請問一下各位強者... 如果多了以下規則 1.任何有細胞的周圍若有2個有細胞則可繼續生存下去(這個我在case2那邊做修改了) 2.任何無細胞的周圍若恰好有3個有細胞則會變成有細胞(原本的程式應該也是這樣設定) 3.相對於第一列來說,第三列也算它的周圍(反之亦然) 4.相對於第一行來說,第三行也算它的周圍(反之亦然) 是不是用上面的程式去改會跑不出來?? 因為試了很久總覺得整個程式的架構跟我這次題目的寫法似乎不太相同.. (原諒我直接先複製原po的程式碼ˊˋ..sorry..... 因為快期末考了..這次的作業讓我感覺很吃重...) 所以想請問沒有高手可以幫忙解惑一下@@ 我會非常非常非常感激你的!!!!!! -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.4.234

06/10 15:59, , 1F
可以把全部規則寫出來嗎
06/10 15:59, 1F

06/10 16:02, , 2F
任何有細胞的周圍有少於兩個或多於三個有細胞則會死亡
06/10 16:02, 2F

06/10 16:03, , 3F
若有細胞的周圍剛好是兩個或三個有細胞 則可生存下去
06/10 16:03, 3F

06/10 16:04, , 4F
任何無細胞的周圍若剛好有三個有細胞則會變成有細胞
06/10 16:04, 4F

06/10 16:06, , 5F
然後最麻煩的規則就是上面講到的第三和第四個規則
06/10 16:06, 5F

06/10 16:07, , 6F
就是 011 111 000
06/10 16:07, 6F

06/10 16:08, , 7F
010→111→000
06/10 16:08, 7F

06/10 16:09, , 8F
   000 111 000
06/10 16:09, 8F
※ 編輯: Howie0417 來自: 140.112.4.234 (06/10 16:11)

06/10 16:15, , 9F
我自己感覺好像要再寫一個兩層的for去跑init[x][y]
06/10 16:15, 9F

06/10 16:16, , 10F
然後if(x==0||x==2||y==0||y==2)的時候要多算
06/10 16:16, 10F

06/10 16:17, , 11F
可是想一想還是覺得怪怪的=   = 
06/10 16:17, 11F

06/10 16:17, , 12F
要怎麼在x=0的時候檢查x=2阿??
06/10 16:17, 12F
文章代碼(AID): #1ABsQaVO (C_and_CPP)
文章代碼(AID): #1ABsQaVO (C_and_CPP)