如何在不重设分数的情况下重设Hangman游戏?

时间:2018-12-14 19:23:37

标签: javascript

我正在从事这个子手游戏,但是在使“损失” /“获胜”计数器无法正常工作时遇到了麻烦。

看来我有损失计数器可以工作,但是猜测的数目会变成负面的。它将把游戏计为损失,但不会重置。

当我设置获胜计数时,它计算的是每次用户点击而不是整个单词。到目前为止,我在游戏中尝试的所有操作均失败了。

在保持得分的情况下,有人输了或赢了之后,如何重置游戏?

function getItem() {
var array = [
    {
        answer: "harvey",
        hint: "Thank you Harvey, I prefer you too."
    },

    {
        answer: "titanic",
        hint: "Jack, I’m flying!"
    },

    {
        answer: "jaws",
        hint: "You’re Gonna Need A Bigger Boat…"
    },

    {
        answer: "rocky",
        hint: "ADRIAAAAAAAAAAAN!!!!!!!!"
    },

    {
        answer: "casablanca",
        hint: "We'll always have Paris."
    },

    {
        answer: "braveheart",
        hint: "They may take away our lives, but they'll never take our freedom!"
    },

    {
        answer: "goodfellas",
        hint: "As far back as I can remember, I always wanted to be a gangster."
    },

    {
        answer: "diehard",
        hint: "Nine million terrorists in the world and I gotta kill one with feet smaller than my sister."
    },

    {
        answer: "chinatown",
        hint: "Forget it, Jake, it's Chinatown."
    },

    {
        answer: "psycho",
        hint: "A boy's best friend is his mother.",
    }

]


var randomNumber = Math.floor(Math.random() * array.length);
return array[randomNumber];



  };


var randomThing = getItem()
document.getElementById("hint-text").innerHTML = ("Here is your 
hint.... " + randomThing.hint.italics());

var blankSpacesAnswer = [];
var blanksAndSuccesses = document.getElementById("blankspaces-text");
for (var i = 0; i < randomThing.answer.length; i++) {
blankSpacesAnswer[i] = "_";
console.log(blankSpacesAnswer);
blanksAndSuccesses.innerHTML = ("Your word: " + 
blankSpacesAnswer.join(" "));
};

var currentWord;
var guessingWord = [];
var winCounter = 0;
var lossCounter = 0;
var numGuesses = 12;
var letterGuessed = "";
var wrongGuesses = [];
var remainingLetters = randomThing.answer.length;

document.onkeyup = function (event) {
var userGuess = event.key;
console.log(userGuess);
var answerLetterArray = randomThing.answer.split("");
guessingWord.length = answerLetterArray.length;
console.log(answerLetterArray);

if (remainingLetters > 0) {
    var guessesEl = document.getElementById("blankspaces-text")
    guessesEl.innerHTML = blankSpacesAnswer.join(" ")

    var notFound = false;
    for (var i = 0; i < answerLetterArray.length; i++) {
        console.log(answerLetterArray[i]);

        if (userGuess === answerLetterArray[i]) {
            blankSpacesAnswer[i] = userGuess
        }

        else if (answerLetterArray[i] !== userGuess) {
            notFound = true;
        }

        blanksAndSuccesses.innerHTML = ("Your word:   " + 
   blankSpacesAnswer.join(" "));
        document.getElementById("usser-guesses-text").innerHTML = 
    ("Your guesses: " + userGuess);
        document.getElementById("wins-text").textContent = ("Wins: " + 
   winCounter);
    }

    if (notFound === true) {
        numGuesses--;
    }
    if (numGuesses === 0)
        lossCounter++;
   }


   document.getElementById("guesses-left").textContent = ("Number of 
   guesses: " + numGuesses);
   document.getElementById("losses-text").textContent = ("Losses: " + 
   lossCounter);

   };

1 个答案:

答案 0 :(得分:1)

造成麻烦的根本原因似乎是,游戏一开始就没有真正的设置,赢或输的想法。

因此,当您跟踪猜测状态时,就不会跟踪游戏本身的状态。

我建议将代码分成几部分:

  1. 设置
  2. 初始化
  3. 运行/循环
  4. 比赛结束

设置阶段应设置用户界面,键处理程序等。

初始化开始游戏(选择一个随机单词,为您想要的内容设置猜测数量,等等)

运行/循环将仅等待您的输入并跟踪游戏状态。如果发生winlose的情况,它应相应地结束游戏。

到达游戏结束时,您可以返回初始化阶段来重置/重启游戏。

如果将代码放入函数setup()initialize()run()win() / lose()中,则可以在需要时调用这些函数/需要切换游戏状态。

更新A solution for your hangman game (and some fixes)