[問題] 生命遊戲run不出來
要寫生命遊戲的程式碼
規則:
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
06/14 02:53, 2F
→
06/14 02:55, , 3F
06/14 02:55, 3F
推
06/14 04:51, , 4F
06/14 04:51, 4F
→
06/14 04:52, , 5F
06/14 04:52, 5F
→
06/14 06:13, , 6F
06/14 06:13, 6F
討論串 (同標題文章)
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章