问题描述
| 试题编号: | 201609-3 |
|---|---|
| 试题名称: | 炉石传说 |
| 时间限制: | 1.0s |
| 内存限制: | 256.0MB |
| 问题描述: | 问题描述 《炉石传说:魔兽英雄传》(Hearthstone: Heroes of Warcraft,简称炉石传说)是暴雪娱乐开发的一款集换式卡牌游戏(如下图所示)。游戏在一个战斗棋盘上进行,由两名玩家轮流进行操作,本题所使用的炉石传说游戏的简化规则如下: |
\
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
using namespace std;
bool endgame = false;//游戏是否结束的标志位
typedef struct follower {//定义随从的数据结构
int health;
int attack;
};
typedef struct hero { //定义英雄的数据结构
int health=30;
int attack=0;
};
typedef struct Player {//定义玩家的数据结构
hero h;
vector<follower> flist;
};
vector<string> split(string in, string sp)//分割字符串,解析参数
{
vector<string> rt;
while (in.find(sp) != -1)
{
string tem = in.substr(0, in.find(sp));
if (tem != "")
rt.push_back(tem);
in = in.substr(in.find(sp) + 1);
}
if (in != "")
rt.push_back(in);
return rt;
}
void one_step(Player& p1, Player& p2, string action,int func)//表示一步操作,p1是当前局玩家,p2是被动玩家
{
vector<string> para = split(action, " ");//解析得到参数
switch (func)//根据不同的操作来选择
{
case 1: {//召唤随从操作
int position = atoi(para[1].c_str())-1;//内存中编号从0开始
int attack = atoi(para[2].c_str());
int health = atoi(para[3].c_str());
follower tf;
tf.attack = attack;
tf.health = health;
p1.flist.insert(p1.flist.begin() + position, tf);
break;
}
case 2://攻击操作
{
int attackerNum = atoi(para[1].c_str())-1; //vector从0开始编号,所以要减去1
int defenderNum = atoi(para[2].c_str())-1;
follower attacker = p1.flist[attackerNum]; //得到攻击的随从
int attacker_attack = attacker.attack;
int attacker_health = attacker.health;
if (defenderNum == -1) //如果攻击的是英雄
{
hero temp_hero = p2.h; //得到原先的生命值
temp_hero.health -= attacker_attack;//英雄生命减少
p2.h = temp_hero; //更新英雄状态
if (temp_hero.health <= 0) //如果英雄生命值小于0结束游戏
endgame = true;
return;
}
else { //如果被攻击的是随从
follower defender = p2.flist[defenderNum];
int defender_attack = defender.attack;
int defender_health = defender.health;
defender_health -= attacker_attack;
attacker_health -= defender_attack;
if (defender_health <= 0)//如果守卫死亡
p2.flist.erase(p2.flist.begin() + defenderNum);
else
{
defender.health = defender_health;//更新生命值
p2.flist[defenderNum] = defender;
}
if (attacker_health <= 0)//如果攻击者死亡
p1.flist.erase(p1.flist.begin() + attackerNum);
else
{
attacker.health = attacker_health;
p1.flist[attackerNum] = attacker;
}
}
break;
}
}
}
int main()
{
//初始化两个玩家
Player p1, p2;
hero h1, h2;
p1.h = h1;
p2.h = h2;
int flag = 1;//表示当前玩家,初始为1,表示1号玩家为先手,-1为2号玩家
int n=1;
vector<string> actions;
for (int i = 0; i < n; i++)
{
string tem;
getline(cin, tem);
n += atoi(tem.c_str());
actions.push_back(tem);
}
actions.erase(actions.begin());//去掉第一行的参数,因为其表示操作次数
int func = 0;//表示一次的操作的功能选择
for (int i = 0; i < actions.size(); i++)
{
func = 0;
string a_action = actions[i];
if (a_action.find("summon") != -1)
func = 1;
else if (a_action.find("attack") != -1)
func = 2;
else if (a_action.find("end") != -1)
func = 3;
switch (func)
{
case 1:
{
if (flag == 1)
{
one_step(p1, p2, a_action, func);
}
else
{
one_step(p2, p1, a_action, func);
}
break;
}
case 2:
{
if (flag == 1)
{
one_step(p1, p2, a_action, func);
}
else
{
one_step(p2, p1, a_action, func);
}
break;
}
case 3:
{
flag = flag*-1;
break;
}
}
if (endgame)
break;
}
//输出最后结果
if (p1.h.health <= 0)
cout << -1 << endl;
else if (p2.h.health <= 0)
cout << 1 << endl;
else
cout << 0 << endl;
cout << p1.h.health << endl;
cout << p1.flist.size()<<" ";//先手守卫存活个数;
for (int i = 0; i < p1.flist.size(); i++)
cout << p1.flist[i].health << " ";
cout << endl;
cout << p2.h.health << endl;
cout << p2.flist.size()<<" ";//先手守卫存活个数;
for (int i = 0; i < p2.flist.size(); i++)
cout << p2.flist[i].health << " ";
//system("pause");
}
\