跳到正文

科技·工程

地铁逃生模拟器

Fire_poetry约 61 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年1月3日
篇幅
约 61 分钟

版本

  1. v1当前
C++
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <map>
#include <algorithm>
#include <memory>
#include <utility>

using namespace std;

class Player;  // 前向声明

// 物品基类
class Item {
protected:
    string name;
    int weight;
    int value;
public:
    Item(const string& n, int w, int v) : name(n), weight(w), value(v) {}
    virtual ~Item() {}
    string getName() const { return name; }
    int getWeight() const { return weight; }
    int getValue() const { return value; }
    virtual string getType() const = 0;
    virtual void use(Player& player) = 0;
    virtual string getDescription() const {
        return name + " (重量:" + to_string(weight) + " 价值:" + to_string(value) + "地铁币)";
    }
};

// 可收集物品类
class Collectible : public Item {
public:
    Collectible(const string& n, int w, int v) : Item(n, w, v) {}
    string getType() const override { return "collectible"; }
    void use(Player& player) override {}
};

// 采矿相关物品
class MiningItem : public Item {
public:
    MiningItem(const string& n, int w, int v) : Item(n, w, v) {}
    string getType() const override { return "mining"; }
    void use(Player& player) override {}
};

// 击败敌人相关物品
class EnemyDropItem : public Item {
public:
    EnemyDropItem(const string& n, int w, int v) : Item(n, w, v) {}
    string getType() const override { return "enemy_drop"; }
    void use(Player& player) override {}
};

// 消耗品类
class Consumable : public Item {
public:
    Consumable(const string& n, int w, int v) : Item(n, w, v) {}
    string getType() const override { return "consumable"; }
};

// 医疗包
class Medkit : public Consumable {
public:
    Medkit() : Consumable("医疗包", 5, 150) {}
    void use(Player& player) override;
};

// 装甲
class Armor : public Item {
    int protection;
public:
    Armor() : Item("装甲", 10, 300), protection(50) {}
    string getType() const override { return "armor"; }
    void use(Player& player) override;
};

// 武器基类
class Weapon : public Item {
protected:
    int damage;
    int ammoCapacity;
public:
    Weapon(const string& n, int w, int v, int dmg, int ammo)
        : Item(n, w, v), damage(dmg), ammoCapacity(ammo) {}
    string getType() const override { return "weapon"; }
    int getDamage() const { return damage; }
    int getAmmoCapacity() const { return ammoCapacity; }
    string getDescription() const override {
        return Item::getDescription() + " 伤害:" + to_string(damage) + " 弹容:" + to_string(ammoCapacity);
    }
};

// 手枪
class Pistol : public Weapon {
public:
    Pistol() : Weapon("手枪", 5, 200, 25, 10) {}
    void use(Player& player) override;
};

// 步枪
class Rifle : public Weapon {
public:
    Rifle() : Weapon("步枪", 8, 500, 40, 30) {}
    void use(Player& player) override;
};

// 加密箱
class SecureContainer : public Item {
    vector<shared_ptr<Item> > items;
    int capacity;
public:
    SecureContainer() : Item("加密箱", 0, 0), capacity(40) {}
    string getType() const override { return "container"; }
    void use(Player& player) override {}

    bool addItem(shared_ptr<Item> item) {
        if (getCurrentWeight() + item->getWeight() <= capacity) {
            items.push_back(item);
            return true;
        }
        return false;
    }

    int getCurrentWeight() const {
        int total = 0;
        for (size_t i = 0; i < items.size(); ++i) {
            total += items[i]->getWeight();
        }
        return total;
    }

    const vector<shared_ptr<Item> >& getItems() const { return items; }
    void clear() { items.clear(); }

    string getDescription() const override {
        return "加密箱 (容量:" + to_string(capacity) + " 当前:" + to_string(getCurrentWeight()) +
               " 物品数:" + to_string(items.size()) + ")";
    }
};

// 采矿系统
class MiningSystem {
private:
    static const vector<pair<string, pair<int, int> > > miningDrops;

public:
    static shared_ptr<Item> tryMine() {
        int roll = rand() % 100;
        int cumulativeProb = 0;

        for (const auto& drop : miningDrops) {
            cumulativeProb += drop.second.first;
            if (roll < cumulativeProb) {
                if (drop.first == "紫矿碎片" || drop.first == "紫矿晶块" || drop.first == "稀有宝石") {
                    return make_shared<MiningItem>(drop.first, 10, drop.second.second);
                }
            }
        }
        return nullptr;
    }

    static void displayMiningChances() {
        cout << "\n=== 采矿概率 ===";
        for (const auto& drop : miningDrops) {
            if (drop.first == "紫矿碎片" || drop.first == "紫矿晶块" || drop.first == "稀有宝石") {
                cout << "\n" << drop.first << ": " << drop.second.first << "%";
            }
        }
        cout << "\n==============\n";
    }
};

// 采矿掉落物配置
const vector<pair<string, pair<int, int> > > MiningSystem::miningDrops = {
    make_pair("石头", make_pair(40, 50)),
    make_pair("铁矿石", make_pair(30, 200)),
    make_pair("铜矿石", make_pair(20, 300)),
    make_pair("紫矿碎片", make_pair(7, 4500)),
    make_pair("紫矿晶块", make_pair(2, 15000)),
    make_pair("稀有宝石", make_pair(1, 120000))
};

// 敌人掉落系统
class EnemyDropSystem {
private:
    static const vector<pair<string, pair<int, int> > > enemyDrops;

public:
    static shared_ptr<Item> tryGetDrop() {
        int roll = rand() % 100;
        int cumulativeProb = 0;

        for (const auto& drop : enemyDrops) {
            cumulativeProb += drop.second.first;
            if (roll < cumulativeProb) {
                if (drop.first == "野兽碎骨" || drop.first == "野兽头骨") {
                    return make_shared<EnemyDropItem>(drop.first, 10, drop.second.second);
                }
            }
        }
        return nullptr;
    }

    static void displayDropChances() {
        cout << "\n=== 敌人掉落概率 ===";
        for (const auto& drop : enemyDrops) {
            if (drop.first == "野兽碎骨" || drop.first == "野兽头骨") {
                cout << "\n" << drop.first << ": " << drop.second.first << "%";
            }
        }
        cout << "\n==================\n";
    }
};

// 敌人掉落配置
const vector<pair<string, pair<int, int> > > EnemyDropSystem::enemyDrops = {
    make_pair("绷带", make_pair(50, 100)),
    make_pair("手枪子弹", make_pair(30, 50)),
    make_pair("野兽碎骨", make_pair(15, 4500)),
    make_pair("野兽头骨", make_pair(5, 4500))
};

// 玩家类
class Player {
    string name;
    int health;
    int armor;
    int metroCoins;
    vector<shared_ptr<Item> > inventory;
    vector<shared_ptr<Item> > backpack;
    shared_ptr<SecureContainer> secureContainer;
    shared_ptr<Weapon> currentWeapon;
    int positionX;
    int positionY;
    bool hasEscaped;
    int ammo;

public:
    Player(const string& playerName) : name(playerName), health(100), armor(0),
                                     metroCoins(0), secureContainer(make_shared<SecureContainer>()),
                                     currentWeapon(), positionX(0), positionY(0),
                                     hasEscaped(false), ammo(30) {
        inventory.push_back(make_shared<Medkit>());
        inventory.push_back(make_shared<Medkit>());
    }

    void move(int dx, int dy) {
        positionX += dx;
        positionY += dy;
        cout << name << " 移动到了位置 (" << positionX << ", " << positionY << ")\n";
    }

    void takeDamage(int damage) {
        if (armor > 0) {
            int remainingArmor = armor - damage;
            if (remainingArmor >= 0) {
                armor = remainingArmor;
                cout << name << " 的装甲吸收了全部伤害!\n";
            } else {
                damage = -remainingArmor;
                armor = 0;
                health -= damage;
                cout << name << " 的装甲吸收了部分伤害,受到 " << damage << " 点伤害!\n";
            }
        } else {
            health -= damage;
            cout << name << " 受到 " << damage << " 点伤害!\n";
        }

        if (health <= 0) {
            health = 0;
            cout << name << " 已被淘汰!\n";
            saveToSecureContainer();
        }
    }

