模拟3

元素选择器

题目描述:

image.png

image.png

image.png

思路:

这就是一个模拟打游戏的过程。
首先我们要把全部的操作读入。然后定义两个player,player1和player2。然后定义一个hero 的结构体,把召唤的hero都加进去,有攻击力和血条
玩过游戏的都知道,事实上英雄本身也是一个单位,所以这个也加进去(没有英雄技能,简化了/咳咳)

然后模拟攻击也是,,算攻击力和血条。死了就移除。

最终还要判断胜负,这就根据双方英雄本体的血条来看了,最后再输出。

思路说起来简单,代码略微复杂:

代码部分

#include 
#include 
#include 
using namespace std;
struct heros
{
    int health;
    int attack;
    heros(int h, int a)
    {
        health = h;
        attack = a;
    }
};
vector player1;
vector 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;

}

你可能感兴趣的:(模拟3)