انتقل للمحتوى
Robo EgyptRobo Egypt
☰ فهرس الكورس
روبو ويب: الصفحة الأولى
0. جولة في المختبر1
1. HTML: محتوى الصفحة7
2. CSS: شكل الصفحة6
3. JavaScript: الحركة7
4. المشروع: قائمة المهام3
5. CSS المحترفين: ظلال وحركة5
6. المصفوفات وبناء الصفحة بالكود3
7. الأحداث والنماذج3
8. الكائنات والوقت3
9. Canvas: الرسم والألعاب3
10. المشروع النهائي: لعبة الذاكرة3

نبني اللعبة

القسم 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 + " ثانية";
  }
}
🖥️ الصفحة في روبو ويب:الصفحة في روبو ويب

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

أنهيت الدرس؟ علّم عليه ليُحسب تقدّمك.