    void heal(int amount) {
        health = min(100, health + amount);
        cout << name << " 恢复了 " << amount << " 点生命值,当前生命值: " << health << "\n";
    }

    void equipArmor(int protection) {
        armor = protection;
        cout << "已装备装甲,防护值: " << armor << "\n";
    }

    void equipWeapon(shared_ptr<Weapon> weapon) {
        currentWeapon = weapon;
        cout << "已装备 " << weapon->getName() << "\n";
    }

    void addMetroCoins(int amount) {
        metroCoins += amount;
        cout << "获得 " << amount << " 地铁币\n";
    }

    bool spendMetroCoins(int amount) {
        if (metroCoins >= amount) {
            metroCoins -= amount;
            return true;
        }
        cout << "地铁币不足!\n";
        return false;
    }

    bool addToBackpack(shared_ptr<Item> item) {
        if (backpack.size() < 10) {
            backpack.push_back(item);
            cout << "将 " << item->getName() << " 放入背包\n";
            return true;
        }
        cout << "背包已满!\n";
        return false;
    }

    bool addToSecureContainer(shared_ptr<Item> item) {
        if (secureContainer->addItem(item)) {
            cout << "将 " << item->getName() << " 放入加密箱\n";
            return true;
        }
        cout << "加密箱空间不足!\n";
        return false;
    }

    void saveToSecureContainer() {
        cout << "\n=== 逃生失败,加密箱物品保存 ===\n";
        const vector<shared_ptr<Item> >& items = secureContainer->getItems();
        for (size_t i = 0; i < items.size(); ++i) {
            cout << "保存: " << items[i]->getDescription() << "\n";
        }
        cout << "==============================\n";
    }

    void sellItem(int index) {
        if (index >= 0 && index < static_cast<int>(backpack.size())) {
            shared_ptr<Item> item = backpack[index];
            int value = item->getValue();
            addMetroCoins(value);
            cout << "出售 " << item->getName() << " 获得 " << value << " 地铁币\n";
            backpack.erase(backpack.begin() + index);
        } else {
            cout << "无效的物品索引!\n";
        }
    }

    void showBackpack() const {
        cout << "\n=== 背包内容 ===\n";
        cout << "地铁币: " << metroCoins << "\n";
        cout << "加密箱: " << secureContainer->getDescription() << "\n";
        for (size_t i = 0; i < backpack.size(); ++i) {
            cout << i << ". " << backpack[i]->getDescription() << "\n";
        }
        cout << "===============\n";
    }

    void showShop() const {
        cout << "\n=== 商店 ===\n";
        cout << "地铁币: " << metroCoins << "\n";
        cout << "1. 医疗包 - 150 地铁币\n";
        cout << "2. 装甲 - 300 地铁币\n";
        cout << "3. 手枪 - 200 地铁币\n";
        cout << "4. 步枪 - 500 地铁币\n";
        cout << "5. 离开商店\n";
        cout << "===========\n";
    }

    void buyItem(int choice) {
        switch(choice) {
            case 1:
                if (spendMetroCoins(150)) {
                    inventory.push_back(make_shared<Medkit>());
                    cout << "购买了医疗包\n";
                }
                break;
            case 2:
                if (spendMetroCoins(300)) {
                    inventory.push_back(make_shared<Armor>());
                    cout << "购买了装甲\n";
                }
                break;
            case 3:
                if (spendMetroCoins(200)) {
                    currentWeapon = make_shared<Pistol>();
                    inventory.push_back(currentWeapon);
                    cout << "购买了手枪\n";
                }
                break;
            case 4:
                if (spendMetroCoins(500)) {
                    currentWeapon = make_shared<Rifle>();
                    inventory.push_back(currentWeapon);
                    cout << "购买了步枪\n";
                }
                break;
            case 5:
                cout << "离开商店\n";
                break;
            default:
                cout << "无效选择\n";
        }
    }

