Game Onet - Jurumiyah
🎓 Game Onet Jurumiyah
const pairs = [
["اسم", "kata benda"],
["فعل", "kata kerja"],
["حرف", "kata sambung"],
["مرفوع", "subjek"],
["منصوب", "objek"],
["مجرور", "berharokat jarr"],
["مجزوم", "fi'il mudhari' tanpa harakat"],
["مبتدأ", "permulaan kalimat"]
];
let shuffledTiles = [];
let selectedTiles = [];
function startGame() {
const gameContainer = document.getElementById("game");
gameContainer.innerHTML = "";
selectedTiles = [];
// Duplikat dan acak pasangan
const flatPairs = [...pairs, ...pairs].map(pair => pair[Math.random() < 0.5 ? 0 : 1]);
shuffledTiles = shuffle(flatPairs);
shuffledTiles.forEach((value, index) => {
const tile = document.createElement("div");
tile.className = "tile col";
tile.textContent = value;
tile.dataset.index = index;
tile.onclick = () => selectTile(tile);
gameContainer.appendChild(tile);
});
}
function selectTile(tile) {
if (tile.classList.contains("matched") || tile.classList.contains("selected")) return;
tile.classList.add("selected");
selectedTiles.push(tile);
if (selectedTiles.length === 2) {
const [first, second] = selectedTiles;
const val1 = first.textContent;
const val2 = second.textContent;
if (isPair(val1, val2)) {
first.classList.add("matched");
second.classList.add("matched");
}
setTimeout(() => {
first.classList.remove("selected");
second.classList.remove("selected");
selectedTiles = [];
}, 600);
}
}
function isPair(a, b) {
return pairs.some(([x, y]) =>
(x === a && y === b) || (x === b && y === a)
);
}
function shuffle(array) {
return array.sort(() => Math.random() - 0.5);
}
window.onload = startGame;