hgman/js/index.js

130 lines
3.3 KiB
JavaScript
Raw Normal View History

2021-02-25 01:33:29 +03:00
'use strict';
// keyboard layouts
const keyboardLayouts = {
'usQwertyKeyboard' : 'abcdefghijklmnopqrstuvwxyz'.toUpperCase(),
'ruQwertyKeyboard' : 'йцукеёнгшщзхъфывапролджэячсмитьбю'.toUpperCase()
}
2021-02-25 02:15:04 +03:00
const words = [
'google', 'speed', 'design', 'forest', 'forever', 'love',
'horizon', 'defect'
];
2021-02-25 01:33:29 +03:00
// game variables
2021-02-25 02:15:04 +03:00
let gameWord = words[Math.floor(Math.random() * words.length)].toUpperCase();
2021-02-25 01:33:29 +03:00
let gameAnswered = new Array(gameWord.length + 1).join('-');
let lives = 6;
2021-02-25 08:44:48 +03:00
// init keyboard
2021-02-25 01:33:29 +03:00
function generateKeyboard(layout)
{
2021-02-25 08:44:48 +03:00
let keyboardHtmlStr = '';
2021-02-25 01:33:29 +03:00
layout.split('').forEach(function(letter)
{
2021-02-25 04:29:23 +03:00
keyboardHtmlStr = keyboardHtmlStr + '<button class="button clear is-marginless" id="' + letter +
2021-02-25 08:44:48 +03:00
'" onclick="letterClick(\'' + letter + '\')"> ' + letter + '</button>';
2021-02-25 01:33:29 +03:00
})
let keyboard = document.querySelector("#keyboard");
keyboard.classList.remove('is-hidden');
document.querySelector("#keyboard").innerHTML = keyboardHtmlStr;
}
2021-02-25 08:44:48 +03:00
// letter click action
function letterClick(letter)
2021-02-25 01:33:29 +03:00
{
2021-02-25 08:44:48 +03:00
let charButton = document.getElementById(letter);
2021-02-25 01:33:29 +03:00
charButton.disabled = true;
2021-02-25 08:44:48 +03:00
let indices = [];
let idx = gameWord.split('').indexOf(letter)
2021-02-25 01:33:29 +03:00
if (idx == -1)
{
2021-02-25 08:44:48 +03:00
wrongLetter();
updateStats('stLetterClick');
2021-02-25 01:33:29 +03:00
return;
}
while (idx != -1)
{
indices.push(idx);
idx = gameWord.split('').indexOf(letter, idx + 1);
}
indices.forEach(function(item, indices)
{
2021-02-25 08:44:48 +03:00
let wordArray = gameAnswered.split('')
2021-02-25 01:33:29 +03:00
wordArray[item] = letter;
gameAnswered = wordArray.join('');
document.querySelector("#word").innerHTML = gameAnswered;
2021-02-25 08:44:48 +03:00
updateStats('stCorrLetter');
2021-02-25 01:33:29 +03:00
})
if (gameAnswered.split('').indexOf('-') == -1)
{
2021-02-25 08:44:48 +03:00
updateStats('stWinWords');
2021-02-25 09:13:37 +03:00
document.querySelector('#keyboard').innerHTML = '\
<img src="https://icongr.am/clarity/happy-face.svg?size=128&color=28bd14">\
<h1 class="text-success">Your winner!!</h1>\
<button class="button error" onclick="document.location.reload(true);"\
>Restart game</button>';
2021-02-25 01:33:29 +03:00
}
}
2021-02-25 08:44:48 +03:00
// action by wrong letter
function wrongLetter()
2021-02-25 01:33:29 +03:00
{
if (lives <= 0)
{
2021-02-25 08:44:48 +03:00
updateStats('stTotalGames');
2021-02-25 09:13:37 +03:00
document.querySelector('#keyboard').innerHTML = '\
<img src="https://icongr.am/clarity/sad-face.svg?size=128&color=d43939">\
<h1 class="text-error">Your lose!!</h1>\
<button class="button error" onclick="document.location.reload(true);"\
>Restart game</button>';
2021-02-25 01:33:29 +03:00
}
lives -= 1;
document.querySelector("#lives").innerHTML = 'Lives ' + lives;
}
2021-02-25 08:44:48 +03:00
// display tab content
2021-02-25 01:33:29 +03:00
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;
2021-02-25 08:44:48 +03:00
if (pageName == pageStatistics) { updateStatsPage(); }
2021-02-25 01:33:29 +03:00
}
2021-02-25 08:44:48 +03:00
// game init
2021-02-25 01:33:29 +03:00
window.onload = function()
{
2021-02-25 08:44:48 +03:00
updateStats('stCheck');
2021-02-25 01:33:29 +03:00
generateKeyboard(keyboardLayouts['usQwertyKeyboard']);
2021-02-25 02:15:04 +03:00
document.querySelector('#lives').innerHTML = 'Lives ' + lives;
console.log('Word:', gameWord.toLowerCase());
2021-02-25 01:33:29 +03:00
document.querySelector('#word').innerHTML = gameAnswered;
2021-02-25 02:15:04 +03:00
2021-02-25 04:29:23 +03:00
if (window.screen.availWidth <= 599)
{
document.querySelector('#lives').classList.remove('error');
document.querySelector('#lives').classList.add('dark');
}
2021-02-25 01:33:29 +03:00
}