    void showStatus() const {
        cout << "\n=== 玩家状态 ===\n";
        cout << "名称: " << name << "\n";
        cout << "生命值: " << health << "\n";
        cout << "装甲: " << armor << "\n";
        cout << "弹药: " << ammo << "\n";
        cout << "地铁币: " << metroCoins << "\n";
        cout << "位置: (" << positionX << ", " << positionY << ")\n";
        if (currentWeapon) {
            cout << "当前武器: " << currentWeapon->getName()
                 << " (伤害: " << currentWeapon->getDamage()
                 << ", 弹容: " << currentWeapon->getAmmoCapacity() << ")\n";
        }
        cout << "===============\n\n";
    }

    void escape() {
        hasEscaped = true;
        cout << name << " 成功逃生了!\n";
        cout << "带出的物品总价值: " << calculateEscapeValue() << " 地铁币\n";
    }

    int calculateEscapeValue() const {
        int total = 0;
        for (size_t i = 0; i < backpack.size(); ++i) {
            total += backpack[i]->getValue();
        }
        const vector<shared_ptr<Item> >& containerItems = secureContainer->getItems();
        for (size_t i = 0; i < containerItems.size(); ++i) {
            total += containerItems[i]->getValue();
        }
        return total;
    }

    bool isAlive() const { return health > 0; }
    bool hasEscapedSuccessfully() const { return hasEscaped; }
    pair<int, int> getPosition() const { return make_pair(positionX, positionY); }
    int getAmmo() const { return ammo; }
    void useAmmo() { ammo--; }
    void addAmmo(int amount) { ammo += amount; }
    int getWeaponDamage() const { return currentWeapon ? currentWeapon->getDamage() : 10; }
    size_t getBackpackSize() const { return backpack.size(); }
    int getMetroCoins() const { return metroCoins; }

    void mine() {
        cout << "开始采矿...\n";
        MiningSystem::displayMiningChances();
        shared_ptr<Item> minedItem = MiningSystem::tryMine();
        if (minedItem) {
            cout << "挖到了: " << minedItem->getDescription() << "\n";
            cout << "要放入 (1)背包 (2)加密箱? ";
            int choice;
            cin >> choice;
            if (choice == 1) {
                if (!addToBackpack(minedItem)) {
                    cout << "背包已满!\n";
                }
            } else if (choice == 2) {
                if (!addToSecureContainer(minedItem)) {
                    cout << "加密箱已满!\n";
                }
            }
        } else {
            cout << "这次没有挖到特殊矿物\n";
        }
    }

    void onEnemyDefeated() {
        shared_ptr<Item> drop = EnemyDropSystem::tryGetDrop();
        if (drop) {
            cout << "获得敌人掉落: " << drop->getDescription() << "\n";
            cout << "要放入 (1)背包 (2)加密箱? ";
            int choice;
            cin >> choice;
            if (choice == 1) {
                if (!addToBackpack(drop)) {
                    cout << "背包已满!\n";
                }
            } else if (choice == 2) {
                if (!addToSecureContainer(drop)) {
                    cout << "加密箱已满!\n";
                }
            }
        } else {
            cout << "敌人没有掉落特殊物品\n";
        }
    }
};

void Medkit::use(Player& player) {
    player.heal(50);
}

void Armor::use(Player& player) {
    player.equipArmor(protection);
}

void Pistol::use(Player& player) {
    player.equipWeapon(make_shared<Pistol>(*this));
}

void Rifle::use(Player& player) {
    player.equipWeapon(make_shared<Rifle>(*this));
}

class Enemy {
    int health;
    int damage;
    int positionX;
    int positionY;

public:
    Enemy(int x, int y) : health(50), damage(15), positionX(x), positionY(y) {}

    void takeDamage(int dmg) {
        health -= dmg;
        if (health <= 0) {
            health = 0;
            cout << "敌人已被击败!\n";
        }
    }

    void moveRandomly(class GameMap& map);

    bool isAlive() const { return health > 0; }
    pair<int, int> getPosition() const { return make_pair(positionX, positionY); }
    int getDamage() const { return damage; }
};

void Enemy::moveRandomly(GameMap& map) {
    if (!isAlive()) return;

    int direction = rand() % 4;
    int newX = positionX;
    int newY = positionY;

    switch(direction) {
        case 0: newX--; break;
        case 1: newX++; break;
        case 2: newY--; break;
        case 3: newY++; break;
    }

    if (newX >= 0 && newX < map.getSize() &&
        newY >= 0 && newY < map.getSize() &&
        map.getCell(newX, newY) == '.') {
        map.updateEnemyPosition(positionX, positionY, newX, newY);
        positionX = newX;
        positionY = newY;
    }
}

class GameMap {
    vector<vector<char> > grid;
    int size;
    vector<pair<int, int> > escapePoints;
    vector<shared_ptr<Item> > items;
    vector<Enemy> enemies;
    vector<pair<int, int> > miningSpots;

public:
    GameMap(int mapSize) : size(mapSize) {
        grid.resize(size, vector<char>(size, '.'));

        // 生成逃生点
        escapePoints.push_back(make_pair(0, 0));
        escapePoints.push_back(make_pair(0, size-1));
        escapePoints.push_back(make_pair(size-1, 0));
        escapePoints.push_back(make_pair(size-1, size-1));
        escapePoints.push_back(make_pair(size/2, 0));
        escapePoints.push_back(make_pair(size/2, size-1));
        escapePoints.push_back(make_pair(0, size/2));
        escapePoints.push_back(make_pair(size-1, size/2));

        for (size_t i = 0; i < escapePoints.size(); ++i) {
            grid[escapePoints[i].first][escapePoints[i].second] = 'E';
        }

        srand(time(0));
        generateItems();
        generateEnemies();
        generateMiningSpots();
    }

    void generateItems() {
        for (int i = 0; i < size*2; ++i) {
            int x = rand() % size;
            int y = rand() % size;

            bool isEscapePoint = false;
            for (size_t j = 0; j < escapePoints.size(); ++j) {
                if (escapePoints[j].first == x && escapePoints[j].second == y) {
                    isEscapePoint = true;
                    break;
                }
            }

            if (!isEscapePoint) {
                int itemId = rand() % 61;
                items.push_back(createCollectible(itemId));
                grid[x][y] = 'I';
            }
        }
    }

    void generateEnemies() {
        for (int i = 0; i < size; ++i) {
            int x, y;
            do {
                x = rand() % size;
                y = rand() % size;
            } while (grid[x][y] != '.');

            enemies.push_back(Enemy(x, y));
            grid[x][y] = 'M';
        }
    }

    void generateMiningSpots() {
        for (int i = 0; i < 5; ++i) {
            int x, y;
            do {
                x = rand() % size;
                y = rand() % size;
            } while (grid[x][y] != '.');

            grid[x][y] = 'O';
            miningSpots.push_back(make_pair(x, y));
        }
    }

    char getCell(int x, int y) const {
        if (x >= 0 && x < size && y >= 0 && y < size) {
            return grid[x][y];
        }
        return ' ';
    }

    void updateEnemyPosition(int oldX, int oldY, int newX, int newY) {
        if (oldX >= 0 && oldX < size && oldY >= 0 && oldY < size) {
            grid[oldX][oldY] = '.';
        }
        if (newX >= 0 && newX < size && newY >= 0 && newY < size) {
            grid[newX][newY] = 'M';
        }
    }

    void moveEnemies() {
        for (size_t i = 0; i < enemies.size(); ++i) {
            enemies[i].moveRandomly(*this);
        }
    }

    void display(int playerX, int playerY) const {
        cout << "\n当前地图 (P=你, E=逃生点, I=物品, M=敌人, O=采矿点):\n";
        for (int i = 0; i < size; ++i) {
            for (int j = 0; j < size; ++j) {
                if (i == playerX && j == playerY) {
                    cout << "P ";
                } else {
                    cout << grid[i][j] << " ";
                }
            }
            cout << "\n";
        }
        cout << "\n";
    }

