1.0.0版本:随便写的,代码很难看,bug很多,但是程序里面看不出来,凑合着能用
1 #include<cstdio> 2 #include<windows.h> 3 #include<ctime> 4 #include<conio.h> 5 const int 6 maxh=19,// 高度 7 maxw=69; //宽度 8 const int 9 maxharr=maxh+10, 10 maxwarr=maxw+10; 11 const int 12 code_unknown=-1,//不知道死活 13 code_life=1,//生 14 code_dead=0;//死 15 const char 16 show_life='*', 17 show_dead=' '; 18 int cnt=0, 19 update_waitms=0, 20 maxcells=500;//初始活细胞 21 int map[maxharr][maxwarr];//细胞分布图 22 int firstmap[maxharr][maxwarr];//初始的细胞分布图 23 void io_hidecur(); 24 void cleanscr(); 25 void pause(); 26 void getrand(int &x,int &y); 27 void arrayset(int val); 28 void showinfo(); 29 void showside(); 30 bool is_inmap(int i,int j); 31 void gotoxy(int x,int y); 32 int is_alive(int i,int j); 33 int getneighbor(int i,int j); 34 void background(); 35 void input(); 36 void init(); 37 void show(); 38 void refresh(); 39 void solve(); 40 41 void io_hidecur(){//隐藏光标 42 CONSOLE_CURSOR_INFO cursor_info={1,0}; 43 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info); 44 } 45 void cleanscr(){ 46 system("cls"); 47 } 48 void pause(){ 49 system("pause>nul"); 50 } 51 void getrand(int &x,int &y){ 52 x=rand()%(maxh+1),y=rand()%(maxw+1); 53 if(x==0 || y==0) getrand(x,y); 54 } 55 void arrayset(int val){ 56 for(int i=0;i<maxharr;++i){ 57 for(int j=0;j<maxwarr;++j){ 58 map[i][j]=val; 59 } 60 } 61 } 62 void showinfo(){ 63 printf("[LifeGame CDsidi Version 1.0.0] "); 64 printf("[Cycle:%d] ",cnt); 65 } 66 void showside(){ 67 for(int i=0;i<=maxw;++i) 68 printf("_"); 69 printf(" "); 70 } 71 bool is_inmap(int i,int j){//判断i,j是否没有越界 72 if(i<=1 || j<=1 || i>maxh || j>maxw) return false; 73 return true; 74 } 75 int is_alive(int i,int j){ 76 if(!is_inmap(i,j) || map[i][j]!=code_life) return 0; 77 return 1; 78 } 79 int getneighbor(int i,int j){ 80 int ret= 81 is_alive(i-1,j-1)+is_alive(i-1,j)+ 82 is_alive(i-1,j+1)+is_alive(i,j-1)+ 83 is_alive(i,j+1)+is_alive(i+1,j-1)+ 84 is_alive(i+1,j)+is_alive(i+1,j+1); 85 return ret; 86 } 87 void gotoxy(int x,int y){ //光标移动到(x,y)位置 88 HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE); 89 COORD pos; 90 pos.X=x; 91 pos.Y=y; 92 SetConsoleCursorPosition(handle,pos); 93 } 94 void background(){ 95 char s[]="生命游戏是英国数学家约翰·何顿·康威在1970年发明的细胞自动机 96 这个世界中的每个方格居住着一个活着的或死了的细胞。 97 一个细胞在下一个时刻生死取决于相邻八个方格中活着的或死了的细胞的数量。 98 如果相邻方格活着的细胞数量过多,这个细胞会因为资源匮乏而在下一个时刻死去; 99 相反,如果周围活细胞过少,这个细胞会因太孤单而死去。 100 在本版本游戏中,用'*'表示细胞"; 101 MessageBox(NULL,TEXT(s),"游戏介绍",MB_OK); 102 } 103 void input(){ 104 int choose=MessageBox(NULL,TEXT("请问您是否第一次进行游戏?"),"游戏介绍",MB_OKCANCEL); 105 if(choose==1){ 106 background(); 107 return; 108 } 109 int a,b; 110 printf("请输入在刷新图片间隔等待的时间(ms)[如果<=0 则采用默认值] "); 111 scanf("%d",&a); 112 if(a<=0) return; 113 printf("请输入初始有生命的细胞最大个数 "); 114 scanf("%d",&b); 115 update_waitms=a; 116 maxcells=b; 117 } 118 void init(){ 119 cleanscr(); 120 srand(time(0)); 121 cnt=0; 122 system("title 生命游戏"); 123 arrayset(code_unknown); 124 for(int i=1;i<=maxcells;++i){ 125 int x,y; 126 getrand(x,y); 127 map[x][y]=code_life; 128 } 129 io_hidecur(); 130 cleanscr(); 131 } 132 void show(){ 133 gotoxy(0,0); 134 showinfo(); 135 showside(); 136 for(int i=1;i<=maxh;++i){ 137 printf("|"); 138 for(int j=1;j<=maxw;++j){ 139 if(map[i][j]==code_life) 140 printf("%c",show_life); 141 else if(map[i][j]==code_dead){ 142 printf("%c",show_dead); 143 }else{ 144 printf("?"); 145 map[i][j]=code_dead; 146 } 147 } 148 printf("| "); 149 } 150 showside(); 151 } 152 void refresh(){ 153 int tmp[maxharr][maxwarr]; 154 for(int i=1;i<=maxh;++i){ 155 for(int j=1;j<=maxw;++j){ 156 int neighbor=getneighbor(i,j);//这样不会越界 157 if(neighbor==3) 158 tmp[i][j]=code_life; 159 else if(neighbor==2) 160 tmp[i][j]=map[i][j]; 161 else 162 tmp[i][j]=code_dead; 163 } 164 } 165 166 for(int i=1;i<=maxh;++i){ 167 for(int j=1;j<=maxw;++j){ 168 map[i][j]=tmp[i][j]; 169 } 170 } 171 } 172 void solve(){ 173 init(); 174 show(); 175 Sleep(update_waitms); 176 cleanscr(); 177 while(kbhit()); 178 while(1){ 179 if(!kbhit() ||cnt==0){ 180 if(cnt==0)kbhit(); 181 cnt++; 182 show(); 183 printf("按任意键暂停,按ESC退出,按e重新游戏"); 184 Sleep(update_waitms); 185 refresh(); 186 }else{ 187 int k=getch(); 188 if(k==27){ 189 cleanscr(); 190 exit(0); 191 }else if(k=='e'){ 192 cleanscr(); 193 return; 194 }else{ 195 gotoxy(0,maxh+4); 196 printf("按任意键继续 "); 197 pause(); 198 } 199 } 200 } 201 } 202 int main(){ 203 input(); 204 while(1){ 205 solve(); 206 } 207 return 0; 208 }
#include<cstdio> #include<windows.h> #include<ctime> #include<conio.h> const int maxh=19,// 高度 maxw=69; //宽度 const int maxharr=maxh+10, maxwarr=maxw+10; const int code_unknown=-1,//不知道死活 code_life=1,//生 code_dead=0;//死 const char show_life='*', show_dead=' '; int cnt=0, update_waitms=0, maxcells=500;//初始活细胞 int map[maxharr][maxwarr];//细胞分布图 int firstmap[maxharr][maxwarr];//初始的细胞分布图 void io_hidecur(); void cleanscr(); void pause(); void getrand(int &x,int &y); void arrayset(int val); void showinfo(); void showside(); bool is_inmap(int i,int j); void gotoxy(int x,int y); int is_alive(int i,int j); int getneighbor(int i,int j); void background(); void input(); void init(); void show(); void refresh(); void solve(); void io_hidecur(){//隐藏光标 CONSOLE_CURSOR_INFO cursor_info={1,0}; SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info); } void cleanscr(){ system("cls"); } void pause(){ system("pause>nul"); } void getrand(int &x,int &y){ x=rand()%(maxh+1),y=rand()%(maxw+1); if(x==0 || y==0) getrand(x,y); } void arrayset(int val){ for(int i=0;i<maxharr;++i){ for(int j=0;j<maxwarr;++j){ map[i][j]=val; } } } void showinfo(){ printf("[LifeGame CDsidi Version 1.0.0] "); printf("[Cycle:%d] ",cnt); } void showside(){ for(int i=0;i<=maxw;++i) printf("_"); printf(" "); } bool is_inmap(int i,int j){//判断i,j是否没有越界 if(i<=1 || j<=1 || i>maxh || j>maxw) return false; return true; } int is_alive(int i,int j){ if(!is_inmap(i,j) || map[i][j]!=code_life) return 0; return 1; } int getneighbor(int i,int j){ int ret= is_alive(i-1,j-1)+is_alive(i-1,j)+ is_alive(i-1,j+1)+is_alive(i,j-1)+ is_alive(i,j+1)+is_alive(i+1,j-1)+ is_alive(i+1,j)+is_alive(i+1,j+1); return ret; } void gotoxy(int x,int y){ //光标移动到(x,y)位置 HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE); COORD pos; pos.X=x; pos.Y=y; SetConsoleCursorPosition(handle,pos); } void background(){ char s[]="生命游戏是英国数学家约翰·何顿·康威在1970年发明的细胞自动机 这个世界中的每个方格居住着一个活着的或死了的细胞。 一个细胞在下一个时刻生死取决于相邻八个方格中活着的或死了的细胞的数量。 如果相邻方格活着的细胞数量过多,这个细胞会因为资源匮乏而在下一个时刻死去; 相反,如果周围活细胞过少,这个细胞会因太孤单而死去。 在本版本游戏中,用'*'表示细胞"; MessageBox(NULL,TEXT(s),"游戏介绍",MB_OK); } void input(){ int choose=MessageBox(NULL,TEXT("请问您是否第一次进行游戏?"),"游戏介绍",MB_OKCANCEL); if(choose==1){ background(); return; } int a,b; printf("请输入在刷新图片间隔等待的时间(ms)[如果<=0 则采用默认值] "); scanf("%d",&a); if(a<=0) return; printf("请输入初始有生命的细胞最大个数 "); scanf("%d",&b); update_waitms=a; maxcells=b; } void init(){ cleanscr(); srand(time(0)); cnt=0; system("title 生命游戏"); arrayset(code_unknown); for(int i=1;i<=maxcells;++i){ int x,y; getrand(x,y); map[x][y]=code_life; } io_hidecur(); cleanscr(); } void show(){ gotoxy(0,0); showinfo(); showside(); for(int i=1;i<=maxh;++i){ printf("|"); for(int j=1;j<=maxw;++j){ if(map[i][j]==code_life) printf("%c",show_life); else if(map[i][j]==code_dead){ printf("%c",show_dead); }else{ printf("?"); map[i][j]=code_dead; } } printf("| "); } showside(); } void refresh(){ int tmp[maxharr][maxwarr]; for(int i=1;i<=maxh;++i){ for(int j=1;j<=maxw;++j){ int neighbor=getneighbor(i,j);//这样不会越界 if(neighbor==3) tmp[i][j]=code_life; else if(neighbor==2) tmp[i][j]=map[i][j]; else tmp[i][j]=code_dead; } } for(int i=1;i<=maxh;++i){ for(int j=1;j<=maxw;++j){ map[i][j]=tmp[i][j]; } } } void solve(){ init(); show(); Sleep(update_waitms); cleanscr(); while(kbhit()); while(1){ if(!kbhit() ||cnt==0){ if(cnt==0)kbhit(); cnt++; show(); printf("按任意键暂停,按ESC退出,按e重新游戏"); Sleep(update_waitms); refresh(); }else{ int k=getch(); if(k==27){ cleanscr(); exit(0); }else if(k=='e'){ cleanscr(); return; }else{ gotoxy(0,maxh+4); printf("按任意键继续 "); pause(); } } } } int main(){ input(); while(1){ solve(); } return 0; }
1 #include<cstdio> 2 #include<windows.h> 3 #include<ctime> 4 #include<conio.h> 5 const int 6 maxh=19,// 高度 7 maxw=69; //宽度 8 const int 9 maxharr=maxh+10, 10 maxwarr=maxw+10; 11 const int 12 code_unknown=-1,//不知道死活 13 code_life=1,//生 14 code_dead=0;//死 15 const char 16 show_life='*', 17 show_dead=' '; 18 int cnt=0, 19 update_waitms=0, 20 maxcells=500;//初始活细胞 21 int map[maxharr][maxwarr];//细胞分布图 22 int firstmap[maxharr][maxwarr];//初始的细胞分布图 23 void io_hidecur(); 24 void cleanscr(); 25 void pause(); 26 void getrand(int &x,int &y); 27 void arrayset(int val); 28 void showinfo(); 29 void showside(); 30 bool is_inmap(int i,int j); 31 void gotoxy(int x,int y); 32 int is_alive(int i,int j); 33 int getneighbor(int i,int j); 34 void background(); 35 void input(); 36 void init(); 37 void show(); 38 void refresh(); 39 void solve(); 40 41 void io_hidecur(){//隐藏光标 42 CONSOLE_CURSOR_INFO cursor_info={1,0}; 43 SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info); 44 } 45 void cleanscr(){ 46 system("cls"); 47 } 48 void pause(){ 49 system("pause>nul"); 50 } 51 void getrand(int &x,int &y){ 52 x=rand()%(maxh+1),y=rand()%(maxw+1); 53 if(x==0 || y==0) getrand(x,y); 54 } 55 void arrayset(int val){ 56 for(int i=0;i<maxharr;++i){ 57 for(int j=0;j<maxwarr;++j){ 58 map[i][j]=val; 59 } 60 } 61 } 62 void showinfo(){ 63 printf("[LifeGame CDsidi Version 1.0.0] "); 64 printf("[Cycle:%d] ",cnt); 65 } 66 void showside(){ 67 for(int i=0;i<=maxw;++i) 68 printf("_"); 69 printf(" "); 70 } 71 bool is_inmap(int i,int j){//判断i,j是否没有越界 72 if(i<=1 || j<=1 || i>maxh || j>maxw) return false; 73 return true; 74 } 75 int is_alive(int i,int j){ 76 if(!is_inmap(i,j) || map[i][j]!=code_life) return 0; 77 return 1; 78 } 79 int getneighbor(int i,int j){ 80 int ret= 81 is_alive(i-1,j-1)+is_alive(i-1,j)+ 82 is_alive(i-1,j+1)+is_alive(i,j-1)+ 83 is_alive(i,j+1)+is_alive(i+1,j-1)+ 84 is_alive(i+1,j)+is_alive(i+1,j+1); 85 return ret; 86 } 87 void gotoxy(int x,int y){ //光标移动到(x,y)位置 88 HANDLE handle=GetStdHandle(STD_OUTPUT_HANDLE); 89 COORD pos; 90 pos.X=x; 91 pos.Y=y; 92 SetConsoleCursorPosition(handle,pos); 93 } 94 void background(){ 95 char s[]="生命游戏是英国数学家约翰·何顿·康威在1970年发明的细胞自动机 96 这个世界中的每个方格居住着一个活着的或死了的细胞。 97 一个细胞在下一个时刻生死取决于相邻八个方格中活着的或死了的细胞的数量。 98 如果相邻方格活着的细胞数量过多,这个细胞会因为资源匮乏而在下一个时刻死去; 99 相反,如果周围活细胞过少,这个细胞会因太孤单而死去。 100 在本版本游戏中,用'*'表示细胞"; 101 MessageBox(NULL,TEXT(s),"游戏介绍",MB_OK); 102 } 103 void input(){ 104 int choose=MessageBox(NULL,TEXT("请问您是否第一次进行游戏?"),"游戏介绍",MB_OKCANCEL); 105 if(choose==1){ 106 background(); 107 return; 108 } 109 int a,b; 110 printf("请输入在刷新图片间隔等待的时间(ms)[如果<=0 则采用默认值] "); 111 scanf("%d",&a); 112 if(a<=0) return; 113 printf("请输入初始有生命的细胞最大个数 "); 114 scanf("%d",&b); 115 update_waitms=a; 116 maxcells=b; 117 } 118 void init(){ 119 cleanscr(); 120 srand(time(0)); 121 cnt=0; 122 system("title 生命游戏"); 123 arrayset(code_unknown); 124 for(int i=1;i<=maxcells;++i){ 125 int x,y; 126 getrand(x,y); 127 map[x][y]=code_life; 128 } 129 io_hidecur(); 130 cleanscr(); 131 } 132 void show(){ 133 gotoxy(0,0); 134 showinfo(); 135 showside(); 136 for(int i=1;i<=maxh;++i){ 137 printf("|"); 138 for(int j=1;j<=maxw;++j){ 139 if(map[i][j]==code_life) 140 printf("%c",show_life); 141 else if(map[i][j]==code_dead){ 142 printf("%c",show_dead); 143 }else{ 144 printf("?"); 145 map[i][j]=code_dead; 146 } 147 } 148 printf("| "); 149 } 150 showside(); 151 } 152 void refresh(){ 153 int tmp[maxharr][maxwarr]; 154 for(int i=1;i<=maxh;++i){ 155 for(int j=1;j<=maxw;++j){ 156 int neighbor=getneighbor(i,j);//这样不会越界 157 if(neighbor==3) 158 tmp[i][j]=code_life; 159 else if(neighbor==2) 160 tmp[i][j]=map[i][j]; 161 else 162 tmp[i][j]=code_dead; 163 } 164 } 165 166 for(int i=1;i<=maxh;++i){ 167 for(int j=1;j<=maxw;++j){ 168 map[i][j]=tmp[i][j]; 169 } 170 } 171 } 172 void solve(){ 173 init(); 174 show(); 175 Sleep(update_waitms); 176 cleanscr(); 177 while(kbhit()); 178 while(1){ 179 if(!kbhit() ||cnt==0){ 180 if(cnt==0)kbhit(); 181 cnt++; 182 show(); 183 printf("按任意键暂停,按ESC退出,按e重新游戏"); 184 Sleep(update_waitms); 185 refresh(); 186 }else{ 187 int k=getch(); 188 if(k==27){ 189 cleanscr(); 190 exit(0); 191 }else if(k=='e'){ 192 cleanscr(); 193 return; 194 }else{ 195 gotoxy(0,maxh+4); 196 printf("按任意键继续 "); 197 pause(); 198 } 199 } 200 } 201 } 202 int main(){ 203 input(); 204 while(1){ 205 solve(); 206 } 207 return 0; 208 }