Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Superhero Memory Game</title>
<link rel="stylesheet" href="./styles/style.css" />
<!-- LINK THE STYLES HERE -->
</head>
<body>
Expand All @@ -19,5 +20,7 @@ <h2>Score</h2>
<div id="memory-board"></div>

<!-- LINK THE JAVASCRIPT FILES HERE (keep in mind that the order in which you link them MATTERS) -->
<script src="./src/memory.js"></script>
<script src="./src/index.js"></script>
</body>
</html>
98 changes: 68 additions & 30 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
const cards = [
{ name: 'aquaman', img: 'aquaman.jpg' },
{ name: 'batman', img: 'batman.jpg' },
{ name: 'captain america', img: 'captain-america.jpg' },
{ name: 'fantastic four', img: 'fantastic-four.jpg' },
{ name: 'flash', img: 'flash.jpg' },
{ name: 'green arrow', img: 'green-arrow.jpg' },
{ name: 'green lantern', img: 'green-lantern.jpg' },
{ name: 'ironman', img: 'ironman.jpg' },
{ name: 'spiderman', img: 'spiderman.jpg' },
{ name: 'superman', img: 'superman.jpg' },
{ name: 'the avengers', img: 'the-avengers.jpg' },
{ name: 'thor', img: 'thor.jpg' },
{ name: 'aquaman', img: 'aquaman.jpg' },
{ name: 'batman', img: 'batman.jpg' },
{ name: 'captain america', img: 'captain-america.jpg' },
{ name: 'fantastic four', img: 'fantastic-four.jpg' },
{ name: 'flash', img: 'flash.jpg' },
{ name: 'green arrow', img: 'green-arrow.jpg' },
{ name: 'green lantern', img: 'green-lantern.jpg' },
{ name: 'ironman', img: 'ironman.jpg' },
{ name: 'spiderman', img: 'spiderman.jpg' },
{ name: 'superman', img: 'superman.jpg' },
{ name: 'the avengers', img: 'the-avengers.jpg' },
{ name: 'thor', img: 'thor.jpg' }
{ name: "aquaman", img: "aquaman.jpg" },
{ name: "batman", img: "batman.jpg" },
{ name: "captain america", img: "captain-america.jpg" },
{ name: "fantastic four", img: "fantastic-four.jpg" },
{ name: "flash", img: "flash.jpg" },
{ name: "green arrow", img: "green-arrow.jpg" },
{ name: "green lantern", img: "green-lantern.jpg" },
{ name: "ironman", img: "ironman.jpg" },
{ name: "spiderman", img: "spiderman.jpg" },
{ name: "superman", img: "superman.jpg" },
{ name: "the avengers", img: "the-avengers.jpg" },
{ name: "thor", img: "thor.jpg" },
{ name: "aquaman", img: "aquaman.jpg" },
{ name: "batman", img: "batman.jpg" },
{ name: "captain america", img: "captain-america.jpg" },
{ name: "fantastic four", img: "fantastic-four.jpg" },
{ name: "flash", img: "flash.jpg" },
{ name: "green arrow", img: "green-arrow.jpg" },
{ name: "green lantern", img: "green-lantern.jpg" },
{ name: "ironman", img: "ironman.jpg" },
{ name: "spiderman", img: "spiderman.jpg" },
{ name: "superman", img: "superman.jpg" },
{ name: "the avengers", img: "the-avengers.jpg" },
{ name: "thor", img: "thor.jpg" },
];

const memoryGame = new MemoryGame(cards);
memoryGame.shuffleCards();

window.addEventListener('load', (event) => {
let html = '';
window.addEventListener("load", (event) => {
let html = "";
memoryGame.cards.forEach((pic) => {
html += `
<div class="card" data-card-name="${pic.name}">
Expand All @@ -39,13 +40,50 @@ window.addEventListener('load', (event) => {
});

// Add all the divs to the HTML
document.querySelector('#memory-board').innerHTML = html;

document.querySelector("#memory-board").innerHTML = html;
const nameArray = [];
let isClickable = true;
// Bind the click event of each element to a function
document.querySelectorAll('.card').forEach((card) => {
card.addEventListener('click', () => {
document.querySelectorAll(".card").forEach((card) => {
card.addEventListener("click", () => {
// TODO: write some code here
console.log(`Card clicked: ${card}`);
if (isClickable) {
card.classList.add("turned");
// console.log(card.dataset.cardName);
const card1 = card.dataset.cardName;
nameArray.push(card1);
//console.log(nameArray);
// console.log(memoryGame.checkIfPair(nameArray[0], nameArray[1]));

if (nameArray.length === 2) {
isClickable = false;
setTimeout(() => {
isClickable = true;
if (!memoryGame.checkIfPair(nameArray[0], nameArray[1])) {
document
.querySelectorAll(".turned:not(.blocked) ")
.forEach((element) => element.classList.remove("turned"));
nameArray.splice(0);
} else {
document
.querySelectorAll(".turned")
.forEach((element) => element.classList.add("blocked"));
nameArray.splice(0);
}
document.querySelector("#pairs-clicked").innerHTML =
memoryGame.pairsClicked;
document.querySelector("#pairs-guessed").innerHTML =
memoryGame.pairsGuessed;
// check finished?
if (memoryGame.checkIfFinished()) {
alert("Congraduations!!! You won!!");
}
}, 800);
// console.log(`game over? ${memoryGame.checkIfFinished()}`);
// console.log(`----- ${memoryGame.pairsGuessed} ----`);
}
}
});
});
});
18 changes: 18 additions & 0 deletions src/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,35 @@ class MemoryGame {
constructor(cards) {
this.cards = cards;
// add the rest of the class properties here
this.pickedCards = [];
this.pairsClicked = 0;
this.pairsGuessed = 0;
}

shuffleCards() {
// ... write your code here

if (!Array.isArray(this.cards)) return undefined;
for (let i = this.cards.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[this.cards[i], this.cards[j]] = [this.cards[j], this.cards[i]];
}
}

checkIfPair(card1, card2) {
// ... write your code here
this.pairsClicked++;
if (card1 === card2) {
this.pairsGuessed++;
return true;
}
return false;
}

checkIfFinished() {
// ... write your code here
if (this.pairsGuessed === 0) return false;
if (this.pairsGuessed >= this.cards.length / 2) return true;
return false;
}
}