子手游戏中的字母递增

时间:2019-08-24 18:09:16

标签: javascript

如果用户猜错单词,我需要一次添加一个字母H,A,N,G,M,A,N

我尝试过使用循环,但是不确定如何做

function playgame(yourword) {

    //yourword array
    /*********************/
    let word = yourword.toUpperCase();
    let letters = word.split("");
    var answerArray = [letters[0]];
    for (var i = 1; i < letters.length; i++) {
        answerArray[i] = "_";
    }
    let answer = answerArray.join();

    //hangman array
    /*********************/
    let hgm = ["H", "A", "N", "G", "M", "A", "N"];
    let hgArray = [hgm[0]];
    for (var i = 1; i < hgm.length; i++) {
        hgArray[i] = "_";
    }
    //loop
    let remainingLetters = word.length;
    let remainingLetter = hgm.length;
    while (remainingLetters > 0 & remainingLetter > 0) {

        //show the array
        console.log(answerArray.join(" ").toUpperCase());
        console.log(hgArray.join(",").toUpperCase());

        let guess = prompt("guess a letter").toUpperCase();
        //add a letter if guessed correctly
        for (var i = 0; i < letters.length; i++) {
            if (word[i] === guess) {
                answerArray[i] = guess;
                remainingLetters--;
            }
            //add a letter to the hangman string if guessed incorrecty 
            //MY PROBLEM IS HERE

            for (var h = 0; h < hgm.length; h++) {
                if (word[h] !== guess) {
                    remainingLetter--;
                }

                //TO HERE

            }

        }
        console.log(answerArray.join(" ").toUpperCase());
        console.log(hgArray.join(",").toUpperCase());
        if (remainingLetters === 0) {
            console.log("you win");
        } else if (remainingLetters !== 0) {
            console.log("you lose");
        }
    }
}

我尝试使用循环的方式与增加下一个字母的方式相同,但是我不确定如何去做。

请帮忙

1 个答案:

答案 0 :(得分:0)

您不需要循环,因为您只想在当前位置插入一个字母。

function playgame(yourword) {

    //yourword array
    /*********************/
    let word = yourword.toUpperCase();
    let letters = word.split("");
    var answerArray = [letters[0]];
    for (var i = 1; i < letters.length; i++) {
        answerArray[i] = "_";
    }
    let answer = answerArray.join();

    //hangman array
    /*********************/
    let hgm = ["H", "A", "N", "G", "M", "A", "N"];
    let hgArray = [hgm[0]];
    for (var i = 1; i < hgm.length; i++) {
        hgArray[i] = "_";
    }
    //loop
    let remainingLetters = word.length - 1;
    let remainingLetter = hgm.length;
    let currentHangmanPosition = 1;
    while (remainingLetters > 0 & remainingLetter > 0) {

        //show the array
        console.log(answerArray.join(" ").toUpperCase());
        console.log(hgArray.join(",").toUpperCase());

        let guess = prompt("guess a letter").toUpperCase();
        //add a letter if guessed correctly

        if (letters[word.length - remainingLetters] == guess) {
          answerArray[word.length - remainingLetters] = guess;
          remainingLetters--;
        }else{
          hgArray[currentHangmanPosition] = hgm[currentHangmanPosition];
          currentHangmanPosition++;
        }


        console.log(answerArray.join(" ").toUpperCase());
        console.log(hgArray.join(",").toUpperCase());
        if (remainingLetters === 0) {
            console.log("you win");
        } else if (currentHangmanPosition == hgm.length) {
            console.log("you lose");
        }
    }
}