Beginner at Javascript: 如何修复“Hangman Game”中的特定错误?

huangapple go评论71阅读模式
英文:

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

问题

以下是翻译好的部分:

早上好,朋友们,

我是全新接触JavaScript的,一直在多个平台上学习,包括"JavaScript For Kids"。在这本书中,我现在正在阅读第7章:"创建一个猜词游戏"……在"编程挑战"部分,我已经完成了前三个挑战。我完全卡住的第四个挑战是"修复一个错误"。它说游戏中有一个"错误",如果你一直猜相同的正确字母,"remainingLetters"将继续减少。你能修复吗?它说你可以添加另一个条件来检查answerArray中的值是否仍然是下划线。如果不是下划线,那么这个字母必定已经被猜过了。我甚至不知道从哪里开始...

请参考下面的代码。我不确定接下来该怎么做。我尝试在Google上搜索,但仍然无法理解。

<script>

//设置最大尝试次数
var maximumTries = 10;

//创建一个单词数组
var words = [
    "quail",
    "chicken",
    "kookaburra",
    "parrot"
];

//随机选择一个单词
var word = words[Math.floor(Math.random() * words.length)];

//设置答案数组
var answerArray = [];
for (var i = 0; i < word.length; i++) {
    answerArray[i] = "_";
}
var remainingLetters = word.length;

//这将保存所有已尝试的字母
var guessAll = "";

//游戏循环
while (remainingLetters >= 0 && guessAll.length < maximumTries) {
    //向玩家显示进度
    alert(answerArray.join(" "));

    //从玩家那里获取一个猜测
    var guess = prompt("猜一个字母,或点击取消停止游戏。");
    guessAll += guess;
    guess = guess.toLowerCase();
    if (guess === null) {
        //退出游戏循环
        break;
    } else if (guess.length !== 1) {
        alert("请只输入一个字母。");
    } else {
        for (var j = 0; j < word.length; j++) {
            if (word[j] === guess) {
                answerArray[j] = guess;
                remainingLetters--;
            }
        }
    }
    //游戏循环的结束

}
//显示答案并祝贺玩家
alert(answerArray.join(" "));
alert("干得好!答案是:" + word);

</script>

希望这有所帮助!如果您需要进一步的指导,请随时提问。

英文:

Good Morning Friends,

I am brand new to JavaScript and have been studying it using many platforms, including the "JavaScript For Kids". In this book, I am now on "Chapter 7: Creating a Hangman Game"...and on the "Programming Challenges", I was able to complete the first three challenges. The fourth one that I'm totally stuck on is "Fixing a Bug". It states that there is a "bug in the game" and that "if you keep guessing the same correct letter, "remainingLetters" will keep decrementing. Can you fix it?". It says that you can add another condition to check whether a value in answerArray is still an underscore. If it's not an underscore, then that letter must have been guessed already. I'm not sure where to even start with this...

Please see code below. I'm not sure where to go from here. I tried googling it, but still, I'm not getting it.

