#include <bits/stdc++.h>
#include<windows.h>
using namespace std;
string g[105][105];
int p[105][105],q[105][105],a[105][105],r[105][105];
string name[]={"洛","古","秦","夏","商","武","明","魏","齐","赵","吴","蜀","汉","燕","唐"};
bool vis[15];
string id;
int n,m;
vector<string> c;
void saveGame() {
ofstream file("save.txt");
file << n << " " << m << endl;
file << id;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
file << g[i][j] << " ";
}
file << endl;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
file << p[i][j] << " ";
}
file << endl;
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
file << a[i][j] << " ";
}
file << endl;
}
file<< c.size();
for (int i = 1; i <= c.size(); i++) {
for (int j = 1; j <= c.size(); j++) {
file << r[i][j] << " ";
}
file << endl;
}
for (int i=1;i<=c.size();i++)
{
cout<<c[i-1]<<" ";
}
file.close();
}
void loadGame() {
ifstream file("save.txt");
if (!file) {
cout << "未找到存档文件" << endl;
return;
}
// 读取地图大小
file >> n >> m;
file>>id;
// 读取国家信息
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
file >> g[i][j];
}
}
// 读取人口信息
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
file >> p[i][j];
}
}
// 读取经济信息
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
file >> a[i][j];
}
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
file >> a[i][j];
}
}
int si;
file>>si;
for (int i = 1; i <= si; i++) {
for (int j = 1; j <= si; j++) {
file >> r[i][j];
}
}
for (int i=1;i<=si;i++)
{
string cc;
cin>>cc;
c.push_back(cc);
}
file.close();
cout << "成功加载游戏" << endl;
}
int main()
{
cout<<"输入地图大小\n";
cin>>n>>m;
srand(n+m+time(NULL));
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
if (rand()%10==1)
{
int ran=rand()%15;
if (vis[ran]==true)
{
g[i][j]="()";
continue;
}
g[i][j]=name[ran];
p[i][j]=rand()%50;
vis[ran]=true;
c.push_back(g[i][j]);
}
else
{
g[i][j]="()";
}
cout<<g[i][j];
}
cout<<"\n";
}
cout<<"请输入您的国家名称:"<<"\n";
cin>>id;
cout<<"输入其坐标"<<"\n";
int x,y;
cin>>x>>y;
g[x][y]=id;
p[x][y]=20;
system("cls");
while(1)
{
for (int i=1;i<=c.size();i++)
{
if (r[0][i]<=-10)
{
cout<<"您与"<<c[i-1]<<"处于战争状态\n";
}
}
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
cout<<g[i][j]<<" ";
}
cout<<"\n";
}
int r_val=3,q_val=3,a_val=3;
while(1)
{
system("cls");
cout<<"1.地图\n";
cout<<"2.外交\n";
cout<<"3.经济\n";
cout<<"4.军事\n";
cout<<"5.下一回合\n";
cout<<"6.保存\n";
cout<<"7.读档\n";
int opt;
cin>>opt;
if (opt==1)
{
cout<<"人口图\n";
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
if (g[i][j]==id)
{
cout<<p[i][j]<<" ";
}
else
{
cout<<"??";
}
}
cout<<"\n";
}
cout<<"\n";
cout<<"经济图\n";
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
if (g[i][j]==id)
{
cout<<q[i][j]<<" ";
}
else
{
cout<<"??";
}
}
cout<<"\n";
}
cout<<"\n";
cout<<"兵力图\n";
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
if (g[i][j]==id)
{
cout<<a[i][j]<<" ";
}
else
{
cout<<"??";
}
}
cout<<"\n";
}
cout<<"\n";
cout<<"输入任意键返回\n";
int back;
cin>>back;
}
if (opt==2)
{
while (r_val>0)
{
system("cls");
cout<<"外交点数"<<r_val<<"\n";
for (int i=0;i<c.size();i++)
{
cout<<i+1<<"."<<c[i]<<" ";
}
cout<<"\n";
int who;
cin>>who;
int what;
cout<<"1.外交侮辱\n";
cout<<"2.改善关系\n";
cin>>what;
if (what==1)
{
r[0][who]--;
r[who][0]--;
}
else
{
r[0][who]++;
r[who][0]++;
}
r_val--;
}
}
if (opt==3)
{
while (q_val>0)
{
system("cls");
cout<<"投资点数"<<q_val<<"\n";
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
if (g[i][j]==id)
{
cout<<q[i][j]<<" ";
}
else
{
cout<<"??";
}
}
cout<<"\n";
}
cout<<"\n";
cout<<"您要投资的坐标\n";
int xp,yp;
cin>>xp>>yp;
q[xp][yp]+=rand()%25;
q_val--;
}
}
if (opt==4)
{
while (a_val>0)
{
cout<<"征兵点数"<<a_val<<"\n";
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
if (g[i][j]==id)
{
cout<<a[i][j]<<" ";
}
else
{
cout<<"??";
}
}
cout<<"\n";
}
cout<<"\n";
cout<<"您要征兵的坐标\n";
int xp,yp;
cin>>xp>>yp;
a[xp][yp]+=((rand()%p[xp][yp])%q[xp][yp]);
}
}
if (opt==5)
{
for (int i=1;i<=n;i++)
{
for (int j=1;j<=m;j++)
{
int who;
for (int i=0;i<c.size();i++)
{
if (c[i]==g[i][j])
{
who=i+1;
}
}
if (g[i][j]!="()")
{
p[i][j]+=rand()%10;
if (g[i][j]!=id)
{
a[i][j]+=rand()%10;
if (rand()%2==1)
{
r[who][rand()%(c.size()+1)]++;
}
else
{
r[who][rand()%(c.size()+1)]--;
}
if (rand()%2==1)
{
r[who][rand()%(c.size()+1)]++;
}
else
{
r[who][rand()%(c.size()+1)]--;
}
if (rand()%2==1)
{
r[who][rand()%(c.size()+1)]++;
}
else
{
r[who][rand()%(c.size()+1)]--;
}
}
int xm[4]={0,1,-1,0};
int ym[4]={0,1,-1,0};
for (int i_1=1;i_1<=3;i_1++)
{
for (int i_2=1;i_2<=3;i_2++)
{
if (i_1==3&&i_2==3)
{
break;
}
if ((i+i_1>=0&&j+i_2>=0)&&(i+i_1<=n&&j+i_2<=m))
{
if (g[i+i_1][j+i_2]=="()"&&rand()%5==1)
{
g[i+i_1][j+i_2]=who;
p[i+i_1][j+i_2]=rand()%10;
}
else
{
int wh_o;
for (int i=0;i<c.size();i++)
{
if (c[i]==g[i][j])
{
wh_o=i+1;
}
}
if (r[who][wh_o]<=-10)
{
if (a[i][j]>a[i+i_1][j+i_2])
{
g[i+i_1][j+i_2]=who;
p[i+i_1][j+i_2]=rand()%10;
}
}
}
}
}
}
}
}
}
break;
}
if (opt==6)
{
saveGame();
}
else
{
loadGame();
}
}
system("cls");
}
return 0;
}
C++战争小游戏
最新推荐文章于 2023-12-03 10:13:17 发布