2021-02-24 22:54:16 +03:00
|
|
|
"use strict";
|
|
|
|
|
2017-02-17 04:34:12 +03:00
|
|
|
var words = ['google', 'speed', 'window', 'horizon'];
|
|
|
|
|
|
|
|
var lives = 6;
|
|
|
|
var gameWord = words[Math.floor(Math.random() * words.length)].toUpperCase();
|
2017-02-26 01:29:28 +03:00
|
|
|
var gameAnswered = new Array(gameWord.length + 1).join('-');
|
2017-02-17 04:34:12 +03:00
|
|
|
|
2017-02-26 02:04:29 +03:00
|
|
|
var keyboardLayouts = {
|
|
|
|
'usQwertyKeyboard' : 'abcdefghijklmnopqrstuvwxyz'.toUpperCase(),
|
|
|
|
'ruQwertyKeyboard' : 'йцукеёнгшщзхъфывапролджэячсмитьбю'.toUpperCase()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Функция генерации html кода клавиатуры
|
|
|
|
function generateKeyboard(layout)
|
|
|
|
{
|
|
|
|
var htmlKeyboardStr = '';
|
|
|
|
|
|
|
|
keyboardLayouts[layout].split('').forEach(function(word)
|
|
|
|
{
|
|
|
|
htmlKeyboardStr = htmlKeyboardStr + '<button id="' + word +
|
|
|
|
'" onclick="offChar(\'' + word + '\')">' + word + '</button>';
|
|
|
|
})
|
|
|
|
|
|
|
|
document.getElementById("keyboard").innerHTML = htmlKeyboardStr;
|
|
|
|
}
|
|
|
|
|
2017-02-17 04:34:12 +03:00
|
|
|
window.onload = function()
|
|
|
|
{
|
2017-02-26 02:04:29 +03:00
|
|
|
generateKeyboard('usQwertyKeyboard');
|
|
|
|
|
2017-02-26 01:29:28 +03:00
|
|
|
console.log(gameWord) // for DEBUG
|
2017-02-17 04:34:12 +03:00
|
|
|
document.getElementById("WORD").innerHTML = gameAnswered;
|
|
|
|
document.getElementById("lives").innerHTML = 'Lives ' + lives;
|
2017-02-26 01:29:28 +03:00
|
|
|
|
|
|
|
// init storage
|
|
|
|
var storageNames = ['gamesWon', 'wordsAnswered', 'gamesFail'];
|
|
|
|
storageNames.forEach(function(item)
|
|
|
|
{
|
|
|
|
if(null == localStorage.getItem(item))
|
|
|
|
localStorage.setItem(item, 0);
|
|
|
|
})
|
2017-02-17 04:34:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function wrong()
|
|
|
|
{
|
2017-02-26 01:29:28 +03:00
|
|
|
if (lives <= 0)
|
|
|
|
{
|
|
|
|
var gamesFail = localStorage.getItem('gamesFail');
|
|
|
|
localStorage.setItem('gamesFail', parseInt(gamesFail) + 1);
|
|
|
|
alert('You dead');
|
2021-02-24 22:45:09 +03:00
|
|
|
document.location.reload(true);
|
2017-02-26 01:29:28 +03:00
|
|
|
}
|
|
|
|
|
2017-02-17 04:34:12 +03:00
|
|
|
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)
|
2017-02-26 01:29:28 +03:00
|
|
|
|
2017-02-17 04:34:12 +03:00
|
|
|
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;
|
2017-02-26 01:29:28 +03:00
|
|
|
var wordsAnswered = localStorage.getItem('wordsAnswered');
|
|
|
|
localStorage.setItem('wordsAnswered', parseInt(wordsAnswered) + 1);
|
2017-02-17 04:34:12 +03:00
|
|
|
})
|
2017-02-26 01:29:28 +03:00
|
|
|
|
|
|
|
if (gameAnswered.split('').indexOf('-') == -1)
|
|
|
|
{
|
|
|
|
var gamesWon = localStorage.getItem('gamesWon');
|
|
|
|
localStorage.setItem('gamesWon', parseInt(gamesWon) + 1);
|
|
|
|
alert('You Win');
|
2021-02-24 22:45:09 +03:00
|
|
|
document.location.reload(true);
|
2017-02-26 01:29:28 +03:00
|
|
|
}
|
2017-02-17 04:34:12 +03:00
|
|
|
}
|