1 line
5.7 KiB
JavaScript
1 line
5.7 KiB
JavaScript
"use strict";const pageCredits='\t<p class="text-center">Font F5.6 by\t\t<a href="http://dotcolon.net/">DOT COLON</a>\t</p>\t\t<p class="text-center">UI by\t\t<a href="https://jenil.github.io/chota/">chota.css</a>\t</p>';"use strict";const pageStatistics='\t<table>\t\t<caption>User statistics</caption>\t\t<thead>\t\t\t<tr>\t\t\t\t<th>Name</th>\t\t\t\t<th>Value</th>\t\t\t</tr>\t\t</thead>\t\t<tbody>\t\t\t<tr>\t\t\t\t<th>Total games</th>\t\t\t\t<th id="stTotalGames">...</th>\t\t\t</tr>\t\t\t<tr>\t\t\t\t<th>Guess words</th>\t\t\t\t<th id="stWinWords">...</th>\t\t\t</tr>\t\t\t<tr>\t\t\t\t<th>% Win</th>\t\t\t\t<th id="stWinPercentage">...</th>\t\t\t</tr>\t\t\t<tr>\t\t\t\t<th>Total letters clicked</th>\t\t\t\t<th id="stLetterClick">...</th>\t\t\t</tr>\t\t\t<tr>\t\t\t\t<th>Correctly guessed letters</th>\t\t\t\t<th id="stCorrLetter">...</th>\t\t\t</tr>\t\t\t<tr>\t\t\t\t<th>% correctly letters</th>\t\t\t\t<th id="stWinLetterPercent">...</th>\t\t\t</tr>\t\t</tbody>\t</table>\t<p class="text-center">\t\t<buttton class="button error" onclick="localStorage.clear();">Clear statistics</buttton>\t</p>';function updateStatsPage(){let stTotalGames=localStorage.getItem("stTotalGames");let stWinWords=localStorage.getItem("stWinWords");let stWinPercentage=localStorage.getItem("stWinPercentage");let stLetterClick=localStorage.getItem("stLetterClick");let stCorrLetter=localStorage.getItem("stCorrLetter");let stWinLetterPercent=localStorage.getItem("stWinLetterPercent");document.querySelector("#stTotalGames").innerHTML=stTotalGames;document.querySelector("#stWinWords").innerHTML=stWinWords;document.querySelector("#stWinPercentage").innerHTML=Math.round(stWinWords/stTotalGames*100);document.querySelector("#stLetterClick").innerHTML=stLetterClick;document.querySelector("#stCorrLetter").innerHTML=stCorrLetter;document.querySelector("#stWinLetterPercent").innerHTML=Math.round(stCorrLetter/stLetterClick*100)}"use strict";function updateStats(statParameter){let stTotalGames=JSON.parse(localStorage.getItem("stTotalGames"));let stWinWords=JSON.parse(localStorage.getItem("stWinWords"));let stLetterClick=JSON.parse(localStorage.getItem("stLetterClick"));let stCorrLetter=JSON.parse(localStorage.getItem("stCorrLetter"));switch(statParameter){case"stTotalGames":localStorage.setItem("stTotalGames",stTotalGames+1);break;case"stWinWords":localStorage.setItem("stTotalGames",stTotalGames+1);localStorage.setItem("stWinWords",stWinWords+1);break;case"stLetterClick":localStorage.setItem("stLetterClick",stLetterClick+1);break;case"stCorrLetter":localStorage.setItem("stLetterClick",stLetterClick+1);localStorage.setItem("stCorrLetter",stCorrLetter+1);break;case"stCheck":if(stTotalGames==null){localStorage.setItem("stTotalGames",0)}if(stWinWords==null){localStorage.setItem("stWinWords",0)}if(stLetterClick==null){localStorage.setItem("stLetterClick",0)}if(stCorrLetter==null){localStorage.setItem("stCorrLetter",0)}break}}"use strict";const keyboardLayouts={usQwertyKeyboard:"abcdefghijklmnopqrstuvwxyz".toUpperCase(),ruQwertyKeyboard:"йцукеёнгшщзхъфывапролджэячсмитьбю".toUpperCase()};const words=["google","speed","design","forest","forever","love","horizon","defect"];let gameWord=words[Math.floor(Math.random()*words.length)].toUpperCase();let gameAnswered=new Array(gameWord.length+1).join("-");let lives=6;function generateKeyboard(layout){let keyboardHtmlStr="";layout.split("").forEach(function(letter){keyboardHtmlStr=keyboardHtmlStr+'<button class="button clear is-marginless" id="'+letter+'" onclick="letterClick(\''+letter+"')\"> "+letter+"</button>"});let keyboard=document.querySelector("#keyboard");keyboard.classList.remove("is-hidden");document.querySelector("#keyboard").innerHTML=keyboardHtmlStr}function letterClick(letter){let charButton=document.getElementById(letter);charButton.disabled=true;let indices=[];let idx=gameWord.split("").indexOf(letter);if(idx==-1){wrongLetter();updateStats("stLetterClick");return}while(idx!=-1){indices.push(idx);idx=gameWord.split("").indexOf(letter,idx+1)}indices.forEach(function(item,indices){let wordArray=gameAnswered.split("");wordArray[item]=letter;gameAnswered=wordArray.join("");document.querySelector("#word").innerHTML=gameAnswered;updateStats("stCorrLetter")});if(gameAnswered.split("").indexOf("-")==-1){updateStats("stWinWords");document.querySelector("#keyboard").innerHTML='\t\t<img src="https://icongr.am/clarity/happy-face.svg?size=128&color=28bd14">\t\t<h1 class="text-success">Your winner!!</h1>\t\t<button class="button error" onclick="document.location.reload(true);"\t\t>Restart game</button>'}}function wrongLetter(){if(lives<=0){updateStats("stTotalGames");document.querySelector("#word").innerHTML=gameWord;document.querySelector("#word").classList.add("text-success");document.querySelector("#keyboard").innerHTML='\t\t<img src="https://icongr.am/clarity/sad-face.svg?size=128&color=d43939">\t\t<h1 class="text-error">Your lose!!</h1>\t\t<button class="button error" onclick="document.location.reload(true);"\t\t>Restart game</button>'}lives-=1;document.querySelector("#lives").innerHTML="Lives "+lives}function showPage(element,pageName){["gameButton","statsButton","creditsButton"].forEach(function(item){document.querySelector("#"+item).classList.remove("active")});document.querySelector("#"+element.id).classList.add("active");document.querySelector("#content").innerHTML=pageName;if(pageName==pageStatistics){updateStatsPage()}}window.onload=function(){updateStats("stCheck");generateKeyboard(keyboardLayouts["usQwertyKeyboard"]);document.querySelector("#lives").innerHTML="Lives "+lives;console.log("Word:",gameWord.toLowerCase());document.querySelector("#word").innerHTML=gameAnswered;if(window.screen.availWidth<=599){document.querySelector("#lives").classList.remove("error");document.querySelector("#lives").classList.add("dark")}}; |