Skip to content

Instantly share code, notes, and snippets.

@mathildathompson
Created April 7, 2014 06:45
Show Gist options
  • Save mathildathompson/10015754 to your computer and use it in GitHub Desktop.
Save mathildathompson/10015754 to your computer and use it in GitHub Desktop.
$(document).ready(function (){
var words = ['javascript', 'processing', 'programming', 'hypertext', 'responsive'];
var correctGuess = [];
var incorrectGuess = [];
var indexes = [];
var $secretWord = $('#secretWord');
var $wrongGuess = $('#wrongGuesses');
selectedWord = _.sample(words).split('');
console.log(selectedWord);
$('.guess').keyup(function(event) {
letter = $(this).val();
if (letter.length > 1){
event.stopPropagation()
// print an error message if the user input is longer than 1 character
alert("You entered " + letter.length + " letters! Please try again with only one.");
} else{
addGuess(letter);
}
$(this).val('');
});
var addGuess = function(singleLetter){
if (_(selectedWord).include(singleLetter)) {
correctGuess.push(singleLetter);
var letterGuess = '';
_(selectedWord).each(function(wordLetter){
if (_(correctGuess).include(wordLetter)){
letterGuess += ('<span>' + wordLetter + '</span>');
} else {
letterGuess += ('<span> _ </span>');
}
});
$secretWord.html(letterGuess);
checkWin()
} else {
incorrectGuess.push(singleLetter);
//Display the wrong guesses on the screen.
//You dont need to empty here, you can simple replace the html;
$wrongGuess.html('<span>' + "Incorrect guesses: " + incorrectGuess + '</span>');
}
displayImages()
checkLoose();
}
var checkWin = function(){
return; //Fill this in to check if the user has won!
}
var checkLoose = function(){
//Limits the number of wrong guesses to 6
if (incorrectGuess.length >= 6 ){
alert("Y U NO LIKE MY GAME?!")
} else{
return;
}
}
var displayImages = function(){
//Display images one by one with the index and displays it at the same location.
_(incorrectGuess).each(function(){
var image = $(".hangman");
$(image[incorrectGuess.length]).show().attr("src", image[incorrectGuess.length + 1].src);
$(image[incorrectGuess.length + 1]).css({"margin-left": -304+"px"});
});
}
//Show the whole word when the user clicks on the give up button.
$('#giveUp').on("click", function(){
/* prints the whole word without the commas */
var word = String(selectedWord).replace(/,/g,'');
$revealWord = $('#secretWord').empty();
$revealWord.append('<span>' + word + '</span>');
});
//Resets the game when the user clicks on the reset button.
$('#reset').on("click", function(){
location.reload();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment