试图从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(“”)); 警告(“你是一个好的猜测者!答案是”+字);
答案 0 :(得分:2)
嗯,您的代码中有一些要指出的内容,看看您是否遵循。请查看以下代码段:
//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) {
...
}
这样,它会检查未编辑的remaningLetters和玩家尝试的猜测量。两个条件都必须为真:剩余未发现的字母数必须大于或等于零并且玩家必须尝试少于四次。
这也会使检查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看到它的实际效果。 请记住,在编程中,几乎总有不止一种方法可以实现相同的结果,这只是其中之一。