#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <iomanip>

using namespace std;

enum CardType { INFANTRY, SUPPORT };

struct Card {
    string name;
    int oil;
    int attack;
    int health;
    CardType type;
    bool canAttack;

    bool isAlive() const { return health > 0; }
};

struct Player {
    string name;
    int baseHp = 30;
    int maxOil = 0;
    int currentOil = 0;
    vector<Card> deck;
    vector<Card> hand;
    vector<Card> frontLine;
    vector<Card> backLine;
    vector<Card> graveyard;

    void drawCard() {
        if (!deck.empty()) {
            hand.push_back(deck.back());
            deck.pop_back();
        }
    }

    void startTurn() {
        if (maxOil < 10) maxOil++;
        currentOil = maxOil;
        drawCard();
        for (auto& c : frontLine) c.canAttack = true;
        for (auto& c : backLine) c.canAttack = true;
    }
};

Player player;
Player ai;

void initDeck(Player& p) {
    p.deck = {
        {"步枪兵", 1, 2, 1, INFANTRY, false},
        {"反坦克炮", 2, 3, 2, INFANTRY, false},
        {"装甲步兵", 3, 4, 3, INFANTRY, false},
        {"中型坦克", 4, 5, 4, INFANTRY, false},
        {"重型坦克", 5, 7, 6, INFANTRY, false},
        {"医疗站", 2, 0, 2, SUPPORT, false},
        {"火炮支援", 3, 0, 3, SUPPORT, false},
        {"指挥中心", 4, 0, 4, SUPPORT, false}
    };
    for (int i = 0; i < 2; i++)
        p.deck.insert(p.deck.end(), p.deck.begin(), p.deck.end());
    random_shuffle(p.deck.begin(), p.deck.end());
}

void initGame() {
    srand(time(0));
    player.name = "盟军指挥部";
    ai.name = "轴心国指挥部";

    initDeck(player);
    initDeck(ai);

    for (int i = 0; i < 4; i++) {
        player.drawCard();
        ai.drawCard();
    }

    cout << "==========================================" << endl;
    cout << "        KARDS 二战卡牌游戏 完整版" << endl;
    cout << "==========================================" << endl << endl;
}

void printLine() {
    cout << "-----------------------------------------------------------" << endl;
}

void showCards(const vector<Card>& line) {
    if (line.empty()) {
        cout << "[ 空 ]";
        return;
    }
    for (int i = 0; i < line.size(); i++) {
        const Card& c = line[i];
        cout << "[" << i + 1 << "." << c.name << " " << c.attack << "/" << c.health << "]  ";
    }
}

// ====================== 【KARDS 完整对称战场】核心显示 ======================
void showFullBattlefield() {
    system("cls"); // Windows清屏
    printLine();
    cout << "                【KARDS 真实战场】" << endl;
    printLine();

    // AI 后方
    cout << "【轴心后方】 ";
    showCards(ai.backLine);
    cout << endl;

    // AI 前线
    cout << "【轴心前线】 ";
    showCards(ai.frontLine);
    cout << endl;

    printLine();
    cout << "基地: " << ai.baseHp << " HP  | 石油: " << ai.currentOil << "/" << ai.maxOil
         << " | 牌库:" << ai.deck.size() << " | 墓地:" << ai.graveyard.size() << endl;
    printLine();

    cout << "                ↓↓↓ 战斗线 ↓↓↓" << endl;
    printLine();

    // 玩家前线
    cout << "【盟军前线】 ";
    showCards(player.frontLine);
    cout << endl;

    // 玩家后方
    cout << "【盟军后方】 ";
    showCards(player.backLine);
    cout << endl;

    printLine();
    cout << "基地: " << player.baseHp << " HP  | 石油: " << player.currentOil << "/" << player.maxOil
         << " | 牌库:" << player.deck.size() << " | 墓地:" << player.graveyard.size() << endl;
    printLine();

    // 手牌
    cout << "\n【你的手牌】" << endl;
    for (int i = 0; i < player.hand.size(); i++) {
        Card c = player.hand[i];
        string type = c.type == INFANTRY ? "前线" : "支援";
        cout << i + 1 << ". " << c.name
             << " | 石油:" << c.oil
             << " | " << c.attack << "/" << c.health
             << " | " << type << endl;
    }
    printLine();
}

