简体   繁体   English

Javascript hangman:用正确的字母替换空格有问题

[英]Javascript hangman: trouble with replacing blanks with correct letters

So I'm having trouble with a specific part of this game. 所以我在这个游戏的特定部分遇到了麻烦。 These are my initial variables: 这些是我的初始变量:

var words = ["sugar","banana","hulk"];
var guessLeft = 12;
var wrongGuess = [];
var hiddenWord = [];
var hiddenWordText = document.getElementById("hiddenword-text");
var chosenWord = words[Math.floor(Math.random() * words.length)];

So far it's correctly picking a random word from my array, and replacing them with underlines with the same length as the chosen word. 到目前为止,它正确地从我的数组中选取一个随机单词,并用与所选单词长度相同的下划线替换它们。 Here's how I did that: 这是我如何做到的:

for (i = 0; i < chosenWord.length; i++) {
    hiddenWord.push("_");
    hiddenWordText.textContent = hiddenWord.join(" ");
}

Now, where I'm having trouble is with when it figures out if my guess matched the letter, how to replace the correct underline index with the letter guessed. 现在,我遇到麻烦的地方是,当它确定我的猜测是否与字母匹配时,如何用猜到的字母替换正确的下划线索引。

This is what I have so far and I'm not sure if I'm on the right track. 这是我到目前为止所做的,我不确定我是否走在正确的轨道上。 Does anyone have any tips or can you guide me in the right direction? 有没有人有任何提示或者你能指导我朝正确的方向发展吗?

document.onkeyup = function(event) {
    var userGuess = event.key;

    if (chosenWord.indexOf(userGuess) >= 0){
        for (var i = 0; i < wordLength; i++){
            if (chosenWord[i] == userGuess){
                hiddenWord[i] == userGuess;

                hiddenWordText.textContent = hiddenWord[i] + hiddenWord;
            }
        }
    }
    else {
        guessLeft = guessLeft - 1;
    }
}

You have several mistakes in your code 你的代码中有几个错误

Use = instead of == for variable assignment 使用=而不是==进行变量赋值

Replace textContext of hiddenWordText with hiddenWord.join(" ") after the loop, in which the correctly guessed word has been revealed. 在循环之后用hiddenWord.join(" ")替换hiddenWordText textContext ,其中已经显示了正确猜测的单词。 Previously you keep replacing the textContext at every loop iteration, which will ignore the match found in the previous iterations 以前,您在每次循环迭代时都会继续替换textContext ,这将忽略先前迭代中找到的匹配

if (chosenWord.indexOf(userGuess) >= 0){
    for (var i = 0; i < chosenWord.length; i++){
        if (chosenWord[i] == userGuess){
            hiddenWord[i] = userGuess;
        }
    }
    hiddenWordText.textContent = hiddenWord.join(" ");
}

Rather than trying to figure out which underscores in the hiddenWord need to be replaced with the true letter, you might instead have an array of letters that have been picked so far, put them in a negative character set in a regular expression, and from the true input word, replace all characters not in that set with an underscore. 而不是试图找出hiddenWord哪些下划线需要用真正的字母替换,你可能会改为到目前为止已经选择了一个字母数组,将它们放在正则表达式中的负字符集中,并且从true输入字,用下划线替换不在该集合中的所有字符。 For example: 例如:

 const chosenWord = 'banana'; const guesses = []; // when a user guesses a letter, push it to guesses: // example, user guesses "a" and "b": guesses.push('a'); guesses.push('b'); // then, when you need to display the partially-filled-in word: const pattern = new RegExp( '[^' + guesses.join('') + ']', 'g' ); // in this case, the pattern will look like: // [^ab] // which means, match any characters that are not "a" nor "b" // Replace all characters that are neither "a" nor "b" with an underscore: const wordToDisplay = chosenWord.replace(pattern, '_'); console.log(wordToDisplay); // hiddenWordText.textContent = wordToDisplay; 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM