// static/js/game.js document.addEventListener("DOMContentLoaded", () => { const TIEMPO_TOTAL = 60; // segundos let segundosRestantes = TIEMPO_TOTAL; let puntuacion = 0; const barraTiempo = document.getElementById("barra-tiempo"); const tiempoTexto = document.getElementById("tiempo-texto"); const contadorAciertos = document.getElementById("contador-aciertos"); const enunciado = document.getElementById("enunciado"); const inputRespuesta = document.getElementById("input-respuesta"); const feedback = document.getElementById("feedback"); const botonFinal = document.getElementById("boton-final"); const verRankingBtn = document.getElementById("ver-ranking"); const jugarOtraBtn = document.getElementById("jugar-otra"); const formNombre = document.getElementById("form-nombre"); const inputNombre = document.getElementById("input-nombre"); const guardarNombreBtn = document.getElementById("guardar-nombre"); const mensajeGuardar = document.getElementById("mensaje-guardar"); let numeroA = 0; let numeroB = 0; let temporizadorInterval = null; // Genera un par aleatorio (dos números entre 10 y 99) function generarPregunta() { numeroA = Math.floor(Math.random() * 90) + 10; // 10…99 numeroB = Math.floor(Math.random() * 90) + 10; enunciado.textContent = `¿Cuánto es ${numeroA} × ${numeroB}?`; inputRespuesta.value = ""; inputRespuesta.focus(); } // Inicia el cronómetro y actualiza cada segundo function iniciarTemporizador() { barraTiempo.style.width = "100%"; tiempoTexto.textContent = `${segundosRestantes} segundos restantes`; temporizadorInterval = setInterval(() => { segundosRestantes--; if (segundosRestantes < 0) { clearInterval(temporizadorInterval); terminarJuego(); return; } // Actualiza texto y barra de progreso tiempoTexto.textContent = `${segundosRestantes} segundos restantes`; const porcentaje = (segundosRestantes / TIEMPO_TOTAL) * 100; barraTiempo.style.width = `${porcentaje}%`; // Cambiar color de barra si queda poco tiempo if (segundosRestantes <= 10) { barraTiempo.classList.remove("bg-success"); barraTiempo.classList.add("bg-danger"); } else if (segundosRestantes <= 30) { barraTiempo.classList.remove("bg-success"); barraTiempo.classList.add("bg-warning"); } }, 1000); } // Función que maneja el envío de la respuesta al presionar Enter inputRespuesta.addEventListener("keydown", (e) => { if (e.key === "Enter" && segundosRestantes > 0) { e.preventDefault(); validarRespuesta(); } }); function validarRespuesta() { const valor = parseInt(inputRespuesta.value); const correcta = numeroA * numeroB; if (!isNaN(valor)) { if (valor === correcta) { puntuacion++; contadorAciertos.textContent = puntuacion; feedback.innerHTML = `Correcto!`; } else { feedback.innerHTML = `Incorrecto. Era ${correcta}.`; } // Mostrar feedback por 800ms y luego limpiarlo setTimeout(() => { feedback.textContent = ""; }, 800); } else { feedback.innerHTML = `Ingresa un número válido.`; setTimeout(() => { feedback.textContent = ""; }, 800); } generarPregunta(); } function terminarJuego() { // Deshabilitar input inputRespuesta.disabled = true; feedback.innerHTML = `