Maintenant, après avoir implémenté le mode indice, la partie deviner ne fonctionne pas, ce qui signifie que je ne peux pas deviner une lettre. Cela signifie que les vies ne diminuent pas et que personne ne peut gagner ou perdre la partie. Le mode indice fonctionne parfaitement.

      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;
};

Autant que je sache, les variables sont toujours les mêmes, donc je ne comprends pas pourquoi une erreur se produit?.

0
Anne 14 avril 2018 à 13:21

3 réponses

Meilleure réponse

En plus du problème tableau dans tableau, vous devez également stocker l'index lui-même et l'utiliser à la place du mot.

Vous avez également une faute de frappe: hint.onclick devrait être 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>
0
Martin Adámek 14 avril 2018 à 10:30

Vous avez déclaré un tableau contenant un tableau:

   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"]  
         ];

Changez-le en un simple tableau:

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"];
1
Sébastien S. 14 avril 2018 à 10:26

Je l'ai trié - Merci pour toute votre aide :) J'ai changé le mot actuel de variable et maintenant ça marche

0
Anne 14 avril 2018 à 11:36