diff --git a/src/game.js b/old/game.js similarity index 99% rename from src/game.js rename to old/game.js index 2f1fe64..6fd792b 100644 --- a/src/game.js +++ b/old/game.js @@ -32,7 +32,6 @@ ion.sound({ // words array located in words.js var word; var incercari; -var missArray; var charArray1; var charArray2; var missArray; diff --git a/old/index.html b/old/index.html new file mode 100644 index 0000000..59dc2b9 --- /dev/null +++ b/old/index.html @@ -0,0 +1,110 @@ + + + + + Spanzuratoarea + + + + + + + + + + + + + + + + +
+

Spanzuratoarea!

+ +
+ + + + + + + diff --git a/src/index.html b/src/index.html index d8f743f..f805a54 100644 --- a/src/index.html +++ b/src/index.html @@ -13,8 +13,9 @@ - - + + + diff --git a/src/js/hangman.js b/src/js/hangman.js new file mode 100644 index 0000000..911db1f --- /dev/null +++ b/src/js/hangman.js @@ -0,0 +1,124 @@ +var cheats = false; +/** + * A class for the old game called Hangman. + */ +function Hangman() { + var word; + var life; + var missedLetters; + var charArray1; + var charArray2; + + /** + * Used to initialize the game. + * @param {array} wordList An array of words used to play the game. + */ + this.newGame = function(wordList) { + word = wordList[Math.floor(Math.random() * wordList.length)]; + life = 6; + charArray1 = []; // word with stars + charArray2 = []; // plain word + missedLetters = []; + + // Fill Arrays + for (var i = 0; i < word.length; i++) { + charArray1[i] = '*' + } + + for (var i = 0; i < word.length; i++) { + charArray2[i] = word.charAt(i); + } + } + + /** + * Checks if the letter is in the word. + * @param {char} guess The letter. + */ + this.check = function(guess) { + guess = guess.toLowerCase(); + return word.indexOf(guess) > -1; + } + + /** + * Performs the necessary tasks if the letter is guessed. + * @param {char} guess Letter. + */ + this.guessed = function(guess) { + guess = guess.toLowerCase(); + + for (i = 0; i < charArray1.length; i++) { + if (charArray2[i] == guess) { + charArray1[i] = guess; + } + } + } + + /** + * Performs the necessary tasks in the letter is not guessed. + * @param {char} guess Letter. + */ + this.missed = function(guess) { + life--; + guess = guess.toLowerCase(); + var len = missedLetters.length; + if (!($.inArray(guess, missedLetters) > -1)) { + missedLetters[len] = guess; + } + } + + /** + * Checks if the game is finished. + * @return {boolean} Returns true if the game is won. + */ + this.gameIsFinished = function() { + var a = charArray1; + var b = charArray2; + + if (a === b) return true; + if (a == null || b == null) return false; + if (a.length != b.length) return false; + + for (var i = 0; i < a.length; ++i) { + if (a[i] !== b[i]) return false; + } + return true; + } + /** + * Checks the life. + * @return {int} Returns the tries the player has left. + */ + this.getLife = function() { + return life; + } + + this.setLife = function(life) { + if (cheats) life = life; + } + + /** + * Used to get the for for displaying after the game is over. + * @return {string} Returns the word. + */ + this.getWord = function() { + if (life == 0 && !cheats) + return word; + if (cheats) + return word; + } + + /** + * Placeholder for word. Eg: **** + * @return {array} Returns the word array. + */ + this.getWordArray = function() { + return charArray1; + } + + /** + * Get the letters that have been typed but are not the the word. + * @return {array} Get the missed letters. + */ + this.getMissedLetters = function () { + return missedLetters; + } +} diff --git a/src/js/main.js b/src/js/main.js new file mode 100644 index 0000000..2e7eb75 --- /dev/null +++ b/src/js/main.js @@ -0,0 +1,118 @@ +(function() { + //General + var h = new Hangman(); + var MOBILE_WIDH = 930; + var WINDOW_W = $(window).width(); + var mobileSiteUrl = "/m"; // relative path + var soundsFolder = "sound/sounds/"; + //Redirect to MobileSite + if (WINDOW_W <= MOBILE_WIDH) { + window.location.replace(mobileSiteUrl); + } + // Sound initialization + ion.sound({ + sounds: [{ + name: "button_tiny", + volume: 0.8, + preload: true + }, { + name: "ta_da", + volume: 0.8, + preload: true + }, { + name: "sad_trombone", + volume: 0.8, + preload: true + }], + volume: 0.5, + path: soundsFolder, + preload: true + }); + $(document).ready(function() { + var listaCuvinte = localStorage.getItem("listaCuvinte"); + if (listaCuvinte != null) { + $("select[name=lista_cuvinte]").val(listaCuvinte); + h.newGame(eval(listaCuvinte)); + } else { + h.newGame(animale_eu); + } + // Update page info + $('.word').html(h.getWordArray()); + $("#human").attr('src', "./img/" + h.getLife() + ".png"); + $('#incercari').html(h.getLife()); + $('.litera_msg2').html(); + $("select[name=lista_cuvinte]").on("change", function() { + localStorage.setItem("listaCuvinte", $(this).val()); + }).next().click(function() { + window.location.reload(); + }); + $('#game').fadeIn("slow"); + }); + $(document).on("keypress", function(e) { + var k = e.keyCode || e.which; + var key = String.fromCharCode(k).toLowerCase(); + ion.sound.play("button_tiny"); + $('.litera_msg').fadeIn('slow'); + $('.litera').html(key); + if (h.check(key)) { + $('.litera_msg2').html("aceasta litera se afla in cuvant."); + h.guessed(key); + $('.word').html(h.getWordArray()); + } else { + $('.litera_msg2').html("aceasta litera nu se afla in cuvant."); + h.missed(key); + if (h.getLife() > -1) { + $("#human").attr('src', "./img/" + h.getLife() + ".png"); + $('#incercari').html(h.getLife()); + $('.litere').html(h.getMissedLetters() + ""); + if (h.getLife() == 0) + gameOver(); + } + } + if (h.gameIsFinished()) { + gameWon(); + } + }); + + function gameWon() { + if (WINDOW_W <= MOBILE_WIDH) { + ion.sound.play("ta_da"); + alert("Felicita e tenersi per mano") + window.location.reload(); // mobile + return; + } + //alert("You're winner!"); + if (WINDOW_W > MOBILE_WIDH) $('#gamewon').fadeIn('slow'); + $(document).off(); // Detach keypress handler + ion.sound.play("ta_da"); // Play victory sound + $('#gamewon img').click(function() { + window.location.reload(); + }); + $(document).on("keypress", function(e) { + var key = e.keyCode || e.which; + if (key == 13) window.location.reload(); // Reset handlers :-) + }); + } + + function gameOver() { + if (WINDOW_W <= MOBILE_WIDH) { + ion.sound.play("sad_trombone"); + alert("Ai pierdut! CUVANT: " + h.getWord()); + window.location.reload(); // for mobile phones + return; // for slower mobile phones + } + $('#incercari_msg').html("Game Over!"); + $('#lose_word').html(h.getWord()); + if (WINDOW_W > MOBILE_WIDH) $('#gameover').fadeIn('slow'); + $(document).off(); // Detach keypress handler + ion.sound.play("sad_trombone"); + $('#gameover img').click(function() { + window.location.reload(); + }); + $(document).on("keypress", function(e) { + var key = e.keyCode || e.which; + if (key == 13) window.location.reload(); + }); + } +})(); + diff --git a/src/words.js b/src/js/words.js similarity index 100% rename from src/words.js rename to src/js/words.js