触发"胜利"在香草JavaScript Hangman游戏中

时间:2017-09-10 18:01:07

标签: javascript function random return counter

所以我已经问了几个关于这个Hangman游戏的问题(并得到了很棒的答案),但是我的#34;得分管理员不断绊倒了#34;。游戏几乎可以完成我所需要的一切,但是一场胜利"在整个单词填写完毕后按下随机键后才会记录...

<!DOCTYPE html>
<html>
<head>
    <title>Hangman</title>
</head>
<body>
    <h1>Hangman!</h1>

    <p>
        <font size="+3"><span id="answer"></span></font>
    </p>
    <p>Lives: <span id="counter"></span></p>
    <p id="wrongGuesses"></p>
    <p>Wins: <span id="wins"></span></p>
    <p>Losses: <span id="losses"></span></p>


    <script type="text/javascript">

var word;

var guess;      //user guess
var letters = [];       //correctly guessed letters
var wrongLetters = [];      //incorrectly guessed letters
var counter;        //counts correct letters
var losses = 0;
var wins = 0;


document.getElementById("losses").innerHTML = losses;
document.getElementById("wins").innerHTML = wins;

var wordList = ["cat", "dog", "wolf", "laser", "apple"];        //FILL LIST LATER!!


//randomly chooses a word from the array and replaces letters with underscores
function start() {
    word = wordList[Math.floor(Math.random() * wordList.length)];
    counter = 7;
    document.getElementById("counter").innerHTML = counter;
    for (i = 0; i < word.length; i++) {
        letters[i] = "__";
    }

    document.getElementById("answer").innerHTML = letters.join(" ");
    console.log(word);

}


//checks if letter is in the word or not
function checkLetter() {
    document.onkeyup = function(event) {
        guess = event.key.toLowerCase();
        //var found = false;
        for (i = 0; i < word.length; i++) {
            if (guess === word[i]) {
                letters[i] = guess;
                document.getElementById("answer").innerHTML = letters.join(" ");
                //found = true;
            } 
        }
        //wrong letters go into the wrongLetters array and are displayed
        //if (found) return;
        if (wrongLetters.indexOf(guess) < 0) {
            wrongLetters.push(guess);
            document.getElementById("wrongGuesses").innerHTML = wrongLetters.join(" ");
            //every wrong guess subtracts one from the counter
            counter--;
            console.log(counter);
            document.getElementById("counter").innerHTML = counter;
            //when counter reaches 0 it's Game Over
            //+1 to the losses if 7 words are missed

            if (counter > 0 && letters.join("") === word) {             //THE ISSUE
                document.getElementById("wins").innerHTML = wins + 1;
                console.log(wins);
                confirm("YOU WIN! Play Again?");
                    wins++;
                    counter = 7;
                    letters = [ ];
                    wrongLetters = [ ];
                start();
            }

            else if (counter === 0) {
                document.getElementById("losses").innerHTML = losses + 1;
                console.log(losses);
                confirm("YOU LOOSE... play again?"); {
                    losses++;
                    counter = 7;
                    letters = [];
                    wrongLetters = [];
                    start();
                }
            }
        }
    }
}


start();
checkLetter();

</script>
</body>
</html>

这是我到目前为止所提供的内容,这里有一个jsfiddle链接:https://jsfiddle.net/t57zfv3t/11/ 我知道主要的问题是找到bool和返回,但我似乎无法找到解决它的方法。如果有任何人有任何见解,我将不胜感激,我向任何已经困扰过这一点的人道歉。谢谢!

1 个答案:

答案 0 :(得分:1)

如果找到了一封信,即使它是一个完整的单词匹配,你也会返回,所以只要在条件中检查它

//wrong letters go into the wrongLetters array and are displayed
if (found && letters.join("") !== word) return;