So I've already asked a few questions regarding this Hangman game (and have gotten awesome answers), but I've been continually tripped up by my "score keeper". The game almost does everything I need it too, but a "win" will not be logged until after a random key is pressed AFTER the entire word has been filled in...
<!DOCTYPE html>
<html>
<head>
<title>Hangman</title>
</head>
<body>
<h1>Hangman!</h1>
<p>
<font size="+3"><span id="answer"></span></font>
</p>
<p>Lives: <span id="counter"></span></p>
<p id="wrongGuesses"></p>
<p>Wins: <span id="wins"></span></p>
<p>Losses: <span id="losses"></span></p>
<script type="text/javascript">
var word;
var guess; //user guess
var letters = []; //correctly guessed letters
var wrongLetters = []; //incorrectly guessed letters
var counter; //counts correct letters
var losses = 0;
var wins = 0;
document.getElementById("losses").innerHTML = losses;
document.getElementById("wins").innerHTML = wins;
var wordList = ["cat", "dog", "wolf", "laser", "apple"]; //FILL LIST LATER!!
//randomly chooses a word from the array and replaces letters with underscores
function start() {
word = wordList[Math.floor(Math.random() * wordList.length)];
counter = 7;
document.getElementById("counter").innerHTML = counter;
for (i = 0; i < word.length; i++) {
letters[i] = "__";
}
document.getElementById("answer").innerHTML = letters.join(" ");
console.log(word);
}
//checks if letter is in the word or not
function checkLetter() {
document.onkeyup = function(event) {
guess = event.key.toLowerCase();
//var found = false;
for (i = 0; i < word.length; i++) {
if (guess === word[i]) {
letters[i] = guess;
document.getElementById("answer").innerHTML = letters.join(" ");
//found = true;
}
}
//wrong letters go into the wrongLetters array and are displayed
//if (found) return;
if (wrongLetters.indexOf(guess) < 0) {
wrongLetters.push(guess);
document.getElementById("wrongGuesses").innerHTML = wrongLetters.join(" ");
//every wrong guess subtracts one from the counter
counter--;
console.log(counter);
document.getElementById("counter").innerHTML = counter;
//when counter reaches 0 it's Game Over
//+1 to the losses if 7 words are missed
if (counter > 0 && letters.join("") === word) { //THE ISSUE
document.getElementById("wins").innerHTML = wins + 1;
console.log(wins);
confirm("YOU WIN! Play Again?");
wins++;
counter = 7;
letters = [ ];
wrongLetters = [ ];
start();
}
else if (counter === 0) {
document.getElementById("losses").innerHTML = losses + 1;
console.log(losses);
confirm("YOU LOOSE... play again?"); {
losses++;
counter = 7;
letters = [];
wrongLetters = [];
start();
}
}
}
}
}
start();
checkLetter();
</script>
</body>
</html>
Here's what I have so far and here is a jsfiddle link: http://ift.tt/2xen3zk I know the main issue is with the found bool and the return, but I can't seem to find a way to work around it. If anyone has any insight it would be greatly appreciated and I apologize in advanced to anyone who I have already bothered with this. THANKS!!
Aucun commentaire:
Enregistrer un commentaire