Descripción del Juego Wordle para un Programador
Wordle es un juego de lógica y palabras donde el jugador tiene que adivinar una palabra secreta de cinco letras en seis intentos. Después de cada intento, el juego proporciona retroalimentación indicando si las letras son correctas y están en la posición correcta (color verde), correctas pero en la posición incorrecta (color amarillo) o incorrectas (color gris).
Características clave para el desarrollo:
- Interfaz de Usuario (UI): Crear una cuadrícula de 6 filas y 5 columnas para mostrar los intentos de palabras.
- Entrada de Usuario: Permitir la entrada de letras a través del teclado o un teclado en pantalla.
- Validación: Comprobar si la palabra ingresada existe en un diccionario predefinido.
- Retroalimentación: Implementar lógica para colorear las letras según la precisión de la adivinanza.
- Gestión de Estado: Mantener el estado del juego entre intentos, incluyendo el seguimiento de intentos restantes y la palabra secreta.
- Responsividad: Asegurar que el juego sea usable en diferentes dispositivos y tamaños de pantalla.
- Accesibilidad: Asegurar que el juego sea accesible para usuarios con discapacidades.
Código del Juego Wordle en HTML5
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Wordle en HTML5</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #121213;
color: #ffffff;
font-family: Arial, sans-serif;
}
#game {
display: grid;
grid-template-rows: repeat(6, 50px);
grid-gap: 5px;
}
.row {
display: grid;
grid-template-columns: repeat(5, 50px);
grid-gap: 5px;
}
.tile {
width: 50px;
height: 50px;
border: 2px solid #3a3a3c;
display: flex;
justify-content: center;
align-items: center;
text-transform: uppercase;
font-size: 24px;
font-weight: bold;
}
.correct {
background-color: #6aaa64;
border: none;
color: white;
}
.present {
background-color: #c9b458;
border: none;
color: white;
}
.absent {
background-color: #3a3a3c;
border: none;
color: white;
}
</style>
</head>
<body>
<div id="game"></div>
<script>
const WORD_LENGTH = 5;
const MAX_ATTEMPTS = 6;
const WORD_LIST = ["APPLE", "BRAVE", "CRANE", "DREAM", "EAGLE", "FRAME", "GRAPE", "HOUSE", "INPUT", "JUMBO", "KNIFE", "LEMON", "MOUSE", "NERVE", "OCEAN", "PRIDE", "QUICK", "ROBIN", "SOUND", "TABLE", "UNITY", "VIRUS", "WHISK", "XENON", "YOUNG", "ZEBRA"];
let secretWord = WORD_LIST[Math.floor(Math.random() * WORD_LIST.length)];
let currentRow = 0;
let currentCol = 0;
let grid = [];
const game = document.getElementById('game');
for(let i=0;i<MAX_ATTEMPTS;i++) {
const row = document.createElement('div');
row.classList.add('row');
grid.push([]);
for(let j=0;j<WORD_LENGTH;j++) {
const tile = document.createElement('div');
tile.classList.add('tile');
grid[i].push(tile);
row.appendChild(tile);
}
game.appendChild(row);
}
document.addEventListener('keydown', handleKey);
function handleKey(e) {
if (e.key === 'Enter') {
submitWord();
} else if (e.key === 'Backspace') {
deleteLetter();
} else if (/^[a-zA-Z]$/.test(e.key)) {
addLetter(e.key.toUpperCase());
}
}
function addLetter(letter) {
if (currentCol < WORD_LENGTH && currentRow < MAX_ATTEMPTS) {
grid[currentRow][currentCol].textContent = letter;
currentCol++;
}
}
function deleteLetter() {
if (currentCol > 0 && currentRow < MAX_ATTEMPTS) {
currentCol--;
grid[currentRow][currentCol].textContent = '';
}
}
function submitWord() {
if (currentCol !== WORD_LENGTH || currentRow >= MAX_ATTEMPTS) return;
let guess = '';
for(let i=0;i<WORD_LENGTH;i++) {
guess += grid[currentRow][i].textContent;
}
if (!WORD_LIST.includes(guess)) {
alert("Palabra no válida");
return;
}
const secret = secretWord;
let secretLetterCount = {};
for(let letter of secret) {
secretLetterCount[letter] = (secretLetterCount[letter] || 0) + 1;
}
for(let i=0;i<WORD_LENGTH;i++) {
let tile = grid[currentRow][i];
let letter = guess[i];
if (letter === secret[i]) {
tile.classList.add('correct');
secretLetterCount[letter]--;
}
}
for(let i=0;i<WORD_LENGTH;i++) {
let tile = grid[currentRow][i];
let letter = guess[i];
if (!tile.classList.contains('correct')) {
if (secret.includes(letter) && secretLetterCount[letter] > 0) {
tile.classList.add('present');
secretLetterCount[letter]--;
} else {
tile.classList.add('absent');
}
}
}
if (guess === secret) {
alert("¡Felicidades! Has adivinado la palabra.");
window.removeEventListener('keydown', handleKey);
return;
}
currentRow++;
currentCol = 0;
if (currentRow === MAX_ATTEMPTS) {
alert(`Has perdido. La palabra era: ${secret}`);
window.removeEventListener('keydown', handleKey);
}
}
</script>
</body>
</html>
Protocolo Paso a Paso
1. Crea un archivo HTML:
- Nombre: `index.html`
2. Copia y pega el siguiente código en `index.html`:
```html
<!DOCTYPE html>
<html lang="es">
<head>
<meta charset="UTF-8">
<title>Wordle en HTML5</title>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #121213;
color: #ffffff;
font-family: Arial, sans-serif;
}
#game {
display: grid;
grid-template-rows: repeat(6, 50px);
grid-gap: 5px;
}
.row {
display: grid;
grid-template-columns: repeat(5, 50px);
grid-gap: 5px;
}
.tile {
width: 50px;
height: 50px;
border: 2px solid #3a3a3c;
display: flex;
justify-content: center;
align-items: center;
text-transform: uppercase;
font-size: 24px;
font-weight: bold;
}
.correct {
background-color: #6aaa64;
border: none;
color: white;
}
.present {
background-color: #c9b458;
border: none;
color: white;
}
.absent {
background-color: #3a3a3c;
border: none;
color: white;
}
</style>
</head>
<body>
<div id="game"></div>
<script>
const WORD_LENGTH = 5;
const MAX_ATTEMPTS = 6;
const WORD_LIST = ["APPLE", "BRAVE", "CRANE", "DREAM", "EAGLE", "FRAME", "GRAPE", "HOUSE", "INPUT", "JUMBO", "KNIFE", "LEMON", "MOUSE", "NERVE", "OCEAN", "PRIDE", "QUICK", "ROBIN", "SOUND", "TABLE", "UNITY", "VIRUS", "WHISK", "XENON", "YOUNG", "ZEBRA"];
let secretWord = WORD_LIST[Math.floor(Math.random() * WORD_LIST.length)];
let currentRow = 0;
let currentCol = 0;
let grid = [];
const game = document.getElementById('game');
for(let i=0;i<MAX_ATTEMPTS;i++) {
const row = document.createElement('div');
row.classList.add('row');
grid.push([]);
for(let j=0;j<WORD_LENGTH;j++) {
const tile = document.createElement('div');
tile.classList.add('tile');
grid[i].push(tile);
row.appendChild(tile);
}
game.appendChild(row);
}
document.addEventListener('keydown', handleKey);
function handleKey(e) {
if (e.key === 'Enter') {
submitWord();
} else if (e.key === 'Backspace') {
deleteLetter();
} else if (/^[a-zA-Z]$/.test(e.key)) {
addLetter(e.key.toUpperCase());
}
}
function addLetter(letter) {
if (currentCol < WORD_LENGTH && currentRow < MAX_ATTEMPTS) {
grid[currentRow][currentCol].textContent = letter;
currentCol++;
}
}
function deleteLetter() {
if (currentCol > 0 && currentRow < MAX_ATTEMPTS) {
currentCol--;
grid[currentRow][currentCol].textContent = '';
}
}
function submitWord() {
if (currentCol !== WORD_LENGTH || currentRow >= MAX_ATTEMPTS) return;
let guess = '';
for(let i=0;i<WORD_LENGTH;i++) {
guess += grid[currentRow][i].textContent;
}
if (!WORD_LIST.includes(guess)) {
alert("Palabra no válida");
return;
}
const secret = secretWord;
let secretLetterCount = {};
for(let letter of secret) {
secretLetterCount[letter] = (secretLetterCount[letter] || 0) + 1;
}
for(let i=0;i<WORD_LENGTH;i++) {
let tile = grid[currentRow][i];
let letter = guess[i];
if (letter === secret[i]) {
tile.classList.add('correct');
secretLetterCount[letter]--;
}
}
for(let i=0;i<WORD_LENGTH;i++) {
let tile = grid[currentRow][i];
let letter = guess[i];
if (!tile.classList.contains('correct')) {
if (secret.includes(letter) && secretLetterCount[letter] > 0) {
tile.classList.add('present');
secretLetterCount[letter]--;
} else {
tile.classList.add('absent');
}
}
}
if (guess === secret) {
alert("¡Felicidades! Has adivinado la palabra.");
window.removeEventListener('keydown', handleKey);
return;
}
currentRow++;
currentCol = 0;
if (currentRow === MAX_ATTEMPTS) {
alert(`Has perdido. La palabra era: ${secret}`);
window.removeEventListener('keydown', handleKey);
}
}
</script>
</body>
</html>
-
Guarda el archivo.
-
Abre index.html en tu navegador web.
-
Empieza a jugar escribiendo letras en el teclado y presionando Enter para enviar la palabra.