简体   繁体   English

有人可以帮我解决Hangman猜字母问题吗?

[英]Can someone help me with my Hangman guessing letter issue?

I'm currently working on a project for my coding class, and I'm having issues with my code. 我目前正在为我的编码课开发一个项目,但是我的代码有问题。 My code is adding correct letters to the wrongLetters array and I'm unsure why. 我的代码正在将错误的字母添加到正确的字母中,我不确定为什么。 It also only does this for the second correct letter and onward being inputted, meaning that if I guessed one correct letter correctly it would add it to the correctLetters array, however every correct letter after that would result in it being added both to correctLetters and wrongLetters when it should only be added to correctLetters. 它还只对第二个正确的字母及以后输入的字母执行此操作,这意味着,如果我猜对了一个正确的字母,它将把它添加到正确的字母数组中,但是此后的每个正确的字母都将导致它同时被添加到正确的字母和错误的字母中仅应将其添加到正确的字母中。 If you can help please do, I'm very confused. 如果可以的话请帮忙,我很困惑。 Thank you. 谢谢。

This link will bring to my current code on CodePen. 该链接将带我到CodePen上的当前代码。 With the HTML and CSS. 带有HTML和CSS。 You will find the issue in the JS portion of my code thank you, which is below as well. 您也可以在下面的我代码的JS部分中找到问题。

var guessWords = ["school", "test", "quiz", "pencil", "ruler", "protractor", "teacher", "homework", "science", "math", "english", "history", "language", "elective", "bully", "grades", "recess", ]
var secretWord = guessWords[Math.floor(Math.random()*guessWords.length)];
var wrongLetters = []
var correctLetters = []
var repeatLetters = []
function startGame() {
  var testWord = document.getElementById("randTest").innerHTML = secretWord; 
  var correctLettersOUT = "";
    document.getElementById("currentGuess").innerHTML = secretBlanks(secretWord)
    function secretBlanks(secretWord) {
      for (var i = 0; i < secretWord.length; i++) {
        correctLettersOUT+=("_ ");
        } return correctLettersOUT;

}}
function correctWord() {
  var guessLetter = document.getElementById("guessLetter").value;
  document.getElementById("letter").innerHTML = guessLetter;

 for (i=0; i < secretWord.length; i++) {
     if (correctLetters.indexOf(guessLetter) === -1)

        if (guessLetter === secretWord[i]) {
          correctLetters.push(guessLetter);
        } else if(wrongLetters.indexOf(guessLetter) === -1) {
          wrongLetters.push(guessLetter);
        }
 }

  console.log(correctLetters);
  console.log(wrongLetters);
  console.log(repeatLetters);
}

See conditions in your for loop - in first iteration, if letter is different from first letter of secret word and is not present in wrongLetters then you add it to this array. 查看for循环中的条件-在第一个迭代中,如果字母与秘密单词的第一个字母不同并且在wrongLetters字母中不存在, wrongLetters其添加到此数组中。
First check whether secret word contains letter and then decide which array should contain this letter. 首先检查秘密单词是否包含字母,然后确定哪个数组应包含该字母。 (BTW. why checking it in loop when you can use indexOf or in ES6 includes function?) (顺便说一句。当可以使用indexOf或在ES6中includes函数时,为什么要在循环中检查它?)

var guessWords = ["school", "test", "quiz", "pencil", "ruler", "protractor", "teacher", "homework", "science", "math", "english", "history", "language", "elective", "bully", "grades", "recess", ]
var secretWord = guessWords[Math.floor(Math.random()*guessWords.length)];
var wrongLetters = []
var correctLetters = []
var repeatLetters = []
function startGame() {
  var testWord = document.getElementById("randTest").innerHTML = secretWord; 
  var correctLettersOUT = "";
    document.getElementById("currentGuess").innerHTML = secretBlanks(secretWord)
    function secretBlanks(secretWord) {
      for (var i = 0; i < secretWord.length; i++) {
        correctLettersOUT+=("_ ");
        } return correctLettersOUT;

}}
function correctWord() {
  var guessLetter = document.getElementById("guessLetter").value;
  document.getElementById("letter").innerHTML = guessLetter;

 for (var i = 0; i < secretWord.length; i++) {
     if (correctLetters.indexOf(guessLetter) === -1)

        if (guessLetter === secretWord[i]) {
          correctLetters.push(guessLetter);
          break;
        }
 }
    if(wrongLetters.indexOf(guessLetter) === -1 && correctLetters.indexOf(guessLetter) === -1) {
         wrongLetters.push(guessLetter);
    }

  console.log(correctLetters);
  console.log(wrongLetters);
  console.log(repeatLetters);
}

The problem was that the code that added letters to the wrongLetters array was inside the for loop that was checking for correct letters, so if the correct letter was lets say letter 2 the loop would check against letter 1 and then add it to the wrongLetters array because it was not the same letter. 问题在于,将字母添加到wrongLetters数组的代码在用于检查正确字母的for循环中,因此,如果让正确的字母为字母2,则该循环将针对字母1进行检查,然后将其添加到错误的wrongLetters数组中因为不是同一封信。

I moved the check to outside of the loop and now letters are added to the correct array. 我将支票移到了循环之外,现在将字母添加到正确的数组中。 Right now if you guess the same letter more then once it will be added to the correctLetters array multiple times, 现在,如果您再次猜测同一字母,那么一旦将其多次添加到正确的字母数组中,

if you don't want that behavior just change the first if check from 如果您不希望这种行为,只需从

if (guessLetter === secretWord[i])

to

if (guessLetter === secretWord[i] && correctLetters.indexOf(guessLetter) === -1)

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

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