JavaScript递归生成Stasis Device制作路径输出缺失步骤问题求解
问题原因
你遇到的步骤缺失是两处拼写不一致导致的递归匹配失败:
- 第一处:
Quantum Processor配方中依赖的是Superconductor(无空格),但你的蓝图db中定义的对应键名是Super Conductor(带空格),名称不匹配导致程序无法识别它是可拆解的蓝图,直接跳过了它和它所有下级依赖的输出 - 第二处:
Semiconductor和Cryo-Pump配方中依赖的是Thermic Condensate(前缀带h),但你的蓝图db中定义的对应键名是Termic Condensate(前缀无h),同样匹配失败导致相关步骤缺失
修复方案
只需要把蓝图db中的两处错误键名修正为和配方中一致即可:
<pre id="log"></pre> <script> const db = Object.create(null); db.Valuables = JSON.parse(`{ "Aronium": ["Paraffinium","Ionised Cobalt"], "Circuit Board": ["Heat Capacitor","Poly Fibre"], "Cryo-Pump": ["Hot Ice","Thermic Condensate"], "Cryogenic Chamber": ["Living Glass","Cryo-Pump"], "Enriched Carbon": ["Radon","Condensed Carbon"], "Glass": ["Frost Crystal"], "Grantine": ["Dioxite","Ionised Cobalt"], "Heat Capacitor": ["Frost Crystal","Solanium"], "Hot Ice": ["Enriched Carbon","Nitrogen Salt"], "Iridesite": ["Aronium","Mango-Gold","Grantine"], "Living Glass": ["Lubricant","Glass"], "Lubricant": ["Faecium","Gamma Root"], "Mango-Gold": ["Phosphorus","Ionised Cobalt"], "Nitrogen Salt": ["Nitrogen","Condensed Carbon"], "Poly Fibre": ["Cactus Flesh","Star Bulb"], "Quantum Processor": ["Circuit Board","Superconductor"], "Semiconductor": ["Thermic Condensate","Nitrogen Salt"], "Stasis Device": ["Quantum Processor","Cryogenic Chamber","Iridesite"], "Superconductor": ["Semiconductor","Enriched Carbon"], "Thermic Condensate": ["Sulphurine","Condensed Carbon"] }`); function analyse(recipe){ let blueprint = db.Valuables[recipe]; for(let i = 0; i < blueprint.length; i++){ if(blueprint[i] in db.Valuables){ analyse(blueprint[i]); } } log.innerHTML += JSON.stringify([recipe,blueprint])+"<br>"; } log = document.getElementById('log'); analyse("Stasis Device"); </script>
补充说明
如果后续你不需要重复输出相同的蓝图制作步骤(比如不需要多次打印Thermic Condensate的配方),可以额外加一个缓存Set记录已经输出过的蓝图,输出前判断是否存在即可,不需要的话保持现有递归逻辑即可完全匹配你的预期输出。
内容的提问来源于stack exchange,提问作者shirha
相关产品推荐
相关产品推荐

