csp月模拟题——炉石传说

csp模拟——炉石传说

在这里插入图片描述

问题分析

一开始写的时候,只定义了玩家结构体,使用下标区分英雄和随从,定义了两个数组,对数组进行操作,导致写了非常冗长复杂的代码,以下将展示借鉴其他同学的较简单的思路
定义英雄和随从两个结构体,英雄的结构体中囊括了随从的数组,然后顺着思路分情况讨论命令指令,利用turn控制双方局完成比赛。

#include<iostream>
#include<string>
using namespace std;
int n;string s;
int a,b,c;

struct attendants //随从
{
	int life;  //随从生命 
	int atk;   //随从攻击力 
};

struct heros  //英雄
{
	int herolife;  //英雄生命 
	attendants att[8];  //随从 
	int size;
}hero[2]; 

int main()
{
	cin>>n;
	int turn=0;  //控制回合 
	hero[0].herolife=hero[1].herolife=30;//初始生命 
	
	for(int i=0;i<n;i++)
	{
		cin>>s;
		if(s=="summon")
		{
	    	cin>>a>>b>>c;
	    	for(int i=7;i>a;i--)
	    	{
	    		hero[turn].att[i]=hero[turn].att[i-1];//复杂但是涵盖了两种情况 
			}
			hero[turn].att[a].atk=b;
			hero[turn].att[a].life=c;
			hero[turn].size++;
		}
		if(s=="attack")
		{
			cin>>a>>b;
			int ax,ay;//进攻方 ,被进攻方 
			
			ax=hero[turn].att[a].atk;
			
			if(b==0)  //如果进攻的是英雄
			{
				hero[!turn].herolife-=ax;
			} 
			else  //随从 
			{
			    ay=hero[!turn].att[b].atk; //被攻击者的攻击 
		     	hero[!turn].att[b].life-=ax;  
				  
		     	if(hero[!turn].att[b].life<=0)   //如果被攻击的随从死掉 
		     	{
		     		for(int j=b;j<7;j++)
		     			hero[!turn].att[j]=hero[!turn].att[j+1];
		     		hero[!turn].att[7].atk=0;
					hero[!turn].att[7].life=0;
		     		hero[!turn].size--;
				} 
		     	hero[turn].att[a].life-=ay;  //攻击结束 
		     	
		     	if(hero[turn].att[a].life<=0)   //如果攻击的随从死掉 
		     	{
		     		for(int j=a;j<7;j++)
		     			hero[turn].att[j]=hero[turn].att[j+1];
		     		hero[turn].att[7].atk=0;
					hero[turn].att[7].life=0;
		     		hero[turn].size--;
				} 
			}
			if(hero[turn].herolife<=0||hero[!turn].herolife<=0) break;
		}
		if(s=="end")
		{
			turn=!turn;
		} 
	} 
	
	if(hero[0].herolife>0&&hero[1].herolife>0) cout<<"0"<<endl;
	else
	{
		if(hero[0].herolife<=0) cout<<"-1"<<endl;
		else cout<<"1"<<endl;
	}
	cout<<hero[0].herolife<<endl;
	cout<<hero[0].size<<" "; 
	for(int i=1;i<8;i++)
	{
		if(hero[0].att[i].life>0)
		    cout<<hero[0].att[i].life<<" ";
	}
	cout<<endl;
	
	cout<<hero[1].herolife<<endl;
	cout<<hero[1].size<<" ";
	for(int i=1;i<8;i++)
	{
		if(hero[1].att[i].life>0)
		    cout<<hero[1].att[i].life<<" ";
	}
	cout<<endl;
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值