跳到正文

个人记录

C++文明模拟器

GODTREE约 16 分钟
原文
文章信息与版本

文章信息

发布
2023年5月28日
收录
2025年12月12日
更新
2026年11月7日
篇幅
约 16 分钟
获赞
1

版本

  1. v1当前

改编自胎神的文明模拟器

C++
#include<iostream> 
#include<cstdio> 
#include<fstream> 
#include<algorithm> 
#include<cmath> 
#include<deque> 
#include<vector> 
#include<queue> 
#include<string> 
#include<cstring> 
#include<map> 
#include<stack> 
#include<set> 
#include<windows.h>
#include<conio.h>
#include<ctime>
using namespace std;

const int maxn=100;
const int sleep=500;//屏幕速度 
int can_dark;
struct civil//文明 
{
	int level;
	vector<int>planets;
	string name;
	bool alive;
	int soc;
}c[maxn];

struct planet//星球 
{
	int dist;
	int belong;
	string name;
}p[maxn];

struct warship//舰队 
{
	int from;
	int frompl;
	int to;
	int ti;
	int army;
	warship(){}
	warship(int _frompl,int _to,int _ti):from(p[_frompl].belong),frompl(_frompl),to(_to),ti(_ti){}
};
struct order
{
	int lv,id,soc;
};
bool cmp(order a,order b)
{
	if (a.lv!=b.lv)
		return a.lv>b.lv;
	else return a.soc>b.soc;
}
bool operator<(warship a,warship b)
{
	return a.ti>b.ti;
}
string lv[]={"石器时代","青铜时代","铁器时代","蒸汽时代","电气时代","原子时代","信息时代","星系大航海时代","星际大航海时代","宇宙大航海时代","光速航行时代","小宇宙时代","宇宙级文明时代","跨宇宙时代","升维时代","规则时代","创世时代","灭世时代","问道时代","本源时代"};
string destroy[]={"高温","极寒","陨石","超级地震","太阳氦闪","太阳坍缩","超级黑洞"};
string name[]={"安","罗","苏","修","可勒","达","陨","蔺维","库","阿尔","桥","乌","吉利","英","奈尔","新","赫拉","级","罗","萨拉","夏","美","尔"};
string Speed[]={"1/1000光速","1/100光速","1/10光速","光速","光速","十倍光速","百倍光速"};
string social[]={"封建帝制","自由资本主义","帝国主义","社会主义","共产主义"};
int social_f[5]={1,2,3,5,10};
priority_queue<warship>q,r;
int sum;

void lose(int id);
string sFromInt(int x);

