gpt4 book ai didi

javascript - Hangman 游戏无法识别猜出的正确字母

转载 作者:行者123 更新时间:2023-11-28 10:54:57 26 4
gpt4 key购买 nike

我最近完成了 Javascript、CSS、HTML 的基础知识,并开始使用 jQuery。我决定我所知道的足以制作一个简单的刽子手游戏,所以我决定这样做。我已经完成了大部分,但是如果他们猜对了字母,我似乎无法让游戏替换底部的破折号。此外,如果他们猜对了字母,游戏会显示 body 部位,即使它不应该。我必须测试字母的代码如下。整个代码是here .由于此代码在 jsfiddle 中效果不佳,您可以下载源代码 here (或者只是从 jsfiddle 中复制它)。

我觉得有问题的部分:

function letterCheck(){
for(var z=0; z < trueWord.length; z++){
if( trueWord[z] == letter ){
trueWord[z] = letter;
numberOfRightLetters++;
alert("Correct!"); //Test to see if player guessed correct letter
document.getElementById('bottom').innerHTML="<p> Current Word: " + displayedWord + "</p>"; //SUPPOSED to place the letter at the bottom if guessed correctly **not working**
} else {
wrongLetter = true; //Tells program that the player guessed the wrong letter
alert("Incorrect!"); //Test to see if player guessed correct letter
}
}

if(wrongLetter == true){
numberOfWrongLetters++; //Increases the number of times the player guessed a wrong letter so the program displays the correct body part

//Displays person and their body parts
switch(numberOfWrongLetters){
case(1):
$('#head').fadeIn("slow");
break;
case(2):
$('#body').fadeIn("slow");
break;
case(3):
$('#LArm').fadeIn("slow");
break;
case(4):
$('#RArm').fadeIn("slow");
break;
case(5):
$('#Lleg').fadeIn("slow");
break;
case(6):
$('#Rleg').fadeIn("slow");
break;
}
}

最佳答案

基本上我对函数 letterCheck 做了一些修改

function letterCheck(letter){

for(var z=0; z < trueWord.length; z++){
if( trueWord[z] == letter ){
numberOfRightLetters++;
wrongLetter = false;
//Iterate the true word and when we find letters that match set the letter
//on the displayed word to the same position it was found on the trueword
$.each(trueWord,function(l){
//l is the index of the letter in the array
if(trueWord[l] == letter){
//replace al the occurrences of the letters on the displayed word
displayedWord[l] = letter
}
})
//Removed the alert in each iteration to the if(wrongLetter == true)
document.getElementById('bottom').innerHTML="<p> Current Word: " + displayedWord + "</p>"; //SUPPOSED to place the letter at the bottom if guessed correctly **not working**
//Added a break to stop the loop on the first ocurrence of the letter
//this is made to have control on the wrongLetter flag
break;
} else {
wrongLetter = true; //Tells program that the player guessed the wrong letter

}
}

if(wrongLetter == true){
alert("Incorrect")
numberOfWrongLetters++; //Increases the number of times the player guessed a wrong letter so the program displays the correct body part

//Displays person and their body parts
switch(numberOfWrongLetters){
case(1):
$('#head').fadeIn("slow");
break;
case(2):
$('#body').fadeIn("slow");
break;
case(3):
$('#LArm').fadeIn("slow");
break;
case(4):
$('#RArm').fadeIn("slow");
break;
case(5):
$('#Lleg').fadeIn("slow");
break;
case(6):
$('#Rleg').fadeIn("slow");
break;
}
}else{
alert("Correct!")
//Added to display one alert correct and not one for each letter
}

Fiddle

关于javascript - Hangman 游戏无法识别猜出的正确字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22647759/

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