//#include<bits/stdc++.h>
#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;//屏幕速度
struct civil//文明
{
int level;
vector<int>planets;
string name;
bool alive;
}c[maxn];
struct planet//星球
{
int dist;
int belong;
string name;
}p[maxn];
struct warship//舰队
{
int from;
int frompl;
int to;
int ti;
warship(){}
warship(int _frompl,int _to,int _ti):from(p[_frompl].belong),frompl(_frompl),to(_to),ti(_ti){}
};
bool operator<(warship a,warship b)
{
return a.ti>b.ti;
}
string lv[]={"石器时代","青铜时代","铁器时代","蒸汽时代","电气时代","原子时代","信息时代","广播恒星坐标","1/100光速航行","1/10光速航行","光速航行","黑暗森林打击"};
string destroy[]={"严寒","烈焰","双日凌空","三日凌空","三日连珠","大撕裂"};
priority_queue<warship>q,r;
int sum;
void lose(int id);
string sFromInt(int x);
int main(){
system("COLOR 0A");
//freopen("ST.in","r",stdin);
//freopen("ST.out","w",stdout);
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=rand()%7;
//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*2;i++)//1~n星球是n个文明的母星
{
p[i].dist=rand()%50+10;
//n+1~n*2星球是无人星球
p[i].belong=0;
p[i].name=sFromInt(i-n)+"号";
/*
//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&&rand()%3))
{
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)//黑暗森林打击到达
{
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==0)//技术爆炸
{
c[i].level++;
cout<<c[i].name<<"文明发生技术爆炸,";
if(c[i].level<=11)
{
if(c[i].level<7) cout<<"进入"<<lv[c[i].level]<<endl;
else cout<<"具备"<<lv[c[i].level]<<"能力"<<endl;
}
else cout<<"等级"<<c[i].level<<endl;
Sleep(sleep);
}
else if(rand()%10==0&&c[i].level>7&&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,10);
int d=p[c[i].planets[0]].dist+p[id].dist;
if(speed==8) d*=100;
if(speed==9) d*=10;
cout<<c[i].name<<"文明向"<<p[id].name<<"星球发出"<<lv[speed]<<"舰队,将在"<<d<<"年后到达"<<endl;
q.push(warship(c[i].planets[0],id,year+d));
Sleep(sleep);
}
}
if(rand()%100==0)//自然灾害(全部来自《三体1》)
{
int id;
do
{
id=rand()%(n*2)+1;
}while(p[id].belong==0);
cout<<p[id].name<<"星球上的文明在"<<destroy[rand()%6]<<"中毁灭了"<<endl;
lose(id);
p[id].belong=0;
Sleep(sleep);
if(sum==1)
{
//system("pause");
return 0;
}
}
}
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++星球争霸游戏
于 2023-04-05 19:59:07 首次发布