Añadido juego de las banderas.
This commit is contained in:
141
banderas/script.js
Normal file
141
banderas/script.js
Normal file
@@ -0,0 +1,141 @@
|
||||
// Lista completa con país y siglas según ISO 3166-1 alpha-2
|
||||
const countryList = [
|
||||
{ name: "España", code: "ES" }, { name: "Francia", code: "FR" }, { name: "Alemania", code: "DE" }, { name: "Italia", code: "IT" },
|
||||
{ name: "Portugal", code: "PT" }, { name: "Reino Unido", code: "GB" }, { name: "Estados Unidos", code: "US" }, { name: "Canadá", code: "CA" },
|
||||
{ name: "México", code: "MX" }, { name: "Brasil", code: "BR" }, { name: "Argentina", code: "AR" }, { name: "Chile", code: "CL" },
|
||||
{ name: "Japón", code: "JP" }, { name: "China", code: "CN" }, { name: "Australia", code: "AU" }, { name: "Rusia", code: "RU" },
|
||||
{ name: "Sudáfrica", code: "ZA" }, { name: "Nueva Zelanda", code: "NZ" }, { name: "India", code: "IN" }, { name: "Egipto", code: "EG" },
|
||||
{ name: "Grecia", code: "GR" }, { name: "Turquía", code: "TR" }, { name: "Ucrania", code: "UA" }, { name: "Suecia", code: "SE" },
|
||||
{ name: "Noruega", code: "NO" }, { name: "Dinamarca", code: "DK" }, { name: "Finlandia", code: "FI" }, { name: "Irlanda", code: "IE" },
|
||||
{ name: "Bélgica", code: "BE" }, { name: "Polonia", code: "PL" }, { name: "Suiza", code: "CH" }, { name: "Países Bajos", code: "NL" }
|
||||
];
|
||||
|
||||
function getFlagEmoji(code) {
|
||||
// Las banderas se generan a partir de letras usando unicode, no por siglas textuales
|
||||
// Solo funcionan para países con código alpha-2 y script latino
|
||||
if (!code || code.length !== 2) return "";
|
||||
return String.fromCodePoint(
|
||||
...code.toUpperCase().split("").map(c => 0x1F1E6 + c.charCodeAt(0) - 65)
|
||||
);
|
||||
}
|
||||
|
||||
// Para elegir N países aleatorios distintos con bandera
|
||||
function pickRandomCountries(list, n) {
|
||||
const chosen = [];
|
||||
const used = {};
|
||||
while (chosen.length < n && list.length > 0) {
|
||||
let idx = Math.floor(Math.random() * list.length);
|
||||
let c = list[idx];
|
||||
let flag = getFlagEmoji(c.code);
|
||||
if (flag && !used[c.code]) {
|
||||
chosen.push({name: c.name, code: c.code, flag});
|
||||
used[c.code] = true;
|
||||
}
|
||||
list.splice(idx,1);
|
||||
}
|
||||
return chosen;
|
||||
}
|
||||
|
||||
let flagsDiv = document.getElementById('flags');
|
||||
let countriesDiv = document.getElementById('countries');
|
||||
let statusDiv = document.getElementById('status');
|
||||
let scoreSpan = document.getElementById('score-value');
|
||||
let scoreTotal = document.getElementById('score-total');
|
||||
let restartBtn = document.getElementById('restart-btn');
|
||||
|
||||
let pairs = [], flags = [], countries = [], selectedFlag = null, selectedCountry = null, score = 0, totalPairs = 12;
|
||||
|
||||
function shuffle(arr) {
|
||||
for (let i = arr.length-1; i>0; i--) {
|
||||
const j = Math.floor(Math.random() * (i+1));
|
||||
[arr[i], arr[j]] = [arr[j], arr[i]];
|
||||
}
|
||||
}
|
||||
|
||||
function startGame() {
|
||||
let fullList = countryList.slice(); // copia
|
||||
pairs = pickRandomCountries(fullList, totalPairs);
|
||||
flags = pairs.map(o=>o);
|
||||
countries = pairs.map(o=>o);
|
||||
shuffle(flags); shuffle(countries);
|
||||
score = 0;
|
||||
scoreSpan.textContent = score;
|
||||
scoreTotal.textContent = pairs.length;
|
||||
selectedFlag = selectedCountry = null;
|
||||
renderFlags();
|
||||
renderCountries();
|
||||
statusDiv.textContent = 'Empareja todas las banderas con su país';
|
||||
}
|
||||
|
||||
function renderFlags() {
|
||||
flagsDiv.innerHTML = '';
|
||||
flags.forEach((p, i) => {
|
||||
const d = document.createElement('div');
|
||||
d.className = 'flag';
|
||||
d.textContent = p.flag;
|
||||
d.setAttribute('tabindex', 0);
|
||||
d.onclick = () => selectFlag(i);
|
||||
if (p.matched) d.classList.add('matched');
|
||||
if (selectedFlag === i) d.classList.add('selected');
|
||||
flagsDiv.appendChild(d);
|
||||
});
|
||||
}
|
||||
|
||||
function renderCountries() {
|
||||
countriesDiv.innerHTML = '';
|
||||
countries.forEach((p, i) => {
|
||||
const d = document.createElement('div');
|
||||
d.className = 'country';
|
||||
d.textContent = p.name;
|
||||
d.setAttribute('tabindex', 0);
|
||||
d.onclick = () => selectCountry(i);
|
||||
if (p.matched) d.classList.add('matched');
|
||||
if (selectedCountry === i) d.classList.add('selected');
|
||||
countriesDiv.appendChild(d);
|
||||
});
|
||||
}
|
||||
|
||||
function selectFlag(i) {
|
||||
if (flags[i].matched) return;
|
||||
selectedFlag = i;
|
||||
renderFlags();
|
||||
if (selectedCountry !== null) checkMatch();
|
||||
}
|
||||
|
||||
function selectCountry(i) {
|
||||
if (countries[i].matched) return;
|
||||
selectedCountry = i;
|
||||
renderCountries();
|
||||
if (selectedFlag !== null) checkMatch();
|
||||
}
|
||||
|
||||
function checkMatch() {
|
||||
const flagObj = flags[selectedFlag];
|
||||
const countryObj = countries[selectedCountry];
|
||||
if (flagObj.code === countryObj.code) {
|
||||
flags[selectedFlag].matched = true;
|
||||
countries[selectedCountry].matched = true;
|
||||
score++;
|
||||
scoreSpan.textContent = score;
|
||||
statusDiv.textContent = '¡Correcto!';
|
||||
renderFlags();
|
||||
renderCountries();
|
||||
if (score === pairs.length) {
|
||||
statusDiv.textContent = '¡Has emparejado todas las banderas! 🎉';
|
||||
}
|
||||
} else {
|
||||
statusDiv.textContent = 'No es correcto, intenta otra vez.';
|
||||
setTimeout(() => {
|
||||
statusDiv.textContent = '';
|
||||
selectedFlag = selectedCountry = null;
|
||||
renderFlags();
|
||||
renderCountries();
|
||||
}, 850);
|
||||
return;
|
||||
}
|
||||
selectedFlag = null;
|
||||
selectedCountry = null;
|
||||
}
|
||||
|
||||
restartBtn.onclick = startGame;
|
||||
startGame();
|
Reference in New Issue
Block a user