首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >相同字母验证javascript游戏

相同字母验证javascript游戏
EN

Stack Overflow用户
提问于 2020-08-10 07:59:01
回答 1查看 121关注 0票数 0

我需要帮助处理我的hangman game,如果玩家之前猜到重复的字母,我该如何停止生命的下降;至于现在,如果我运行它,玩家猜出相同的字母,它会输出出他已经做了这个猜测,但生命也在下降。此外,如果玩家保持输入相同的正确字母,它将输出,他已经做了这个猜测,但它会说,他在输入相同的字母4-5次后获胜。

第一个错误:lives dropping even if players use letter that is guessed before

第二个错误:players input the same correct letter guessed and game will say he won after inputting 4-5 times

代码语言:javascript
复制
guesses = [];

// Show player their progress | .join returned answer as a string
while (remainingLetters > 0 && lives > 0) {
    (answerArray.join(""));

    guess = readline.question(name+"'s guess (Enter 9 for lifelines or 0 to pass): ");
    guess = guess.toUpperCase();

    //if guess is more than 1 letter or no letter, alert player to guess 1 letter only
    if (guess.length !== 1) {
        console.log("Please enter 1 letter only.");
    }

    //if valid guess
    else {
        correctGuess = 0;
        for (var j = 0; j < Word.length; j++) {
            if (Word[j] == guess) {
                answerArray[j] = guess;
                remainingLetters--;
                correctGuess = 1;
            }
        }

        if (correctGuess == 1) {
                console.log("\nGood job! "+guess+" is one of the letters!\n");
                console.log(JSON.stringify(answerArray)+"\n");
                console.log(JSON.stringify(alphabets)+"\n");
        } else {
            lives -= 1;
            console.log("\nSorry. "+guess+" is not a part of the word.\n");
            console.log(JSON.stringify(answerArray)+"\n");
            console.log(JSON.stringify(alphabets)+"\n");
            console.log("You have "+lives+" lives remaining.\n");
        }
        
        if (guesses.includes(guess)) {
            console.log("You have already made this guess, please try another letter!\n");
        } else {
            guesses.push(guess)
        }
    }

    if (remainingLetters == 0) {
        console.log("Congratulation! You managed to guess the word!\n");
        break;
    }
    
    if (lives == 0) {
        console.log("Game Over... You failed to guess the word. The word is "+Word+".\n")
    }
    
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-10 08:18:26

else for valid guess中,将整个代码移动到else of if (guesses.includes(guess)) {中。它会解决你们的两个问题。

代码语言:javascript
复制
// Show player their progress | .join returned answer as a string
while (remainingLetters > 0 && lives > 0) {
  (answerArray.join(""));

  guess = readline.question(name + "'s guess (Enter 9 for lifelines or 0 to pass): ");
  guess = guess.toUpperCase();

  //if guess is more than 1 letter or no letter, alert player to guess 1 letter only
  if (guess.length !== 1) {
    console.log("Please enter 1 letter only.");
  }

  //if valid guess
  else {
    if (guesses.includes(guess)) {
      console.log("You have already made this guess, please try another letter!\n");
    } else {
      guesses.push(guess);
      correctGuess = 0;
      for (var j = 0; j < Word.length; j++) {
        if (Word[j] == guess) {
          answerArray[j] = guess;
          remainingLetters--;
          correctGuess = 1;
        }
      }

      if (correctGuess == 1) {
        console.log("\nGood job! " + guess + " is one of the letters!\n");
        console.log(JSON.stringify(answerArray) + "\n");
        console.log(JSON.stringify(alphabets) + "\n");
      } else {
        lives -= 1;
        console.log("\nSorry. " + guess + " is not a part of the word.\n");
        console.log(JSON.stringify(answerArray) + "\n");
        console.log(JSON.stringify(alphabets) + "\n");
        console.log("You have " + lives + " lives remaining.\n");
      }
    }
  }

  if (remainingLetters == 0) {
    console.log("Congratulation! You managed to guess the word!\n");
    break;
  }
  
  if (lives == 0) {
    console.log("Game Over... You failed to guess the word. The word is " + Word + ".\n")
  }

}

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63336260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档