-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathapp.js
More file actions
138 lines (124 loc) · 4.33 KB
/
app.js
File metadata and controls
138 lines (124 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Grab a couple of things
const section = document.querySelector("section");
const playerLivesCount = document.querySelector("span");
let playerLives = 7;
// Link text
playerLivesCount.textContent = playerLives;
//Generated data for cards
const getData = () => [
{ imgSrc: "./images/card1.jpg", id: 1, name: "card 1" },
{ imgSrc: "./images/card2.jpg", id: 2, name: "card 2" },
{ imgSrc: "./images/card3.jpg", id: 3, name: "card 3" },
{ imgSrc: "./images/card4.jpg", id: 4, name: "card 4 " },
{ imgSrc: "./images/card5.jpg", id: 5, name: "card 5" },
{ imgSrc: "./images/card6.jpg", id: 6, name: "card 6" },
{ imgSrc: "./images/card7.jpg", id: 7, name: "card 7" },
{ imgSrc: "./images/card8.jpg", id: 8, name: "card 8" },
{ imgSrc: "./images/card1.jpg", id: 9, name: "card 1" },
{ imgSrc: "./images/card2.jpg", id: 10, name: "card 2" },
{ imgSrc: "./images/card3.jpg", id: 11, name: "card 3" },
{ imgSrc: "./images/card4.jpg", id: 12, name: "card 4 " },
{ imgSrc: "./images/card5.jpg", id: 13, name: "card 5" },
{ imgSrc: "./images/card6.jpg", id: 14, name: "card 6" },
{ imgSrc: "./images/card7.jpg", id: 15, name: "card 7" },
{ imgSrc: "./images/card8.jpg", id: 16, name: "card 8" },
];
// Randomize cards
const randomize = () => {
//Grabs cards from data function with stored array of cards.
const cardData = getData();
//Sort function retrieves array and with Math.random(), it shuffles array.
cardData.sort(() => Math.random() - 0.5);
return cardData;
};
// Card Generator Function
const cardGenerator = () => {
const cardData = randomize();
// Generate 16 card html
cardData.forEach((item) => {
const card = document.createElement("div");
const face = document.createElement("img");
const back = document.createElement("div");
card.classList = "card";
face.classList = "face";
back.classList = "back";
// Attach information to card
face.src = item.imgSrc;
card.setAttribute("name", item.name);
// Attach cards to the sections
section.appendChild(card);
card.appendChild(face);
card.appendChild(back);
/* Add event listener */
card.addEventListener("click", (e) => {
card.classList.toggle("toggleCard");
checkCards(e);
})
});
};
// Check if cards match
const checkCards = (e) => {
console.log(e);
const clickedCard = e.target;
// Card flip logic
clickedCard.classList.add("flipped");
const flippedCards = document.querySelectorAll(".flipped");
const toggleCard = document.querySelectorAll(".toggleCard");
console.log(flippedCards);
if (flippedCards.length === 2) {
if (
flippedCards[0].getAttribute("name") ===
flippedCards[1].getAttribute("name")
) {
console.log("match");
flippedCards.forEach((card) => {
card.classList.remove("flipped");
card.style.pointerEvents = "none";
});
} else {
console.log("wrong");
flippedCards.forEach((card) => {
card.classList.remove("flipped");
//setting bufferzone from instant logic reversed flip when wrong
setTimeout(() => card.classList.remove("toggleCard"), 1000);
});
playerLives--;
playerLivesCount.textContent = playerLives;
// players lives stops at 0 to avoid negative numbers
if (playerLives === 0) {
restart("Try agin");
}
}
}
// Run check to see if you won
if(toggleCard.length === 16){
restart("you won");
}
};
// Restart counter
const restart = (text) => {
let cardData = randomize();
let faces = document.querySelectorAll(".face");
let cards = document.querySelectorAll(".card");
section.style.pointerEvents = "none";
cardData.forEach((item, index) => {
cards[index].classList.remove("toggleCard");
// Randomize cards and update when lost
cards[index].style.pointerEvents = "all";
//Updating here
//SetTimeout here allows cards to reload at settime before
// Allowing user to play new game.
setTimeout(() =>{
faces[index].src = item.imgSrc;
cards[index].setAttribute("name", item.name);
section.style.pointerEvents = "all";
}, 1000);
});
// if we lose game loop over each card and remove
//classlist togglecard which will reflip cards
// back to player live counter default
playerLives = 7;
playerLivesCount.textContent = playerLives;
setTimeout(() => window.alert(text), 100);
};
cardGenerator();