-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
261 lines (225 loc) · 9.43 KB
/
script.js
File metadata and controls
261 lines (225 loc) · 9.43 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
let targetWord = '';
let currentGuess = '';
let currentRow = 0;
let gameEnded = false;
let validWords = [];
const board = document.getElementById('board');
const keyboard = document.getElementById('keyboard');
const message = document.getElementById('message');
// List of common 5-letter Spanish words to pick a random target word
const commonWords = [
'AMIGO', 'BARCO', 'CIELO', 'DAMAS', 'FUEGO', 'GRANO',
'HIELO', 'JOVEN', 'LENTO', 'MANGO', 'NIEVE', 'PARED',
'QUESO', 'RELOJ', 'SILLA', 'TARDE', 'VACIO', 'YERNO',
'ZORRO', 'ARBOL', 'BESOS', 'COSER', 'DUCHA', 'FINCA',
'GATOS', 'HUEVO', 'JAULA', 'LUCHA', 'MENTA', 'NARIZ',
// Added more words for local validation
'PIANO', 'FELIZ', 'LIBRO', 'COMER', 'BELLO', 'CAMPO',
'MUJER', 'HABER', 'JUGAR', 'NUEVO', 'MEJOR', 'HACER',
'MUNDO', 'CASA', 'VIVIR', 'TENER', 'AQUEL', 'PODER',
'VERDE', 'BEBER', 'CASAR', 'NUBES', 'CINCO', 'ANDAR',
// Common Spanish words that API might not recognize
'NOCHE', 'LECHE', 'PERRO', 'GATO', 'PLAYA', 'DULCE',
'CORTO', 'LARGO', 'COCHE', 'VIAJE', 'PUNTO', 'CLASE',
'COLOR', 'FORMA', 'JARRO', 'BURRO', 'BANCO', 'TIRAR',
'PLATO', 'TECHO', 'FALSO', 'PARAR', 'SACAR', 'MIRAR'
];
async function isValidWord(word) {
try {
// Update API status display
document.getElementById('api-status').textContent = 'API Status: Checking...';
document.getElementById('api-word-check').textContent = `Last word checked: ${word}`;
// First check our local dictionary for common words
if (commonWords.includes(word)) {
document.getElementById('api-status').textContent = 'API Status: Using local dictionary';
document.getElementById('api-response').textContent = 'API Response: Word found in local dictionary';
return true;
}
// Try WordsAPI instead - more reliable Spanish dictionary
// Using proxy URL to avoid CORS issues on GitHub Pages
const response = await fetch(`https://api.dictionaryapi.dev/api/v2/entries/es/${word.toLowerCase()}`);
if (response.ok) {
const data = await response.json();
document.getElementById('api-status').textContent = 'API Status: Response received';
document.getElementById('api-response').textContent = `API Response: Word found in dictionary`;
// If we got a response with meanings, the word exists
if (data && data.length > 0 && data[0].meanings && data[0].meanings.length > 0) {
return true;
}
return false;
} else if (response.status === 404) {
// 404 means word not found in dictionary
document.getElementById('api-status').textContent = 'API Status: Word not found';
document.getElementById('api-response').textContent = `API Response: Word not in dictionary`;
return false;
}
document.getElementById('api-status').textContent = 'API Status: Response error';
document.getElementById('api-response').textContent = `API Response: HTTP ${response.status}`;
// Fallback to local dictionary if API fails
return commonWords.includes(word);
} catch (error) {
console.error('Error checking word validity:', error);
document.getElementById('api-status').textContent = 'API Status: Failed';
document.getElementById('api-response').textContent = `API Response: ${error.message}`;
// Fallback - if API fails, check if word is in our common words list
return commonWords.includes(word);
}
}
async function loadWords() {
try {
// Set a random target word from our predefined list
targetWord = commonWords[Math.floor(Math.random() * commonWords.length)];
console.log('Target word loaded:', targetWord);
// We don't need to preload all valid words, as we'll check them dynamically
showMessage('¡Juego listo! Intenta adivinar la palabra de 5 letras.');
} catch (error) {
console.error('Error setting up game:', error);
showMessage('Error al iniciar el juego. Por favor, recarga la página.');
}
}
function createBoard() {
for (let i = 0; i < 6; i++) {
for (let j = 0; j < 5; j++) {
const letterBox = document.createElement('div');
letterBox.className = 'letter-box';
letterBox.id = `row-${i}-col-${j}`;
board.appendChild(letterBox);
}
}
}
function createKeyboard() {
const rows = [
'QWERTYUIOP',
'ASDFGHJKL',
'ZXCVBNM'
];
rows.forEach(row => {
const rowElement = document.createElement('div');
rowElement.className = 'keyboard-row';
row.split('').forEach(key => {
const keyElement = document.createElement('div');
keyElement.className = 'key';
keyElement.id = `key-${key}`;
keyElement.textContent = key;
keyElement.addEventListener('click', () => handleKeyPress(key));
rowElement.appendChild(keyElement);
});
keyboard.appendChild(rowElement);
});
const enterKey = document.createElement('div');
enterKey.className = 'key';
enterKey.id = 'key-ENTER';
enterKey.textContent = 'ENTER';
enterKey.addEventListener('click', () => handleKeyPress('ENTER'));
const deleteKey = document.createElement('div');
deleteKey.className = 'key';
deleteKey.id = 'key-DELETE';
deleteKey.textContent = 'DELETE';
deleteKey.addEventListener('click', () => handleKeyPress('DELETE'));
const lastRow = document.createElement('div');
lastRow.className = 'keyboard-row';
lastRow.appendChild(enterKey);
lastRow.appendChild(deleteKey);
keyboard.appendChild(lastRow);
}
async function handleKeyPress(key) {
if (gameEnded) return;
if (key === 'ENTER') {
if (currentGuess.length === 5) {
// Check if the word is valid using the dictionary API
const isValid = await isValidWord(currentGuess);
if (isValid) {
checkGuess();
return;
} else {
showMessage('La palabra no es válida');
}
} else {
showMessage('La palabra debe tener 5 letras');
}
return;
}
if (key === 'DELETE') {
currentGuess = currentGuess.slice(0, -1);
updateBoard();
return;
}
if (currentGuess.length < 5 && /^[A-Z]$/.test(key)) {
currentGuess += key;
updateBoard();
}
}
function handlePhysicalKeyPress(event) {
const key = event.key.toUpperCase();
if (key === 'ENTER' || key === 'BACKSPACE' || /^[A-Z]$/.test(key)) {
handleKeyPress(key === 'BACKSPACE' ? 'DELETE' : key);
}
}
function updateBoard() {
for (let i = 0; i < 5; i++) {
const letterBox = document.getElementById(`row-${currentRow}-col-${i}`);
letterBox.textContent = currentGuess[i] || '';
}
}
function checkGuess() {
const guess = currentGuess;
const targetLetters = targetWord.split('');
const guessLetters = guess.split('');
const letterCount = {};
for (let i = 0; i < 5; i++) {
letterCount[targetLetters[i]] = (letterCount[targetLetters[i]] || 0) + 1;
}
// First pass: mark correct letters
for (let i = 0; i < 5; i++) {
const letterBox = document.getElementById(`row-${currentRow}-col-${i}`);
const letter = guess[i];
if (letter === targetWord[i]) {
letterBox.classList.add('correct');
updateKeyboard(letter, 'correct');
letterCount[letter]--;
}
}
// Second pass: mark present and absent letters
for (let i = 0; i < 5; i++) {
const letterBox = document.getElementById(`row-${currentRow}-col-${i}`);
const letter = guess[i];
if (letter !== targetWord[i]) {
if (targetLetters.includes(letter) && letterCount[letter] > 0) {
letterBox.classList.add('present');
updateKeyboard(letter, 'present');
letterCount[letter]--;
} else {
letterBox.classList.add('absent');
updateKeyboard(letter, 'absent');
}
}
}
if (guess === targetWord) {
showMessage('¡Felicidades! Adivinaste la palabra.');
gameEnded = true;
} else {
currentRow++;
currentGuess = '';
if (currentRow === 6) {
gameEnded = true;
showMessage(`¡Juego terminado! La palabra era ${targetWord}`);
}
}
}
function updateKeyboard(letter, status) {
const keyElement = document.getElementById(`key-${letter}`);
if (status === 'correct') {
keyElement.className = 'key correct';
} else if (status === 'present' && !keyElement.classList.contains('correct')) {
keyElement.className = 'key present';
} else if (status === 'absent' && !keyElement.classList.contains('correct') && !keyElement.classList.contains('present')) {
keyElement.className = 'key absent';
}
}
function showMessage(text) {
message.textContent = text;
}
createBoard();
createKeyboard();
loadWords();
document.addEventListener('keydown', handlePhysicalKeyPress);