Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/382.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
刽子手游戏相同的字母验证javascript只_Javascript - Fatal编程技术网

刽子手游戏相同的字母验证javascript只

刽子手游戏相同的字母验证javascript只,javascript,Javascript,我需要帮助玩我的《刽子手游戏》,如果玩家之前猜到了重复的字母,我如何阻止生命下降,现在如果我运行它,玩家猜到了相同的字母,它将输出他已经猜到了,但生命也在下降。另外,如果玩家一直输入相同的正确字母,它将输出他已经做出了这个猜测,但在输入相同的字母4-5次后,它会说他赢了 第一个错误:即使玩家使用之前猜到的字母,生命也会下降 第二个错误:玩家输入猜到的相同正确字母,游戏将在输入4-5次后说他赢了 代码 guesses = []; // Show player their progress | .

我需要帮助玩我的《刽子手游戏》,如果玩家之前猜到了重复的字母,我如何阻止生命下降,现在如果我运行它,玩家猜到了相同的字母,它将输出他已经猜到了,但生命也在下降。另外,如果玩家一直输入相同的正确字母,它将输出他已经做出了这个猜测,但在输入相同的字母4-5次后,它会说他赢了

第一个错误:
即使玩家使用之前猜到的字母,生命也会下降

第二个错误:
玩家输入猜到的相同正确字母,游戏将在输入4-5次后说他赢了

代码

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")
    }
    
}
猜测=[];
//向玩家显示他们的进度|。将返回的答案作为字符串加入
while(remaingletters>0&&lifes>0){
(answerArray.join(“”);
guess=readline.question(name+“)的猜测(输入9表示生命线或输入0表示通过):”;
guess=guess.toUpperCase();
//如果猜测超过1个字母或没有字母,提醒玩家只猜测1个字母
if(guess.length!==1){
console.log(“请仅输入1个字母”);
}
//如果猜对了
否则{
修正猜测=0;
for(var j=0;j
Inside
else
对于
有效猜测
将整个代码移动到
else
中,如果(猜测.包括(猜测)){
。它将解决您的两个问题

//向玩家显示他们的进度|。将返回的答案作为字符串加入
while(remaingletters>0&&lifes>0){
(answerArray.join(“”);
guess=readline.question(name+“)的猜测(输入9表示生命线或输入0表示通过):”;
guess=guess.toUpperCase();
//如果猜测超过1个字母或没有字母,提醒玩家只猜测1个字母
if(guess.length!==1){
console.log(“请仅输入1个字母”);
}
//如果猜对了
否则{
如果(猜测。包括(猜测)){
console.log(“您已经猜到了,请尝试另一个字母!\n”);
}否则{
猜。推(猜);
修正猜测=0;
for(var j=0;j}
非常感谢你,Karan!