From 3cde193e2680d4d71f2a77b1651edfa50ac5123a Mon Sep 17 00:00:00 2001 From: vonBarbarosa Date: Sun, 21 Feb 2016 21:30:35 -0300 Subject: [PATCH] Implement chances counting/game over (but still ugly design for this) --- index.html | 1 + js/functions.js | 62 ++++++++++++++++++++++++++++++++++++++----------- js/wordsDB.js | 6 ++++- 3 files changed, 54 insertions(+), 15 deletions(-) diff --git a/index.html b/index.html index d876356..62a22a7 100644 --- a/index.html +++ b/index.html @@ -25,6 +25,7 @@

Forca Fonética

+
diff --git a/js/functions.js b/js/functions.js index f6ca4f8..cdc403c 100644 --- a/js/functions.js +++ b/js/functions.js @@ -8,21 +8,37 @@ var currentWord = { var app = { +chances: { + //const value + MAX: 6, + current: 0 +}, + init: function() { - this.bindEvents(); + app.bindEvents(); app.newGame(); }, //listener for the buttons bindEvents: function() { $(".btn-phon").on("click", function(){ - $(this).addClass("disabled"); - var letter = $(this).text(); - app.putLetter(letter); - }); + //tries to fit letter, if no match, chances-- + if (!app.putLetter(letter)){ + app.loseOneChance(); + }; + //checks if game is over + if (app.chancesAreOver()){ + app.gameOver(); + } + //tests victory and acts accordingly + else if (app.victory()){ + app.congratulate(); + app.newGame() + } + }); }, //turns string into array of phonetic chars @@ -138,8 +154,9 @@ writePhon: function(){ }; }, -//tests new symbol input and refreshes guessing word +//tests new symbol input and refreshes guessing word; returns true if there is any match putLetter: function(letter){ + var match = false; var newWordState = new Array(); //loop for each different phonetic form @@ -150,6 +167,7 @@ putLetter: function(letter){ if (currentWord.answered[i] == "_"){ if (letter == currentWord.phonology[j][i]) { newWordState.push(letter); + match = true; } else{ newWordState.push("_"); @@ -166,11 +184,7 @@ putLetter: function(letter){ //prints on proper place #campo-foco2 app.writePhon(); - //tests victory and acts accordingly - if (app.victory(currentWord.answered)){ - app.congratulate(); - app.newGame() - } + return match; }, //shows congratulations message @@ -187,10 +201,27 @@ congratulate: function(){ alert(message); }, +//returns true if chances are already over +chancesAreOver: function(){ + return (this.chances.current<= 0); +}, + +//game over procedure +gameOver: function(){ + + alert("Tente novamente") + app.newGame(); +}, + +loseOneChance: function(){ + this.chances.current--; + $("#chances").text(this.chances.current); +}, + //tests victory, returns true or false -victory: function(newWordState){ +victory: function(){ //if no more blank spaces - if (newWordState.indexOf("_") === -1){ + if (currentWord.answered.indexOf("_") === -1){ return true; } else{ @@ -198,8 +229,11 @@ victory: function(newWordState){ } }, -//functions for seting a new game +//functions for setting a new game newGame: function(){ + //resets chances + this.chances.current = this.chances.MAX; + $("#chances").text(this.chances.current); app.resetbuttons(); app.getNewWord(); app.writeWord(); diff --git a/js/wordsDB.js b/js/wordsDB.js index 720a859..2874550 100644 --- a/js/wordsDB.js +++ b/js/wordsDB.js @@ -1,3 +1,8 @@ +/* + * This file creates the words DataBase and shuffles it + */ + +// https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm function shuffleArray(array) { for (var i = array.length - 1; i > 0; i--) { var j = Math.floor(Math.random() * (i + 1)); @@ -8,7 +13,6 @@ function shuffleArray(array) { return array; } - var wordsDB = [ { //agora a.ˈgɔ.rə