    shared_ptr<Item> findItemAt(int x, int y) const {
        for (size_t i = 0; i < items.size(); ++i) {
            pair<int, int> pos = getItemPosition(i);
            if (pos.first == x && pos.second == y) {
                return items[i];
            }
        }
        return shared_ptr<Item>();
    }

    Enemy* findEnemyAt(int x, int y) {
        for (size_t i = 0; i < enemies.size(); ++i) {
            pair<int, int> pos = enemies[i].getPosition();
            if (pos.first == x && pos.second == y) {
                return &enemies[i];
            }
        }
        return NULL;
    }

    void removeItem(int x, int y) {
        for (size_t i = 0; i < items.size(); ++i) {
            pair<int, int> pos = getItemPosition(i);
            if (pos.first == x && pos.second == y) {
                grid[x][y] = '.';
                items.erase(items.begin() + i);
                break;
            }
        }
    }

    void removeEnemy(int x, int y) {
        for (size_t i = 0; i < enemies.size(); ++i) {
            pair<int, int> pos = enemies[i].getPosition();
            if (pos.first == x && pos.second == y) {
                grid[x][y] = '.';
                enemies.erase(enemies.begin() + i);
                break;
            }
        }
    }

    pair<int, int> getItemPosition(int index) const {
        int count = 0;
        for (int i = 0; i < size; ++i) {
            for (int j = 0; j < size; ++j) {
                if (grid[i][j] == 'I') {
                    if (count == index) {
                        return make_pair(i, j);
                    }
                    count++;
                }
            }
        }
        return make_pair(-1, -1);
    }

    bool isEscapePoint(int x, int y) const {
        for (size_t i = 0; i < escapePoints.size(); ++i) {
            if (escapePoints[i].first == x && escapePoints[i].second == y) {
                return true;
            }
        }
        return false;
    }

    bool isMiningSpot(int x, int y) const {
        for (size_t i = 0; i < miningSpots.size(); ++i) {
            if (miningSpots[i].first == x && miningSpots[i].second == y) {
                return true;
            }
        }
        return false;
    }

    int getSize() const { return size; }
};

shared_ptr<Item> createCollectible(int id) {
    static const vector<tuple<string, int, int> > items = {
        make_tuple("指南针", 10, 1000),
        make_tuple("明信片", 10, 700),
        make_tuple("旧式录像带", 10, 1000),
        make_tuple("扑克牌", 10, 700),
        make_tuple("军用水壶", 10, 800),
        make_tuple("罐头", 10, 800),
        make_tuple("杂志", 10, 600),
        make_tuple("旅行手册", 10, 700),
        make_tuple("机油", 10, 2100),
        make_tuple("怀表", 10, 1800),
        make_tuple("怡神精油", 10, 1900),
        make_tuple("爱心项链", 10, 2000),
        make_tuple("燃气瓶", 10, 4000),
        make_tuple("润滑油", 10, 4400),
        make_tuple("汽车钥匙", 10, 5100),
        make_tuple("柴油", 10, 4000),
        make_tuple("军用手表", 10, 5000),
        make_tuple("野兽碎骨", 10, 4500),
        make_tuple("野兽头骨", 10, 4500),
        make_tuple("紫矿碎片", 10, 4500),
        make_tuple("收发信器", 10, 4500),
        make_tuple("钛合金", 10, 4500),
        make_tuple("制动引擎", 10, 4500),
        make_tuple("胶卷", 10, 4500),
        make_tuple("拉刀", 10, 4500),
        make_tuple("打火机", 10, 4500),
        make_tuple("黑胶唱片", 10, 4500),
        make_tuple("窃听装置", 10, 4500),
        make_tuple("摄像头", 10, 4500),
        make_tuple("雷管", 10, 4500),
        make_tuple("科技部件", 10, 18000),
        make_tuple("狗牌", 10, 25000),
        make_tuple("信号发生器", 10, 15600),
        make_tuple("净水器", 10, 13200),
        make_tuple("CPU处理器", 10, 13200),
        make_tuple("异变的肩骨", 10, 15000),
        make_tuple("紫矿晶块", 10, 15000),
        make_tuple("探测器", 10, 48000),
        make_tuple("平板电脑", 10, 43200),
        make_tuple("军用电池", 10, 54400),
        make_tuple("军用电路板", 10, 54400),
        make_tuple("机甲装甲片", 10, 50000),
        make_tuple("黑鹰勋章", 10, 50000),
        make_tuple("金狼雕像", 10, 50000),
        make_tuple("密信", 10, 50000),
        make_tuple("紫色试管药剂", 10, 50000),
        make_tuple("数据芯片", 10, 50000),
        make_tuple("精密仪器蓝图", 10, 112000),
        make_tuple("绝密情报", 10, 128000),
        make_tuple("设计图纸", 10, 128000),
        make_tuple("显卡", 10, 112000),
        make_tuple("镜头", 10, 112000),
        make_tuple("珍稀古钱币", 10, 128000),
        make_tuple("一根金条", 10, 200000),
        make_tuple("光子鸡奖杯", 20, 388888),
        make_tuple("机甲能源部件", 10, 120000),
        make_tuple("机甲爆发部件", 10, 120000),
        make_tuple("稀有宝石", 10, 120000),
        make_tuple("精英纪念奖杯", 20, 588888),
        make_tuple("黄金狼头权杖", 20, 588888),
        make_tuple("龙腾瑞兆金雕", 50, 1888888)
    };

    if (id >= 0 && id < static_cast<int>(items.size())) {
        return make_shared<Collectible>(get<0>(items[id]), get<1>(items[id]), get<2>(items[id]));
    }
    return shared_ptr<Item>();
}

