const deckCards=["image2.png","image2.png","image3.png","image3.png","image4.png","image4.png","image5.png","image5.png"],deck=document.querySelector(".deck");let opened=[],matched=[];const modal=document.getElementById("modal"),reset=document.querySelector(".reset-btn"),playAgain=document.querySelector(".play-again-btn"),movesCount=document.querySelector(".moves-counter");let moves=0;const star=document.getElementById("star-rating").querySelectorAll(".star");let starCount=3;const timeCounter=document.querySelector(".timer");let time,minutes=0,seconds=0,timeStart=!1;function shuffle(e){let t,n,o=e.length;for(;0!==o;)n=Math.floor(Math.random()*o),o-=1,t=e[o],e[o]=e[n],e[n]=t;return e}function startGame(){const e=shuffle(deckCards);for(let t=0;t<e.length;t++){const n=document.createElement("LI");n.classList.add("card");const o=document.createElement("IMG");n.appendChild(o),o.setAttribute("src","./img/"+e[t]+"?raw=true"),o.setAttribute("alt","image of vault boy from fallout"),deck.appendChild(n)}}function removeCard(){for(;deck.hasChildNodes();)deck.removeChild(deck.firstChild)}function timer(){time=setInterval((function(){seconds++,60===seconds&&(minutes++,seconds=0),timeCounter.innerHTML="<i class='fa fa-hourglass-start'></i> Χρόνος: "+minutes+" Λεπτά "+seconds+" Δευτερόλεπτα"}),1e3)}function stopTime(){clearInterval(time)}function resetEverything(){stopTime(),timeStart=!1,seconds=0,minutes=0,timeCounter.innerHTML="<i class='fa fa-hourglass-start'></i> Timer: 00:00",star[1].firstElementChild.classList.add("fa-star"),star[2].firstElementChild.classList.add("fa-star"),starCount=3,moves=0,movesCount.innerHTML=0,matched=[],opened=[],removeCard(),startGame()}function movesCounter(){movesCount.innerHTML++,moves++}function starRating(){14===moves&&(star[2].firstElementChild.classList.remove("fa-star"),starCount--),18===moves&&(star[1].firstElementChild.classList.remove("fa-star"),starCount--)}function compareTwo(){2===opened.length&&(document.body.style.pointerEvents="none"),2===opened.length&&opened[0].src===opened[1].src?match():2===opened.length&&opened[0].src!=opened[1].src&&noMatch()}function match(){setTimeout((function(){opened[0].parentElement.classList.add("match"),opened[1].parentElement.classList.add("match"),matched.push(...opened),document.body.style.pointerEvents="auto",winGame(),opened=[]}),600),movesCounter(),starRating()}function noMatch(){setTimeout((function(){opened[0].parentElement.classList.remove("flip"),opened[1].parentElement.classList.remove("flip"),document.body.style.pointerEvents="auto",opened=[]}),700),movesCounter(),starRating()}function AddStats(){const e=document.querySelector(".modal-content");for(let t=1;t<=3;t++){const t=document.createElement("p");t.classList.add("stats"),e.appendChild(t)}let t=e.querySelectorAll("p.stats");t[0].innerHTML="Χρόνος παιχνιδιού: "+minutes+" λεπτά και "+seconds+" δευτερόλεπτα",t[1].innerHTML="Κινήσεις: "+moves,t[2].innerHTML="Βαθμολογία: "+starCount+" από 3"}function displayModal(){const e=document.getElementsByClassName("close")[0];modal.style.display="block",e.onclick=function(){modal.style.display="none"},window.onclick=function(e){e.target==modal&&(modal.style.display="none")}}function winGame(){8===matched.length&&(stopTime(),AddStats(),displayModal())}startGame(),deck.addEventListener("click",(function(e){"LI"===e.target.nodeName&&(!1===timeStart&&(timeStart=!0,timer()),e.target.classList.add("flip"),0!==opened.length&&1!==opened.length||opened.push(e.target.firstElementChild),compareTwo())})),reset.addEventListener("click",resetEverything),playAgain.addEventListener("click",(function(){modal.style.display="none",resetEverything()}));