我的刽子手猜测功能正在行动,因为它不应该。如果thinkLetter =="",那么当它猜测的值没有减少时,它会做它应该做的事情,但是当guessLetter处于secretWord或者它不是&时,它会下降#39;吨。问题出在我的代码的底部,在wordGuess()函数中。如果你能感谢,请帮忙。 **
这是我的CODEPEN代码的链接 https://codepen.io/MilkyBOii/pen/LyPdRp?editors=0011
**
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 = []
var guesses = Math.round((secretWord.length) + (.5*secretWord.length))
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); **Used to see if the letters were added to the correct array**
console.log(wrongLetters);*/
wordGuess();
}
function wordGuess() {
if(guessLetter.value === ""){
alert("You didn't guess anything.");
}else if(guessLetter === secretWord){
guesses = guesses + 0;
}else {
guesses--;
}
console.log(guesses)
}
答案 0 :(得分:0)
试试my modification to your CodePen。我已修复它,以便猜测计数到0&amp;通过消息显示游戏。
问题在于此方法(AKA功能):
function wordGuess() {
if (guessLetter.value === '') {
alert("You didn't guess anything.");
} else if (guesses > 1) {
// Counts down.
guesses--;
console.log('Guesses Left: ' + guesses);
// Resets the input to a blank value.
let guessLetter = document.getElementById('guessLetter');
guessLetter.value = '';
} else {
console.log('game over');
}
/* You don't want to do this, because it's not decrementing the guesses.
for (var i = 0; i < secretWord.length; i++) {
if (guessLetter.value === secretWord[i])
guesses = guesses + 0;
}
*/
}
在脚本的顶部,猜测变量被设置为以下公式:
var guesses = Math.round((secretWord.length) + (.5 * secretWord.length))
这表示您希望使用guesses--;
倒计时,而不是使用guesses++;
计算。前者的工作方式如下:guesses -= 1;
或guesses = guesses - 1;
反之亦然。
现在您可以继续使用您的Hangman游戏!祝你好运! :)
JS Beautifier是一个方便的工具,顺便说一句!