C++战争小游戏

#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;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
游戏分里外两个部分组成,里部分(用户不可见) 通过里部分执行判断,地图数组更改,和各种值的改变。更改完里部分再根据相应变化更改表部分。(用户可视部分)表部分的打印通过gotoxy去到相应坐标再printf打印出字符,通过文本函数改变文字字体颜色与文字背景颜色与字符组合实现图形界面。 程序通过 计数器+循环判断 的思想,类似单核cpu的多线程实现(单线程在不同程序/函数间来回执行)省去了多线程。(具体过程在功能设计与描述有详细描述) 另AI实现与加强依赖于rand随机函数的运用,进一步强化AI,增加游戏乐趣 功能方面,游戏参考于80年代任天堂红白机(FC/FamilyComputer)上的游戏坦克大战(Battle City),包括地图,游戏模式等等(当时的游戏直接烧在电路板上)。所以游戏平衡方面已经有了很好的参考,无需再花大量时间测试平衡性。 但诸如地图中的树林元素,随机道具等没有实现。但较之原版,该游戏由C/C++编写PC运行,由字符界面实现游戏画面。原版一辆坦克的子弹未消失之前不能发射第二颗。导致子弹打击远处CD长,近处CD短。该游戏每个子弹都有相同CD,子弹未消失只要CD达到即可发射第二颗,第三颗…增加了真实性,相较于原版是个改进。且考虑到PC性能不一内置了游戏速度调整。玩家可根据PC性能调整至合适的速度。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值