int main(){
	system("COLOR 0");
	int i,j,n,year,cnt;
	unsigned int sd=2333;
	cout<<"文明的数量:";
	cin>>n;//输入:第一行字符串个数,接下来若干行字符串 
	sum=n;
	cout<<n<<"个文明的名字:"<<endl;
	for(i=1;i<=n;i++)
	{
		cin>>c[i].name;
		p[i].name=c[i].name;
		unsigned int tmp=622;
		for(j=0;j<c[i].name.size();j++) tmp=703*tmp+c[i].name[j];
		sd=sd*3071+tmp;
		srand(time(NULL));//每个名字(文明)的初始等级是确定的,类似名字竞技场 
		c[i].level=-1;
		c[i].soc=0;
		//cout<<"level:"<<c[i].level<<endl;
		c[i].planets.push_back(i);
		c[i].alive=true;
		p[i].dist=rand()%50+10;
		p[i].belong=i;
	}
	cout<<"宇宙在一片黑暗中诞生了..."<<endl;
	Sleep(2000);
	system("cls"); 
	srand(time(NULL));//之后的演化会受所有输入名字的共同影响,也会被名字顺序影响 
	for(i=n+1;i<=n*3;i++)//1~n星球是n个文明的母星
	{
		p[i].dist=rand()%50+10;
		//n+1~n*3星球是无人星球 
		p[i].belong=0;
		int x=rand()%5+1;
		for (int j=1;j<=x;j++)
		{
			p[i].name+=name[rand()%20];
		}
		/*
		  //n+i星球属于第i个文明 
		  p[i].belong=i-n;
		  p[i].name=c[i-n].name+"2号";
		  c[i-n].planets.push_back(i);
		 */
	}
	for(year=0;;year+=10)
	{
		//		cout<<"第"<<year<<"年"<<endl; 
		while(!q.empty()&&year>q.top().ti)//舰队到达 
		{
			warship now=q.top();
			q.pop();
			int id=now.to;
			i=now.from;
			if(p[id].belong==i) continue;
			cout<<"来自"<<c[i].name<<"文明的舰队到达"<<p[id].name<<"星球,";
			if(p[id].belong==0||(c[p[id].belong].level<c[i].level&&c[p[id].belong].soc<c[i].soc))
			{
				cout<<"并占领了"<<p[id].name<<"星球"<<endl;
				lose(id);
				p[id].belong=i;
				c[i].planets.push_back(id);
				if(!c[i].alive)
				{
					cout<<c[i].name<<"文明重生"<<endl;
					c[i].alive=true;
					sum++;
				}
			}
			else
			{
				cout<<"攻击失败"<<endl;
				if(c[p[id].belong].level>=7)
				{
					cout<<p[id].name<<"星球广播了"<<p[now.frompl].name<<"星球的位置"<<endl;
					if(rand()%2) r.push(warship(0,now.frompl,rand()%5*10+year+p[now.frompl].dist+p[id].dist));
				}
			}
			Sleep(sleep);
		}
		
		while(!r.empty()&&year>r.top().ti&&can_dark>0)//黑暗森林打击到达 
		{
			int id=r.top().to;
			r.pop();
			cout<<p[id].name<<"星球被黑暗森林打击"<<endl;
			lose(id);
			p[id].belong=0;
			Sleep(sleep);
		}	
		for(i=1;i<=n;i++)
		{
			if(!c[i].alive) continue;
			if(rand()%(20*c[i].level+1)==0)//技术爆炸 
			{
				if(c[i].level<=20)
				{
					c[i].level++;
					cout<<c[i].name<<"文明发生技术爆炸,";
					cout<<"进入"<<lv[c[i].level]<<endl;
				}
				if (lv[c[i].level]=="宇宙级文明时代")
				{
					can_dark++;
				}
				Sleep(sleep);
			}
			else if(rand()%10==0&&c[i].level>6&&c[i].planets.size()<n*2)//发出舰队 
			{
				int id;
				do
				{
					id=rand()%(n*2)+1;
				}while(p[id].belong==i);
				int speed=min(c[i].level,13)-7;
				int d=p[c[i].planets[0]].dist+p[id].dist;
				if (speed==0)
				{
					d*=1000;
				}
				if (speed==1)
				{
					d*=100;
				}
				if (speed==2)
				{
					d*=10;
				}
				if (speed==4)
				{
					d/=10;
				}
				if (speed==5)
				{
					d/=100;
				}
				cout<<c[i].name<<"文明向"<<p[id].name<<"星球发出"<<Speed[speed]<<"舰队,将在"<<d<<"年后到达"<<endl;
				q.push(warship(c[i].planets[0],id,year+d));
				Sleep(sleep);
			}
		}
		if(rand()%100==0)
		{
			int id,temp=rand()%7;
			do
			{
				id=rand()%(n*2)+1;
			}while(p[id].belong==0);
			if (c[id].level-1<temp)
			{
				cout<<p[id].name<<"星球上的文明,在"<<destroy[temp]<<"危机中毁灭了"<<endl;
				lose(id);
				p[id].belong=0;
				Sleep(sleep);
				if(sum==1) 
				{
					system("cls");
					system("COLOR 1");
					cout<<"最后的胜者出现了!"<<endl;
					for (int i=1;i<=n;i++)
					{
						if (c[i].alive==true)
						{
							cout<<"那就是"<<c[i].name<<"文明";
							break;
						}
					}
					return 0;
				}
			}
			else
			{
				cout<<p[id].name<<"星球上的文明战胜了"<<destroy[temp]<<"危机"<<endl;
			}
		}
		for (int i=sum+1;i<=sum*3;i++)
		{
			if (p[i].belong==0&&rand()%9999==0)
			{
				cout<<"在"<<p[i].name<<"星球上,诞生了"<<p[i].name<<"文明"<<endl;
				n++;
				c[n].name=p[i].name;
				unsigned int tmp=622;
				for(j=0;j<c[n].name.size();j++) tmp=703*tmp+c[n].name[j];
				sd=sd*3071+tmp;
				srand(time(NULL));
				c[n].level=-1;
				//cout<<"level:"<<c[i].level<<endl;
				c[n].planets.push_back(i);
				c[n].alive=true;
				c[n].soc=0;
				p[n].dist=rand()%50+10;
				p[n].belong=n;
			}
		}
		for (int i=1;i<=n;i++)
		{
			if (!c[i].alive)
			{
				continue;
			}
			if (c[i].soc==0&&rand()%100==0&&c[i].level>=3)
			{
				cout<<c[i].name<<"文明,爆发了资本主义革命。成为"<<social[1]<<"国家\n";
				c[i].soc=1;
			}
			if (c[i].soc==1&&(rand()%1000==0||(c[i].level>=5&&rand()%5==0)))
			{
				if (rand()%100==0)
				{
					cout<<c[i].name<<"文明惨遭"<<social[0]<<"复辟\n";
					c[i].soc=0;
				}
				else
				{
					cout<<c[i].name<<"文明发展成为"<<social[2]<<"国家\n";
					c[i].soc=2;
				}
			}
			if (c[i].soc==2&&rand()%1000==0)
			{
				if (rand()%2==0)
				{
					cout<<c[i].name<<"文明,爆发了社会主义革命。成为"<<social[3]<<"国家\n";
					c[i].soc=3;
				}
				else
				{
					cout<<c[i].name<<"文明发展成为"<<social[2]<<"国家\n";
					c[i].soc=2;
				}
			}
			if (c[i].soc==3&&rand()%10000==0)
			{
				cout<<c[i].name<<"文明发展成为"<<social[4]<<"国家\n";
				c[i].soc=4;
			}
			if (c[i].soc==3&&rand()%1000==0)
			{
				cout<<c[i].name<<"文明修啦!成为"<<social[2]<<"国家\n";
				c[i].soc=2;
			}
		}
		if (year%1000==0&&year!=0)
		{
			system("cls");
			cout<<"第"<<year/1000<<"纪元结算:"<<endl;
			cout<<"__________________________"<<endl;
			order s[n+1];
			for (int i=1;i<=n;i++)
			{
				s[i].id=i;
				s[i].lv=c[i].level;
				s[i].soc=c[i].soc;
			}
			sort(s+1,s+n+1,cmp);
			for (int j=1;j<=n;j++)
			{
				cout<<"第"<<j<<"名:"<<c[s[j].id].name<<"文明 等级:"<<lv[s[j].lv];
				if(s[j].lv==-1)cout<<"未开化";
				cout<<" "<<social[s[j].soc]<<endl;
			}
			cout<<"__________________________"<<endl;
		}
	}
	return 0;
}