void deployCard() {
    if (player.hand.empty()) {
        cout << "\n无手牌!" << endl;
        system("pause");
        return;
    }

    cout << "\n选择要部署的卡牌 (0退出): ";
    int idx;
    cin >> idx;
    idx--;
    if (idx < 0 || idx >= player.hand.size()) return;

    Card c = player.hand[idx];
    if (c.oil > player.currentOil) {
        cout << "石油不足!" << endl;
        system("pause");
        return;
    }

    if (c.type == INFANTRY)
        player.frontLine.push_back(c);
    else
        player.backLine.push_back(c);

    player.currentOil -= c.oil;
    player.hand.erase(player.hand.begin() + idx);
    cout << "部署成功: " << c.name << endl;
    system("pause");
}

void attack() {
    if (player.frontLine.empty()) {
        cout << "\n前线无单位!" << endl;
        system("pause");
        return;
    }

    cout << "\n选择攻击单位 (0退出): ";
    int aIdx;
    cin >> aIdx;
    aIdx--;
    if (aIdx < 0 || aIdx >= player.frontLine.size()) return;

    Card& attacker = player.frontLine[aIdx];
    if (!attacker.canAttack) {
        cout << "本回合已攻击!" << endl;
        system("pause");
        return;
    }

    cout << "1.攻击基地  2.攻击前线: ";
    int mode;
    cin >> mode;

    if (mode == 1) {
        ai.baseHp -= attacker.attack;
        cout << attacker.name << " 攻击基地!造成" << attacker.attack << "伤害!" << endl;
    }
    else if (mode == 2 && !ai.frontLine.empty()) {
        cout << "选择目标: ";
        int tIdx;
        cin >> tIdx;
        tIdx--;
        if (tIdx < 0 || tIdx >= ai.frontLine.size()) return;

        Card& target = ai.frontLine[tIdx];
        target.health -= attacker.attack;
        attacker.health -= target.attack;
        attacker.canAttack = false;

        cout << "【战斗】" << attacker.name << " ? " << target.name << endl;

        if (!target.isAlive()) {
            ai.graveyard.push_back(target);
            ai.frontLine.erase(ai.frontLine.begin() + tIdx);
        }
        if (!attacker.isAlive()) {
            player.graveyard.push_back(attacker);
            player.frontLine.erase(player.frontLine.begin() + aIdx);
        }
    }
    system("pause");
}

bool checkGameOver() {
    if (player.baseHp <= 0) {
        cout << "\n===== 你的基地被摧毁!轴心国胜利!=====" << endl;
        return true;
    }
    if (ai.baseHp <= 0) {
        cout << "\n===== 轴心国基地被摧毁!你胜利!=====" << endl;
        return true;
    }
    return false;
}

void aiTurn() {
    cout << "\n===== 轴心国回合 =====" << endl;
    ai.startTurn();

    for (int i = 0; i < ai.hand.size(); i++) {
        if (ai.hand[i].oil <= ai.currentOil) {
            ai.currentOil -= ai.hand[i].oil;
            if (ai.hand[i].type == INFANTRY)
                ai.frontLine.push_back(ai.hand[i]);
            else
                ai.backLine.push_back(ai.hand[i]);
            cout << "AI部署: " << ai.hand[i].name << endl;
            ai.hand.erase(ai.hand.begin() + i);
            break;
        }
    }

    if (!ai.frontLine.empty() && !player.frontLine.empty()) {
        Card& a = ai.frontLine[0];
        Card& t = player.frontLine[0];
        t.health -= a.attack;
        a.health -= t.attack;
        cout << a.name << " 攻击 " << t.name << endl;

        if (!t.isAlive()) {
            player.graveyard.push_back(t);
            player.frontLine.erase(player.frontLine.begin());
        }
        if (!a.isAlive()) {
            ai.graveyard.push_back(a);
            ai.frontLine.erase(ai.frontLine.begin());
        }
    }
    else if (!ai.frontLine.empty()) {
        player.baseHp -= ai.frontLine[0].attack;
        cout << "AI直接攻击基地!造成" << ai.frontLine[0].attack << "伤害!" << endl;
    }
    system("pause");
}

void playerTurn() {
    cout << "\n===== 你的回合 =====" << endl;
    player.startTurn();
    while (true) {
        showFullBattlefield();
        cout << "\n1.部署   2.攻击   3.结束回合" << endl;
        int op;
        cin >> op;
        if (op == 1) deployCard();
        else if (op == 2) attack();
        else if (op == 3) break;
    }
}

void gameLoop() {
    while (true) {
        playerTurn();
        if (checkGameOver()) break;
        aiTurn();
        if (checkGameOver()) break;
    }
}

int main() {
    initGame();
    gameLoop();
    return 0;
}