C_and_CPP 板


LINE

要写生命游戏的程式码 规则: 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







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:WOW站内搜寻

TOP