\&lt;script\&gt;
//Set a maximum number of tries
var maximumTries = 10; 
// Create an array of words
var words = [
&quot;quail&quot;,
&quot;chicken&quot;,
&quot;kookaburra&quot;,
&quot;parrot&quot;
];
// 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 &lt; word.length; i++) {
answerArray[i] = &quot;_&quot;;
}
var remainingLetters = word.length;
// This will hold all the letters tried
var guessAll = &quot;&quot;;
// The game loop
while (remainingLetters &gt;= 0 &amp;&amp; guessAll.length &lt; maximumTries) {
// Show the player their progress
alert(answerArray.join(&quot; &quot;));
// Get a guess from the player
var guess = prompt(&quot;Guess a letter, or click Cancel to stop playing.&quot;);
guessAll += guess;
guess = guess.toLowerCase();
if (guess === null) {
// Exit the game loop
break;
} else if (guess.length !== 1) {
alert(&quot;Please enter a single letter.&quot;);
} else {
for (var j = 0; j &lt; 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(&quot; &quot;));
alert(&quot;Good job!  The answer was &quot; + word);
\&lt;/script\&gt;

答案1

得分: 1

以下是翻译好的部分:

让我们看一下他们给出的提示:

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

是的,这是正确的。当 guess 与先前的猜测相同时,以下的 if 条件仍将为真,并且其代码块将执行:

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

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

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

这是一个好的提示,因为第一次猜测时,我们已经用 guess 替换了下划线:

            answerArray[j] = guess;

所以...如果再次猜测相同的字母,answerArray[j] 处的字符将不再是下划线,而是已猜测的字母。因此,在决定进入该 if 块之前,首先检查 answerArray[j] 处的字符是什么是一个好主意。

这有助于找出你应该做什么吗?

如果不行,以下是作为剧透的解决方案:

>! <code>if (word[j] == guess && answerArray[j] == "_") {</code>

英文:

Let's go through the hints they gave:

> if you keep guessing the same correct letter, "remainingLetters" will keep decrementing

Yes, that is true. When guess is the same as a previous guess, then still the following if condition will be true and its code block will execute:

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

We don't want a repeated guess to have any effect on the remainingLetters countdown. It should remain unchanged.

> ...you can add another condition to check whether a value in answerArray is still an underscore.

That is a good hint, because the first time that guess was made, we had replaced an underscore with guess:

            answerArray[j] = guess;

So... if the same guess is made again, the character at answerArray[j] will no longer be an underscore, but the guessed letter. Therefore it would be a good idea to first check what the character is at answerArray[j] before we decide to enter that if block.

Does this help to find out what you should do?

If not, here is the solution as a spoiler

>! <code>if (word[j] == guess && answerArray[j] == "_") {</code>

答案2

得分: 0

// 设置最大尝试次数
var maximumTries = 10;

// 创建一个单词数组
var words = [
    "quail",
    "chicken",
    "kookaburra",
    "parrot"
];

// 随机选择一个单词
var word = words[Math.floor(Math.random() * words.length)];

// 设置答案数组
var answerArray = [];
for (var i = 0; i < word.length; i++) {
    answerArray[i] = "_";
}
var remainingLetters = word.length;

// 这将保存所有尝试过的字母
var guessAll = "";

// 游戏循环
while (remainingLetters > 0 && guessAll.length < maximumTries) {
    // 显示玩家的进展
    alert(answerArray.join(" "));

    // 从玩家那里获取一个猜测
    var guess = prompt("Guess a letter, or click Cancel to stop playing.");
    if (guess === null) {
        // 退出游戏循环
        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!");
            }
        }
    }
}

// 显示答案并祝贺玩家
alert(answerArray.join(" "));
if (remainingLetters === 0) {
    alert("Congratulations! You guessed the word: " + word);
} else {
    alert("Game over! The word was: " + word);
}
英文:
// Set a maximum number of tries
var maximumTries = 10;
// Create an array of words
var words = [
&quot;quail&quot;,
&quot;chicken&quot;,
&quot;kookaburra&quot;,
&quot;parrot&quot;
];
// 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 &lt; word.length; i++) {
answerArray[i] = &quot;_&quot;;
}
var remainingLetters = word.length;
// This will hold all the letters tried
var guessAll = &quot;&quot;;
// The game loop
while (remainingLetters &gt; 0 &amp;&amp; guessAll.length &lt; maximumTries) {
// Show the player their progress
alert(answerArray.join(&quot; &quot;));
// Get a guess from the player
var guess = prompt(&quot;Guess a letter, or click Cancel to stop playing.&quot;);
if (guess === null) {
// Exit the game loop
break;
} else if (guess.length !== 1 || !/[a-z]/i.test(guess)) {
alert(&quot;Please enter a single alphabetic character.&quot;);
} else {
guess = guess.toLowerCase();
if (guessAll.includes(guess)) {
alert(&quot;You have already guessed that letter.&quot;);
} else {
guessAll += guess;
var found = false;
for (var j = 0; j &lt; word.length; j++) {
if (word[j] === guess) {
answerArray[j] = guess;
remainingLetters--;
found = true;
}
}
if (!found) {
alert(&quot;Wrong guess!&quot;);
}
}
}
}
// Show the answer and congratulate the player
alert(answerArray.join(&quot; &quot;));
if (remainingLetters === 0) {
alert(&quot;Congratulations! You guessed the word: &quot; + word);
} else {
alert(&quot;Game over! The word was: &quot; + word);
}

huangapple
  • 本文由 发表于 2023年5月21日 00:34:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76296256.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定