生命游戏——一款用二维数组实现的C语言游戏开发

生命游戏也算比较古老的游戏,但是其中涉及到的编程思维还是很值得借鉴的,尤其是用来动画演示具体条件下的某群体动态变化,也可以比较粗略的描绘群体中疫情的传播与蔓延

游戏的初始化

生命游戏里,每个小格子里生命如果存活值为1,如果死亡值为0,可以通过所有元素的生命状态输出得到相应的图案

生命游戏初始化

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<time.h>

#define High 25		//画面尺寸 
#define Width 50

int cells[High][Width];

void gotoxy(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos); 
}

void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void startup()
{
	int i, j;
	for(i = 0; i < High; i++)		//数据初始化 
	{
		for(j = 0; j < Width; j++)
		{
			cells[i][j] = rand() % 2;	//随机初始化 
		}
	}
 } 

void show()
{
	gotoxy(0, 0);
	HideCursor();
	int i, j;
	for(i = 0; i <= High; i++)
	{
		for(j = 0; j <= Width; j++)
		{
			if(cells[i][j] == 1)
				printf("*");		//输出细胞 
			else
				printf(" ");
			sleep(1);
		}
		
		printf("\n");
	}
	
}

void updateWithoutInput()
{
}

void updateWithInput()
{
}

int main()
{
	startup();
	while(1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	} 
	return 0;
} 

繁衍或死亡

生命游戏的演化规则如下:
1、如果一个细胞周围有3活细胞,则该细胞就活过来(即若该细胞若原本死了,则复活;若该细胞原本就是活的,则保持不变)
2、如果一个细胞周围有两个活细胞,则该细胞的生死状态不变
3、其他情况下,该细胞为死(即该细胞若原本是活的,则死了;若原本是死的,仍保持死)

在细胞随机分布的情况下:

初始状态随机的情况

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<time.h>


#define High 25		//画面尺寸 
#define Width 50

int cells[High][Width];


void gotoxy(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos); 
}

void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void startup()
{
	int i, j;
	for(i = 0; i < High; i++)		//数据初始化 
	{
		for(j = 0; j < Width; j++)
		{
			cells[i][j] = rand() % 2;	//随机初始化 
//			cells[i][j] = 1;
		}
	}
 } 

void show()
{
	gotoxy(0, 0);
	HideCursor();
	int i, j;
	for(i = 0; i <= High; i++)
	{
		for(j = 0; j <= Width; j++)
		{
			if(cells[i][j] == 1)
				printf("*");		//输出细胞 
			else
				printf(" ");
//			sleep(1);
		}
		sleep(1);
		printf("\n");
	}
	
}

void updateWithoutInput()
{
	int New[High][Width];
	int Neighbnum = 0;
	int i, j;
	for(i = 0; i < High; i++)
	{
		for(j = 0; j < Width; j++)
		{
			Neighbnum = 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(Neighbnum == 3)
				New[i][j] = 1;
			else if(Neighbnum == 2)
				New[i][j] = cells[i][j];
			else
				New[i][j] = 0; 
		}
	}
	for(i = 1; i < High; i++)
	{
		for(j = 1; j < Width; j++)
		{
			cells[i][j] = New[i][j];
		}
	}
}

void updateWithInput()
{
}

int main()
{
	startup();
	while(1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	} 
	return 0;
} 

在细胞都在的情况下

我们会发现每一帧的变化都是对称的,每一帧意思就是每一轮

初始全是活细胞的情况

#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
#include<windows.h>
#include<time.h>


#define High 25		//画面尺寸 
#define Width 50

int cells[High][Width];


void gotoxy(int x, int y)
{
	HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(handle, pos); 
}

void HideCursor()
{
	CONSOLE_CURSOR_INFO cursor_info = {1, 0};
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}

void startup()
{
	int i, j;
	for(i = 0; i < High; i++)		//数据初始化 
	{
		for(j = 0; j < Width; j++)
		{
//			cells[i][j] = rand() % 2;	//随机初始化 
			cells[i][j] = 1;
		}
	}
 } 

void show()
{
	gotoxy(0, 0);
	HideCursor();
	int i, j;
	for(i = 0; i <= High; i++)
	{
		for(j = 0; j <= Width; j++)
		{
			if(cells[i][j] == 1)
				printf("*");		//输出细胞 
			else
				printf(" ");
//			sleep(1);
		}
		sleep(1);
		printf("\n");
	}
	
}

void updateWithoutInput()
{
	int New[High][Width];
	int Neighbnum = 0;
	int i, j;
	for(i = 0; i < High; i++)
	{
		for(j = 0; j < Width; j++)
		{
			Neighbnum = 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(Neighbnum == 3)
				New[i][j] = 1;
			else if(Neighbnum == 2)
				New[i][j] = cells[i][j];
			else
				New[i][j] = 0; 
		}
	}
	for(i = 1; i < High; i++)
	{
		for(j = 1; j < Width; j++)
		{
			cells[i][j] = New[i][j];
		}
	}
}

void updateWithInput()
{
}

int main()
{
	startup();
	while(1)
	{
		show();
		updateWithoutInput();
		updateWithInput();
	} 
	return 0;
} 

如果喜欢我的文章,请记得一键三连哦,点赞关注收藏,你的每一个赞每一份关注每一次收藏都将是我前进路上的无限动力 !!!↖(▔▽▔)↗感谢支持,明天我们不见不散!!!

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值