Skip to content

Instantly share code, notes, and snippets.

@mathildathompson
Created April 12, 2014 23:48
Show Gist options
  • Save mathildathompson/10562534 to your computer and use it in GitHub Desktop.
Save mathildathompson/10562534 to your computer and use it in GitHub Desktop.
$(document).ready(function (){
//Declare all variables inside document.ready up here, avoid having them dispersed throughout the programme;
var words = ['beagle', 'pug', 'daschund', 'kelpie'];
var $guess = $('#guess');
var $guesses = $('#guesses');
var $solution = $('#solution h1');
var $startAgain = $('#startAgain');
var correctGuess = [];
var incorrectGuess = [];
var selectedWord = _.sample(words).split('');
var counter = 8;
console.log(selectedWord);
//Put event listenser here;
$startAgain.on('click', function(){
//Think is makes more sense to reset the varibales here instead of at the top of the start game function (even better could be to create a reset game function);
correctGuess = [];
incorrectGuess.length = 0;
counter = 8;
$guesses.html('No incorrect guesses... yet');
startGame.matchLetter();
});
//enter a letter as a guess
$guess.keyup(function () {
var letter = $(this).val();
$(this).val('');
startGame.matchLetter(letter);
//Check here that they have only input one letter;
}); //end guess listener
var startGame = {
// check the .guess(letter) against the selectedWord[letters] - if it is in the selectedWord then add to the correct array, if not, add to the incorrect array
matchLetter: function(letter){
if (_.contains(selectedWord, letter)) {
correctGuess.push(letter);
console.log('Correct guesses', correctGuess);
this.showSolution(correctGuess);
} else {
incorrectGuess.push(letter);
console.log('Incorrect guesses', incorrectGuess, '. Am I being run twice');
counter--; //Decrement the counter when the use guesses incorrectly;
$('#guesses').html('Incorrect guesses: ' + incorrectGuess);
};
},//end matchLetter
showSolution: function(correctGuess){
console.log('Solution', correctGuess);
$solution.html('');
$.each(selectedWord, function(index, letter) {
if (_.contains(correctGuess, letter)){
$solution.append(letter + ' ');
} else {
$solution.append(' _ ');
};
});
//You need to implemenent functionality to check whether the user has won/lost and update the number of gusees they have left;
}, //end showSolution
updatePage: function(){
},
checkWin: function(){
},
checkLoose: function(){
}
}; //end startGame
startGame.showSolution();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment