
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <windows.h>
#include <conio.h>
#include <algorithm>

using namespace std;

// 控制台工具函数
void SetColor(int color) {
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
void ClearScreen() { system("cls"); }
void Pause(string msg = "按任意键继续...") {
    SetColor(7);
    cout << msg;
    _getch();
}

// 卡牌结构体
struct Card {
    string name;
    string desc;
    int hpBonus;
    int atkBonus;
    int defBonus;
    int cdReduce;
    bool isExclusive;
    
    Card(string n, string d, int h=0, int a=0, int df=0, int c=0, bool ex=false)
        : name(n), desc(d), hpBonus(h), atkBonus(a), defBonus(df), cdReduce(c), isExclusive(ex) {}
};

// 神符结构体
struct Rune {
    string name;
    string effect;
    int duration;
    
    Rune(string n, string e, int d) : name(n), effect(e), duration(d) {}
};

// 英雄结构体
struct Hero {
    string name;       
    int maxHp;
    int currentHp;
    int attack;
    int defense;
    string skillName;  
    string skillDesc;  
    int skillCdMax;
    int currentCd;
    bool isInBush;     
    bool isOnBouncer;  
    bool isStunned;    
    bool isCritReady;  
    
    // AI 难度参数
    int aiAggression; // 攻击倾向 (0-100)
    int aiIntelligence; // 智能倾向 (0-100)，决定是否会保留技能应对关键情况

    Hero(string n, int mh, int atk, int def, string sn, string sd, int cd, int agg=50, int intel=50) 
        : name(n), 
          maxHp(mh), 
          currentHp(mh),
          attack(atk), 
          defense(def), 
          skillName(sn), 
          skillDesc(sd), 
          skillCdMax(cd), 
          currentCd(0),
          isInBush(false), 
          isOnBouncer(false), 
          isStunned(false), 
          isCritReady(false),
          aiAggression(agg),
          aiIntelligence(intel) {}
    
    Hero() : maxHp(0), currentHp(0), attack(0), defense(0), skillCdMax(0), currentCd(0),
             isInBush(false), isOnBouncer(false), isStunned(false), isCritReady(false),
             aiAggression(50), aiIntelligence(50) {}
    
    int takeDamage(int dmg) {
        if (isStunned) return 0;
        if (isInBush) dmg = (int)(dmg * 0.8);
        int actualDmg = max(1, dmg - defense / 2);
        currentHp -= actualDmg;
        if (currentHp < 0) currentHp = 0;
        return actualDmg;
    }
    
    void heal(int amount) {
        currentHp += amount;
        if (currentHp > maxHp) currentHp = maxHp;
    }
    
    bool isAlive() const { return currentHp > 0; }
    
    double getHpPercent() const {
        if (maxHp == 0) return 0;
        return (double)currentHp / maxHp;
    }
};

// 游戏主类
class FinalEggArena {
private:
    Hero player;
    vector<Hero> bots;
    vector<Card> allCards;
    vector<Rune> allRunes;
    int roundCount;
    bool gameEnd;

public:
    FinalEggArena() : roundCount(1), gameEnd(false) {
        srand(time(0));
        initCards();
        initRunes();
    }

    void initCards() {
        allCards.push_back(Card("巨人之怒", "体型变大，生命值+20%，攻击力+10%", 20, 5, 0, 0));
        allCards.push_back(Card("隐形鞋", "进入隐身状态，首次攻击必定暴击", 0, 5, 0, -1));
        allCards.push_back(Card("急救包", "立即恢复30%最大生命值", 30, 0, 0, 0));
        allCards.push_back(Card("冷却鞋", "技能冷却时间减少20%", 0, 0, 0, -2));
        allCards.push_back(Card("荆棘甲", "受到攻击时反弹10%伤害", 10, 0, 5, 0));
        allCards.push_back(Card("自然之友", "在草丛中每回合自动回复5点生命", 10, 0, 0, 0));
        allCards.push_back(Card("草里藏刀", "草丛中暴击率提升至100%", 0, 8, 0, 0));
        allCards.push_back(Card("渐入佳境", "普攻15次后伤害提升50%", 0, 10, 0, 0));
        allCards.push_back(Card("殊死一搏", "生命值越低伤害越高", 0, 12, -5, 0));
        allCards.push_back(Card("隆重谢幕", "生命值低于30%时原地爆炸", 0, 0, 0, 0));
        allCards.push_back(Card("逆境求生", "生命值低于50%时获得20%吸血", 5, 5, 5, 0));
        
        allCards.push_back(Card("引力炸弹", "吸附范围内敌人并造成范围伤害", 0, 12, 0, 0, true));
        allCards.push_back(Card("高能激光", "穿透性直线范围伤害", 0, 18, -2, 0, true));
        allCards.push_back(Card("律动节拍", "释放音波使敌人失控眩晕", 0, 10, 0, 0, true));
        allCards.push_back(Card("咸鱼突刺", "手持咸鱼造成高额物理伤害", 0, 10, 0, 0, true));
    }

    void initRunes() {
        allRunes.push_back(Rune("热血神符", "立即恢复20点生命值", 1));
        allRunes.push_back(Rune("力量神符", "永久增加5点攻击力", 999));
        allRunes.push_back(Rune("迅捷神符", "本回合技能无冷却", 1));
        allRunes.push_back(Rune("隐身神符", "获得隐身效果，受击3次后破隐", 3));
        allRunes.push_back(Rune("巨人体型神符", "体型变大，受击9次解除增益", 9));
        allRunes.push_back(Rune("冷却缩减神符", "技能冷却大幅缩减，受击12次解除", 12));
    }

    void selectHero() {
        ClearScreen();
        SetColor(14);
        cout << "========================================" << endl;
        cout << "   蛋仔派对 超燃竞技场        " << endl;
        cout << "========================================" << endl;
        SetColor(7);
        cout << "\n请选择你的出战英雄:\n" << endl;
        
        cout << "1. 仔仔熊   HP:120 ATK:10 DEF:8 | 技能: 爱的抱抱" << endl;
        cout << "2. 失心熊   HP:100 ATK:14 DEF:4 | 技能: 失心索要" << endl;
        cout << "3. 小黄     HP:90  ATK:16 DEF:2 | 技能: 滚动" << endl;
        cout << "4. 小黑     HP:95  ATK:15 DEF:3 | 技能: 隐身" << endl;
        cout << "5. 魔鬼蛋   HP:105 ATK:13 DEF:5 | 技能: 过肩摔" << endl;
        cout << "6. 小红     HP:95  ATK:15 DEF:2 | 技能: 破刀式" << endl;
        cout << "7. 黑拳     HP:85  ATK:18 DEF:1 | 技能: 钢铁格挡&撼地重击" << endl;
        
        int choice;
        cout << "\n输入选择(1-7): ";
        cin >> choice;
        
        switch(choice) {
            case 1: 
                player = Hero("仔仔熊", 120, 10, 8, "爱的抱抱", "给自己恢复生命值", 4); 
                break;
            case 2: 
                player = Hero("失心熊", 100, 14, 4, "失心索要", "控制敌人并在原地持续造成伤害", 3); 
                break;
            case 3: 
                player = Hero("小黄", 90, 16, 2, "滚动", "撞击敌人使其击退并造成伤害", 2); 
                break;
            case 4: 
                player = Hero("小黑", 95, 15, 3, "隐身", "进入隐身，下次普攻造成额外伤害", 3); 
                break;
            case 5: 
                player = Hero("魔鬼蛋", 105, 13, 5, "过肩摔", "眩晕敌人并造成高额伤害", 3); 
                break;
            case 6: 
                player = Hero("小红", 95, 15, 2, "破刀式", "发动强力的普通攻击", 3); 
                break;
            case 7: 
                player = Hero("黑拳", 85, 18, 1, "钢铁格挡&撼地重击", "免疫一次伤害并反击造成伤害", 2); 
                break;
            default: 
                player = Hero("仔仔熊", 120, 10, 8, "爱的抱抱", "给自己恢复生命值", 4);
        }
        
        // 初始化单个对手，增加难度：高攻击性，高智能
        bots.clear();
        bots.push_back(Hero("人机", 120, 14, 5, "致命连击", "连续攻击造成多段伤害", 3, 80, 90));
        
        Pause("欢迎进入超燃竞技场！");
    }

    void showBattleStatus() {
        ClearScreen();
        SetColor(11);
        cout << "================ 第 " << roundCount << " 回合 ================" << endl;
        SetColor(7);
        
        SetColor(10);
        cout << "[玩家] " << player.name;
        if(player.isInBush) cout << " ??草丛中";
        if(player.isOnBouncer) cout << " ??弹板上";
        if(player.isStunned) cout << " ?眩晕中";
        cout << endl;
        cout << "HP: " << player.currentHp << "/" << player.maxHp;
        cout << " | ATK: " << player.attack;
        cout << " | 技能CD: " << (player.currentCd > 0 ? to_string(player.currentCd) : "就绪") << endl;
        
        cout << "\n--- 敌方单位 ---" << endl;
        for(int i=0; i<bots.size(); i++) {
            if(bots[i].isAlive()) {
                SetColor(12);
                cout << "[敌人] " << bots[i].name;
                if(bots[i].isInBush) cout << " ??草丛中";
                if(bots[i].isOnBouncer) cout << " ??弹板上";
                if(bots[i].isStunned) cout << " ?眩晕中";
                cout << " HP: " << bots[i].currentHp << "/" << bots[i].maxHp;
                cout << " | 技能CD: " << (bots[i].currentCd > 0 ? to_string(bots[i].currentCd) : "就绪") << endl;
            } else {
                SetColor(8);
                cout << "[敌人] " << bots[i].name << " 已淘汰" << endl;
            }
        }
        SetColor(7);
    }

    void playerAction() {
        showBattleStatus();
        SetColor(14);
        cout << "\n>>> 行动选项 <<<" << endl;
        SetColor(7);
        cout << "1. 普通攻击" << endl;
        cout << "2. 使用技能: " << player.skillName << " (" << player.skillDesc << ")" << (player.currentCd > 0 ? " [冷却中]" : "") << endl;
        cout << "3. 进入草丛（减伤20%，下次攻击必定暴击）" << endl;
        cout << "4. 踩弹板（位移躲避，本回合免疫远程伤害）" << endl;
        
        int opt;
        cout << "请选择: ";
        cin >> opt;
        
        player.isInBush = false;
        player.isOnBouncer = false;
        
        if(opt == 3) {
            player.isInBush = true;
            player.isCritReady = true;
            cout << "你躲进了草丛，身形隐蔽，下次攻击必定暴击！" << endl;
        } else if(opt == 4) {
            player.isOnBouncer = true;
            cout << "你踩上弹板腾空而起，躲避所有远程攻击！" << endl;
        } else if(opt == 2 && player.currentCd <= 0 && !player.isStunned) {
            cout << "你释放了技能【" << player.skillName << "】！" << endl;
            
            if (player.name == "仔仔熊") {
                int healAmount = 30 + player.maxHp * 0.1; // 技能随成长增强
                player.heal(healAmount);
                cout << "你使用了【爱的抱抱】，恢复了 " << healAmount << " 点生命值！" << endl;
            } else if (player.name == "失心熊") {
                for(auto& bot : bots) {
                    if(bot.isAlive()) {
                        bot.isStunned = true;
                        int dmg = player.attack * 1.2;
                        int actual = bot.takeDamage(dmg);
                        cout << "对" << bot.name << "施加【失心索要】，造成" << actual << "点伤害并眩晕！" << endl;
                        break;
                    }
                }
            } else if (player.name == "小黄") {
                for(auto& bot : bots) {
                    if(bot.isAlive()) {
                        int dmg = player.attack * 1.5;
                        int actual = bot.takeDamage(dmg);
                        cout << "你使用【滚动】撞飞了" << bot.name << "，造成" << actual << "点伤害！" << endl;
                        break;
                    }
                }
            } else if (player.name == "小黑") {
                player.isCritReady = true;
                player.attack += 10;
                cout << "你进入【隐身】状态，下次攻击将造成额外伤害！" << endl;
            } else if (player.name == "魔鬼蛋") {
                for(auto& bot : bots) {
                    if(bot.isAlive()) {
                        bot.isStunned = true;
                        int dmg = player.attack * 2;
                        int actual = bot.takeDamage(dmg);
                        cout << "你对" << bot.name << "使用【过肩摔】，造成" << actual << "点伤害并眩晕！" << endl;
                        break;
                    }
                }
            } else if (player.name == "小红") {
                for(auto& bot : bots) {
                    if(bot.isAlive()) {
                        int dmg = player.attack * 1.2;
                        int actual = bot.takeDamage(dmg);
                        cout << "你使用【破刀式】攻击" << bot.name << "，造成" << actual << "点伤害！" << endl;
                        break;
                    }
                }
            } else if (player.name == "黑拳") {
                player.defense += 20;
                for(auto& bot : bots) {
                    if(bot.isAlive()) {
                        int dmg = player.attack;
                        int actual = bot.takeDamage(dmg);
                        cout << "你使用【钢铁格挡&撼地重击】反击" << bot.name << "，造成" << actual << "点伤害！" << endl;
                        break;
                    }
                }
            } else {
                for(auto& bot : bots) {
                    if(bot.isAlive()) {
                        int skillDmg = (int)(player.attack * 1.5);
                        int actual = bot.takeDamage(skillDmg);
                        cout << "对" << bot.name << "造成" << actual << "点技能伤害！" << endl;
                        break;
                    }
                }
            }
            
            player.currentCd = player.skillCdMax + 1;
        } else {
            if(player.isStunned) {
                cout << "你处于眩晕状态，无法行动！" << endl;
            } else {
                for(auto& bot : bots) {
                    if(bot.isAlive()) {
                        int atkDmg = player.attack + rand()%3;
                        if(player.isCritReady) {
                            atkDmg *= 2;
                            player.isCritReady = false;
                            cout << "草丛暴击！";
                        }
                        int actual = bot.takeDamage(atkDmg);
                        cout << "你攻击了" << bot.name << "，造成" << actual << "点伤害！" << endl;
                        break;
                    }
                }
            }
        }
        Sleep(1500);
    }

    // 增强的AI逻辑
    void botAction() {
        for(auto& bot : bots) {
            if(!bot.isAlive()) continue;
            
            // 状态检查
            if(bot.isStunned) {
                bot.isStunned = false;
                cout << bot.name << "眩晕解除，恢复行动！" << endl;
                continue;
            }

            // AI 决策变量
            bool willUseSkill = false;
            bool willDefend = false;
            bool willAttack = true;

            // 1. 防御决策 (智能倾向高时更倾向于保命)
            if (bot.getHpPercent() < 0.3 && bot.aiIntelligence > 60) {
                // 低血量尝试防御或躲避
                if (rand() % 100 < bot.aiIntelligence) {
                    if (rand() % 2 == 0) {
                        bot.isInBush = true;
                        bot.isCritReady = true;
                        cout << bot.name << " 机智地躲入草丛寻求庇护！" << endl;
                        willAttack = false; // 躲起来通常不攻击
                    } else {
                        bot.isOnBouncer = true;
                        cout << bot.name << " 踩上弹板规避伤害！" << endl;
                        willAttack = false;
                    }
                    willDefend = true;
                }
            }

            // 2. 技能决策
            if (bot.currentCd <= 0 && willAttack) {
                // 计算技能收益
                bool isHealSkill = (bot.name.find("仔仔熊") != string::npos);
                bool isControlSkill = (bot.name.find("失心熊") != string::npos || bot.name.find("魔鬼蛋") != string::npos);
                
                // 高智能AI会根据局势判断
                if (bot.aiIntelligence > 70) {
                    if (isHealSkill && bot.getHpPercent() < 0.6) {
                        willUseSkill = true; // 血少必奶
                    } else if (isControlSkill && player.getHpPercent() < 0.5) {
                        willUseSkill = true; // 敌人血少必控杀
                    } else if (bot.aiAggression > 70) {
                        willUseSkill = true; // 高攻击性，有技能就用
                    }
                } else {
                    // 低智能AI随机使用
                    if (rand() % 100 < bot.aiAggression) {
                        willUseSkill = true;
                    }
                }
            }

            // 执行动作
            if (willUseSkill && !willDefend) {
                cout << bot.name << " 释放了技能【" << bot.skillName << "】！" << endl;
                
                if (bot.name == "仔仔熊") {
                    int healAmount = 30 + bot.maxHp * 0.1;
                    bot.heal(healAmount);
                    cout << bot.name << " 恢复了 " << healAmount << " 点生命值！" << endl;
                } else if (bot.name == "失心熊") {
                    player.isStunned = true;
                    int dmg = bot.attack;
                    int actual = player.takeDamage(dmg);
                    cout << "对你施加【失心索要】，造成" << actual << "点伤害并眩晕！" << endl;
                } else if (bot.name == "小黄") {
                    int dmg = bot.attack * 1.5;
                    int actual = player.takeDamage(dmg);
                    cout << bot.name << " 使用【滚动】撞飞了你，造成" << actual << "点伤害！" << endl;
                } else if (bot.name == "小黑") {
                    bot.isCritReady = true;
                    bot.attack += 10;
                    cout << bot.name << " 进入【隐身】状态！" << endl;
                } else if (bot.name == "魔鬼蛋") {
                    player.isStunned = true;
                    int dmg = bot.attack * 2;
                    int actual = player.takeDamage(dmg);
                    cout << bot.name << " 对你使用【过肩摔】，造成" << actual << "点伤害并眩晕！" << endl;
                } else if (bot.name == "小红") {
                    int dmg = bot.attack * 1.2;
                    int actual = player.takeDamage(dmg);
                    cout << bot.name << " 使用【破刀式】攻击你，造成" << actual << "点伤害！" << endl;
                } else if (bot.name == "黑拳") {
                    bot.defense += 20;
                    int dmg = bot.attack;
                    int actual = player.takeDamage(dmg);
                    cout << bot.name << " 使用【钢铁格挡&撼地重击】反击，造成" << actual << "点伤害！" << endl;
                } else {
                    // 默认技能
                    int skillDmg = (int)(bot.attack * 1.5);
                    int actual = player.takeDamage(skillDmg);
                    cout << bot.name << " 对你造成" << actual << "点技能伤害！" << endl;
                }
                
                bot.currentCd = bot.skillCdMax + 1;
            } else if (willAttack && !willDefend) {
                // 普通攻击
                int dmg = bot.attack;
                // 暴击判定
                if (bot.isCritReady) {
                    dmg *= 2;
                    bot.isCritReady = false;
                    cout << bot.name << " 发动了暴击！";
                }
                
                if (player.isOnBouncer && rand() % 3 == 0) {
                    cout << bot.name << " 的攻击被你弹板躲避！" << endl;
                } else {
                    int actual = player.takeDamage(dmg);
                    cout << bot.name << " 普通攻击，造成" << actual << "点伤害！" << endl;
                }
            } else {
                 cout << bot.name << " 采取了守势。" << endl;
            }

            // 清理临时状态
            bot.isInBush = false; // 回合结束出草
            bot.isOnBouncer = false; // 回合结束下板
            
            bot.currentCd = max(0, bot.currentCd - 1);
            Sleep(800);
        }
    }

    void cardPick() {
        ClearScreen();
        SetColor(13);
        cout << "\n>>> 卡牌补给站 <<<" << endl;
        SetColor(7);
        cout << "从3张卡牌中选择一张强化:\n" << endl;
        
        vector<Card> options;
        for(int i=0; i<3; i++) {
            options.push_back(allCards[rand() % allCards.size()]);
        }
        
        for(int i=0; i<3; i++) {
            cout << i+1 << ". 【" << options[i].name << "】" << endl;
            cout << "   效果: " << options[i].desc << endl;
        }
        
        int pick;
        cout << "\n选择卡牌(1-3): ";
        cin >> pick;
        if(pick <1 || pick>3) pick =1;
        
        Card selected = options[pick-1];
        player.maxHp += selected.hpBonus;
        player.currentHp += selected.hpBonus;
        player.attack += selected.atkBonus;
        player.defense += selected.defBonus;
        player.skillCdMax = max(1, player.skillCdMax + selected.cdReduce);
        
        cout << "获得卡牌【" << selected.name << "】，强化完成！" << endl;
        Sleep(1000);
    }

    void runeEvent() {
        if(rand()%10 < 3) {
            Rune r = allRunes[rand() % allRunes.size()];
            ClearScreen();
            SetColor(13);
            cout << "\n? 神符刷新: 【" << r.name << "】" << endl;
            SetColor(7);
            cout << "效果: " << r.effect << endl;
            char opt;
            cout << "是否拾取(y/n): ";
            cin >> opt;
            if(opt == 'y' || opt == 'Y') {
                if(r.name == "热血神符") player.heal(20);
                if(r.name == "力量神符") player.attack +=5;
                if(r.name == "迅捷神符") player.currentCd =0;
                cout << "成功拾取神符【" << r.name << "】！" << endl;
            }
            Pause();
        }
    }

    void checkWinLose() {
        bool allBotDead = true;
        for(auto& bot : bots) {
            if(bot.isAlive()) allBotDead = false;
        }
        
        if(!player.isAlive()) {
            gameEnd = true;
            ClearScreen();
            SetColor(12);
            cout << "\n========================================" << endl;
            cout << "       你被淘汰了，本局结束！           " << endl;
            cout << "========================================" << endl;
        } else if(allBotDead) {
            gameEnd = true;
            ClearScreen();
            SetColor(10);
            cout << "\n========================================" << endl;
            cout << "       恭喜你获得超燃竞技场胜利！       " << endl;
            cout << "========================================" << endl;
        }
    }

    void runGame() {
        selectHero();
        while(!gameEnd) {
            runeEvent();
            cardPick();
            playerAction();
            checkWinLose();
            if(gameEnd) break;
            botAction();
            player.currentCd = max(0, player.currentCd - 1);
            roundCount++;
        }
        Pause("游戏结束，感谢游玩！");
    }
};

int main() {
    FinalEggArena game;
    game.runGame();
    return 0;
}



