编程c++模拟疫情游戏

**

编程c++模拟疫情游戏

疫情期,写代码,我写你不写
下面直接附代码

#include<stdio.h>
#include<windows.h>
#include<time.h> 
#include<bits/stdc++.h>
using namespace std;

int somepeople=2000,chuangwei=200,shtime=7,viruspropagation=40,treatment=93,deadment=100,virusspreadingfar=1;
int immunizetime=160;
double carefully=0.33,runpeople=0.221;
/*
    新冠 2000,200,7,65,5,6,2,180,0.66 
   SARS 2000,200,7,60,93,100,1,120,0.66
  流感 2000,200,3,40,99,100,1,160,0.33*/
int illpeople=0,zhiyupeople=0,deadpeople=0;


struct people{
	int x,y;//x列 y行 
	int health_subhealth_ill;//health,subhealth(亚健康,即处在潜伏期(潜伏期也传染)),ill分别对应1,2,3;
	int forward;//上下左右 1,2,3,4
	int work;//步数 
	int subhealthtime,healthtime;
	int inhospital;//在,不在 1,0
	int dead;//死,活 1,0
	int firstill;//first,not 0,1
	int firstillture;//first,not 0,1
}whp[20000];

struct hospital{
	int x,y;
}whh[20000];

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

void ColorChoose(int color)   //颜色选择函数
{
	switch(color)
	{	
		case 1:               //天蓝色
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN|FOREGROUND_BLUE);
		break;
		case 2:               //绿色
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_GREEN);	
		break;
		case 3:               //黄色
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_GREEN);
		break;
		case 4:               //红色
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED);
		break;
		case 5:               //紫色
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE);
		break;
		case 6:               //白色
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN);
		break;
		case 7:               //深蓝色
		SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_BLUE);
		break;
	}
}

void spreading(){
	for(int i=1;i<=somepeople;i++){
		if(whp[i].firstill==1){
			whp[i].healthtime+=1;
		}
		if(whp[i].healthtime==immunizetime&&whp[i].firstill==1){
			whp[i].healthtime=0;
			whp[i].firstill=0;
			gotoxy(whp[i].x,whp[i].y);
			ColorChoose(6);
			printf("#"); 
		}
	}
	for(int i=1;i<=somepeople;i++){
		if((whp[i].health_subhealth_ill==2||whp[i].health_subhealth_ill==3)&&whp[i].inhospital==0){
			for(int j=1;j<=somepeople;j++){
				if(abs(whp[j].x-whp[i].x)+abs(whp[j].y-whp[i].y)<=virusspreadingfar&&whp[j].health_subhealth_ill==1){
					int tmp=rand()%100*(1+carefully);
					if(tmp<=viruspropagation&&whp[j].firstill==0){
						whp[j].health_subhealth_ill=2;
					}
				}
			}
		}
		if(whp[i].health_subhealth_ill==2){
			whp[i].subhealthtime+=1;
		}
		if(whp[i].subhealthtime>shtime&&whp[i].health_subhealth_ill==2){
			whp[i].health_subhealth_ill=3;
			if(whp[i].firstillture==0)illpeople++;
		}
	}
}

void workanddealwith(){
	for(int i=1;i<=somepeople;i++){
		if(chuangwei>0&&whp[i].inhospital==0){
			if(whp[i].health_subhealth_ill==3){
				whp[i].inhospital=1;
				gotoxy(whp[i].x,whp[i].y);
				printf(" "); 
				chuangwei--;
			}
		}
	}
	for(int i=1;i<=somepeople;i++){
		int tmp1=rand()%1000;
		int tmp2=rand()%100;
		double tmpsum=tmp2*1.0+tmp1*1.0/1000;
		if(tmpsum<=runpeople){
			gotoxy(whp[i].x,whp[i].y);
			printf(" "); 
			whp[i].x=rand()%120;
			whp[i].y=rand()%50;
			gotoxy(whp[i].x,whp[i].y);
			if(whp[i].firstill==0&&whp[i].health_subhealth_ill==1)ColorChoose(6);
			else if(whp[i].health_subhealth_ill==2)ColorChoose(1);
			else if(whp[i].firstill==1&&whp[i].health_subhealth_ill==1)ColorChoose(2);
			else ColorChoose(4);
			printf("#");
		}
	}
	for(int i=1;i<=somepeople;i++){
		if(whp[i].inhospital==0){
			if(whp[i].forward==1){
				gotoxy(whp[i].x,whp[i].y);
				printf(" "); 
				whp[i].y=whp[i].y-whp[i].work>=0?whp[i].y-whp[i].work:0;
				gotoxy(whp[i].x,whp[i].y);
				if(whp[i].firstill==0&&whp[i].health_subhealth_ill==1)ColorChoose(6);
				else if(whp[i].health_subhealth_ill==2)ColorChoose(1);
				else if(whp[i].firstill==1&&whp[i].health_subhealth_ill==1)ColorChoose(2);
				else ColorChoose(4);
				printf("#"); 
			}
			if(whp[i].forward==2){
				gotoxy(whp[i].x,whp[i].y);
				printf(" "); 
				whp[i].y=whp[i].y+whp[i].work<50?whp[i].y+whp[i].work:49;
				gotoxy(whp[i].x,whp[i].y);
				if(whp[i].firstill==0&&whp[i].health_subhealth_ill==1)ColorChoose(6);
				else if(whp[i].health_subhealth_ill==2)ColorChoose(1);
				else if(whp[i].firstill==1&&whp[i].health_subhealth_ill==1)ColorChoose(2);
				else ColorChoose(4);
				printf("#"); 
			}
			if(whp[i].forward==3){
				gotoxy(whp[i].x,whp[i].y);
				printf(" "); 
				whp[i].x=whp[i].x-whp[i].work>=0?whp[i].x-whp[i].work:0;
				gotoxy(whp[i].x,whp[i].y);
				if(whp[i].firstill==0&&whp[i].health_subhealth_ill==1)ColorChoose(6);
				else if(whp[i].health_subhealth_ill==2)ColorChoose(1);
				else if(whp[i].firstill==1&&whp[i].health_subhealth_ill==1)ColorChoose(2);
				else ColorChoose(4);
				printf("#"); 
			}
			if(whp[i].forward==4){
				gotoxy(whp[i].x,whp[i].y);
				printf(" "); 
				whp[i].x=whp[i].x+whp[i].work<120?whp[i].x+whp[i].work:119;
				gotoxy(whp[i].x,whp[i].y);
				if(whp[i].firstill==0&&whp[i].health_subhealth_ill==1)ColorChoose(6);
				else if(whp[i].health_subhealth_ill==2)ColorChoose(1);
				else if(whp[i].firstill==1&&whp[i].health_subhealth_ill==1)ColorChoose(2);
				else ColorChoose(4);
				printf("#"); 
			}
		}
	}
}

