试图完成JS for Kids挑战#3的Hangman游戏。它要求限制玩家的猜测数目。
我当时以为我需要将所有的猜测转储到一个新的数组中,然后在新数组的长度大于猜测的数量但无法正常工作时退出游戏。本书中的提示是使用&&,但并没有真正看到在其他条件下会出现什么情况。
感谢我对哪里出错的任何想法-
//create an array of words
var words =["java","monkey","amazing","pankcake"];
//pick a random words
var word= words[Math.floor(Math.random() * words.length)];
//set up the answer array
var answerArray=[];
for(var i=0; i< word.length; i++){
answerArray[i]="_";
}
var remainingLetters=word.length;
//The game loop
while(remainingLetters >= 0) {
//show the player their progress
alert(answerArray.join(" "));
//Get a guess from the player
var guess=prompt("guess a letter, or click cancel to stop playing");
**//add guesses to an answerArray
var guessAll="";
guessAll+=guess;**
//convert toLowerCase
var guessLower=guess.toLowerCase();
if(guessLower===null){
break;
**} else if(guessAll.length >4) {
break;**
} else if(guessLower.length !== 1){
alert("Please pick single character");
}else{
//update the game state with the guess
for(var j=0; j<word.length; j++){
if(word[j] ===guessLower){
answerArray[j]= guessLower;
remainingLetters--;
}
}
}
}
//游戏循环结束
alert(answerArray.join(“”));
警报(“你是一个好猜测者!!答案是“ +单词”);
最佳答案
好了,您的代码中有几点要指出,看看是否遵循。检查以下代码段:
//The game loop
while(remainingLetters >= 0) {
//show the player their progress, Get a guess from the player, etc.
//add guesses to an answerArray
var guessAll="";
guessAll+=guess;
// all other stuff
}
注意
var guessAll="";
在循环中如何?这意味着每次交互(每次循环时),您都将guessAll
重新定义为空白,因此消除了您本来应该跟踪的猜测。现在,如果将此声明移出循环,如下所示:// This is your answers array
var guessAll="";
//The game loop
while(remainingLetters >= 0) {
//show the player their progress, Get a guess from the player, etc.
//add guesses to an answerArray
guessAll+=guess;
// all other stuff
}
现在,变量
guessAll
仅初始化一次。在每个循环中,都会在循环之间保留一个新的猜测值,因为该值不再删除。我相信使用
&&
的提示(这意味着AND)将应用于while
检查:while(remainingLetters >= 0 && guessAll.length < 4) {
...
}
这样,它既检查未猜测的回信,又检查玩家尝试的猜测量。两种情况都必须成立:未发现的剩余字母数必须大于或等于零,并且玩家必须尝试少于四次。
这也具有使此检查
else if(guessAll.length > 4) break;
不必要的效果,因为在达到限制时循环将终止。但是,由于您有更长的单词(“令人惊奇”,“猴子”),因此玩家将仅用4次尝试就无法完成单词,因此应提高此限制。很好的做法是将其转换为参数,如下所示:
var maximumTries = 10;
(...)
while (remainingLetters >= 0 && guessAll.length < maximumTries) {
...
它提高了代码的可读性,并且易于维护。假设您后来发现10次尝试太难了,而您想使其达到15次:您只需要在代码的第一行中更改值,而无需深入编程逻辑并精确分析应该在哪里进行在循环中更改它,测试结果,等等。
因此,我建议的代码修复方法是:
// set the maximum number of tries
var maximumTries = 10;
// create an array of words
var words =["java","monkey","amazing","pancake"];
// pick a random word
var word = words[Math.floor(Math.random() * words.length)];
// set up the answer array
var answerArray=[];
for(var i=0; i< word.length; i++){
answerArray[i]="_";
}
var remainingLetters=word.length;
// this will hold all the letters tried
var guessAll="";
// The game loop
while(remainingLetters >= 0 && guessAll.length < maximumTries) {
// show the player their progress
alert(answerArray.join(" "));
// Get a guess from the player
var guess = prompt("guess a letter, or click cancel to stop playing.");
// add guesses to an answerArray
guessAll += guess;
//convert toLowerCase
var guessLower = guess.toLowerCase();
if (guessLower === null) {
break;
} else if(guessLower.length !== 1){
alert("Please pick single character");
} else {
//update the game state with the guess
for(var j=0; j<word.length; j++){
if(word[j] === guessLower){
answerArray[j] = guessLower;
remainingLetters--;
}
}
}
}
您可能会在JS Fiddle看到它的运行。
请记住,在编程中几乎总是有不止一种方法来达到相同的结果,这只是其中一种。
关于javascript - 子手游戏试图限制猜测的数量-JavaScript,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40567934/