From 6541d2ca70e29b91096e17013e5fb0646641835b Mon Sep 17 00:00:00 2001 From: Alexander Popov Date: Thu, 25 Feb 2021 01:33:29 +0300 Subject: [PATCH] make game logic --- _media/js/game.js | 113 ++++++++++++++++++++++++++++++++++++++++++++++ game.old.js | 96 --------------------------------------- index.html | 8 ++-- 3 files changed, 117 insertions(+), 100 deletions(-) create mode 100644 _media/js/game.js delete mode 100644 game.old.js diff --git a/_media/js/game.js b/_media/js/game.js new file mode 100644 index 0000000..8b5be20 --- /dev/null +++ b/_media/js/game.js @@ -0,0 +1,113 @@ +'use strict'; + +// keyboard layouts +const keyboardLayouts = { + 'usQwertyKeyboard' : 'abcdefghijklmnopqrstuvwxyz'.toUpperCase(), + 'ruQwertyKeyboard' : 'йцукеёнгшщзхъфывапролджэячсмитьбю'.toUpperCase() +} + +const pageCredits = '\ +

Font F5.6 by\ + DOT COLON\ +

\ +'; + +const pageStatistics = ''; + +// game variables +let gameWord = 'speed'.toUpperCase(); +let gameAnswered = new Array(gameWord.length + 1).join('-'); +let lives = 6; + +// Функция генерации html кода клавиатуры +function generateKeyboard(layout) +{ + var keyboardHtmlStr = ''; + + layout.split('').forEach(function(letter) + { + keyboardHtmlStr = keyboardHtmlStr + ''; + }) + + let keyboard = document.querySelector("#keyboard"); + keyboard.classList.remove('is-hidden'); + document.querySelector("#keyboard").innerHTML = keyboardHtmlStr; +} + +function offChar(letter) +{ + var charButton = document.getElementById(letter); + charButton.disabled = true; + + var indices = []; + var idx = gameWord.split('').indexOf(letter) + + if (idx == -1) + { + wrong(); + return; + } + + while (idx != -1) + { + indices.push(idx); + idx = gameWord.split('').indexOf(letter, idx + 1); + } + + indices.forEach(function(item, indices) + { + var wordArray = gameAnswered.split('') + wordArray[item] = letter; + gameAnswered = wordArray.join(''); + document.querySelector("#word").innerHTML = gameAnswered; + var wordsAnswered = localStorage.getItem('wordsAnswered'); + localStorage.setItem('wordsAnswered', parseInt(wordsAnswered) + 1); + }) + + if (gameAnswered.split('').indexOf('-') == -1) + { + var gamesWon = localStorage.getItem('gamesWon'); + localStorage.setItem('gamesWon', parseInt(gamesWon) + 1); + alert('You Win'); + document.location.reload(true); + } +} + +function wrong() +{ + if (lives <= 0) + { + var gamesFail = localStorage.getItem('gamesFail'); + localStorage.setItem('gamesFail', parseInt(gamesFail) + 1); + alert('You dead'); + document.location.reload(true); + } + + lives -= 1; + document.querySelector("#lives").innerHTML = 'Lives ' + lives; +} + + +function showPage(element, pageName) +{ + // alert(event.srcElement.id); + + ['gameButton', 'statsButton', 'creditsButton'].forEach(function(item) + { + document.querySelector("#" + item).classList.remove('active'); + }); + document.querySelector("#" + element.id).classList.add('active'); + + document.querySelector("#content").innerHTML = pageName; +} + + + +// game +window.onload = function() +{ + generateKeyboard(keyboardLayouts['usQwertyKeyboard']); + + document.querySelector('#word').innerHTML = gameAnswered; +} diff --git a/game.old.js b/game.old.js deleted file mode 100644 index 43f6f3e..0000000 --- a/game.old.js +++ /dev/null @@ -1,96 +0,0 @@ -"use strict"; - -var words = ['google', 'speed', 'window', 'horizon']; - -var lives = 6; -var gameWord = words[Math.floor(Math.random() * words.length)].toUpperCase(); -var gameAnswered = new Array(gameWord.length + 1).join('-'); - -var keyboardLayouts = { - 'usQwertyKeyboard' : 'abcdefghijklmnopqrstuvwxyz'.toUpperCase(), - 'ruQwertyKeyboard' : 'йцукеёнгшщзхъфывапролджэячсмитьбю'.toUpperCase() -} - -// Функция генерации html кода клавиатуры -function generateKeyboard(layout) -{ - var htmlKeyboardStr = ''; - - keyboardLayouts[layout].split('').forEach(function(word) - { - htmlKeyboardStr = htmlKeyboardStr + ''; - }) - - document.getElementById("keyboard").innerHTML = htmlKeyboardStr; -} - -window.onload = function() -{ - generateKeyboard('usQwertyKeyboard'); - - console.log(gameWord) // for DEBUG - document.getElementById("WORD").innerHTML = gameAnswered; - document.getElementById("lives").innerHTML = 'Lives ' + lives; - - // init storage - var storageNames = ['gamesWon', 'wordsAnswered', 'gamesFail']; - storageNames.forEach(function(item) - { - if(null == localStorage.getItem(item)) - localStorage.setItem(item, 0); - }) -} - -function wrong() -{ - if (lives <= 0) - { - var gamesFail = localStorage.getItem('gamesFail'); - localStorage.setItem('gamesFail', parseInt(gamesFail) + 1); - alert('You dead'); - document.location.reload(true); - } - - lives -= 1; - document.getElementById("lives").innerHTML = 'Lives ' + lives; -} - -function offChar(char) -{ - var charButton = document.getElementById(char); - charButton.disabled = true; - - var indices = []; - var idx = gameWord.split('').indexOf(char) - - if (idx == -1) - { - wrong(); - return; - } - - while (idx != -1) - { - indices.push(idx); - idx = gameWord.split('').indexOf(char, idx + 1); - } - - indices.forEach(function(item, indices) - { - var wordArray = gameAnswered.split('') - wordArray[item] = char; - gameAnswered = wordArray.join(''); - document.getElementById("WORD").innerHTML = gameAnswered; - var wordsAnswered = localStorage.getItem('wordsAnswered'); - localStorage.setItem('wordsAnswered', parseInt(wordsAnswered) + 1); - }) - - if (gameAnswered.split('').indexOf('-') == -1) - { - var gamesWon = localStorage.getItem('gamesWon'); - localStorage.setItem('gamesWon', parseInt(gamesWon) + 1); - alert('You Win'); - document.location.reload(true); - } -} diff --git a/index.html b/index.html index 92bbe2d..92e2c3d 100644 --- a/index.html +++ b/index.html @@ -27,14 +27,14 @@
-
+

Loading word...