انتقل للمحتوى
Robo EgyptRobo Egypt
☰ Course contents
Robo Web: home
0. Lab tour1
1. HTML: the page's content7
2. CSS: the page's look6
3. JavaScript: the actions7
4. The project: the to-do list3
5. Pro CSS: shadows and movement5
6. Arrays and building the page with code3
7. Events and forms3
8. Objects and time3
9. Canvas: drawing and games3
10. The final project: the memory game3

We build the game

Section 10: The final project: the memory gameLesson 10.2 · ⏱ 18 min
  • emojis.concat(emojis) repeats the array, and sort(() => Math.random() - 0.5) shuffles it.
  • open keeps the two cards that are open now.
  • setTimeout(check, 600) waits a little so the player sees the second card before it turns back.
  • const [a, b] = open takes the two cards from the array in one line.
HTML
<h2>🧠 Memory game</h2>
<p>Tries: <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 = "You won in " + moves + " tries! 🎉";
  }
}
🖥️ The page in Robo Web:The page in Robo Web
In the middle of the game: two pairs found and turned green
In the middle of the game: two pairs found and turned green
The whole game in Robo Web: the code on the right and the page on the left
The whole game in Robo Web: the code on the right and the page on the left
🖥️ What happens on the stage?
We played in the lab: one wrong try, then we found the four pairs, and “You won in 5 tries! 🎉” appeared.

📌 Another example: memory with six pairs

The same game with six emojis (12 cards), and stars for the number of tries.

🌐 Memory with six pairs
HTML
<h2>🧠 Memory game</h2>
<p>Tries: <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 = "You won in " + moves + " tries! 🎉" + (moves <= 8 ? " ⭐⭐⭐" : " ⭐");
  }
}
🖥️ The page in Robo Web:The page in Robo Web
✍️ Practise
Try the example here: copy the code into its tabs, change it, and watch the page change.
✍️ Practise here in the lessonor open the lab in a new page ↗

🎯 Challenge yourself

Easy

Change the emojis to animals.

👀 Show the solution
🌐 Solution
HTML
<h2>🧠 Memory game</h2>
<p>Tries: <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 = "You won in " + moves + " tries! 🎉";
  }
}
🖥️ The page in Robo Web:The page in Robo Web

Result: You won in 5 tries! 🎉

Medium

For every right pair: the message “A right pair ✅” in the Console.

👀 Show the solution
🌐 Solution
HTML
<h2>🧠 Memory game</h2>
<p>Tries: <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("A right pair ✅");
  } else {
    a.textContent = "";
    b.textContent = "";
  }
  a.classList.remove("open");
  b.classList.remove("open");
  open = [];
  if (found === emojis.length) {
    document.getElementById("msg").textContent = "You won in " + moves + " tries! 🎉";
  }
}
🖥️ The page in Robo Web:The page in Robo Web
🖥️ The Console in Robo Web:
A right pair ✅
A right pair ✅
A right pair ✅
A right pair ✅

Result: 4 messages.

Hard

A timer that starts with the game and stops when you win.

👀 Show the solution
🌐 Solution
HTML
<h2>🧠 Memory game</h2>
<p>Tries: <b id="moves">0</b></p>
<p>Time: <b id="time">0</b> s</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 = "You won in " + moves + " tries! 🎉" + " ph " + seconds + " seconds";
  }
}
🖥️ The page in Robo Web:The page in Robo Web

Result: the time stops when you win.

Finished the lesson? Mark it so your progress counts.