二維矩形世界,每個方格住著活或死的細胞。下一時刻生死取決於相鄰八格活細胞數:
形成動態平衡。
__int8 world[102][102]:世界狀態陣列(含邊界)。IMAGE imgLive, imgEmpty:活/死細胞圖案。Init():初始化視窗、隨機種子、繪製細胞圖案(綠色圓點/灰色方框)、輸出說明、產生預設方形分布世界。SquareWorld():邊界為活細胞的方形世界。RandWorld():隨機分布世界。PaintWorld():根據 world 繪製畫面。Evolution():計算下一代狀態,使用臨時陣列 tmp 存放新狀態,最後 memcpy 回 world。int main() {
Init();
int Speed = 500; // 毫秒
while (true) {
if (kbhit() || Speed == 900) {
char c = getch();
if (c == ' ' && Speed != 900) c = getch();
if (c >= '0' && c <= '9') Speed = ('9' - c) * 100;
switch (c) {
case 's': case 'S': SquareWorld(); break;
case 'r': case 'R': RandWorld(); break;
case VK_ESCAPE: goto END;
}
}
Evolution();
PaintWorld();
if (Speed != 900) Sleep(Speed);
}
END:
closegraph();
return 0;
}
