简体   繁体   English

Hangman Game Javascript:如果猜到单词中的最后一个字符,则单词完成

[英]Hangman Game Javascript: Word completes if last character in word is guessed

I am coding a hangman game for a class assignment.我正在为课堂作业编写一个刽子手游戏。 The wordcheck() function, which compares the index of the characters of the word to the index of the spaces, is not working as it should.wordcheck()的字符索引与空格索引进行比较的wordcheck()函数无法正常工作。 If the user guesses a letter in the middle of the word, the letter will fill the _ space just as it should.如果用户猜测单词中间的字母,则该字母将按其应有的方式填充 _ 空间。 However, if the user guesses the last letter of the word, it will automatically go to the win screen and reveal the full word even if the spaces before do not match.但是,如果用户猜到单词的最后一个字母,即使前面的空格不匹配,它也会自动进入获胜屏幕并显示完整的单词。 I am positive the problem is in this function.我很肯定问题出在这个函数中。 I believe it's because the loop runs for however long the characters list is (or how long the word is) and if the last value returns as equal to the last value in the spaces list, it will return as true even if the previous values return as unequal.我相信这是因为循环运行了字符列表的长度(或单词的长度),并且如果最后一个值返回等于空格列表中的最后一个值,即使先前的值返回,它也会返回为真作为不平等。 Still, even if this is the problem, I have no idea how to fix it.尽管如此,即使这是问题,我也不知道如何解决它。

Thank you!谢谢!

function wordCheck() {
    var wordComplete = false;
    for (var i = 0; i < letters.length; i++) {
        if (letters[i] !== blanks[i]) {
            wordComplete = false;
        }
        else {
            wordComplete = true;
        }
    }
    if (wordCompleted == true) {
        gameWon();
    }
}

The problem is, you need to set the value of true ONLY if the condition for true exists (and in this case, you only know that for a fact AFTER you have evaluated all the characters and spaces).问题是,只有当 true 的条件存在时才需要设置 true 的值(在这种情况下,您只知道在评估所有字符和空格后的事实)。 In your code, you set to either true or false EVERY time you evaluate a characters/space match (which means you can unset the result you are looking for).在您的代码中,您每次评估字符/空格匹配时都设置为 true 或 false(这意味着您可以取消设置您要查找的结果)。 This little logic trap gets coders all the time.这个小逻辑陷阱一直让编码员着迷。

Consider it this way: I get to ask your name five times, and I only have to get it right once to win, but I always have to guess five times (after which you will tell me if one of my guesses was correct).可以这样想:我可以问你的名字五次,我只需要猜对一次就能赢,但我总是要猜五次(之后你会告诉我我的猜测是否正确)。 If you evaluate my answer each time, and the last answer is wrong, it will never matter if I ever got it right.如果您每次都评估我的答案,而最后一个答案是错误的,那么我是否做对都无关紧要。 It will only work if the last guess is correct.只有最后的猜测是正确的,它才会起作用。 So you would either stop evaluating answers once I've guessed correctly, of you would wait for all five guesses and THEN determine if I win/lose.因此,一旦我猜对了,您要么停止评估答案,要么等待所有五个猜测,然后确定我是赢还是输。

In your case, you require that all "guesses" be made before determining win/lose.在您的情况下,您需要在确定输赢之前进行所有“猜测”。 So you just track them, and afterwards, make the win/lose decision.所以你只需跟踪他们,然后做出输赢的决定。

So you'd do something like this:所以你会做这样的事情:

function wordCheck(){
    // track the matches
    var matches = 0;
    for ( var i = 0; i < spaces.length; i++ ) { 
       // If there's a match, increment matches. Else, leave it alone.
       matches = ( characters [ i ] === spaces [ i ] ? matches + 1 : matches );
    }
    // Now that you have evaluated ALL matches, you can make the decision.
    // If matches === your spaces length, all matches were found, return true, else false
    return ( matches === spaces.length );
}
function wordCheck() {
    wordCompleted = true; //set it to true first,
    for (var i = 0; i < characters.length; i++) {
        if (characters[i] !== spaces[i]) {
            wordCompleted = false; //then, if a single char is false, set it to false
        }
    }
    if (wordCompleted == true) {
        gameWon();
    }
}

Your issue was that you were setting wordCompleted to true if a letter was right.您的问题是,如果字母正确,您将 wordCompleted 设置为 true。 This meant that if only the last letter was correct, it would still be set to true (there were no false letters after to set it to false)这意味着如果只有最后一个字母是正确的,它仍然会被设置为真(之后没有假字母将其设置为假)

Yes, indeed your function is changing the value of wordCompleted in each iteration.是的,您的函数确实在每次迭代中更改wordCompleted的值。 If the last char matches, then wordCompleted is true , and you call gameWon() .如果最后一个字符匹配,则wordCompletedtrue ,并且您调用gameWon() I think the expected behavior would be if just call gameWon() when the whole word is guessed, right?我认为预期的行为是在猜出整个单词时调用gameWon() ,对吗? I think this version of your function make what you want.我认为这个版本的功能可以满足您的需求。

function wordCheck(){
    let i = 0;
    while(i < characters.length && characters[i] === spaces[i]){
        i++; 
    }

    const wordCompleted = i === characters.length;
    if (wordCompleted == true) {
        gameWon();
    }
}

Now with this function, we are iterating over the characters and spaces arrays until their elements are different or the whole array was iterated.现在使用这个函数,我们迭代字符和空格数组,直到它们的元素不同或整个数组被迭代。 Then we just check if we reach the last element, if yes, the game is won.然后我们只检查是否到达最后一个元素,如果是,则游戏获胜。

If there is a correct letter after a bad letter your script says all your word is correct because you will set the boolean for each letters hence override the bad letter check.如果在一个坏字母之后有一个正确的字母,您的脚本会说您的所有单词都是正确的,因为您将为每个字母设置布尔值,从而覆盖坏字母检查。

So just define a boolean to true at the beginning and then set it to false if a letter if wrong but don't set it back to true if the letter is right.因此,只需在开始时将布尔值定义为true ,然后在字母错误时将其设置为 false,但如果字母正确,则不要将其设置回true At then end if your boolean is false , it means one of your letter is wrong.最后,如果您的 boolean 为false ,则意味着您的其中一封信是错误的。

function wordCheck(){
  var wordCompleted = true;
  for (var i = 0; i < characters.length; i++) {
    if (characters[i] !== spaces[i]) {
      wordCompleted = false;
    }
  }
  if (wordCompleted == true) {
    gameWon();
  }
}

You're partially correct that the problem is that you continue through the loop to the end - the thing is, the way you've set up the function means that only the last character matters.您部分正确,问题在于您继续循环到最后 - 问题是,您设置函数的方式意味着只有最后一个字符很重要。

What you really care about though, is how many of the characters in the entire word match.不过,您真正关心的是整个单词中有多少字符匹配。

Take a look at this function:看看这个函数:

function wordCheck(){
  var correctCharacters = 0;
  for (var i = 0; i < characters.length; i++) {
    if (characters[i] === spaces[i]) {
      correctCharacters ++;
    }
  }
  if (correctCharacters === characters.length) {
    gameWon();
  }
}

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

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