CCF-炉石传说

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

其实只要构建一个结构体,结构体保存随从的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;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值