[問題] 生命遊戲程式碼 我錯在哪??
要寫生命遊戲的程式碼
: 規則:
: 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++)
:
: }
: 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)
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: 218.168.90.250
→
08/01 14:47, , 1F
08/01 14:47, 1F
→
08/01 15:02, , 2F
08/01 15:02, 2F
推
08/01 15:05, , 3F
08/01 15:05, 3F
→
08/01 17:08, , 4F
08/01 17:08, 4F
→
08/01 19:11, , 5F
08/01 19:11, 5F
噓
08/01 19:30, , 6F
08/01 19:30, 6F
推
08/01 21:53, , 7F
08/01 21:53, 7F
推
08/01 23:47, , 8F
08/01 23:47, 8F
→
08/01 23:47, , 9F
08/01 23:47, 9F