gpt4 book ai didi

javascript - 如何将按下的键与存储的数组进行比较,然后替换另一个数组中的字母

转载 作者:行者123 更新时间:2023-11-28 02:33:03 25 4
gpt4 key购买 nike

好吧,我知道标题令人困惑所以这里是解释。

  1. 这是一项家庭作业,我完全无法完成这项任务。
  2. 我正在创建一个简单的 Hangman 游戏,并且已经让计算机从列表中随机选择一个单词(当前代码将列表设置为 1 个项目,而我正在修复其他几个错误)。

这就是我要问的。在第 45 行,我试图将存储在 lettersGuessed 中的字母与存储在 randomWord 中的值进行比较。如果该值与 randomWord 数组中的字母匹配,则将其打印到 HTML 文档,如果不匹配,则继续允许用户尝试,直到他们达到 0 次剩余尝试。我尝试了一个带有嵌套 if 的 for 循环,但比较不起作用。

我知道我必须清理一些代码,但首先我想让它正常工作然后重构它。

提前致谢!

//This will hold an list of words to guess in the game
var wordsToGuess = ["abc"];
//Global variables//
//variables to hold score count
var wins = 0;
var losses = 0;
var guessesRemaining = 10;
//Empty arras to store letters guessed
var lettersGuessed = [];
//Empty arras to store the word to guess
var randomWord = [];
var word = [];


//When the enter key is pressed a new word is slected and the Current Word section is replaced with dashes
window.addEventListener("keyup", function(e) {
if (e.keyCode === 13) {
word = [];
start();
}
}, false);

//function to start and re-start the game when needed
function start() {

//Randomizer to pick random a word from wordsToGuess array
randomWord = wordsToGuess[Math.floor(Math.random() * wordsToGuess.length)];
console.log(randomWord);

//This adds the dashes for the number of letters of the picked word
for (var i = 0; i < randomWord.length; i++) {
word[i] = "_";
}
console.log(word);
document.getElementById("current-word").innerHTML = word;


//when the player presses a key well run some code
document.onkeyup = function(event) {

//To determine what key is pressed and save it to lettersGuessed
lettersGuessed.push(event.key);

//Print letters guessed to page
document.getElementById("guessed-letters").innerHTML = lettersGuessed;
console.log(lettersGuessed);
//check if letter guessed is equal to any of the letters in the word
if (randomWord.indexOf(lettersGuessed) === -1) {
word.push(lettersGuessed);
}else{
guessesRemaining--;
document.getElementById("guesses").innerHTML = guessesRemaining;
console.log(guessesRemaining);
}
/*
guessesRemaining--;
document.getElementById("guesses").innerHTML = guessesRemaining;
console.log(guessesRemaining);
*/
}
}

//each time the player guesses a correct letter
//current word should update

//each time the player wins

//each time the player loses
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="utf-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Hangman-Game</title>
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link rel="stylesheet" type="text/css" media="screen" href="../assets/css/style.css" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="../assets/Javascript/game.js"></script>

</head>

<body>

<div class="card mb-3">
<img class="card-img-top" src="../assets/images/Star_Wars_Logo.png" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">Card title</h5>
<p class="card-text">This is my StarWars Hangman Game. Here are the rules...just guess letters and try to win. All words can come from any StarWars movie but will be limited to characters in the movies and Cartoon shows (not including books) Press ENTER to start.
</p>
</div>
</div>
<div class="container">
<div class="row">
<div class="col">
This column will hold my images
</div>
<div class="col">
This side will hold by game
<p class="wins">Wins:
<span id="wins">0</span>
</p>
<p class="losses">Losses:
<span id="losses">0</span>
</p>
<br/>
<p>Current Word: </p>
<p>
<span id="current-word">""</span>
</p>
<p>Guessed Letters: </p>
<p>
<span id="guessed-letters">""</span>
</p>
<p>Guesses Remaining: </p>
<p>
<span id="guesses">10</span>
</p>
</div>
</div>
</div>
</div>





<footer class="footer position-relative">
<div class="container">
<span class="text-muted">Copyright Roberto Benites 2018</span>
</div>
</footer>

<!--BootStrap links-->
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

</body>

</html>

最佳答案

我会将正确的单词放入 var 中并循环遍历其中的每个字符,然后将正确单词中的每个字符放入正确的单词数组中。当用户输入一个字母时,您可以循环遍历正确的单词数组并检查该字母是否在正确的单词数组中。如果是。将其放入猜中的正确字母数组中。如果不是。把它放入一个猜错字母的数组中。还要记住,您猜测的字母可能多次出现在正确的单词数组中。您还想检查猜测是否剩余 == 0,如果猜测的字母错误,还需要删除部分刽子手。这个基本逻辑虽然应该将你的正确字母和你不正确的字母分成两个单独的数组,你需要对其执行更多逻辑。您还需要在正确的位置显示正确的字母,这应该不会太棘手。祝你好运,我希望这会有所帮助。

var str = "correctWord"
var word = [];

for (var i = 0; i < str.length; i++) {
word.push(str.charAt(i));
}
for(var i =0; i< word.length; i++)
{
if(i == guessedletter)
{
correctlettersGuessed.push(i);
}
else
{
wrongGuessedLetter.push(guessedletter);
}
guessesRemaining--;


}

关于javascript - 如何将按下的键与存储的数组进行比较,然后替换另一个数组中的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49242593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com