作者biggerlin (比格)
看板C_and_CPP
标题[问题] 生命游戏run不出来
时间Sun Jun 14 00:34:38 2009
要写生命游戏的程式码
规则:
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
1F:推 yauhh://将边角做处理 的地方弄出一个无穷回圈了 06/14 02:13
2F:推 snowlike:同一楼i=m?倒数第三行请给个回答的机会;while条件错了 06/14 02:53
3F:→ snowlike:建议你有new有相对应的delete、尝试使用srand配合ctime 06/14 02:55
4F:推 wa007123456:这怎麽写的阿 我Run的出来阿 囧 06/14 04:51
5F:→ wa007123456:你可能忘记加srand(time(NULL));了 06/14 04:52
6F:→ yauhh:别逗了,这程式内容错误相当多,怎麽可能run出什麽 06/14 06:13