class MetroRoyaleGame {
    Player player;
    GameMap gameMap;
    bool gameOver;

public:
    MetroRoyaleGame(const string& playerName) : player(playerName), gameMap(20), gameOver(false) {}

    void start() {
        cout << "=== 和平精英·地铁逃生模拟 ===\n";
        cout << "目标: 找到逃生点(E)并成功逃生\n";
        cout << "指令: w(上), s(下), a(左), d(右), h(治疗), f(战斗)\n";
        cout << "      g(采矿), b(背包), v(出售物品), m(商店), c(加密箱), s(状态), q(退出)\n";
        cout << "提示: 敌人会自主移动,收集物品出售后可购买更好的装备\n";
        cout << "      加密箱中的物品在逃生失败时仍可保留\n";
        cout << "      紫矿碎片、紫矿晶块、稀有宝石需要通过采矿概率获得\n";
        cout << "      野兽碎骨、野兽头骨需要通过击败敌人概率获得\n";

        while (!gameOver && player.isAlive() && !player.hasEscapedSuccessfully()) {
            pair<int, int> pos = player.getPosition();
            gameMap.display(pos.first, pos.second);
            player.showStatus();

            cout << "请输入指令: ";
            char command;
            cin >> command;

            processCommand(command);

            gameMap.moveEnemies();

            pos = player.getPosition();
            Enemy* enemy = gameMap.findEnemyAt(pos.first, pos.second);
            if (enemy != NULL && enemy->isAlive()) {
                cout << "敌人移动到了你的位置! 必须战斗!\n";
                fight();
            }

            if (gameMap.isEscapePoint(pos.first, pos.second)) {
                cout << "你发现了一个逃生点! 是否要逃生? (y/n): ";
                char choice;
                cin >> choice;
                if (choice == 'y' || choice == 'Y') {
                    player.escape();
                    gameOver = true;
                }
            }
        }

        if (player.hasEscapedSuccessfully()) {
            cout << "恭喜! 你成功逃生了!\n";
            cout << "最终获得地铁币: " << player.getMetroCoins() << "\n";
        } else if (!player.isAlive()) {
            cout << "游戏结束! 你被淘汰了!\n";
        }

        cout << "感谢游玩和平精英·地铁逃生模拟!\n";
    }

private:
    void processCommand(char command) {
        int dx = 0, dy = 0;

        switch (command) {
            case 'w': dx = -1; break;
            case 's': dx = 1; break;
            case 'a': dy = -1; break;
            case 'd': dy = 1; break;
            case 'h':
                useMedkit();
                return;
            case 'f':
                fight();
                return;
            case 'g':
                handleMining();
                return;
            case 'b':
                player.showBackpack();
                return;
            case 'v':
                sellItem();
                return;
            case 'm':
                shopMenu();
                return;
            case 'c':
                manageContainer();
                return;
            case 'q':
                gameOver = true;
                cout << "游戏已退出。\n";
                return;
            default:
                cout << "无效指令!\n";
                return;
        }

        pair<int, int> newPos = player.getPosition();
        newPos.first += dx;
        newPos.second += dy;

        if (newPos.first < 0 || newPos.first >= gameMap.getSize() ||
            newPos.second < 0 || newPos.second >= gameMap.getSize()) {
            cout << "不能移动到地图外!\n";
            return;
        }

        player.move(dx, dy);

        shared_ptr<Item> item = gameMap.findItemAt(newPos.first, newPos.second);
        if (item) {
            cout << "发现: " << item->getDescription() << "\n";
            cout << "要放入 (1)背包 (2)加密箱 (3)忽略? ";
            int choice;
            cin >> choice;
            if (choice == 1) {
                if (!player.addToBackpack(item)) {
                    cout << "背包已满!\n";
                    return;
                }
            } else if (choice == 2) {
                if (!player.addToSecureContainer(item)) {
                    cout << "加密箱已满!\n";
                    return;
                }
            } else {
                cout << "已忽略物品\n";
                return;
            }
            gameMap.removeItem(newPos.first, newPos.second);
        }
    }

