Hangman 游戏相同的字母验证仅 javascript

我需要我的帮助,hangman game如果玩家之前猜了重复的字母,我如何阻止生命下降,至于现在如果我运行它并且玩家猜测相同的字母,它会输出他已经做出了这个猜测但生命也在下降. 另外,如果玩家一直输入相同的正确字母,它会输出他已经猜对了,但在输入相同的字母 4-5 次后会说他赢了。

第一个错误:lives dropping even if players use letter that is guessed before

第二个错误:players input the same correct letter guessed and game will say he won after inputting 4-5 times

代码

guesses = [];


// Show player their progress | .join returned answer as a string

while (remainingLetters > 0 && lives > 0) {

    (answerArray.join(""));


    guess = readline.question(name+"'s guess (Enter 9 for lifelines or 0 to pass): ");

    guess = guess.toUpperCase();


    //if guess is more than 1 letter or no letter, alert player to guess 1 letter only

    if (guess.length !== 1) {

        console.log("Please enter 1 letter only.");

    }


    //if valid guess

    else {

        correctGuess = 0;

        for (var j = 0; j < Word.length; j++) {

            if (Word[j] == guess) {

                answerArray[j] = guess;

                remainingLetters--;

                correctGuess = 1;

            }

        }


        if (correctGuess == 1) {

                console.log("\nGood job! "+guess+" is one of the letters!\n");

                console.log(JSON.stringify(answerArray)+"\n");

                console.log(JSON.stringify(alphabets)+"\n");

        } else {

            lives -= 1;

            console.log("\nSorry. "+guess+" is not a part of the word.\n");

            console.log(JSON.stringify(answerArray)+"\n");

            console.log(JSON.stringify(alphabets)+"\n");

            console.log("You have "+lives+" lives remaining.\n");

        }

        

        if (guesses.includes(guess)) {

            console.log("You have already made this guess, please try another letter!\n");

        } else {

            guesses.push(guess)

        }

    }


    if (remainingLetters == 0) {

        console.log("Congratulation! You managed to guess the word!\n");

        break;

    }

    

    if (lives == 0) {

        console.log("Game Over... You failed to guess the word. The word is "+Word+".\n")

    }

    

}


Smart猫小萌
浏览 102回答 1
1回答

ibeautiful

Insideelse用于valid guess将您的整个代码移动else到if (guesses.includes(guess)) {. 它将解决您的两个问题。// Show player their progress | .join returned answer as a stringwhile (remainingLetters > 0 && lives > 0) {&nbsp; (answerArray.join(""));&nbsp; guess = readline.question(name + "'s guess (Enter 9 for lifelines or 0 to pass): ");&nbsp; guess = guess.toUpperCase();&nbsp; //if guess is more than 1 letter or no letter, alert player to guess 1 letter only&nbsp; if (guess.length !== 1) {&nbsp; &nbsp; console.log("Please enter 1 letter only.");&nbsp; }&nbsp; //if valid guess&nbsp; else {&nbsp; &nbsp; if (guesses.includes(guess)) {&nbsp; &nbsp; &nbsp; console.log("You have already made this guess, please try another letter!\n");&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; guesses.push(guess);&nbsp; &nbsp; &nbsp; correctGuess = 0;&nbsp; &nbsp; &nbsp; for (var j = 0; j < Word.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; if (Word[j] == guess) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; answerArray[j] = guess;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; remainingLetters--;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; correctGuess = 1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; if (correctGuess == 1) {&nbsp; &nbsp; &nbsp; &nbsp; console.log("\nGood job! " + guess + " is one of the letters!\n");&nbsp; &nbsp; &nbsp; &nbsp; console.log(JSON.stringify(answerArray) + "\n");&nbsp; &nbsp; &nbsp; &nbsp; console.log(JSON.stringify(alphabets) + "\n");&nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; lives -= 1;&nbsp; &nbsp; &nbsp; &nbsp; console.log("\nSorry. " + guess + " is not a part of the word.\n");&nbsp; &nbsp; &nbsp; &nbsp; console.log(JSON.stringify(answerArray) + "\n");&nbsp; &nbsp; &nbsp; &nbsp; console.log(JSON.stringify(alphabets) + "\n");&nbsp; &nbsp; &nbsp; &nbsp; console.log("You have " + lives + " lives remaining.\n");&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; }&nbsp; if (remainingLetters == 0) {&nbsp; &nbsp; console.log("Congratulation! You managed to guess the word!\n");&nbsp; &nbsp; break;&nbsp; }&nbsp;&nbsp;&nbsp; if (lives == 0) {&nbsp; &nbsp; console.log("Game Over... You failed to guess the word. The word is " + Word + ".\n")&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript