diff --git a/index.html b/index.html index 9386faaf4..b7abb1863 100644 --- a/index.html +++ b/index.html @@ -5,7 +5,7 @@ Superhero Memory Game - +
@@ -19,5 +19,9 @@

Score

+ + + + diff --git a/src/index.js b/src/index.js index 37f3a672d..8977b747c 100644 --- a/src/index.js +++ b/src/index.js @@ -1,34 +1,34 @@ 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); -window.addEventListener('load', (event) => { - let html = ''; +window.addEventListener("load", (event) => { + let html = ""; memoryGame.cards.forEach((pic) => { html += `
@@ -39,13 +39,40 @@ window.addEventListener('load', (event) => { }); // Add all the divs to the HTML - document.querySelector('#memory-board').innerHTML = html; + document.querySelector("#memory-board").innerHTML = html; // Bind the click event of each element to a function - document.querySelectorAll('.card').forEach((card) => { - card.addEventListener('click', () => { - // TODO: write some code here - console.log(`Card clicked: ${card}`); + document.querySelectorAll(".card").forEach((card) => { + card.addEventListener("click", () => { + card.classList.toggle("turned"); + memoryGame.pickedCards.push(card); + + if (memoryGame.pickedCards.length === 2) { + const card1 = memoryGame.pickedCards[0]; + const card2 = memoryGame.pickedCards[1]; + const card1Name = card1.getAttribute("data-card-name"); + const card2Name = card2.getAttribute("data-card-name"); + + const isTheSame = memoryGame.checkIfPair(card1Name, card2Name); + if (!isTheSame) { + setTimeout(() => { + card1.classList.toggle("turned"); + card2.classList.toggle("turned"); + }, 1000); + } + + memoryGame.pickedCards = []; + + const pairsClicked = document.getElementById("pairs-clicked"); + const pairsGuessed = document.getElementById("pairs-guessed"); + + pairsClicked.innerText = memoryGame.pairsClicked; + pairsGuessed.innerText = memoryGame.pairsGuessed; + + if (memoryGame.checkIfFinished()) { + alert("You Won"); + } + } }); }); }); diff --git a/src/memory.js b/src/memory.js index f6644827e..679b3b480 100644 --- a/src/memory.js +++ b/src/memory.js @@ -1,18 +1,36 @@ 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 (this.cards) { + for (let i = this.cards.length - 1; i > 0; i--) { + const random = Math.floor(Math.random() * (i + 1)); + ([this.cards[i], this.cards[random]] = [ + this.cards[random], + this.cards[i], + ]); + } + return this.cards; + } else { + return undefined; + } } checkIfPair(card1, card2) { - // ... write your code here + this.pairsClicked++; + if (card1 === card2) { + this.pairsGuessed++; + return true; + } + return false; } checkIfFinished() { - // ... write your code here + return this.pairsGuessed === this.cards.length / 2; } }