我做了一个合成金属的游戏这个游戏里的合成表是随机的完全无规律请勿作为科学用途教程游戏里都有不要故意犯错应该是没有bug的#include iostream #include vector #include string #include map #include algorithm #include cstdlib #include ctime #include iomanip using namespace std; // 金属名称映射8种金属 const vectorstring METAL_NAMES {氢, 氦, 锂, 铍, 硼, 碳, 氮, 氧}; // 合成表结构体 struct Recipe { int result; vectorint components; string description; }; // 商人收购信息 struct MerchantOffer { int metal_type; int price; int demand; // 需求量 }; class MetalGame { private: int n; // 金属种类数固定为8 vectorint metals; // 当前拥有的各类金属数量 vectorRecipe recipes; // 所有合成配方 vectorMerchantOffer merchant_offers; // 商人收购信息 int target_metal; // 目标金属类型 int target_amount; // 目标数量 bool game_won; int gold_coins; // 金币数量 vectorbool reachable; // 记录哪些金属是可以达到的 public: MetalGame() : n(8), game_won(false), gold_coins(100) { // 初始100金币 initializeGame(); } void initializeGame() { srand(static_castunsigned int(time(nullptr))); // 初始化金属库存随机1-3个 metals.assign(n, 0); for (int i 0; i n; i) { metals[i] rand() % 3 1; } // 设定目标金属和数量 target_metal rand() % n; target_amount rand() % 16 10; // 10-25个目标 // 生成合成表 generateRecipes(); // 计算可达性 calculateReachable(); cout endl; cout 金属合成游戏 v2.0 endl; cout endl; cout 目标获得 target_amount 个 METAL_NAMES[target_metal] endl; cout 提示使用基础金属逐步合成高级金属 endl; cout 新增金币系统、费用控制、容量限制 endl; cout endl; } void generateRecipes() { // 创建确保能到达高级金属的路径 createGuaranteedPath(); // 添加额外合成表 int additional_recipes 5; for (int i 0; i additional_recipes; i) { int result rand() % (n - 2) 2; int component_count rand() % 2 2; // 2-3个成分 vectorint components; for (int j 0; j component_count; j) { int comp rand() % min(result 2, n - 1); components.push_back(comp); } Recipe recipe; recipe.result result; recipe.components components; recipe.description METAL_NAMES[result] - ; for (size_t k 0; k components.size(); k) { if (k 0) recipe.description ; recipe.description METAL_NAMES[components[k]]; } recipes.push_back(recipe); } } void createGuaranteedPath() { // 创建一条从基础金属到目标金属的路径 vectorint path_nodes; path_nodes.push_back(0); // 从最基础的开始 // 创建中间节点 int levels rand() % 3 2; // 2-4层 for (int level 0; level levels; level) { int node rand() % (target_metal - level - 1) level 1; if (node n - 1) { path_nodes.push_back(node); } } path_nodes.push_back(target_metal); // 为路径上的每一对节点创建合成表 for (size_t i 1; i path_nodes.size(); i) { int result path_nodes[i]; int component1 path_nodes[i-1]; int component2 rand() % min(result, 5); Recipe recipe; recipe.result result; recipe.components {component1, component2}; recipe.description METAL_NAMES[result] - METAL_NAMES[component1] METAL_NAMES[component2]; recipes.push_back(recipe); } } void calculateReachable() { reachable.assign(n, false); // 基础金属初始拥有0的都是可达的 for (int i 0; i n; i) { if (metals[i] 0) { reachable[i] true; } } // 不断更新可达集合直到稳定 bool updated true; while (updated) { updated false; for (const auto recipe : recipes) { if (reachable[recipe.result]) continue; // 已经可达 bool all_components_reachable true; for (int comp : recipe.components) { if (!reachable[comp]) { all_components_reachable false; break; } } if (all_components_reachable) { reachable[recipe.result] true; updated true; } } } } void displayInventory() { cout \n 当前库存 (金币: gold_coins ) endl; for (int i 0; i n; i) { cout setw(2) i1 . METAL_NAMES[i] : metals[i] 个; if (!reachable[i]) { cout (不可达); } cout endl; } cout endl; } void showRecipes() { cout \n 可用合成表 endl; for (size_t i 0; i recipes.size(); i) { cout [ (i 1) ] recipes[i].description; // 检查是否可以合成 if (canCraft(i)) { cout [可合成]; } else { cout [材料不足]; } cout endl; } cout endl; } bool canCraft(int recipe_index) { if (recipe_index 0 || recipe_index static_castint(recipes.size())) { return false; } const Recipe recipe recipes[recipe_index]; for (int component : recipe.components) { if (metals[component] 0) { return false; } } return true; } void craftItem(int recipe_index) { if (recipe_index 0 || recipe_index static_castint(recipes.size())) { cout 无效的选择 endl; return; } if (canCraft(recipe_index)) { const Recipe recipe recipes[recipe_index]; // 消耗材料 for (int component : recipe.components) { metals[component]--; } // 获得产品 metals[recipe.result]; cout 成功合成 METAL_NAMES[recipe.result] endl; // 更新可达性 calculateReachable(); // 检查胜利条件 checkWinCondition(); } else { cout 材料不足无法合成 endl; } } void refreshMarket() { merchant_offers.clear(); int offer_count rand() % 3 2; // 2-4个收购报价 for (int i 0; i offer_count; i) { MerchantOffer offer; offer.metal_type rand() % n; offer.price rand() % 15 5; // 5-20的价格 offer.demand rand() % 8 1; // 1-8的需求量 merchant_offers.push_back(offer); } cout \n 交易行刷新 endl; for (size_t i 0; i merchant_offers.size(); i) { const MerchantOffer offer merchant_offers[i]; cout [ (i1) ] 商人收购 METAL_NAMES[offer.metal_type] 单价: offer.price 金币需求量: offer.demand 个 endl; } cout endl; } void sellToMerchant(int offer_index) { if (offer_index 0 || offer_index static_castint(merchant_offers.size())) { cout 无效的选择 endl; return; } const MerchantOffer offer merchant_offers[offer_index]; int sell_amount min(metals[offer.metal_type], offer.demand); if (sell_amount 0) { cout 你没有这种金属可以出售 endl; return; } int earnings sell_amount * offer.price; metals[offer.metal_type] - sell_amount; gold_coins earnings; cout 出售了 sell_amount 个 METAL_NAMES[offer.metal_type] 获得 earnings 金币 endl; // 移除已使用的报价 merchant_offers.erase(merchant_offers.begin() offer_index); } void miningExpedition() { if (gold_coins 80) { cout 金币不足需要80金币才能进行探险。 endl; return; } cout \n 挖掘探险 endl; cout 花费80金币深入矿洞... endl; gold_coins - 80; // 限制单次获得的金属数量不超过10个 int total_found 0; for (int i 0; i 30; i) { int metal_type rand() % n; if (metals[metal_type] 10) { // 限制每种金属最多10个 metals[metal_type]; total_found; } } cout 挖掘完成获得了 total_found 个金属 endl; cout endl; // 检查胜利条件 checkWinCondition(); } void checkWinCondition() { if (metals[target_metal] target_amount) { cout \n?? 恭喜你完成了目标 ?? endl; cout 你成功获得了 target_amount 个 METAL_NAMES[target_metal] endl; cout 游戏结束谢谢游玩 endl; game_won true; } } void showHelp() { cout \n 游戏帮助 endl; cout I - 查看当前库存 endl; cout R - 显示所有合成表 endl; cout Cnumber - 使用指定编号的合成表进行合成 endl; cout M - 进入交易行刷新商人报价 endl; cout Snumber - 出售金属给商人 endl; cout D - 挖掘探险花费80金币获得30个随机金属 endl; cout T - 查看当前目标 endl; cout H - 显示此帮助信息 endl; cout Q - 退出游戏 endl; cout endl; } void showTarget() { cout \n?? 当前目标 endl; cout 获得 target_amount 个 METAL_NAMES[target_metal] endl; cout 当前已有: metals[target_metal] 个 endl; cout 还需: max(0, target_amount - metals[target_metal]) 个 endl; } void run() { string input; while (!game_won) { cout \n请输入命令 (H查看帮助): ; cin input; if (input.empty()) continue; char command input[0]; switch (command) { case I: case i: displayInventory(); break; case R: case r: showRecipes(); break; case C: case c: { if (input.length() 1) { try { int index stoi(input.substr(1)) - 1; craftItem(index); } catch (...) { cout 无效的合成表编号 endl; } } else { cout 请指定合成表编号例如C1 endl; } break; } case M: case m: refreshMarket(); break; case S: case s: { if (input.length() 1) { try { int index stoi(input.substr(1)) - 1; sellToMerchant(index); } catch (...) { cout 无效的商人编号 endl; } } else { cout 请指定商人编号例如S1 endl; } break; } case D: case d: miningExpedition(); break; case T: case t: showTarget(); break; case H: case h: showHelp(); break; case Q: case q: cout 感谢游玩再见 endl; return; default: cout 未知命令输入 H 查看帮助 endl; } } } }; int main() { MetalGame game; game.run(); return 0; }如果您认为好玩欢迎点赞收藏加关注如果大家有意见可在评论提出我们下期再见