JavaScript初学者:我如何修复“Hangman Game”中的此特定错误?

7hiiyaii  于 2023-05-21  发布在  Java
关注(0)|答案(2)|浏览(146)

早上好,朋友们,
我是JavaScript的新手,一直在使用许多平台学习它,包括“JavaScript For Kids”。在这本书中,我现在在“第七章:创建一个刽子手游戏”...而在“编程挑战”上,我能够完成前三个挑战。第四个我完全被卡住的是“修复Bug”。它指出,有一个“游戏中的错误”,“如果你一直猜测相同的正确字母,”remainingLetters“将不断递减。你能修好吗“。它说你可以添加另一个条件来检查answerArray中的值是否仍然是下划线。如果它不是下划线,那么这个字母一定已经被猜到了。我都不知道从何说起...
请看下面的代码。我不知道接下来该怎么办。我试着谷歌了一下,但还是没找到。

\<script\>

//Set a maximum number of tries

var maximumTries = 10; 

// Create an array of words
var words = [
    "quail",
    "chicken",
    "kookaburra",
    "parrot"
];

// 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.");
guessAll += guess;
guess = guess.toLowerCase();
if (guess === null) {
    // Exit the game loop
    break;
} else if (guess.length !== 1) {
    alert("Please enter a single letter.");
} else {
    for (var j = 0; j < word.length; j++) {
        if (word[j] === guess) {
            answerArray[j] = guess;
            remainingLetters--;
        }
    }
}
// The end of the game loop

}
//Show the answer and congratulate the player
alert(answerArray.join(" "));
alert("Good job!  The answer was " + word);

\</script\>
vpfxa7rd

vpfxa7rd1#

让我们来看看他们给出的提示:
如果你一直猜同一个正确的字母,“remainingLetters”会一直递减
是的,这是真的。当guess与之前的猜测相同时,则以下if条件仍然为真,并且其代码块将执行:

if (word[j] === guess) {
            answerArray[j] = guess;
            remainingLetters--;
        }

我们不希望重复的猜测对remainingLetters倒计时有任何影响。应该保持不变。
...您可以添加另一个条件来检查answerArray中的值是否仍然是下划线。
这是一个很好的提示,因为第一次猜测时,我们用guess替换了下划线:

answerArray[j] = guess;

所以...如果再次进行同样的猜测,answerArray[j]处的字符将不再是下划线,而是猜测的字母。因此,在我们决定进入if块之前,首先检查answerArray[j]处的字符是什么是一个好主意。
这是否有助于找出你应该做什么?
如果没有,这里是作为一个扰流器的解决方案
if (word[j] == guess && answerArray[j] == "_") {

iyzzxitl

iyzzxitl2#

// Set a maximum number of tries
var maximumTries = 10;

// Create an array of words
var words = [
    "quail",
    "chicken",
    "kookaburra",
    "parrot"
];

// 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.");
    if (guess === null) {
        // Exit the game loop
        break;
    } else if (guess.length !== 1 || !/[a-z]/i.test(guess)) {
        alert("Please enter a single alphabetic character.");
    } else {
        guess = guess.toLowerCase();
        if (guessAll.includes(guess)) {
            alert("You have already guessed that letter.");
        } else {
            guessAll += guess;
            var found = false;
            for (var j = 0; j < word.length; j++) {
                if (word[j] === guess) {
                    answerArray[j] = guess;
                    remainingLetters--;
                    found = true;
                }
            }
            if (!found) {
                alert("Wrong guess!");
            }
        }
    }
}

// Show the answer and congratulate the player
alert(answerArray.join(" "));
if (remainingLetters === 0) {
    alert("Congratulations! You guessed the word: " + word);
} else {
    alert("Game over! The word was: " + word);
}

相关问题