نبني اللعبة
القسم 10: المشروع النهائي: لعبة الذاكرةالدرس 10.2 · ⏱ 18 دقائق
emojis.concat(emojis)يكرّر المصفوفة، وsort(() => Math.random() - 0.5)يخلطها.openيحفظ البطاقتين المفتوحتين الآن.setTimeout(check, 600)ينتظر قليلًا حتى يرى اللاعب البطاقة الثانية قبل أن تنقلب.const [a, b] = openيأخذ البطاقتين من المصفوفة في سطر واحد.
HTML
<h2>🧠 لعبة الذاكرة</h2>
<p>المحاولات: <b id="moves">0</b></p>
<div id="board"></div>
<p id="msg"></p>
CSS
body { font-family: Tahoma, sans-serif; text-align: center; }
#board { display: grid; grid-template-columns: repeat(4, 70px); gap: 10px; justify-content: center; }
.card {
height: 70px;
font-size: 34px;
border: none;
border-radius: 14px;
background: #2557c9;
cursor: pointer;
}
.card.open { background: #fff3c4; }
.card.done { background: #c8f0d0; }
#msg { font-size: 24px; font-weight: bold; color: #1a8a34; }
JavaScript
const emojis = ["🦁", "🍉", "🚀", "⚽"];
const deck = emojis.concat(emojis); // every emoji twice
deck.sort(() => Math.random() - 0.5); // shuffle
let open = []; // the cards open now (at most 2)
let moves = 0;
let found = 0;
deck.forEach(function (emoji) {
const card = document.createElement("button");
card.className = "card";
card.dataset.emoji = emoji;
card.onclick = function () { flip(card); };
document.getElementById("board").appendChild(card);
});
function flip(card) {
if (open.length === 2 || card.classList.contains("open") || card.classList.contains("done")) return;
card.textContent = card.dataset.emoji;
card.classList.add("open");
open.push(card);
if (open.length === 2) {
moves++;
document.getElementById("moves").textContent = moves;
setTimeout(check, 600);
}
}
function check() {
const [a, b] = open;
if (a.dataset.emoji === b.dataset.emoji) {
a.classList.add("done");
b.classList.add("done");
found++;
} else {
a.textContent = "";
b.textContent = "";
}
a.classList.remove("open");
b.classList.remove("open");
open = [];
if (found === emojis.length) {
document.getElementById("msg").textContent = "فزت في " + moves + " محاولات! 🎉";
}
}
🖥️ الصفحة في روبو ويب:



🖥️ ماذا يحدث على المسرح؟
لعبنا في المختبر: محاولة خاطئة، ثم وجدنا الأزواج الأربعة، فظهر «فزت في 5 محاولات! 🎉».
📌 مثال آخر: ذاكرة بستة أزواج
نفس اللعبة بستة إيموجي (12 بطاقة)، ونجوم حسب عدد المحاولات.
HTML
<h2>🧠 لعبة الذاكرة</h2>
<p>المحاولات: <b id="moves">0</b></p>
<div id="board"></div>
<p id="msg"></p>
CSS
body { font-family: Tahoma, sans-serif; text-align: center; }
#board { display: grid; grid-template-columns: repeat(4, 70px); gap: 10px; justify-content: center; }
.card {
height: 70px;
font-size: 34px;
border: none;
border-radius: 14px;
background: #2557c9;
cursor: pointer;
}
.card.open { background: #fff3c4; }
.card.done { background: #c8f0d0; }
#msg { font-size: 24px; font-weight: bold; color: #1a8a34; }
JavaScript
const emojis = ["🦁", "🍉", "🚀", "⚽", "🎈", "🐢"];
const deck = emojis.concat(emojis);
deck.sort(() => Math.random() - 0.5);
let open = [];
let moves = 0;
let found = 0;
deck.forEach(function (emoji) {
const card = document.createElement("button");
card.className = "card";
card.dataset.emoji = emoji;
card.onclick = function () { flip(card); };
document.getElementById("board").appendChild(card);
});
function flip(card) {
if (open.length === 2 || card.classList.contains("open") || card.classList.contains("done")) return;
card.textContent = card.dataset.emoji;
card.classList.add("open");
open.push(card);
if (open.length === 2) {
moves++;
document.getElementById("moves").textContent = moves;
setTimeout(check, 600);
}
}
function check() {
const [a, b] = open;
if (a.dataset.emoji === b.dataset.emoji) {
a.classList.add("done");
b.classList.add("done");
found++;
} else {
a.textContent = "";
b.textContent = "";
}
a.classList.remove("open");
b.classList.remove("open");
open = [];
if (found === emojis.length) {
document.getElementById("msg").textContent = "فزت في " + moves + " محاولات! 🎉" + (moves <= 8 ? " ⭐⭐⭐" : " ⭐");
}
}
🖥️ الصفحة في روبو ويب:

✍️ طبّق بنفسك
جرّب المثال هنا: انسخ الكود إلى تبويباته، وغيّر فيه، وشاهد الصفحة تتغيّر.
✍️ طبّق هنا في الدرس✖ أغلق المعملأو افتح المعمل في صفحة جديدة ↗
🎯 تحدَّ نفسك
سهل
غيّر الإيموجي إلى حيوانات.
👀 اعرض الحل🙈 أخفِ الحل
HTML
<h2>🧠 لعبة الذاكرة</h2>
<p>المحاولات: <b id="moves">0</b></p>
<div id="board"></div>
<p id="msg"></p>
CSS
body { font-family: Tahoma, sans-serif; text-align: center; }
#board { display: grid; grid-template-columns: repeat(4, 70px); gap: 10px; justify-content: center; }
.card {
height: 70px;
font-size: 34px;
border: none;
border-radius: 14px;
background: #2557c9;
cursor: pointer;
}
.card.open { background: #fff3c4; }
.card.done { background: #c8f0d0; }
#msg { font-size: 24px; font-weight: bold; color: #1a8a34; }
JavaScript
const emojis = ["🐶", "🐱", "🐰", "🐸"];
const deck = emojis.concat(emojis);
deck.sort(() => Math.random() - 0.5);
let open = [];
let moves = 0;
let found = 0;
deck.forEach(function (emoji) {
const card = document.createElement("button");
card.className = "card";
card.dataset.emoji = emoji;
card.onclick = function () { flip(card); };
document.getElementById("board").appendChild(card);
});
function flip(card) {
if (open.length === 2 || card.classList.contains("open") || card.classList.contains("done")) return;
card.textContent = card.dataset.emoji;
card.classList.add("open");
open.push(card);
if (open.length === 2) {
moves++;
document.getElementById("moves").textContent = moves;
setTimeout(check, 600);
}
}
function check() {
const [a, b] = open;
if (a.dataset.emoji === b.dataset.emoji) {
a.classList.add("done");
b.classList.add("done");
found++;
} else {
a.textContent = "";
b.textContent = "";
}
a.classList.remove("open");
b.classList.remove("open");
open = [];
if (found === emojis.length) {
document.getElementById("msg").textContent = "فزت في " + moves + " محاولات! 🎉";
}
}
🖥️ الصفحة في روبو ويب:

النتيجة: فزت في 5 محاولات! 🎉
متوسط
عند كل زوج صحيح: رسالة «زوج صحيح ✅» في الـ Console.
👀 اعرض الحل🙈 أخفِ الحل
HTML
<h2>🧠 لعبة الذاكرة</h2>
<p>المحاولات: <b id="moves">0</b></p>
<div id="board"></div>
<p id="msg"></p>
CSS
body { font-family: Tahoma, sans-serif; text-align: center; }
#board { display: grid; grid-template-columns: repeat(4, 70px); gap: 10px; justify-content: center; }
.card {
height: 70px;
font-size: 34px;
border: none;
border-radius: 14px;
background: #2557c9;
cursor: pointer;
}
.card.open { background: #fff3c4; }
.card.done { background: #c8f0d0; }
#msg { font-size: 24px; font-weight: bold; color: #1a8a34; }
JavaScript
const emojis = ["🦁", "🍉", "🚀", "⚽"];
const deck = emojis.concat(emojis);
deck.sort(() => Math.random() - 0.5);
let open = [];
let moves = 0;
let found = 0;
deck.forEach(function (emoji) {
const card = document.createElement("button");
card.className = "card";
card.dataset.emoji = emoji;
card.onclick = function () { flip(card); };
document.getElementById("board").appendChild(card);
});
function flip(card) {
if (open.length === 2 || card.classList.contains("open") || card.classList.contains("done")) return;
card.textContent = card.dataset.emoji;
card.classList.add("open");
open.push(card);
if (open.length === 2) {
moves++;
document.getElementById("moves").textContent = moves;
setTimeout(check, 600);
}
}
function check() {
const [a, b] = open;
if (a.dataset.emoji === b.dataset.emoji) {
a.classList.add("done");
b.classList.add("done");
found++;
console.log("زوج صحيح ✅");
} else {
a.textContent = "";
b.textContent = "";
}
a.classList.remove("open");
b.classList.remove("open");
open = [];
if (found === emojis.length) {
document.getElementById("msg").textContent = "فزت في " + moves + " محاولات! 🎉";
}
}
🖥️ الصفحة في روبو ويب:

🖥️ الـ Console في روبو ويب:
زوج صحيح ✅ زوج صحيح ✅ زوج صحيح ✅ زوج صحيح ✅
النتيجة: 4 رسائل.
صعب
عدّاد وقت يبدأ مع اللعبة ويتوقف عند الفوز.
👀 اعرض الحل🙈 أخفِ الحل
HTML
<h2>🧠 لعبة الذاكرة</h2>
<p>المحاولات: <b id="moves">0</b></p>
<p>الوقت: <b id="time">0</b> ث</p>
<div id="board"></div>
<p id="msg"></p>
CSS
body { font-family: Tahoma, sans-serif; text-align: center; }
#board { display: grid; grid-template-columns: repeat(4, 70px); gap: 10px; justify-content: center; }
.card {
height: 70px;
font-size: 34px;
border: none;
border-radius: 14px;
background: #2557c9;
cursor: pointer;
}
.card.open { background: #fff3c4; }
.card.done { background: #c8f0d0; }
#msg { font-size: 24px; font-weight: bold; color: #1a8a34; }
JavaScript
let seconds = 0;
const timer = setInterval(function () {
seconds++;
document.getElementById("time").textContent = seconds;
}, 1000);
const emojis = ["🦁", "🍉", "🚀", "⚽"];
const deck = emojis.concat(emojis);
deck.sort(() => Math.random() - 0.5);
let open = [];
let moves = 0;
let found = 0;
deck.forEach(function (emoji) {
const card = document.createElement("button");
card.className = "card";
card.dataset.emoji = emoji;
card.onclick = function () { flip(card); };
document.getElementById("board").appendChild(card);
});
function flip(card) {
if (open.length === 2 || card.classList.contains("open") || card.classList.contains("done")) return;
card.textContent = card.dataset.emoji;
card.classList.add("open");
open.push(card);
if (open.length === 2) {
moves++;
document.getElementById("moves").textContent = moves;
setTimeout(check, 600);
}
}
function check() {
const [a, b] = open;
if (a.dataset.emoji === b.dataset.emoji) {
a.classList.add("done");
b.classList.add("done");
found++;
} else {
a.textContent = "";
b.textContent = "";
}
a.classList.remove("open");
b.classList.remove("open");
open = [];
if (found === emojis.length) {
clearInterval(timer);
document.getElementById("msg").textContent = "فزت في " + moves + " محاولات! 🎉" + " في " + seconds + " ثانية";
}
}
🖥️ الصفحة في روبو ويب:

النتيجة: الوقت يتوقف عند الفوز.

