Changed around line 1
+ const words = ['gatos', 'perro', 'pulga', 'vacuna', 'zorro'];
+ let secretWord = words[Math.floor(Math.random() * words.length)];
+ let currentAttempt = '';
+ let attempts = [];
+ const maxAttempts = 6;
+
+ const gameBoard = document.getElementById('game-board');
+ const keyboard = document.getElementById('keyboard');
+ const message = document.getElementById('message');
+ const resetButton = document.getElementById('reset-button');
+
+ function createGameBoard() {
+ for (let i = 0; i < maxAttempts; i++) {
+ const row = document.createElement('div');
+ row.className = 'row';
+ for (let j = 0; j < 5; j++) {
+ const cell = document.createElement('div');
+ cell.className = 'cell';
+ row.appendChild(cell);
+ }
+ gameBoard.appendChild(row);
+ }
+ }
+
+ function createKeyboard() {
+ const keyboardLayout = [
+ ['q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p'],
+ ['a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l'],
+ ['Enter', 'z', 'x', 'c', 'v', 'b', 'n', 'm', '⌫']
+ ];
+
+ keyboardLayout.forEach(row => {
+ const keyboardRow = document.createElement('div');
+ keyboardRow.className = 'keyboard-row';
+ row.forEach(key => {
+ const keyButton = document.createElement('button');
+ keyButton.className = 'key';
+ keyButton.textContent = key;
+ keyButton.addEventListener('click', () => handleKeyPress(key));
+ keyboardRow.appendChild(keyButton);
+ });
+ keyboard.appendChild(keyboardRow);
+ });
+ }
+
+ function handleKeyPress(key) {
+ if (key === 'Enter') {
+ submitAttempt();
+ } else if (key === '⌫') {
+ currentAttempt = currentAttempt.slice(0, -1);
+ } else if (currentAttempt.length < 5) {
+ currentAttempt += key;
+ }
+ updateGameBoard();
+ }
+
+ function submitAttempt() {
+ if (currentAttempt.length === 5) {
+ attempts.push(currentAttempt);
+ if (currentAttempt === secretWord) {
+ showMessage('¡Felicidades! ¡Has ganado!');
+ endGame();
+ } else if (attempts.length === maxAttempts) {
+ showMessage(`¡Oh no! La palabra era ${secretWord}`);
+ endGame();
+ }
+ currentAttempt = '';
+ updateGameBoard();
+ updateKeyboard();
+ }
+ }
+
+ function updateGameBoard() {
+ const rows = gameBoard.querySelectorAll('.row');
+ rows.forEach((row, i) => {
+ const cells = row.querySelectorAll('.cell');
+ cells.forEach((cell, j) => {
+ const attempt = attempts[i];
+ if (attempt) {
+ cell.textContent = attempt[j] || '';
+ if (attempt[j] === secretWord[j]) {
+ cell.classList.add('correct');
+ } else if (secretWord.includes(attempt[j])) {
+ cell.classList.add('present');
+ } else {
+ cell.classList.add('absent');
+ }
+ } else if (i === attempts.length) {
+ cell.textContent = currentAttempt[j] || '';
+ } else {
+ cell.textContent = '';
+ cell.classList.remove('correct', 'present', 'absent');
+ }
+ });
+ });
+ }
+
+ function updateKeyboard() {
+ const keys = keyboard.querySelectorAll('.key');
+ keys.forEach(key => {
+ const letter = key.textContent;
+ if (secretWord.includes(letter)) {
+ if (secretWord.indexOf(letter) === currentAttempt.indexOf(letter)) {
+ key.classList.add('correct');
+ } else {
+ key.classList.add('present');
+ }
+ } else {
+ key.classList.add('absent');
+ }
+ });
+ }
+
+ function showMessage(msg) {
+ message.textContent = msg;
+ }
+
+ function endGame() {
+ keyboard.querySelectorAll('.key').forEach(key => key.disabled = true);
+ }
+
+ function resetGame() {
+ secretWord = words[Math.floor(Math.random() * words.length)];
+ currentAttempt = '';
+ attempts = [];
+ gameBoard.innerHTML = '';
+ keyboard.innerHTML = '';
+ message.textContent = '';
+ createGameBoard();
+ createKeyboard();
+ }
+
+ resetButton.addEventListener('click', resetGame);
+
+ createGameBoard();
+ createKeyboard();