简体   繁体   English

在a子手游戏js中显示提示模式

[英]Displaying a hint mode in a hangman game js UPDATE

Now after implementing the hint mode, the guessing part does not work, meaning I cannot guess a letter. 现在,在实现提示模式后,猜测部分将不起作用,这意味着我无法猜测字母。 This means that the lives do not reduce and no one can win or lose the game. 这意味着生命不会减少,没有人可以赢得或输掉比赛。 The hint mode works perfectly. 提示模式可以完美运行。

      function setup() {
      alphabet = "abcdefghijklmnopqrstuvwxyz";
      lives = 8;
      var words = ["ayeupmeducks", "pieceofcake", 
     "bullinachinashop", "hangfire","greeneyedmonster", 
     "hairraising","bringhomethebacon","adiamondintherough","onceinabluemoon",
     "afootinthedoor","bitethebullet"];

    messages = {
        win: 'Congratulations you have won the game of Hangman!',
        lose: 'You have been Hanged !!',
        guessed: ' already guessed, please try again...',
        validLetter: 'Please enter a letter from A-Z'
    };

      var getHint = document.getElementById("hint");
      var showClue = document.getElementById("clue");
      getHint.onclick = function() {
      hints = ["Stoke Greeting","Saying Something is Easy",
               "Very Clumsy","delaying something for a minute", 
               "When you are jealous of something",
              "Something is frightening", "Earn Money", 
              "Rough Exterior however has some potential", 
              "When something rarely happens", 
              "When you have succeeded/ getting yourself known by a company",
              "accepting something , when you do not want to"];

    var hintIndex = words
    showClue.innerHTML = "Clue: - " +  hints [idx];};

    gussedLetter = matchedLetter = '';
    numberofMatchedLetters = 0;
    var idx = Math.floor(Math.random() * words.length);
    var currentWord = words[idx];
    output = document.getElementById("output");
    message = document.getElementById("message");
    guessInput = document.getElementById("letter");
    message.innerHTML = 'You have ' + lives + ' lives remaining';
    output.innerHTML = '';
    document.getElementById("letter").value = '';

    guessButton = document.getElementById("guess");
    guessInput.style.display = 'inline';
    guessButton.style.display = 'inline';

    letters = document.getElementById("letters");
    letters.innerHTML = '<li class="current-word">Current word:</li>';
    var letter, i;
    for (i = 0; i < currentWord.length; i++) {
        letter = '<li class="letter letter' + currentWord.charAt(i).toUpperCase() + '">' + currentWord.charAt(i).toUpperCase() + '</li>';
        letters.insertAdjacentHTML('beforeend', letter);
    }
}

function gameOver(win) {
    if (win) {
        output.innerHTML = messages.win;
        output.classList.add('win');
    } else {
        output.innerHTML = messages.lose;
        output.classList.add('error');
    } 
    guessInput.style.display = guessButton.style.display = 'none';
    guessInput.value = '';
}

window.onload = setup();

document.getElementById("restart").onclick = setup;

guessInput.onclick = function () {
    this.value = '';
};
document.getElementById('hangman').onsubmit = function (e) {
    if (e.preventDefault) e.preventDefault();
    output.innerHTML = '';
    output.classList.remove('error', 'warning');
    guess = guessInput.value;

    if (guess) {
        if (alphabet.indexOf(guess) > -1) {
            if ((matchedLetter && matchedLetter.indexOf(guess) > -1) || (gussedLetter && gussedLetter.indexOf(guess) > -1)) {
                output.innerHTML = '"' + guess.toUpperCase() + '"' + messages.guessed;
                output.classList.add("warning");
            }
            else if (currentWord.indexOf(guess) > -1) {
                var lettersToShow;
                lettersToShow = document.querySelectorAll(".letter" + guess.toUpperCase());

                for (var i = 0; i < lettersToShow.length; i++) {
                     lettersToShow[i].classList.add("correct");
                }

                for (var j = 0; j < currentWord.length; j++) {
                    if (currentWord.charAt(j) === guess) {
                        numberofMatchedLetters += 1;
                    }
                }

                matchedLetter += guess;
                if (numberofMatchedLetters === currentWord.length) {
                    gameOver(true);
                }
            }
            else {
                gussedLetter += guess;
                lives--;
                message.innerHTML = 'You have ' + lives + ' lives remaining';
                if (lives === 0) gameOver();
            }
        }
        else {
            output.classList.add('error');
            output.innerHTML = messages.validLetter;
        }
    }
    else {
        output.classList.add('error');
        output.innerHTML = messages.validLetter;
    }
    return false;
};

The variables as far as I can see are still the same, so I do not get why an error occurs?. 据我所知,变量仍然相同,所以我不明白为什么会发生错误?

You declared an array containing an array : 您声明了一个包含数组的数组:

   hints = [
        ["Stoke Greeting","Saying Something is Easy", "Very Clumsy","delaying something for a minute", "When you are jealous of something","Something is frightening", "Earn Money", "Rough Exterior however has some potential", "When something rarely happens", "When you have succeeded/ getting yourself known by a company","accepting something , when you do not want to"]  
         ];

Change it to a simple array : 将其更改为一个简单数组:

hints = ["Stoke Greeting","Saying Something is Easy", "Very Clumsy","delaying something for a minute", "When you are jealous of something","Something is frightening", "Earn Money", "Rough Exterior however has some potential", "When something rarely happens", "When you have succeeded/ getting yourself known by a company","accepting something , when you do not want to"];

In addition to the array in array problem, you also need to store the index itself and use that instead of the word. 除了数组中的数组问题外,您还需要存储索引本身并使用它代替单词。

Also you have typo: hint.onclick should be getHint.onclick . 您也有错别字: hint.onclick应该是getHint.onclick

 var words = ["ayeupmeducks", "pieceofcake", "bullinachinashop", "hangfire","greeneyedmonster", "hairraising","bringhomethebacon","adiamondintherough","onceinabluemoon","afootinthedoor","bitethebullet"]; var idx = Math.floor(Math.random() * words.length); var currentWord = words[idx]; var getHint = document.getElementById("hint"); var showClue = document.getElementById("clue"); getHint.onclick = function() { var hints = ["Stoke Greeting","Saying Something is Easy", "Very Clumsy","delaying something for a minute", "When you are jealous of something","Something is frightening", "Earn Money", "Rough Exterior however has some potential", "When something rarely happens", "When you have succeeded/ getting yourself known by a company","accepting something , when you do not want to"]; showClue.innerHTML = "Clue: - " + hints [idx]; }; 
 <button id="hint" name="hint" type="button">Hint</button> <p id="clue">Clue: </p> 

排序-谢谢您的所有帮助:)我将其改为一个变量,现在可以使用了

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

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