C++合成金属游戏
我做了一个合成金属的游戏这个游戏里的合成表是随机的完全无规律请勿作为科学用途教程游戏里都有不要故意犯错应该是没有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; }如果您认为好玩欢迎点赞收藏加关注如果大家有意见可在评论提出我们下期再见

相关新闻

正念笔记混乱想法3月9日

正念笔记混乱想法3月9日

今天听了一段正念引导语。QQ歌曲名:04附:慈心正念引导语 (播客),歌手名:老Q_0xfJ,专辑名:成长Q语。https://c6.y.qq.com/base/fcgi-bin/u?__pHwBCLFJDU2I这是我今天的第一次冥想,也是寒假开学后…

2026/7/3 11:01:00 阅读更多 →
基于微信小程序的剧本杀服务平台设计与实现

基于微信小程序的剧本杀服务平台设计与实现

一、项目介绍 基于微信小程序的剧本杀服务平台是一种创新的娱乐服务模式,充分利用微信小程序的便捷性和广泛的用户基础,为用户提供一站式的剧本杀游戏体验。该平台不仅整合了丰富的剧本资源,还提供了用户友好的界面设计、智能匹配系统、实时通…

2026/7/3 0:35:04 阅读更多 →
大数据面临什么挑战?大数据如何转化为洞察?

大数据面临什么挑战?大数据如何转化为洞察?

你点开这篇文章,很可能是因为你正在处理庞大而复杂的数据工作。在公司里,大数据被视为关键资产,但日常工作中,报表混乱、口径不一、系统性能不足以及来自管理层对直接结论的追问,才是真正的常态。数据就在那里&#xf…

2026/5/17 11:52:37 阅读更多 →

最新新闻

群智能算法优化随机森林参数实战指南

群智能算法优化随机森林参数实战指南

1. 项目概述:当随机森林遇上群智能 在机器学习实战中,随机森林(Random Forest)因其出色的鲁棒性和易用性成为算法工程师的"瑞士军刀"。但很多人不知道,默认参数下的随机森林可能只发挥了60%的潜力。去年我在电商用户流失预测项目中…

2026/7/4 15:08:23 阅读更多 →
AI论文写作工具全攻略:从文献检索到格式排版

AI论文写作工具全攻略:从文献检索到格式排版

1. 论文写作工具现状与需求分析 本科阶段的论文写作对大多数学生来说都是个不小的挑战。从选题开题到文献综述,从数据分析到格式排版,每个环节都可能成为拦路虎。传统的人工写作方式效率低下,特别是在文献检索和初稿撰写阶段,往往…

2026/7/4 15:06:23 阅读更多 →
Google OAuth 2.0 完整集成指南:从原理到实战,涵盖Web应用与SPA

Google OAuth 2.0 完整集成指南:从原理到实战,涵盖Web应用与SPA

1. 项目概述:为什么你需要一个完整的Google OAuth指南 如果你正在开发一个需要用户登录的Web应用、移动App,或者一个需要访问用户Google日历、Gmail或云端硬盘数据的服务,那么集成Google OAuth认证几乎是绕不开的一步。你可能已经看过官方文档…

2026/7/4 15:06:23 阅读更多 →
TransPaste:基于本地大模型的“复制即翻译”工具实战指南

TransPaste:基于本地大模型的“复制即翻译”工具实战指南

🚀 30款热门AI模型一站整合,DeepSeek/GLM/Claude 随心用,限时 5 折。 👉 点击领海量免费额度 在日常开发、阅读文档或处理多语言资料时,你是否也厌倦了在浏览器、翻译软件和编辑器之间反复切换?复制、粘…

2026/7/4 15:06:23 阅读更多 →
Si4731与PIC18F87J60打造可编程网络收音机系统

Si4731与PIC18F87J60打造可编程网络收音机系统

1. 项目背景与硬件选型解析这个DIY音频探索项目的核心在于将收音机芯片与微控制器结合,打造一个可编程的旋律捕捉系统。Si4731作为Silicon Labs推出的数字调谐收音机芯片,支持AM/FM/SW接收,而PIC18F87J60则是Microchip旗下集成以太网功能的8位…

2026/7/4 15:02:22 阅读更多 →
大模型量化技术评测与实战指南

大模型量化技术评测与实战指南

1. 大模型量化技术概述在深度学习领域,模型量化已经成为解决大语言模型(LLM)部署难题的关键技术。简单来说,量化就是通过降低模型参数的数值精度来减少存储和计算开销的过程。想象一下,当你需要搬运一堆书籍时,精装版虽然精美但占…

2026/7/4 15:00:21 阅读更多 →

日新闻

Memcached 1.6.43 发布:关键安全修复版本,多项问题得到解决

Memcached 1.6.43 发布:关键安全修复版本,多项问题得到解决

Memcached 1.6.43 正式发布,这是一个关键的安全修复版本,修复了多个方面的问题,还对部分功能进行了优化。 安全修复亮点 此次发布在安全修复上表现突出。binprot 避免了项目引用计数溢出,mcmc 因安全问题提升了上游版本号&#xf…

2026/7/4 0:04:29 阅读更多 →
终极指南:使用HMCL启动器跨平台畅玩Minecraft的完整解决方案

终极指南:使用HMCL启动器跨平台畅玩Minecraft的完整解决方案

终极指南:使用HMCL启动器跨平台畅玩Minecraft的完整解决方案 【免费下载链接】HMCL A Minecraft Launcher which is multi-functional, cross-platform and popular 项目地址: https://gitcode.com/gh_mirrors/hm/HMCL HMCL(Hello Minecraft! Lau…

2026/7/4 0:06:29 阅读更多 →
KMX63与PIC18F66K40在嵌入式HMI中的硬件协同与低功耗设计

KMX63与PIC18F66K40在嵌入式HMI中的硬件协同与低功耗设计

1. KMX63与PIC18F66K40的硬件协同架构解析KMX63作为一款三轴加速度计和磁力计组合传感器,与PIC18F66K40微控制器的搭配堪称嵌入式HMI开发的黄金组合。这套硬件组合的核心优势在于KMX63提供的高精度运动感知能力与PIC18F66K40强大的信号处理能力形成了完美互补。KMX6…

2026/7/4 0:06:29 阅读更多 →

周新闻

月新闻