繁体   English   中英

当使用JavaScript在a子手游戏中还剩下3个猜测(生命)时,如何显示提示?

[英]How to display a hint when you have 3 guesses (lives) left in a hangman game using JavaScript?

我是新手,在我的子手游戏中,我希望当用户/播放器苦苦挣扎时,或者当用户/播放器还剩下3个猜测(生命)时,我的HTML代码出现在页面上。 当用户只剩下3个猜测(生存时间)时,我希望它显示在页面上。 我不知道该怎么做。 请帮忙。 提前致谢。

这是我的提示的HTML代码

  <form>
     <input type="text" id="hint" value="Hint: words are web related." readonly></input>
  </form>

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

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

    var randomWordIndex = randomNumber();
    currentPlayingWord = wordbank[randomWordIndex];
    underscores = wordloop(currentPlayingWord); 
    wordOutCome(underscores);
    guessCounter = 10;
    livesLeft = 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);
                    noMoreLives();
                  }


                  $('#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;
      livesLeft = livesLeft - 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";
    }

    var noMoreLives = function() {
      var showLives = "You have " + livesLeft + " lives";
      var test = document.getElementById("mylives");
      test.textContent = showLives;

    }

在letterPress函数的else“如果结果为true”中,将其添加到reduceGuessCount之后。

if (livesLeft === 3) alert('3 lives left');

另外,由于result是布尔值,您可以编写

if (result)

代替

if (result == true)

暂无
暂无

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

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