CSP-201609-3-炉石传说

题目

在这里插入图片描述在这里插入图片描述在这里插入图片描述

注意事项

1.英雄死后血量清零
2.注意每次状态改变后更改个数组的值
3.掌握stringstream,可以用于格式转换、读取字符串

代码

//英雄死后血量清零
//注意每次状态改变后更改个数组的值 
#include<iostream>
#include<sstream>
#include<string>
#include<string.h>
using namespace std;

struct player
{
	int h,a;
	player(){
	} 
	player(int _a,int _h)
	{
		a=_a;
		h=_h;
	}
}players[5][10];//一维:先手后手,二维:角色 

int pos[5][10];//表示位置是否有角色 
int tot[5];//双方玩家的随从数目 
int winner=2;//未决出胜负

void hero()
{
	tot[1]=tot[2]=0;
	player p(0,30);
	players[1][0]=p;
	players[0][0]=p;

	
	
}
void scgodie(int xh,int poss)
{
	for(int i=poss+1;i<=tot[xh];i++)
		players[xh][i-1]=players[xh][i];
	pos[xh][tot[xh]]=0;
	tot[xh]--;
	
}
int hgodie(int xh,int poss)
{
	int flag=0;
	if(poss==0)//英雄死了
	{
		//游戏结束 
		flag=1;
		//把英雄的血量=0
		players[xh][0].h=0; 
		winner=(xh+1)%2;
	} 
	else
	{
		for(int i=poss+1;i<=tot[xh];i++)
			players[xh][i-1]=players[xh][i];
		pos[xh][tot[xh]]=0;
		tot[xh]--;
		
	}
	return flag;
}
void output()
{
	if(winner==0)//先手赢
		cout<<1<<endl;
	else if(winner==1)
		cout<<-1<<endl;
	else cout<<0<<endl;
	
	for(int k=0;k<=1;k++)
	{
		
		cout<<players[k][0].h<<endl<<tot[k]<<' ';
		for(int i=1;i<=tot[k];i++)
			cout<<players[k][i].h<<' ';
		cout<<endl;
	}
}
int main()
{
	int n;int xianhou=0;//1表示先手, 2后手 
	cin>>n;
	string str;
	getchar();//吸收换行符,不然getline会获取缓冲区里的换行符 
	
	
	//设置英雄
	hero(); 
	
	while(n--)
	{
		getline(cin,str);//对于 string,getline可以获取含有空格的字符串,遇到换行自动结束
		if(str=="end")
		{
			xianhou++;
			xianhou%=2;
			continue;
		}
		stringstream sstream(str);
		string tmp;
		sstream>>tmp;//读入第一个操作符 
		if(tmp=="summon")
		{
			int t1,t2,t3;
			sstream>>t1>>t2>>t3;
			player p(t2,t3);
			if(pos[xianhou][t1]==1)//该位置已有角色
			{
				for(int i=tot[xianhou]+1;i>=t1+1;i--)
				{
					players[xianhou][i]=players[xianhou][i-1];
				}
				pos[xianhou][tot[xianhou]+1]=1;//最右边的位置 
			}
			else
			{
				pos[xianhou][t1]=1;
				
			} 
			tot[xianhou]++;
			players[xianhou][t1]=p;
		}
		else if(tmp=="attack")
		{
			int t1,t2;
			sstream>>t1>>t2;
			int other=(xianhou+1)%2;//对方 
			//生命力down down 
			players[xianhou][t1].h-=players[other][t2].a;
			players[other][t2].h-=players[xianhou][t1].a;
			//如果有角色失血过多go die,判断他是英雄还是随从(但攻击的一方肯定是随从) 
			int f;
			if(players[xianhou][t1].h<=0)scgodie(xianhou,t1);//此方随从死了 
			if(players[other][t2].h<=0)f=hgodie(other,t2);
			if(f==1)//游戏结束
				break;
		}

		
	}
	output();	
	return 0;
 } 
 
 
 
 
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值