写的 简单 的生命游戏 新手 各位勿喷!!
程序代码:
#include <stdio.h> #include <graphics.h> #include <conio.h> #define W_WIDTH 640 #define W_HEIGHT 480 int main() { int circle_x,circle_y,circle_radius = 480 / 6 / 2,circle_local[7][8] = {{0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,1,0,0,0,0,0}, {0,1,1,1,1,1,1,0}, //初始状况 "T" 1为 存活 0 死亡 {0,0,1,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}}; int n1,n2,life = 0,circle_local_re[7][8] = {{0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,1,0,0,0,0,0}, {0,1,1,1,1,1,1,0}, //---------------------------- {0,0,1,0,0,0,0,0}, {0,0,0,0,0,0,0,0}, {0,0,0,0,0,0,0,0}};; initgraph(W_WIDTH,W_HEIGHT); while(true) { for(circle_x = 1;circle_x <= 5;circle_x++)//------------------绘图 { for(circle_y = 1;circle_y <= 6;circle_y++) { circle((circle_x - 1) * circle_radius * 2 + circle_radius,(circle_y- 1) * circle_radius * 2 + circle_radius,circle_radius); if(circle_local[circle_x][circle_y]) { fillcircle((circle_x - 1) * circle_radius * 2 + circle_radius,(circle_y - 1) * circle_radius * 2 + circle_radius,circle_radius); } } } Sleep(1000); //------------------逻辑 for(circle_x = 1;circle_x <= 5;circle_x++) { for(circle_y = 1;circle_y <= 6;circle_y++) { for(n1 = 0;n1 <= 2;n1++) { for(n2 = 0;n2 <= 2;n2++) { if(circle_local[circle_x - 1 + n1][circle_y - 1 + n2] && !(n1 == 1 && n2 == 1)) { life++; printf("%d\n",life); } } } if(life == 3) { circle_local_re[circle_x][circle_y] = 1; } else if(life != 2) { circle_local_re[circle_x][circle_y] = 0; } life = 0; } } for(circle_x = 1;circle_x <= 5;circle_x++) { for(circle_y = 1;circle_y <= 6;circle_y++) { circle_local[circle_x][circle_y] = circle_local_re[circle_x][circle_y]; } } cleardevice(); } getch(); closegraph(); return 0; }