2019春第二次课程设计实验报告
一、实验项目名称
生命游戏
二、实验项目功能描述
利用上周的游戏框架进行初始化,输出静态的生命状态。二维数组int cells[High][Width]记录所有位置细胞的存活状态,1表示生、0表示死。
三、项目模快结构介绍
   #include<stdio.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#define High 25    //游戏画面尺寸
#define Width 50
//全局变量
int cells[High][Width];   //所有位置细胞生1或死0
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 startup()   //数据初始化
 {
 	int i,j;
 	for(i=0;i<High;i++)  //随机初始化
	   for(j=0;j<Width;j++)
	   {
	   	cells[i][j] = 1;
		} 
  } 
  
void show()   //显示画面
{
	gotoxy(0,0);  //光标移动到原点位置,以下重画清屏
	int i,j; 
	for(i=0;i<=High;i++)
	{
		for(j=0;j<=Width;j++)
		{
			if(cells[i][j]==1)
			  printf("*");  //输出活的细胞
			else
			  printf(" "); 
		}
		printf("\n");
	}
 }
void updateWithoutInput()   //与用户输入无关的更新
{
	int NewCells[High][Width];   //下一帧的细胞情况
	int NeibourNumber;   //统计邻居个数
	int i,j;
	for(i = 1;i<=High-1;i++)
	{
		for(j=1;j<=Width-1;j++)
		{
			NeibourNumber = cells[i-1][j-1] + cells[i-1][j] + cells[i-1][j+1] + cells[i][j-1] + cells[i][j+1] + cells[i+1][j-1] + cells[i+1][j] + cells[i+1][j+1];
			if(NeibourNumber==3)
			   NewCells[i][j] = 1;
			else if(NeibourNumber==2)
			   NewCells[i][j] = cells[i][j];
			else
			   NewCells[i][j] = 0;
		}
	}
	for(i=1;i<=High-1;i++)
	   for(j=1;j<Width-1;j++)
	     cells[i][j] = NewCells[i][j];
 }
 
 void updateWithInput()   //与用户输入有关的更新
 {
  }
  
int main()
{
	startup();  //数据的初始化
	while(1)   //游戏循环执行
	{
		show();   //显示画面
		updateWithoutInput();  //与用户输入无关的更新
		updateWithInput();   //与用户输入有关的更新 
	 }
	 return 0; 
 } 
四、实现界面展示


五、代码托管链接
https://gitee.com/gaotian250yj912/lemon/blob/master/生命游戏.cpp
六、实验总结
书上代码进行了一定的改变前后运行结果也不同
如:cells[i][j] = rand()%2  改为了  cells[i][j] = 1;使前后结果出现明显差异
书上代码中有这么一行sleep(50),但是代码运行错误,找了好久终于找到问题
未在代码运行范围内声明sleep,将sleep(50)这行删掉代码就能够运行了;
这周还是看书学习,按照书上的代码打,一边打一边熟悉课程设计的一些知识,每天都为期末的结课设计忙碌着,但是每天码代码似乎成为了习惯,一天没写,总觉得自己还有事没完成,每天也需要代码充实自己的生活。加油,期末!!!
                    
                
                
            
        
浙公网安备 33010602011771号