-
Notifications
You must be signed in to change notification settings - Fork 0
/
scripts.js
231 lines (184 loc) · 5.76 KB
/
scripts.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
const $gridTd = Array.from(document.querySelectorAll(".grid-td"));
const $tr = document.querySelectorAll('tr');
const $keyboard = document.querySelector(".keyboard");
const $keyboardKey = Array.from(document.querySelectorAll('.key'));
let actual = 0;
let column = 0;
let wordInput = ""
const maxRow = $tr.length - 1;
let row = 0;
const keywords = ["abeja","zorro","araña","burro","cabra","cebra","gallo","mosca","raton","perro","pulpo","erizo","cerdo","tigre","cisne","corzo","ganso","gamba","hiena","huron","koala","lince","llama","mamut","morsa","oruga","ostra","oveja","potro","pulga","tejon","yegua"];
const allowLetters = ["q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l", "ñ", "z","x","c","v","b","n","m"];
const maxValue = keywords.length - 1;
const maxColumn = 5
const btnClose = document.getElementById('btn-close');
const message = document.querySelector('.message');
const gameOver = document.querySelector('.message--game-over');
const closeDialogGameOver = gameOver.querySelector('.close');
const messageVictory = document.querySelector('.message--victory');
const showInstruction = (sessionStorage.getItem('instruction') !== null)? sessionStorage.getItem('instruction') : null;
let wordDiscover = false;
if(!showInstruction){
message.classList.remove('hidden');
sessionStorage.setItem('instruction',true)
}
const restarGame = () =>{
location.reload();
}
const getRandomWorld = (maxValue) =>{
const randomIndex = Math.round(Math.random() * maxValue);
return keywords[randomIndex];
}
const showVictory = () =>{
messageVictory.classList.remove('hidden');
}
const deletedListeners = () =>{
$keyboard.removeEventListener('click',responseClick);
document.body.removeEventListener('keydown',responseKeyboard);
}
const keyword = getRandomWorld(maxValue);
console.log(` Palabra clave ${keyword}`);
const endGame = () =>{
const $answerText = gameOver.querySelector('.answer__text');
$answerText.textContent = keyword;
setTimeout(()=>{
gameOver.classList.remove('hidden');
},1000)
deletedListeners();
}
const incrementRow = () =>{
if(row === maxRow){
console.log("No hay mas filas");
if(!wordDiscover) endGame();
return
}
row++;
}
const incrementColumn = () =>{
if(column === maxColumn) return;
column++;
}
const validateColumn = () =>{
const maxRow = $tr[row].childElementCount - 1;
if(column > maxRow){
console.log("No hay mas columnas");
return false;
}
return true;
}
const addLetter = (letter) =>{
if(validateColumn()){
$tr[row].children[column].classList.add('active-border');
$tr[row].children[column].classList.add('animateLetteer');
$tr[row].children[column].textContent = letter;
incrementColumn();
}
}
const decreaseColumn = () =>{
if(column === 0) return;
column--;
}
const resetColumn = () =>{
column = 0;
}
const deletedLetter = () =>{
if(column <= 0) return;
$tr[row].children[column-1].classList.remove('active-border');
$tr[row].children[column-1].classList.remove('animateLetteer');
$tr[row].children[column-1].textContent = "";
decreaseColumn();
}
const victory = () =>{
if(keyword === wordInput){
wordDiscover = true;
setTimeout(()=>{
showVictory();
},1000)
deletedListeners();
return true;
}
return false;
}
const paintKeyboard = ({keyStyle, letter}) =>{
$keyboardKey.forEach(($key)=>{
if($key.textContent.toLowerCase() === letter){
$key.classList.add(keyStyle);
}
})
}
const checkLetters = () =>{
const word = Array.from($tr[row].querySelectorAll(".grid-td"));
const final = keyword.length - 1;
for(let i = 0; i <= final; i++){
wordInput+= word[i].textContent;
if(keyword[i] === word[i].textContent){
word[i].classList.add('letter-correct')
paintKeyboard({
keyStyle: 'letter-correct',
letter: word[i].textContent
});
} else if(keyword.includes(word[i].textContent)){
word[i].classList.add('letter-is')
paintKeyboard({
keyStyle: 'letter-is',
letter: word[i].textContent
})
}else{
word[i].classList.add('letter-not')
paintKeyboard({
keyStyle: 'letter-not',
letter: word[i].textContent
})
}
}
victory();
wordInput = '';
incrementRow();
resetColumn();
}
const check = () =>{
if(column < $tr[0].childElementCount) return;
checkLetters();
}
const responseClick = (event) =>{
const target = event.target;
if(target.matches(".key-deleted") || target.matches(".key-deleted > * ")){
deletedLetter();;
}
if(target.matches(".key-enter") || target.matches(".key-enter > *")){
check();
}
if(target.matches(".key")){
const letter = target.textContent.toLowerCase();
if(allowLetters.includes(letter)){
addLetter(letter);
}
}
}
const responseKeyboard = (event) =>{
const key = event.key.toLowerCase();
if(key === "backspace"){
deletedLetter();
}
if(key === "enter"){
check()
}
if(allowLetters.includes(key)){
addLetter(key);
}
}
document.addEventListener('click',(event)=>{
const target = event.target;
if(target.matches('.close')){
const message = target.closest('.message');
message.classList.add('hidden');
}
if(target.matches('.btngame')){
restarGame();
}
})
$keyboard.addEventListener('click',responseClick);
document.body.addEventListener('keydown',responseKeyboard)
document.ondblclick = function(e) {
e.preventDefault();
}