Re: [問題] 生命遊戲
※ 引述《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
06/10 16:07, 6F
→
06/10 16:08, , 7F
06/10 16:08, 7F
→
06/10 16:09, , 8F
06/10 16:09, 8F
※ 編輯: Howie0417 來自: 140.112.4.234 (06/10 16:11)
→
06/10 16:15, , 9F
06/10 16:15, 9F
→
06/10 16:16, , 10F
06/10 16:16, 10F
→
06/10 16:17, , 11F
06/10 16:17, 11F
→
06/10 16:17, , 12F
06/10 16:17, 12F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章