模擬3

元素選擇器

題目描述:

image.png

image.png

image.png

思路:

這就是一個模擬打游戲的過程。
首先我們要把全部的操作讀入。然后定義兩個player,player1和player2。然后定義一個hero 的結構體,把召喚的hero都加進去,有攻擊力和血條
玩過游戲的都知道,事實上英雄本身也是一個單位,所以這個也加進去(沒有英雄技能,簡化了/咳咳)

然后模擬攻擊也是,,算攻擊力和血條。死了就移除。

最終還要判斷勝負,這就根據(jù)雙方英雄本體的血條來看了,最后再輸出。

思路說起來簡單,代碼略微復雜:

代碼部分

#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;

}
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

友情鏈接更多精彩內(nèi)容