试图显示在Hangman游戏中留下的猜测量

时间:2015-11-09 13:17:09

标签: javascript

我是一个新手,试图显示10个刽子手游戏中剩下的猜测数量,当你每次碰到错误的字母(错误的猜测)时,它会降到0。我想将用户重定向到名为“Looser Page.html”的新页面。目前它确实如此,但公众/用户看不到猜测。我不知道该怎么做。请帮忙。 提前致谢。

以下是我用来创建此页面的完整JavaScript代码。

var wordbank = ['browser', 'binary', 'cache', 'cookie', 'CSS', 'HTML', 'javascript', 'gigabyte', 'google', 'download']
    var currentPlayingWord = "";
    var underscores = "";
    var guessCounter = 0;
    $(document).ready(function() {

    var randomWordIndex = randomNumber();
    currentPlayingWord = wordbank[randomWordIndex];
    underscores = wordloop(currentPlayingWord); 
    wordOutCome(underscores);
    guessCounter = 10;

     $('#all-the-buttons button').click(function () {
      letterPress($(this));
     });



    });

    var wordloop = function(word){
      var wordcount = 0
      var underscores = "";
      while(wordcount < word.length) {
        underscores = underscores + "_";
        wordcount ++;
      }
      return underscores;
    }

    var randomNumber = function(){
      var random = Math.floor((Math.random() * 9) + 0);

      return random;
    }

    var wordOutCome = function(underscores){
      var wordoutcome = document.getElementById('word-outcome');
      wordoutcome.value = underscores;
    }

    function letterPress(button) {
                var text = button.text();
                if ("RESET" === text){
                  resetButton();
                }
                else { 
                  var currentText = $('#word-outcome').val();
                  //var output = currentText + text;

                  var result = isLetterInWord(text, currentPlayingWord);
                  if(result == true) {
                    replaceDashesForLetter(text);
                    var hasDashes = noMoreDashes();
                    if(hasDashes == true) {
                        navigateToWinnerPage();
                    }

                  }
                  else {
                    decreaseGuessCount();
                    noMoreGuesses();
                    addIncorrectGuessToWrongGuesses(text);
                  }


                  $('#word-outcome').val(underscores);

                }
    }

    function isLetterInWord(guess, word) {
      var uppercaseGuess = guess.toUpperCase();
      var uppercaseWord = word.toUpperCase();
      for (var i = 0; i < uppercaseWord.length; i++){
        console.log(i);
        if (uppercaseWord[i] === uppercaseGuess){
          return true; 
        }
        //get letter from word
        //is letter from word the same as guess
        //if letter from word is the same as guess return true
        //return false if guess is not in the word
      }
      return false;
    }

    function replaceDashesForLetter(letter) {
           for (var i = 0; i < currentPlayingWord.length; i++){
            console.log(currentPlayingWord);
            var playingLetter = currentPlayingWord[i];
            var upperCaseCurrentLetter = playingLetter.toUpperCase();
              if (upperCaseCurrentLetter == letter){
                underscores = setCharAt(underscores, i, letter);
              }
           }
              //for each letter in current word being played
            //does letter guessed match the letter in the current word
            //if letter guessed matches the letter in the current word - then replace the dash at the index (count in loop) with the letter guessed




      //for each of the letters in the word being played there is a dash
      //if the letter is at the index of a dash then replace that dash with the letter (which is the users guess)
    }

    function setCharAt(str,index,chr) {
      //get first part of word up to character we want to replace
      var first = str.substr(0,index);
      //get second part of word ONE letter AFTER character we want to replace
      var second = str.substr(index+1);
      //result is the first part plus the character to replace plus the second part
      return first + chr + second;
    }

    var addIncorrectGuessToWrongGuesses = function (guess) {
        var currentText = document.getElementById("wrong-guesses").value;
        document.getElementById("wrong-guesses").value = currentText + guess;
      //As the guess is wrong
      //add the guess to the list of incorrect guesses displayed on the screen
    }

    var greyOutButton = function (button) {
      //grey out the button
      //make sure that the user cannot press the button anymore
    }

    function resetButton () {

            location.href = "Hangman.html";

      //Send user to the home page
    }

    var decreaseGuessCount = function () {
      guessCounter = guessCounter - 1;
    //guess count should be decreased by one 
    }

    var noMoreGuesses = function() {
      if (guessCounter === 0){
         location.href = "Looser Page.html";
      }
      //do something when no more guesses (navigate to loser page)
    }

    var noMoreDashes = function() {
        var i = underscores.indexOf("_");
        if (i > -1){
          return false;
        }
        return true;
      //if index of '_' is not -1 then there are dashes
    }

    var navigateToWinnerPage = function() {
      location.href = "Winner Page.html";
    }

1 个答案:

答案 0 :(得分:0)

要显示剩余的猜测量,您需要一个带有id的范围,然后每当您减少猜测时,将该值放入范围的text属性中。

HTML:

<span id="guessesLeft"></span>

jQuery的:

$("#guessesLeft").text(guessesLeft);