入坑尚浅 , 结构、函数没用好导致代码臃肿。。 最后提交80分,测不出哪里少了些考虑
题目:
csp:
* 玩家会控制一些角色,每个角色有自己的生命值和攻击力。当生命值小于等于 0 时,该角色死亡。
角色分为英雄和随从。
* 玩家各控制一个英雄,游戏开始时,英雄的生命值为 30,攻击力为 0。当英雄死亡时,游戏结束
,英雄未死亡的一方获胜。
* 玩家可在游戏过程中召唤随从。棋盘上每方都有 7 个可用于放置随从的空位,从左到右一字排开
,被称为战场。当随从死亡时,它将被从战场上移除。
* 游戏开始后,两位玩家轮流进行操作,每个玩家的连续一组操作称为一个回合。
* 每个回合中,当前玩家可进行零个或者多个以下操作:
1) 召唤随从:玩家召唤一个随从进入战场,随从具有指定的生命值和攻击力。
2) 随从攻击:玩家控制自己的某个随从攻击对手的英雄或者某个随从。
3) 结束回合:玩家声明自己的当前回合结束,游戏将进入对手的回合。该操作一定是一个回合的最后
一个操作。
* 当随从攻击时,攻击方和被攻击方会同时对彼此造成等同于自己攻击力的伤害。受到伤害的角色的
生命值将会减少,数值等同于受到的伤害。例如,随从 X 的生命值为 HX、攻击力为 AX,随从 Y 的生命
值为 HY、攻击力为 AY,如果随从 X 攻击随从 Y,则攻击发生后随从 X 的生命值变为 HX - AY,随从 Y
的生命值变为 HY - AX。攻击发生后,角色的生命值可以为负数。
本题将给出一个游戏的过程,要求编写程序模拟该游戏过程并输出最后的局面。
输入格式
输入第一行是一个整数 n,表示操作的个数。接下来 n 行,每行描述一个操作,格式如下:
<action> <arg1> <arg2> ...
其中<action>表示操作类型,是一个字符串,共有 3 种:summon表示召唤随从,attack表示
随从攻击,end表示结束回合。这 3 种操作的具体格式如下:
* summon <position> <attack> <health>:当前玩家在位置<position>召唤一个
生命值为<health>、攻击力为<attack>的随从。其中<position>是一个 1 到 7 的整数,表示召唤的随从
出现在战场上的位置,原来该位置及右边的随从都将顺次向右移动一位。
* attack <attacker> <defender>:当前玩家的角色<attacker>攻击对方的角色 <defender>。
<attacker>是 1 到 7 的整数,表示发起攻击的本方随从编号,<defender>是 0 到 7 的整数,表示
被攻击的对方角色,0 表示攻击对方英雄,1 到 7 表示攻击对方随从的编号。
* end:当前玩家结束本回合。
注意:随从的编号会随着游戏的进程发生变化,当召唤一个随从时,玩家指定召唤该随从放入战场的
位置,此时,原来该位置及右边的所有随从编号都会增加 1。而当一个随从死亡时,它右边的所有随从
编号都会减少 1。任意时刻,战场上的随从总是从1开始连续编号。
输出格式
输出共 5 行。
---第 1 行包含一个整数,表示这 n 次操作后(以下称为 T 时刻)游戏的胜负结果,1 表示先手玩家
获胜,-1 表示后手玩家获胜,0 表示游戏尚未结束,还没有人获胜。
第 2 行包含一个整数,表示 T 时刻先手玩家的英雄的生命值。
第 3 行包含若干个整数,第一个整数 p 表示 T 时刻先手玩家在战场上存活的随从个数,之后 p 个
整数,分别表示这些随从在 T 时刻的生命值(按照从左往右的顺序)。
第 4 行和第 5 行与第 2 行和第 3 行类似,只是将玩家从先手玩家换为后手玩家。
样例输入
8
summon 1 3 6
summon 2 4 2
end
summon 1 4 5
summon 1 2 1
attack 1 2
end
attack 1 1
样例输出
0
30
1 2
30
1 2
样例说明
按照样例输入从第 2 行开始逐行的解释如下:
1. 先手玩家在位置 1 召唤一个生命值为 6、攻击力为 3 的随从 A,是本方战场上唯一的随从。
2. 先手玩家在位置 2 召唤一个生命值为 2、攻击力为 4 的随从 B,出现在随从 A 的右边。
3. 先手玩家回合结束。
4. 后手玩家在位置 1 召唤一个生命值为 5、攻击力为 4 的随从 C,是本方战场上唯一的随从。
5. 后手玩家在位置 1 召唤一个生命值为 1、攻击力为 2 的随从 D,出现在随从 C 的左边。
6. 随从 D 攻击随从 B,双方均死亡。
7. 后手玩家回合结束。
8. 随从 A 攻击随从 C,双方的生命值都降低至 2
代码:
#include<iostream>
#include<vector>
#include<string>
using namespace std;
struct Card
{
int position;
int health;
int attack;
//Card(int _position,int _health,int _attack):position(_position),health(_health),attack(_attack){}
bool operator < (const Card &other) const
{
if (position < other.position) return true;
else return false;
}
};
void print(vector<Card>& lt) {
for(vector<Card>::iterator it = lt.begin(); it != lt.end(); it++) {
if(it == lt.begin()) {
cout << it->health << endl;
cout << lt.size() - 1;
} else cout << " " << it->health;
}
}
int main()
{
string str[] = { "summon","attack","end" };
string input;
Card card_temp;
vector<Card> play1;
vector<Card> play2;
Card cardt;
cardt.position = cardt.attack = 0;
cardt.health = 30;
play1.push_back(cardt);
play2.push_back(cardt);
bool task; //判断是否插入中间
int n, jud = 0, player = 1; // jud判断胜负
int att, def; //攻击、被攻击
cin >> n;
for (int i = 0; i<n; i++)
{
task = false;
cin >> input;
if(jud == 0)
{
if (input == str[0])
{
cin >> card_temp.position >> card_temp.attack >> card_temp.health;
if (player == 1)
{
for (int k = 0; k<play1.size(); k++)
{
if (card_temp.position == play1[k].position)
{
play1.insert(play1.begin() + k, card_temp);
for (int j = k; j<play1.size(); j++)
play1[j].position++;
task = true;
break;
}
}
if (!task) play1.push_back(card_temp);
}
else if (player == 2)
{
for (int k = 0; k<play2.size(); k++)
{
if (card_temp.position == play2[k].position)
{
play2.insert(play2.begin() + k, card_temp);
for (int j = k; j<play2.size(); j++)
play2[j].position++;
task = true;
break;
}
}
if (!task) play2.push_back(card_temp);
}
}
else if (input == str[1])
{
cin >> att >> def;
if (player == 1)
{
play2[def].health = play2[def].health - play1[att].attack;
play1[att].health = play1[att].health - play2[def].attack;
if (play2[def].health <= 0)
{
if (def == 0 && jud == 0) jud = 1;
else
{
vector<Card>::iterator it = play2.begin() + def;
play2.erase(it);
for (int i = def; i<play2.size(); i++)
{
play2[i].position--;
}
}
}
if (play1[att].health <= 0)
{
if (def == 0 && jud == 0) jud = -1;
else
{
vector<Card>::iterator it = play1.begin() + att;
play1.erase(it);
for (int k = att; k<play1.size(); k++)
{
play1[k].position--;
}
}
}
}
else if (player == 2)
{
play1[def].health = play1[def].health - play2[att].attack;
play2[att].health = play2[att].health - play1[def].attack;
if (play1[def].health <= 0)
{
if (def == 0)
{
if (jud == 0) jud = -1;
}
else
{
vector<Card>::iterator it = play1.begin() + def;
play1.erase(it);
for (int k = def; k<play1.size(); k++)
{
play1[k].position--;
}
}
}
if (play2[att].health <= 0)
{
if (att == 0)
{
if (jud == 0) jud = 1;
}
else
{
vector<Card>::iterator it = play2.begin() + att;
play2.erase(it);
for (int i = att; i<play2.size(); i++)
{
play2[i].position--;
}
}
}
}
}
else if (input == str[2])
{
if (player == 1) player = 2;
else if (player == 2) player = 1;
}
}
else break;
}
cout << jud << endl;
print(play1);
cout << endl;
print(play2);
return 0;
}
/*
cout << play1[0].health << endl;
cout << play1.size() - 1;
for (int j = 1; j<play1.size(); j++)
{
cout << " " << play1[j].health;
}
cout << endl;
cout << play2[0].health << endl;
cout << play2.size() - 1;
for (int j = 1; j<play2.size(); j++)
{
cout << " " << play2[j].health;
}*/
欢迎指正。。