题目
注意事项
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;
}