元素選擇器
題目描述:
image.png
image.png
image.png
思路:
這就是一個模擬打游戲的過程。
首先我們要把全部的操作讀入妈倔。然后定義兩個player授霸,player1和player2。然后定義一個hero 的結(jié)構(gòu)體损离,把召喚的hero都加進(jìn)去,有攻擊力和血條
玩過游戲的都知道绝编,事實(shí)上英雄本身也是一個單位僻澎,所以這個也加進(jìn)去(沒有英雄技能,簡化了/咳咳)
然后模擬攻擊也是瓮增,,算攻擊力和血條哩俭。死了就移除绷跑。
最終還要判斷勝負(fù),這就根據(jù)雙方英雄本體的血條來看了凡资,最后再輸出砸捏。
思路說起來簡單,代碼略微復(fù)雜:
代碼部分
#include <iostream>
#include <vector>
#include <string>
using namespace std;
struct heros
{
int health;
int attack;
heros(int h, int a)
{
health = h;
attack = a;
}
};
vector<heros> player1;
vector<heros> player2;
int pos, h, a;
int att, deff;
int main() {
int N;
cin >> N;
int pid = 0;
player1.push_back(heros(30, 0));
player2.push_back(heros(30, 0));
while (N--)
{
string type;
cin >> type;
if (type == "summon")
{
cin >> pos >> a >> h;
if (!pid) player1.insert(player1.begin() + pos, heros(h, a));
else player2.insert(player2.begin() + pos, heros(h, a));
}
else if (type == "attack") {
cin >> att >> deff;
if (!pid)
{
player1[att].health -= player2[deff].attack;
player2[deff].health -= player1[att].attack;
if (player1[att].health <= 0 && att != 0) {
player1.erase(player1.begin() + att);
}
if (player2[deff].health <= 0 && deff != 0) {
player2.erase(player2.begin() + deff);
}
}
else
{
player2[att].health -= player1[deff].attack;
player1[deff].health -= player2[att].attack;
if (player2[att].health <= 0 && att != 0) {
player2.erase(player2.begin() + att);
}
if (player1[deff].health <= 0 && deff != 0) {
player1.erase(player1.begin() + deff);
}
}
}
else if (type == "end")
{
pid = !pid;
}
}
if (player1[0].health > 0 && player2[0].health > 0)
{
cout << 0 << endl;
}
else if (player1[0].health > 0)
{
cout << 1 << endl;
}
else cout << -1 << endl;
cout << player1[0].health << endl;
cout << player1.size() - 1 << " ";
for (int j = 1; j < player1.size(); j++) {
cout << player1[j].health << " ";
}
cout << endl;
cout << player2[0].health << endl;
cout << player2.size() - 1 << " ";
for (int j = 1; j < player2.size(); j++) {
cout << player2[j].health << " ";
}
cout << endl;
}