Skip to content

Pro3

原始文件为 CPP 代码,本文是转换后的 Markdown 文件。

#include <bits/stdc++.h>
using namespace std;

class Card
{
public:
    int attack, health;
    Card* next;

    Card() {next = NULL;}
    Card(int a, int h) : attack(a), health(h) {next = NULL;}

    bool isAlive()
    {
        return (health > 0);
    }
    void attack_other(Card* other)
    {
        this->health -= other->attack;
        other->health -= this->attack;
    }
};
class Player
{
public:
    Card* lo;
    Card* hi;
    int num;

    Card* get_card(int pos)
    {
        Card* result = lo;
        for (int i = 0; i < pos; ++i)
            result = result->next;
        return result;
    }

    void remove_card(int pos)
    {
        Card* prev_card = get_card(pos-1);
        Card* dele_card = prev_card->next;
        prev_card->next = dele_card->next;
        delete dele_card;
        --num;
    }

    void add_card(int pos, Card c)
    {
        Card* new_card = new Card(c);
        Card* cur_card = get_card(pos-1);

        new_card->next = cur_card->next;
        cur_card->next = new_card;
        ++num;
    }

    // return value: player1 kill player2
    bool attack_other(int pos_1, int pos_2, Player* other)
    {
        Card* attack_card = get_card(pos_1);
        Card* defend_card = other->get_card(pos_2);
        attack_card->attack_other(defend_card);
        if (!attack_card->isAlive())
            remove_card(pos_1);

        if (!defend_card->isAlive())
            if (pos_2 == 0) 
                return true;
            else
                other->remove_card(pos_2);
        return false;

    }

    Player()
    {
        lo = new Card{0, 30};
        lo->next = hi;
        num = 0;
    }

    void print()
    {
        Card* cur_card = lo;
        cout << cur_card->health << endl;
        cout << num << " ";
        for (int i = 0; i < num; ++i)
        {
            cur_card = cur_card->next;
            cout << cur_card->health << " ";
        }
        cout << endl;
    }

};
int main()
{
    // freopen("in.txt","r",stdin);
    int n; cin >> n;
    int someone_gg = 0;
    int player_id = 0;
    map<string, int> order_map;
    order_map["summon"] = 1;
    order_map["attack"] = 2;
    order_map["end"] = 3;

    Player* player[2];
    for (int i = 0; i < 2; ++i) 
        player[i] = new Player();

    for (int i = 0; i < n; ++i)
    {
        string order_str; cin >> order_str;
        int order_int = order_map[order_str];

        switch (order_int)
        {
            case 1:
            {
                int pos, attack, health; cin >> pos >>attack >> health;
                Card c = {attack, health};
                player[player_id]->add_card(pos, c);
                break;
            }
            case 2:
            {
                int pos_1, pos_2; cin >> pos_1 >> pos_2;
                bool victory = player[player_id]->attack_other(pos_1, pos_2, player[!player_id]);
                if (victory) someone_gg = player_id ? -1 : 1;
                break;
            }
            case 3:
            {
                player_id = !player_id;
                break;
            }
        }
    }
    cout << someone_gg << endl;
    player[0]->print();
    player[1]->print();
}