Last active
May 23, 2021 23:28
-
-
Save josecarneiro/50ed4d7c63d3c13798274313b4dbe298 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// memory.js contents | |
class MemoryGame { | |
constructor(cards) { | |
this.cards = cards; | |
this.pickedCards = []; | |
this.pairsClicked = 0; | |
this.pairsGuessed = 0; | |
} | |
shuffleCards() { | |
for (let i = this.cards.length - 1; i > 0; i--) { | |
const j = Math.floor(Math.random() * (i + 1)); | |
const card = this.cards[i]; | |
this.cards[i] = this.cards[j]; | |
this.cards[j] = card; | |
} | |
} | |
checkIfPair(card1, card2) { | |
this.pairsClicked++; | |
if (card1 === card2) { | |
this.pairsGuessed++; | |
return true; | |
} else { | |
return false; | |
} | |
} | |
checkIfFinished() { | |
return this.pairsGuessed === this.cards.length / 2; | |
} | |
} | |
// The following is required for automated testing. Please, ignore it. | |
module.exports = MemoryGame; | |
// index.js contents | |
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' } | |
]; | |
const memoryGame = new MemoryGame(cards); | |
window.addEventListener('load', (event) => { | |
memoryGame.shuffleCards(); | |
let html = ''; | |
memoryGame.cards.forEach((pic) => { | |
html += ` | |
<div class="card" data-card-name="${pic.name}"> | |
<div class="back" name="${pic.img}"></div> | |
<div class="front" style="background: url(img/${pic.img}) no-repeat"></div> | |
</div> | |
`; | |
}); | |
// Add all the divs to the 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', () => { | |
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'); | |
if (memoryGame.checkIfPair(card1Name, card2Name)) { | |
card1.classList.add('blocked'); | |
card2.classList.add('blocked'); | |
} else { | |
setTimeout(() => { | |
card1.classList.toggle('turned'); | |
card2.classList.toggle('turned'); | |
}, 1000); | |
} | |
memoryGame.pickedCards.splice(0, 2); | |
} | |
document.getElementById('pairs-clicked').innerHTML = | |
memoryGame.pairsClicked; | |
document.getElementById('pairs-guessed').innerHTML = | |
memoryGame.pairsGuessed; | |
if (memoryGame.checkIfFinished()) { | |
alert('You are a winner, baby!'); | |
} | |
}); | |
}); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment