Javascript 初学者:如何修复“刽子手游戏”中的这个特定错误?

Beginner at Javascript: How can I fix this specific bug in the "Hangman Game"?

提问人:StarShining 提问时间:5/21/2023 更新时间:5/21/2023 访问量:25

问:

早上好,朋友们,

我是 JavaScript 的新手,并且一直在使用许多平台学习它,包括“儿童 JavaScript”。在这本书中,我现在在“第 7 章:创建刽子手游戏”......在“编程挑战”中,我能够完成前三个挑战。我完全坚持的第四个是“修复错误”。它指出“游戏中存在一个错误”,并且“如果你一直猜同一个正确的字母,”剩余字母“将不断减少。你能修复它吗?它说您可以添加另一个条件来检查 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\>
javascript html 循环 调试 条件语句

评论


答:

1赞 trincot 5/21/2023 #1

让我们来看看他们给出的提示:

如果你一直猜同一个正确的字母,“remainingLetters”会不断减少

是的,这是真的。当与之前的猜测相同时,则以下条件仍将为真,并且其代码块将执行:guessif

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

我们不希望重复的猜测对倒计时产生任何影响。它应该保持不变。remainingLetters

...您可以添加另一个条件来检查 中的值是否仍为下划线。answerArray

这是一个很好的提示,因为在第一次做出这个猜测时,我们用以下划线替换了下划线:guess

            answerArray[j] = guess;

所以。。。如果再次进行相同的猜测,则字符 AT 将不再是下划线,而是猜测的字母。因此,在我们决定进入该块之前,最好先检查角色的位置。answerArray[j]answerArray[j]if

这是否有助于找出您应该做什么?

如果没有,这里是作为剧透的解决方案

if (word[j] == guess && answerArray[j] == "_") {

0赞 Shawn2208 5/21/2023 #2
// 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);
}

评论

0赞 Shawn2208 5/21/2023
为您重写了代码,现在应该可以正常工作了。我也是相当新的,所以我知道当你遇到问题时会很痛苦。此外,while 循环不正确。它应该是 remainingLetters > 0,而不是 remainingLetters >= 0。我添加了一个新变量“found”。