简体   繁体   English

子手游戏无法识别猜测的正确字母

[英]Hangman Game Not Identifying Correct Letters Guessed

I recently finished up the basics of Javascript, CSS, HTML, and have started jQuery. 我最近完成了Javascript,CSS,HTML的基础知识,并开始使用jQuery。 I decided that I knew enough to make a simple hangman game, so I've decided to do just that. 我决定知道足够多的知识来制作一个简单的game子手游戏,所以我决定这样做。 I have most of it done, however I cannot seem to get the game to replace the dashes at the bottom if they guess the correct letter. 我已经完成了大部分工作,但是,如果他们猜对了正确的字母,我似乎无法让游戏取代底部的破折号。 Also if they guess the correct letter the game reveals a body part, even though it shouldn't. 同样,如果他们猜对了正确的字母,游戏就会揭示出身体的一部分,即使事实并非如此。 The code I have to test for the letters is as follows. 我必须测试字母的代码如下。 The entirety of the code is here . 整个代码在这里 Since this code does not work the best in jsfiddle, you can download the source here (or just copy it from the jsfiddle). 由于此代码在jsfiddle中无法发挥最佳效果,因此您可以在此处下载源代码(或仅从jsfiddle复制它)。

The portion I seem to be having trouble with: 我似乎遇到问题的部分:

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;
        }
    }

Basically i made some changes to the function letterCheck 基本上我对函数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 小提琴

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

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