Re: [問題] 生命遊戲run不出來
※ 引述《biggerlin (比格)》之銘言:
: 要寫生命遊戲的程式碼
: 規則:
: 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;
: }
妳這邊不是在建立一個和 a 相同的陣列而是建立一個新的不同陣列
與 a 一點相關性也沒有
把 for(int j=0; j<n .... 裡面改成
b[i][j] = a[i][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++;
: }
: }
b[i][j] 應該是一個 true or false
為什麼會把他 %2 ???
void newArray(bool **&arr,int m,int n);
// new a Array which size is m*n
void freeArray(bool **&arr,int m,int n);
// delete Array which size is m*n
void copyArray(bool **origin,bool **copy,int m,int n);
// copy Array origin value to Array copy
void randArray(bool **arr,int m,int n);
// setup Array value by random value
void updateArray(bool **arr,int m,int n) {
// update Array by spec. rule
//1. 活細胞身邊八格若少於兩個鄰居會死亡
//2. 活細胞身邊八格若多於三個鄰居會死亡
//3. 活細胞身邊若剛好有二或三個鄰居可活到下一世代
//4. 死細胞身邊若剛好有三個鄰居會復活
//→前三條可以看成是
//3*3中活細胞含自己少於3活著或多於4活著則死亡否則該細胞存活
int count;
bool **tmp;
newArray(tmp,m,n);
copyArray(arr,tmp,m,n);
for(int i = 0 ; i < m ; i++) {
for(int j = 0 ; j < n ; j++) {
count = 0;
int x = (i==0 ? m-1 : i), y = (j==0 ? n-1 : j);
for(int s = -1 ; s <= 1 ; s++)
for(int t = -1 ; t <= 1 ; t++)
if(tmp[(x+s)%m][(y+t)%m] == true)
count++;
if(tmp[i][j] == true && (count < 3 || count > 4)
arr[i][j] = false;
else if(tmp[i][j] = false && count == 3)
arr[i][j] = true;
}
}
freeArray(tmp,m,n);
}
: 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: 211.74.9.2
※ 編輯: csihcs 來自: 211.74.9.2 (06/14 14:03)
※ 編輯: csihcs 來自: 211.74.9.2 (06/14 14:04)
討論串 (同標題文章)