如何让游戏等级解锁提示仅触发一次?Node.js代码求助
Node.js趣味问答游戏等级提示优化方案
问题分析
原代码中,当玩家分数停留在4、7、9区间时(比如答对到4分后答错后续题目),每次执行判断都会重复输出对应等级的解锁提示——原因是未记录等级提示是否已触发,仅通过当前分数区间判断是否输出。
优化方案
新增一个unlockedLevels对象,用于记录每个等级的提示是否已触发。仅当玩家分数首次达到对应等级且未触发过提示时,输出解锁信息并标记该等级为已触发。
优化后完整代码
var readlineSync = require('readline-sync'); var userName = readlineSync.question("May we know your name please? "); console.log("Hello " + userName + " to the neog.camp fun quiz game!!\n"); console.log("Please answer 4 questions correctly to reach Level 1 of the game,7 to reach Level 2 of the game, and 9 to reach Level 3 of the game.\nALL THE BEST :) \n\n"); var currentScore = 0; // 新增:记录已触发的等级提示 var unlockedLevels = { level1: false, level2: false, level3: false }; var highScores = { azhar: 10, bhargav: 7 }; function ask(question, answer) { var userAnswer = readlineSync.question(question); if (userAnswer === answer) { console.log("Correct!!"); currentScore++; console.log("score: ", currentScore); // 仅在分数提升时检查等级,避免重复判断 if(currentScore === 4 && !unlockedLevels.level1) { console.log("Congrats!! You entered LEVEL 1 of the game!!"); unlockedLevels.level1 = true; } if(currentScore === 7 && !unlockedLevels.level2) { console.log("Congrats!! You entered LEVEL 2 of the game!!"); unlockedLevels.level2 = true; } if(currentScore === 9 && !unlockedLevels.level3) { console.log("Congrats!! You entered LEVEL 3 of the game!! Yippeee ;) "); unlockedLevels.level3 = true; } } else { console.log("Wrong!!"); console.log("score: ", currentScore); } } var questions = [ { question: "What is the capital of INDIA? ", answer: "New Delhi" }, { question: "What is the full name of MS Dhoni? ", answer: "Mahendra Singh Dhoni" }, { question: "Who founded Amazon ?", answer: "Jeff Bezos" }, { question: "Which is the largest country in Asia? ", answer: "China" }, { question: "How many sides does a quadrilateral have? ", answer: "4" }, { question: "Which Indian Cricketer did hit six sixes in six balls against England in 2007? ", answer: "Yuvraj Singh" }, { question: "What is the full form of CS GO? ", answer: "Counter Strike Global Offensive" }, { question: "How many players are there in a football team excluding the goal keeper? ", answer: "10" }, { question: "Which language is called the mother of all programming languages?", answer: "C" }, { question: "What is the name of the highest mountain in the world? ", answer: "Mount Everest" } ]; for (var i = 0; i < questions.length; i++) { var currentQuestion = questions[i]; ask(currentQuestion.question, currentQuestion.answer); } console.log("\nYour final score is : ", currentScore); if (currentScore >= highScores.azhar) { console.log("Congratulations!! You are the new highest scorer!! \t Kindly send the screenshot of the score to us.\nThank You") } else { console.log("Oops!! You failed to beat the highest scorer!!\nBetter Luck Next Time") console.log("High Scores :- \n" + "Azhar : " + highScores.azhar + "\nBhargav : " + highScores.bhargav); }
关键修改说明
- 新增
unlockedLevels对象:存储每个等级的提示触发状态,初始值均为false - 调整等级判断逻辑:将等级检查移至答对题的分支中(仅分数增加时触发),同时判断对应等级是否未触发过,输出提示后立即标记为已触发
- 精准分数判断:将原区间判断改为精准匹配
currentScore === 4/7/9,确保仅在分数首次达到该值时触发提示
内容的提问来源于stack exchange,提问作者Azhar Choudhury
相关产品推荐
相关产品推荐

