全部评论 1

  • 这是c++版的:

    #include <iostream>
    #include <string>
    #include <vector>
    #include <map>
    #include <random>
    #include <ctime>
    #include <algorithm>
    #include <chrono>
    #include <thread>
    
    using namespace std;
    
    // 随机数生成器
    random_device rd;
    mt19937 gen(rd());
    
    // Player类定义
    class Player {
    private:
        string name;
        int health;
        int maxHealth;
        int magic;
        int maxMagic;
        vector<string> inventory;
        int gold;
        int level;
        int xp;
        int xpToLevel;
    
    public:
        Player(string playerName) : name(playerName) {
            health = 100;
            maxHealth = 100;
            magic = 100;
            maxMagic = 100;
            gold = 50;
            level = 1;
            xp = 0;
            xpToLevel = 100;
        }
    
        // 显示玩家状态
        void displayStats() {
            cout << "\n=== " << name << "'s 状态 ===" << endl;
            cout << "生命值: " << health << "/" << maxHealth << endl;
            cout << "魔法值: " << magic << "/" << maxMagic << endl;
            cout << "金币: " << gold << endl;
            cout << "等级: " << level << " (" << xp << "/" << xpToLevel << " XP)" << endl;
            cout << "物品: ";
            if (inventory.empty()) {
                cout << "无" << endl;
            } else {
                for (size_t i = 0; i < inventory.size(); i++) {
                    cout << inventory[i];
                    if (i < inventory.size() - 1) cout << ", ";
                }
                cout << endl;
            }
            cout << "====================\n" << endl;
        }
    
        // 获得经验值
        void gainXp(int amount) {
            xp += amount;
            cout << "获得 " << amount << " XP!" << endl;
            if (xp >= xpToLevel) {
                levelUp();
            }
        }
    
        // 升级
        void levelUp() {
            level++;
            xp -= xpToLevel;
            xpToLevel = int(xpToLevel * 1.5);
            health = maxHealth;
            magic = maxMagic;
            cout << "\n恭喜! 你升到了 " << level << " 级!" << endl;
            cout << "生命值和魔法值已恢复满。" << endl;
        }
    
    

    11小时前 来自 广东

    0
    • userId_undefined

      3.14

      回复3.14
      // 受到伤害
          void takeDamage(int amount) {
              health -= amount;
              if (health < 0) {
                  health = 0;
              }
              cout << "你受到了 " << amount << " 点伤害!" << endl;
              if (health == 0) {
                  cout << "你已经死亡...游戏结束!" << endl;
                  exit(0);
              }
          }
      
          // 使用魔法
          bool useMagic(int amount) {
              if (magic >= amount) {
                  magic -= amount;
                  return true;
              } else {
                  cout << "魔法值不足!" << endl;
                  return false;
              }
          }
      
          // 添加物品
          void addItem(string item) {
              inventory.push_back(item);
              cout << "获得物品: " << item << "!" << endl;
          }
      
          // 移除物品
          bool removeItem(string item) {
              auto it = find(inventory.begin(), inventory.end(), item);
              if (it != inventory.end()) {
                  inventory.erase(it);
                  return true;
              } else {
                  cout << "你没有 " << item << "!" << endl;
                  return false;
              }
          }
      
          // Getter方法
          string getName() const { return name; }
          int getHealth() const { return health; }
          int getMagic() const { return magic; }
          int getGold() const { return gold; }
          const vector<string>& getInventory() const { return inventory; }
          bool hasItem(string item) const {
              return find(inventory.begin(), inventory.end(), item) != inventory.end();
          }
      };
      
      // 游戏类定义
      class Game {
      private:
          Player* player;
          map<string, map<string, any>> locations; // 地点数据
          map<string, map<string, any>> npcs;      // NPC数据
          map<string, map<string, any>> items;     // 物品数据
          string currentLocation;
          bool gameOver;
      
          // 工具函数 - 生成随机数
          int randomInt(int min, int max) {
              uniform_int_distribution<> dis(min, max);
              return dis(gen);
          }
      
          // 暂停函数,模拟Python的time.sleep
          void sleep(int seconds) {
              this_thread::sleep_for(chrono::seconds(seconds));
          }
      
      

      10小时前 来自 广东

      0
    • userId_undefined

      3.14

      回复3.14
      public:
          Game() : player(nullptr), gameOver(false) {}
      
          ~Game() {
              if (player) {
                  delete player;
              }
          }
      
          // 游戏介绍
          void intro() {
              cout << "欢迎来到《魔法森林的秘密》!" << endl;
              string name;
              cout << "请输入你的角色名字: ";
              cin >> name;
              player = new Player(name);
              cout << "\n" << player->getName() << ", 你是一位年轻的冒险者,听说在古老的魔法森林深处藏着一个惊人的秘密..." << endl;
              sleep(2);
              cout << "你决定进入这片神秘的森林,探索其中的奥秘..." << endl;
              sleep(2);
              setupGame();
              currentLocation = "入口";
              play();
          }
      
          // 设置游戏数据
          void setupGame() {
              // 设置地点
              locations = {
                  {"入口", {
                      {"description", "你站在魔法森林的入口。阳光透过树叶洒下斑驳的光影。一条小路通向森林深处。"},
                      {"exits", map<string, string>{{"北", "古老小径"}, {"东", "小溪边"}, {"南", "返回村庄"}}},
                      {"events", vector<string>{"encounter", "search"}}
                  }},
                  {"古老小径", {
                      {"description", "这是一条古老的小径,两旁树木参天。空气中弥漫着神秘的气息。"},
                      {"exits", map<string, string>{{"南", "入口"}, {"西", "神秘洞穴"}, {"北", "森林空地"}}},
                      {"events", vector<string>{"encounter", "puzzle"}}
                  }},
                  {"小溪边", {
                      {"description", "一条清澈的小溪从森林中流过。溪水清凉见底,溪边生长着各种奇异的植物。"},
                      {"exits", map<string, string>{{"西", "入口"}, {"北", "瀑布"}}},
                      {"events", vector<string>{"encounter", "search", "water"}}
                  }},
                  {"神秘洞穴", {
                      {"description", "一个黑暗的洞穴入口。洞口闪烁着微弱的蓝光,仿佛在吸引你进去。"},
                      {"exits", map<string, string>{{"东", "古老小径"}}},
                      {"events", vector<string>{"cave", "treasure"}}
                  }},
      
      

      10小时前 来自 广东

      0
    • userId_undefined

      3.14

      回复3.14
      {"森林空地", {
                      {"description", "一片开阔的空地,中央有一棵巨大的古树。树周围环绕着神秘的符文。"},
                      {"exits", map<string, string>{{"南", "古老小径"}, {"东", "迷雾山谷"}}},
                      {"events", vector<string>{"boss", "runes"}}
                  }},
                  {"瀑布", {
                      {"description", "一道壮观的瀑布从高处倾泻而下,水花四溅。瀑布后面似乎有一个洞口。"},
                      {"exits", map<string, string>{{"南", "小溪边"}}},
                      {"events", vector<string>{"waterfall", "hidden"}}
                  }},
                  {"迷雾山谷", {
                      {"description", "山谷中弥漫着浓密的雾气,视线严重受阻。你能听到奇怪的声音从雾中传来。"},
                      {"exits", map<string, string>{{"西", "森林空地"}}},
                      {"events", vector<string>{"encounter", "fog"}}
                  }}
              };
      
              // 设置NPCs
              npcs = {
                  {"老巫师", {
                      {"location", "入口"},
                      {"dialogue", vector<string>{
                          "欢迎,年轻的冒险者。这片森林充满了魔法和危险。",
                          "如果你想找到森林的秘密,你需要探索每一个角落。",
                          "小心森林中的生物,有些是友好的,有些则不是。"
                      }},
                      {"trade", vector<string>{"治疗药水", "魔法卷轴"}}
                  }},
                  {"小精灵", {
                      {"location", "小溪边"},
                      {"dialogue", vector<string>{
                          "你好呀,陌生人!我是森林的守护者之一。",
                          "你在寻找森林的秘密吗?也许我能帮你一点小忙...",
                          "如果你能回答我的谜题,我会给你一份礼物。"
                      }},
                      {"puzzle", map<string, any>{
                          {"question", "什么东西越洗越脏,不洗有人吃,洗了没人吃?"},
                          {"answer", "水"},
                          {"reward", "魔法钥匙"}
                      }}
                  }},
                  {"洞穴守卫", {
                      {"location", "神秘洞穴"},
                      {"dialogue", vector<string>{
                          "谁允许你进入这个神圣的地方?",
                          "如果你想通过,必须证明你的价值。",
                          "回答我的问题,或者用武力通过。"
                      }},
                      {"fight", map<string, any>{
                          {"name", "洞穴守卫"},
                          {"health", 50},
                          {"damage", 10},
                          {"xp", 50},
       
      

      10小时前 来自 广东

      0

热门讨论