-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
110 lines (107 loc) · 4.77 KB
/
script.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
let game = (function() {
let words = ['madagaskar', 'shrek', 'herkules', 'zaplątani', 'nietykalni', 'iniemamocni', 'minionki', 'siedem', 'pianista', 'django', 'gladiator', 'służące', 'interstellar', 'zwierzogród', 'filadelfia', 'pokój', 'blow', 'seksmisja', 'iluzjonista', 'deadpool', 'uprowadzona', 'sherlock', 'narcos', 'wikingowie', 'suits', 'przyjaciele' ];
let randomIndex = Math.floor(Math.random() * words.length);
let gameWord = words[randomIndex];
let wordLength = gameWord.length;
let numberOfErrors = 6;
let userAnswers = [];
let drawBoard = () => {
for (let i=0; i<wordLength; i++) {
let div = document.createElement("div");
div.classList.add("board-section__field");
div.classList.add("class"+i);
document.getElementById("board").appendChild(div);
}
};
let checkLetter = (letter) => {
document.getElementById("letter").value = '';
if(letter.length>1) {
document.getElementById("error-message").innerHTML = letter +" to nie jedna litera!";
}
else {
for (let j=0; j<gameWord.length; j++) {
if(gameWord[j] === letter) {
userAnswers.push(letter);
let className = "class"+j;
document.getElementsByClassName(className)[0].innerHTML = letter;
}
}
if (gameWord.includes(letter) === false){
addPoints();
document.getElementById("used-letters").innerHTML += " "+letter;
if (numberOfErrors===0) {
document.getElementById("error-message").innerHTML = "Przegrywasz! " + "<i class='fas fa-frown'></i>" + " Poprawny tytuł to: " + gameWord;
blockInputs();
} else {
document.getElementById("error-message").innerHTML = "Brak litery " + letter + " w słowie!";
}
}
}
};
let getClue = () => {
let randomIndex = Math.floor(Math.random() * gameWord.length);
console.log(userAnswers);
if(userAnswers.indexOf(gameWord[randomIndex]) === -1) {
checkLetter(gameWord[randomIndex]);
addPoints();
} else {
getClue();
}
};
let checkWord = (word) => {
document.getElementById("word").value = '';
if (gameWord === word.toLowerCase()){
for (let j=0; j<gameWord.length; j++) {
let className = "class"+j;
document.getElementsByClassName(className)[0].innerHTML = gameWord.charAt(j);
}
document.getElementById("error-message").innerHTML = "Brawo! To prawidłowa odpowiedź! " + "<i class='fas fa-trophy'></i>";
document.getElementById("error-message").style.color = "#61AB00";
blockInputs();
} else {
document.getElementById("error-message").innerHTML = word + " nie jest poprawnym rozwiązaniem!" + " Przegrywasz! " + "<i class='fas fa-frown'></i>" + " Poprawny tytuł to: " + gameWord;
blockInputs();
}
};
let addPoints = () => {
numberOfErrors-=1;
document.getElementById("points").innerText = "Nieudane próby: "+numberOfErrors;
}
let blockInputs = () => {
document.getElementById('letter').disabled = true;
document.getElementById('word').disabled = true;
document.getElementById('check-letter').disabled = true;
document.getElementById('check-word').disabled = true;
}
return {
drawBoard: drawBoard,
checkLetter: checkLetter,
checkWord: checkWord,
numberOfErrors: numberOfErrors,
getClue: getClue
}
})();
game.drawBoard();
let btnLetter = document.getElementById("check-letter");
let inputLetter = document.getElementById("letter");
let btnWord = document.getElementById("check-word");
let inputWord = document.getElementById("word");
let btnNewGame = document.getElementById("btn-newgame");
let btnClue = document.getElementById("btn-get-clue");
document.getElementById("points").innerText += " "+game.numberOfErrors;
btnLetter.addEventListener('click', function() { game.checkLetter(inputLetter.value.toLowerCase()) });
btnWord.addEventListener('click', function() { game.checkWord(inputWord.value) });
btnClue.addEventListener('click', function() { game.getClue() });
btnNewGame.addEventListener('click', function() { location.reload() });
inputLetter.addEventListener('keyup', function(event){
event.preventDefault();
if (event.keyCode === 13) {
game.checkLetter(inputLetter.value.toLowerCase())
}
});
inputWord.addEventListener('keyup', function(event){
event.preventDefault();
if (event.keyCode === 13) {
game.checkWord(inputWord.value);
}
});