【问题标题】:JavaScript/jQuery hangman IssuesJavaScript/jQuery 刽子手问题
【发布时间】:2018-02-07 20:23:41
【问题描述】:

我正在制作一个刽子手游戏,但我似乎无法让游戏循环正确。我试图让它贯穿整个单词,如果它是正确的答案,则替换该字母。相反,它是:

  • 循环遍历单词
  • 如果猜测的字母位于单词中的任何位置
  • 它返回index[0]

我尝试拆分单词并使用各个索引,但如果我能做到这一点,我宁愿学习如何做到这一点。此外,我仍在试图弄清楚如何循环提示!感谢您的宝贵时间!

$(document).ready(function() {

  var words = ["RUGRATS", "DOUG", "DARIA", "POKEMON", "RECESS", "ANAMANIACS", "CARMEN/SAN/DIEGO", "REN AND STIMPY", "THE SIMPSONS", "POWER RANGERS", "BEAVIS AND BUTTHEAD", "ROCKOS MODERN LIFE", "NINJA TURTLES", "MATILDA"]
  var chosenWord = words[Math.floor(Math.random() * words.length)]
  // var space = ''
  var underScores = [] //underscores
  var wrongGuesses = [] //store wrong guesses
  var guesses = 10 //guess max
  //replace 
  var wins = 0;
  $(".wins").html("WINS : " + wins)
  var hints = ["Gang of Babies", "Quail man's identity", "90's femenist", "Gotta Catch 'em all!", "playtime during school, not lunch", "They live in the WB tower", "Where in the world is...", "'It's log, log...", "Longest running cartoon.", "Morphin' time!", "'I need teepee for my bunghole'", "Friendly walabe", "Pizza lovin' turtles", "Girl Genius"]
  var hintLength = hints.length
  var getHint = $("#hintBtn")
  $(".hintBtn").on("click", function() {
    if (words[0] === chosenWord) {
      $("#hint").text(hints[0])
    } else if (words[1] === chosenWord) {
      $("#hint").text(hints[1])
    } else if (words[2] === chosenWord) {
      $("#hint").text(hints[2])
    } else if (words[3] === chosenWord) {
      $("#hint").text(hints[3])
    } else if (words[4] === chosenWord) {
      $("#hint").text(hints[4])
    } else if (words[5] === chosenWord) {
      $("#hint").text(hints[5])
    } else if (words[6] === chosenWord) {
      $("#hint").text(hints[6])
    } else if (words[7] === chosenWord) {
      $("#hint").text(hints[7])
    } else if (words[8] === chosenWord) {
      $("#hint").text(hints[8])
    } else if (words[9] === chosenWord) {
      $("#hint").text(hints[9])
    } else if (words[10] === chosenWord) {
      $("#hint").text(hints[10])
    } else if (words[11] === chosenWord) {
      $("#hint").text(hints[11])
    } else if (words[12] === chosenWord) {
      $("#hint").text(hints[12])
    } else if (words[13] === chosenWord) {
      $("#hint").text(hints[12])
    }
  })

  $("#newGame").on("click", function() {
    chosenWord = words[Math.floor(Math.random() * words.length)]
    underScores = []
    for (var i = 0; i < chosenWord.length; i++) {
      underScores.push("_ ")
    }
    guesses = 10
    $(".guessesLeft").html("You have " + guesses + " guesses left!")
    $(".display").html(underScores)
    $("#hint").html("Press the button to get a hint!")
    console.log("your random word is " + chosenWord)
  })

  if (guesses > 0) {
    $(document).on("keyup", function(event) {
      guess = String.fromCharCode(event.keyCode).toUpperCase()
      // console.log(guess)
      if (chosenWord.indexOf(guess) > -1) {
        for (i = 0; i < chosenWord.length; i++) {
          if (chosenWord[i] = guess) {
            underScores[i] = chosenWord[i]
            console.log(underScores)
            underScores.join(" ")
            return
          }
        }
      } else {
        for (i = 0; i < chosenWord.length; i++) {
          if (chosenWord[i] != guess) {
            guesses--
            var wrongGuesses = guess
            $(".wrongGuesses").append(wrongGuesses)
            console.log(wrongGuesses)
            return
          }

        }
      }
    })
  }

})
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<!DOCTYPE html>
<html lang="en">

<head>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <link rel="stylesheet" href="assets/css/style.css">
  <link href='https://fonts.googleapis.com/css?family=Bevan' rel='stylesheet'>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <div class="container-fluid">
    <div class="row">
      <!-- main content -->
      <div class="col-sm-2"></div>
      <div class="col-sm-8">
        <div class="jumbotron">
          <h1>Hangman : 90's Cartoons & Movies!</h1>
          <h2>Press any key to get started!</h2>
          <!-- button for new word -->
          <button id="newGame">NEW GAME</button>
          <div class="wins"></div>
          <div class="losses"></div>

          <div class="display"></div>
          <div class="wrongGuesses"></div>
          <div class="guessesLeft"></div>
          <div id="buttons"></div>
          <button class="hintBtn">HINT!</button>
          <p id="hint"></p>
        </div>
      </div>
      <div class="col-sm-2"></div>
    </div>


    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E=" crossorigin="anonymous"></script>

    <script src="assets/javascript/game2.js"></script>
</body>

</html>