    void useMedkit() {
        cout << "使用医疗包恢复生命值\n";
        player.heal(50);
    }

    void fight() {
        pair<int, int> pos = player.getPosition();
        Enemy* enemy = gameMap.findEnemyAt(pos.first, pos.second);

        if (enemy == NULL || !enemy->isAlive()) {
            cout << "附近没有敌人!\n";
            return;
        }

        if (player.getAmmo() <= 0) {
            cout << "你没有弹药了! 只能承受敌人攻击!\n";
            player.takeDamage(enemy->getDamage());
            return;
        }

        cout << "你向敌人开火!\n";
        player.useAmmo();
        enemy->takeDamage(player.getWeaponDamage());

        if (enemy->isAlive()) {
            cout << "敌人反击了!\n";
            player.takeDamage(enemy->getDamage());
        } else {
            player.onEnemyDefeated();
            gameMap.removeEnemy(pos.first, pos.second);
        }
    }

    void handleMining() {
        pair<int, int> pos = player.getPosition();
        if (gameMap.isMiningSpot(pos.first, pos.second)) {
            player.mine();
        } else {
            cout << "这里不是采矿点!\n";
        }
    }

    void sellItem() {
        player.showBackpack();
        if (player.getBackpackSize() > 0) {
            cout << "输入要出售的物品编号: ";
            int index;
            cin >> index;
            player.sellItem(index);
        }
    }

    void shopMenu() {
        int choice;
        do {
            player.showShop();
            cout << "请选择要购买的物品: ";
            cin >> choice;
            player.buyItem(choice);
        } while (choice != 5);
    }

    void manageContainer() {
        cout << "\n=== 加密箱管理 ===\n";
        player.showBackpack();
        cout << "1. 从背包转移物品到加密箱\n";
        cout << "2. 返回\n";
        cout << "选择操作: ";
        int choice;
        cin >> choice;

        if (choice == 1) {
            cout << "输入要转移的物品编号: ";
            int index;
            cin >> index;
            cout << "物品转移功能正在开发中...\n";
        }
    }
};

int main() {
    cout << "请输入你的角色名称: ";
    string playerName;
    getline(cin, playerName);

    MetroRoyaleGame game(playerName);
    game.start();

    return 0;
}
原文