void lose(int id)
{
	int i=p[id].belong;
	if(i==0) return;
	vector<int>&v=c[i].planets;
	vector<int>::iterator it;
	for(it=v.begin();it!=v.end();it++) if(*it==id) break;
	v.erase(it);
	if(v.size()==0)
	{
		c[i].alive=false;
		sum--;
		cout<<">>"<<c[i].name<<"文明毁灭了"<<endl;
		Sleep(sleep*2);
	}
}

string sFromInt(int x)
{
	string ret;
	while(x)
	{
		ret=char(x%10+'0')+ret;
		x/=10;
	}
	return ret;
}

##附带懒人取名器:

C++
#include <bits/stdc++.h>
using namespace std;
string name[]={"安","罗","苏","修","可勒","达","陨","蔺维","库","阿尔","桥","乌","吉利","英","奈尔","新","赫拉","级","利维","亚","格林","歌","暗","顿","卡尔","晓夫","戈尔巴","利钦","费加","斯坦","特","杰洛","米","索","森","天下","元","明","吉鲁","神","冰","夏","王","者","吞食","碳基","硅基","氨基","联合"};
int main()
{
	int n;
	cin>>n;
	srand(n+1+time(NULL));
	for (int i=1;i<=n;i++)
	{
		int x=rand()%5+2;
		for (int j=1;j<=x;j++)
		{
			cout<<name[rand()%50];
		}
		cout<<endl;
	}
	return 0;
}
原文
存档评论5 条抓取于