void hospital(){
	for(int i=1;i<=somepeople;i++){
		if(whp[i].inhospital==1&&whp[i].dead==0){
			int tmp=rand()%100,tmp1=rand()%100;
			if(tmp<treatment){
				whp[i].x=whh[rand()%10].x;
				whp[i].y=whh[rand()%10].y;
				whp[i].inhospital=0;
				whp[i].health_subhealth_ill=1;
				whp[i].subhealthtime=0;
				gotoxy(whp[i].x,whp[i].y);
				whp[i].health_subhealth_ill=1;
				if(whp[i].firstill==0&&whp[i].health_subhealth_ill==1)ColorChoose(6);
				else if(whp[i].health_subhealth_ill==2)ColorChoose(1);
				else if(whp[i].firstill==1&&whp[i].health_subhealth_ill==1)ColorChoose(2);
				else ColorChoose(4);
				printf("#"); 
				if(whp[i].firstillture==0)zhiyupeople++,whp[i].firstill=1,whp[i].firstillture=1;
				chuangwei++;
			}
			else if(tmp1<deadment){
				whp[i].dead=1;
				deadpeople++;
				chuangwei++;
			}
		}
	}
}

void baofa(){
	system("cls");
	ColorChoose(6);
	gotoxy(125,2);
	printf("确诊 : %d   person",illpeople); 
	gotoxy(125,4);
	printf("治愈 : %d   person",zhiyupeople); 
	gotoxy(125,6);
	printf("死亡 : %d   person",deadpeople); 
	ColorChoose(6);
	gotoxy(125,10);
	printf("白色#表示healthy person"); 
	gotoxy(125,12);
	printf("蓝色#表示during the incubation period"); 
	gotoxy(125,14);
	printf("红色#表示ill person");
	gotoxy(125,16);
	printf("绿色#表示recovering person(immune person)");
	gotoxy(125,20);
	ColorChoose(3);
	printf("制作者:SB");
	ColorChoose(5);
	printf("n");
	while(1){
		for(int i=1;i<=somepeople;i++){
			whp[i].forward=rand()%4+1;
			whp[i].work=rand()%3;
		}
		spreading();
		workanddealwith();
		hospital();
		ColorChoose(6);
		gotoxy(132,2);
		printf("%d",illpeople); 
		gotoxy(132,4);
		printf("%d",zhiyupeople); 
		gotoxy(132,6);
		printf("%d",deadpeople); 
//		Sleep(200);
	}
}

int main(){
	system("mode con cols=180 lines=70");
	srand(time(0));
	for(int i=1;i<=somepeople;i++){
		whp[i].x=rand()%120;
		whp[i].y=rand()%50;
		whp[i].inhospital=0;
		whp[i].health_subhealth_ill=1;
		whp[i].subhealthtime=0,whp[i].healthtime=0;
		whp[i].firstill=0;
		whp[i].firstillture=0;
	}
	for(int i=0;i<=9;i++){
		whh[i].x=rand()%120;
		whh[i].y=rand()%50;
	}
	printf("Thanks for you watch this process which simulate the virus's spreading.");
//	Sleep(5000); 
	system("cls");
	printf("Waiting...");
//	Sleep(5000);
	whp[4].health_subhealth_ill=2; 
	baofa();
	return 0;
} 

版权声明:本文为weixin_42704122原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。