【问题讨论】:

    标签: javascript jquery html loops


    【解决方案1】:

    你们非常接近——只有两件事需要改变。

    您的if 语句是分配而不是比较。改变这个:

    if (chosenWord[i] = guess) {
    

    到这里:

    if (chosenWord[i] == guess) {
    

    此外,您需要刷新 HTML 以在猜到正确字母时显示正确的字母。在正确猜测后返回之前添加此行:

    $(".display").html(underScores)
    

    需要注意的一点:您的函数在第一个正确字母实例之后返回。我删除了return 语句,这样它就可以完成对单词的循环,因此它将填充正确字母的任何实例的下划线。

    这是一个工作示例:

    $(document).ready(function() {
    
      var words = ["RUGRATS", "DOUG", "DARIA", "POKEMON", "RECESS", "ANAMANIACS", "CARMEN/SAN/DIEGO", "REN AND STIMPY", "THE SIMPSONS", "POWER RANGERS", "BEAVIS AND BUTTHEAD", "ROCKOS MODERN LIFE", "NINJA TURTLES", "MATILDA"]
      var chosenWord = words[Math.floor(Math.random() * words.length)]
      // var space = ''
      var underScores = [] //underscores
      var wrongGuesses = [] //store wrong guesses
      var guesses = 10 //guess max
      //replace 
      var wins = 0;
      $(".wins").html("WINS : " + wins)
      var hints = ["Gang of Babies", "Quail man's identity", "90's femenist", "Gotta Catch 'em all!", "playtime during school, not lunch", "They live in the WB tower", "Where in the world is...", "'It's log, log...", "Longest running cartoon.", "Morphin' time!", "'I need teepee for my bunghole'", "Friendly walabe", "Pizza lovin' turtles", "Girl Genius"]
      var hintLength = hints.length
      var getHint = $("#hintBtn")
      $(".hintBtn").on("click", function() {
        if (words[0] === chosenWord) {
          $("#hint").text(hints[0])
        } else if (words[1] === chosenWord) {
          $("#hint").text(hints[1])
        } else if (words[2] === chosenWord) {
          $("#hint").text(hints[2])
        } else if (words[3] === chosenWord) {
          $("#hint").text(hints[3])
        } else if (words[4] === chosenWord) {
          $("#hint").text(hints[4])
        } else if (words[5] === chosenWord) {
          $("#hint").text(hints[5])
        } else if (words[6] === chosenWord) {
          $("#hint").text(hints[6])
        } else if (words[7] === chosenWord) {
          $("#hint").text(hints[7])
        } else if (words[8] === chosenWord) {
          $("#hint").text(hints[8])
        } else if (words[9] === chosenWord) {
          $("#hint").text(hints[9])
        } else if (words[10] === chosenWord) {
          $("#hint").text(hints[10])
        } else if (words[11] === chosenWord) {
          $("#hint").text(hints[11])
        } else if (words[12] === chosenWord) {
          $("#hint").text(hints[12])
        } else if (words[13] === chosenWord) {
          $("#hint").text(hints[12])
        }
      })
    
      $("#newGame").on("click", function() {
        chosenWord = words[Math.floor(Math.random() * words.length)]
        underScores = []
        for (var i = 0; i < chosenWord.length; i++) {
          underScores.push("_ ")
        }
        guesses = 10
        $(".guessesLeft").html("You have " + guesses + " guesses left!")
        $(".display").html(underScores)
        $("#hint").html("Press the button to get a hint!")
        console.log("your random word is " + chosenWord)
      })
    
      if (guesses > 0) {
        $(document).on("keyup", function(event) {
          guess = String.fromCharCode(event.keyCode).toUpperCase()
          // console.log(guess)
          if (chosenWord.indexOf(guess) > -1) {
            for (i = 0; i < chosenWord.length; i++) {
              if (chosenWord[i] == guess) {
                underScores[i] = chosenWord[i]
                console.log(underScores)
                underScores.join(" ")
                $(".display").html(underScores)
              }
            }
          } else {
            for (i = 0; i < chosenWord.length; i++) {
              if (chosenWord[i] != guess) {
                guesses--
                var wrongGuesses = guess
                $(".wrongGuesses").append(wrongGuesses)
                console.log(wrongGuesses)
                return
              }
    
            }
          }
        })
      }
    
    })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <!DOCTYPE html>
    <html lang="en">
    
    <head>
      <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
      <link rel="stylesheet" href="assets/css/style.css">
      <link href='https://fonts.googleapis.com/css?family=Bevan' rel='stylesheet'>
      <meta charset="UTF-8">
      <meta name="viewport" content="width=device-width, initial-scale=1.0">
      <meta http-equiv="X-UA-Compatible" content="ie=edge">
      <title>Document</title>
    </head>
    
    <body>
      <div class="container-fluid">
        <div class="row">
          <!-- main content -->
          <div class="col-sm-2"></div>
          <div class="col-sm-8">
            <div class="jumbotron">
              <h1>Hangman : 90's Cartoons & Movies!</h1>
              <h2>Press any key to get started!</h2>
              <!-- button for new word -->
              <button id="newGame">NEW GAME</button>
              <div class="wins"></div>
              <div class="losses"></div>
    
              <div class="display"></div>
              <div class="wrongGuesses"></div>
              <div class="guessesLeft"></div>
              <div id="buttons"></div>
              <button class="hintBtn">HINT!</button>
              <p id="hint"></p>
            </div>
          </div>
          <div class="col-sm-2"></div>
        </div>
    
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha256-3edrmyuQ0w65f8gfBsqowzjJe2iM6n0nKciPUp8y+7E=" crossorigin="anonymous"></script>
    
        <script src="assets/javascript/game2.js"></script>
    </body>
    
    </html>

    【讨论】:

    • 谢谢!这解决了问题!这是一项正在进行的工作,但我无法超越这个哈哈
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-29
    • 2016-02-13
    • 1970-01-01
    • 2014-09-10
    • 2010-10-28
    • 1970-01-01
    相关资源
    最近更新 更多