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
7 changes: 6 additions & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
<meta http-equiv="X-UA-Compatible" content="ie=edge" />
<title>Superhero Memory Game</title>
<!-- LINK THE STYLES HERE -->
<link rel="stylesheet" href="/styles/style.css">
</head>
<body>
<div>
Expand All @@ -16,8 +17,12 @@ <h2>Score</h2>
<p>Pairs clicked: <span id="pairs-clicked">0</span></p>
<p>Pairs guessed: <span id="pairs-guessed">0</span></p>
</div>
<div id="memory-board"></div>
<div id="memory-board">

</div>

<!-- LINK THE JAVASCRIPT FILES HERE (keep in mind that the order in which you link them MATTERS) -->
</body>
<script src="src/memory.js"></script>
<script src="src/index.js"></script>
</html>
57 changes: 55 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,18 @@ const cards = [
];

const memoryGame = new MemoryGame(cards);
const pairsClicked = document.getElementById("pairs-clicked");
const pairsGuessed = document.getElementById("pairs-guessed");

window.addEventListener('load', (event) => {

//Shuffle
console.log("Cards pre-shuffle:", memoryGame.cards);

memoryGame.shuffleCards();

console.log("Cards post-shuffle:", memoryGame.cards);

let html = '';
memoryGame.cards.forEach((pic) => {
html += `
Expand All @@ -44,8 +54,51 @@ window.addEventListener('load', (event) => {
// 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}`);

//Toggle styles
card.classList.toggle("turned");

//Add clicked card to array
memoryGame.pickedCards.push(card);

//Check the array
if (memoryGame.pickedCards.length === 2) {
const card1 = memoryGame.pickedCards[0];
const card2 = memoryGame.pickedCards[1];
pairsClicked.textContent = memoryGame.pairsClicked;

//Check pair
if (memoryGame.checkIfPair(card1.dataset.cardName, card2.dataset.cardName)) {
//Update counter
pairsGuessed.textContent = memoryGame.pairsGuessed;

//Check if finished
if (memoryGame.checkIfFinished()) {
setTimeout(() => {
alert("You've won!");
const restart = confirm("Do you want to try again?");
if(restart){
window.location.reload();
}
})
}

} else {

//Toggle styles after 1s
setTimeout(() => {
card1.classList.toggle("turned");
card2.classList.toggle("turned");
}, 1000);
}

//Set array to []
memoryGame.pickedCards = [];
}

});
});
});


// document.querySelectorAll(".card").forEach((card) => card.classList.toggle("turned"));
39 changes: 35 additions & 4 deletions src/memory.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,49 @@
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
const deck = this.cards.slice();
let length = this.cards.length;
for (let i = deck.length -1; i > 1; i--) {
//Get random number
const random = Math.floor(Math.random() * length);
//Get last card
const card = deck[i];
//Get random card
const randomCard = deck[random];

//Swap places
deck[deck.indexOf(card)] = randomCard;
deck[random] = card;

//console.log("=========");
//console.log("Vuelta: ", i);
//console.log("Card: ", card, " - ", this.cards.indexOf(card));
//console.log("RandomCard: ", randomCard, " - ", this.cards.indexOf(randomCard));

length--;
}
this.cards = deck;
}

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

}

checkIfFinished() {
// ... write your code here
return this.pairsGuessed === (this.cards.length / 2);
}
}
5 changes: 2 additions & 3 deletions styles/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ body {
position: relative;
}

.card.turned {
.blocked {
pointer-events: none;
}

Expand All @@ -34,8 +34,7 @@ body {
transform: rotateY(180deg);
}

.card .back,
.card .front {
.card .back,.card .front {
width: 71px;
height: 71px;
margin: 10px;
Expand Down