您的位置:首页 > 其它

4000 CCF-炉石传说

2016-11-30 14:21 127 查看
这题原本想的比较复杂,因为每次召唤随从或随从死亡时都要对右边的随从编号进行变动,然后构建了链表的数据结构,但是越写越复杂,所以马上放弃这个思路。

其实只要构建一个结构体,结构体保存随从的health和attack,以及一个flag表示随从是否死亡。构建两个size为8的数组,用以保存随从即可。

#include <iostream>
#include <string>
#include <vector>
using namespace std;
struct monster
{
int attack;
int health;
bool is_alive;
};
void summon(monster *player, int pos, int attack, int health);
void attack(monster *attack, monster *defend, int attacker, int defender);
void move_right(monster *player, int pos);
void move_left(monster *player, int pos);
void display(monster *player);
void test(monster *player);
int main()
{
monster offen[8];
monster defen[8];
int cur_round = 1; //1 for offensive, -1 for defensive
int winner = 0; // 0 for no winner, 1 for offen,-1 for defen
//init
offen[0].health = 30;
defen[0].health = 30;
offen[0].attack = 0;
defen[0].attack = 0;
for (int i = 1; i <= 7; i++)
{
offen[i].is_alive = false;
defen[i].is_alive = false;
}

int num;
string command;
int arg1;
int arg2;
int arg3;
cin >> num;
while (num--)
{
cin >> command;
if (command == "summon")
{
cin >> arg1 >> arg2 >> arg3;
if (cur_round == 1)
{
summon(offen, arg1, arg2, arg3);
}
else
{
summon(defen, arg1, arg2, arg3);
}
}
else if (command == "attack")
{
cin >> arg1 >> arg2;
if (cur_round == 1)
{
attack(offen, defen, arg1, arg2);
}
else
{
attack(defen, offen, arg1, arg2);
}
if (offen[0].health <= 0)
{
winner = -1;
break;
}
else if (defen[0].health <= 0)
{
winner = 1;
break;
}
}
else
{
cur_round *= -1; //change round
continue;
}
}
//display
cout << winner << endl;
cout << offen[0].health << endl;
display(offen);
cout << defen[0].health << endl;
display(defen);
return 0;
}
void summon(monster *player, int pos, int attack, int health)
{
if (player[pos].is_alive == false)
{
player[pos].health = health;
player[pos].attack = attack;
player[pos].is_alive = true;
for (int i = 6; i > pos; i--)
{
//move right
if (player[i].is_alive)
{
move_right(player, i);
}
}
}
else
{
for (int i = 6; i >= pos; i--)
{
//move right
if (player[i].is_alive)
{
move_right(player, i);
}
}
player[pos].health = health;
player[pos].attack = attack;
player[pos].is_alive = true;
}
}

void attack(monster *attack, monster *defend, int attacker, int defender)
{
if (defender == 0)
{
defend[0].health -= attack[attacker].attack;
}
else
{
defend[defender].health -= attack[attacker].attack;
attack[attacker].health -= defend[defender].attack;
if (defend[defender].health <= 0)
{
defend[defender].is_alive = false;
//move left
for (int i = defender + 1; i <= 7; i++)
{
if (defend[i].is_alive)
{
move_left(defend, i);
}
}
}
if (attack[attacker].health <= 0)
{
attack[attacker].is_alive = false;
//move left
for (int i = attacker + 1; i <= 7; i++)
{
if (attack[i].is_alive)
{
move_left(attack, i);
}
}
}
}
}
void move_right(monster *player, int pos)
{
if (player[pos + 1].is_alive == true)
{
return;
}
else
{ //move right
player[pos + 1].attack = player[pos].attack;
player[pos + 1].health = player[pos].health;
player[pos + 1].is_alive = true;
player[pos].is_alive = false;
}
}

void move_left(monster *player, int pos)
{
if (player[pos - 1].is_alive == true)
{
return;
}
else
{ //move right
player[pos - 1].attack = player[pos].attack;
player[pos - 1].health = player[pos].health;
player[pos - 1].is_alive = true;
player[pos].is_alive = false;
}
}
void display(monster *player)
{
vector<int> temp;
int count = 0;
for (int i = 1; i <= 7; i++)
{
if (player[i].is_alive)
{
count++;
temp.push_back(player[i].health);
}
}
cout << count << " ";
for (int i = 0; i < temp.size(); i++)
{
cout << temp.at(i) << " ";
}
cout << endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: