⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 simulation.cpp

📁 本程序通过在200*100的格子上按照一定的密度随机洒点模拟生命游戏 默认的密度为0.5
💻 CPP
字号:
// Simulation.cpp: implementation of the Simulation class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "Pattern_2.h"
#include "Simulation.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

Simulation::Simulation(char * filename)
:Length(200),Depth(100),Density(0.5),timepast(0)
{
	for (int i=0;i<Length;i++)
		for (int j=0;j<Depth;j++)
		{
			MyMap[i][j]=0;
			MyMapb[i][j]=0;
		}
	if(filename!=NULL)
	{
		ifstream readfile(filename,ios::in);   // Read file
			}
	else
		Initialize();
}

Simulation::~Simulation()
{
}

void Simulation::Initialize()
{
	srand (unsigned (time(NULL)) );
	for (int i=0; i<Length; i++)
		for (int j=0; j<Depth; j++)
		{
			if(rand()/32767.0<Density)
				MyMap[i][j]=1;
			else MyMap[i][j]=0;
			MyMapb[i][j]=MyMap[i][j];
		}
}

void Simulation::RunIt()
{
	int i,j;
	timepast++;
	if ( (MyMap[0][1]+MyMap[1][0]+MyMap[1][1]==2 && MyMap[1][1]==1)					 //左上角
		|| MyMap[0][1]+MyMap[1][0]+MyMap[1][1]==3 )
		MyMapb[0][0]=1;
	else MyMapb[0][0]=0;
	if ( (MyMap[Length-2][0]+MyMap[Length-1][1]+MyMap[Length-2][1]==2 && MyMapb[Length-1][0]==1)		//右上角
		|| MyMap[Length-2][0]+MyMap[Length-1][1]+MyMap[Length-2][1]==3 )
		MyMapb[Length-1][0]=1;
	else MyMapb[Length-1][0]=0;
	if ( (MyMap[0][Depth-2]+MyMap[1][Depth-1]+MyMap[1][Depth-2]==2 && MyMapb[0][Depth-1]==1)		//左下角
		|| MyMap[0][Depth-2]+MyMap[1][Depth-1]+MyMap[1][Depth-2]==3 )
		MyMapb[0][Depth-1]=1;
	else MyMapb[0][Depth-1]=0;
	if ( (MyMap[Length-2][Depth-1]+MyMap[Length-1][Depth-2]+MyMap[Length-2][Depth-2]==2 && MyMapb[Length-1][Depth-1]==1)	//右下角
		|| MyMap[Length-2][Depth-1]+MyMap[Length-1][Depth-2]+MyMap[Length-2][Depth-2]==3 )
		MyMapb[Length-1][Depth-1]=1;
	else MyMapb[Length-1][Depth-1]=0;
	for (i=1;i<Length-1;i++)
	{
		if ( (MyMap[i-1][0]+MyMap[i+1][0]+MyMap[i-1][1]+MyMap[i][1]+MyMap[i+1][1]==2 && MyMap[i][0]==1)		//上边
			|| MyMap[i-1][0]+MyMap[i+1][0]+MyMap[i-1][1]+MyMap[i][1]+MyMap[i+1][1]==3 )
			MyMapb[i][0]=1;
		else MyMapb[i][0]=0;
		if ( (MyMap[i-1][Depth-1]+MyMap[i+1][Depth-1]+MyMap[i-1][Depth-2]+MyMap[i][Depth-2]+MyMap[i+1][Depth-2]==2		//下边
			&& MyMap[i][Depth-1]==1)
			|| MyMap[i-1][Depth-1]+MyMap[i+1][Depth-1]+MyMap[i-1][Depth-2]+MyMap[i][Depth-2]+MyMap[i+1][Depth-2]==3 )
			MyMapb[i][Depth-1]=1;
		else MyMapb[i][Depth-1]=0;
	}
	for (i=1;i<Depth-1;i++)
	{
		if ( (MyMap[0][i-1]+MyMap[0][i+1]+MyMap[1][i-1]+MyMap[1][i]+MyMap[1][i+1]==2 && MyMap[0][i]==1)		//左边
			|| MyMap[0][i-1]+MyMap[0][i+1]+MyMap[1][i-1]+MyMap[1][i]+MyMap[1][i+1]==3 )
			MyMapb[0][i]=1;
		else MyMapb[0][i]=0;
		if ( (MyMap[Length-1][i-1]+MyMap[Length-1][i+1]+MyMap[Length-2][i-1]+MyMap[Length-2][i]+MyMap[Length-2][i+1]==2		//右边
			&& MyMap[Length-1][i]==1)
			|| MyMap[Length-1][i-1]+MyMap[Length-1][i+1]+MyMap[Length-2][i-1]+MyMap[Length-2][i]+MyMap[Length-2][i+1]==3 )
			MyMapb[Length-1][i]=1;
		else MyMapb[Length-1][i]=0;
	}
	for (i=1;i<Length-1;i++)												//中间
		for(j=1;j<Depth-1;j++)
			if( (MyMap[i-1][j-1]+MyMap[i][j-1]+MyMap[i+1][j-1]+MyMap[i-1][j]+MyMap[i+1][j]
				+MyMap[i-1][j+1]+MyMap[i][j+1]+MyMap[i+1][j+1]==2 && MyMap[i][j]==1)
				|| MyMap[i-1][j-1]+MyMap[i][j-1]+MyMap[i+1][j-1]+MyMap[i-1][j]+MyMap[i+1][j]
				+MyMap[i-1][j+1]+MyMap[i][j+1]+MyMap[i+1][j+1]==3 )
				MyMapb[i][j]=1;
			else MyMapb[i][j]=0;
	for (i=0;i<Length;i++)
		for(j=0;j<Depth;j++)
			MyMap[i][j]=MyMapb[i][j];

}


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -