-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjavascript.js
More file actions
137 lines (117 loc) ยท 4.31 KB
/
javascript.js
File metadata and controls
137 lines (117 loc) ยท 4.31 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
//Add game modal
const boardDiv = document.querySelector("#game-board");
const totalMovesScore = document.querySelector(".total-moves");
const matchingPairsScore = document.querySelector(".matching-pairs-score");
const modal = document.querySelector(".modal");
const winningMessage = document.querySelector(".winning-message");
let cards = ["๐", "๐", "๐ฆ", "๐ถ", "๐ป", "๐ฅ", "๐ฆ", "๐ค", "๐", "๐ฎ"];
let cardsPaired = [];
let totalMoves = 0;
let matchingPairMoves = 0;
let nameMatch = [];
let flipCount = 0;
let matchingCards = [];
let j = 0;
function gameReset() {
document.getElementById("game-board").innerHTML = "";
totalMovesScore.textContent = 0;
matchingPairsScore.textContent = 0;
cardsPaired = [];
totalMoves = 0;
matchingPairMoves = 0;
nameMatch = [];
flipCount = 0;
matchingCards = [];
j = 0;
}
function main() {
//Create card pairs array
for (let i = 0; i < cards.length * 2; i++) {
if (j >= cards.length) j = 0;
cardsPaired.push(cards[j]);
j++;
}
//Randomize cards
cardsPaired.sort(() => 0.5 - Math.random());
//Create cards
for (let i = 0; i < cardsPaired.length; i++) {
const flipCard = document.createElement("div");
const flipCardInner = document.createElement("div");
const flipCardFront = document.createElement("div");
const flipCardBack = document.createElement("div");
flipCard.classList.add("flip-card");
flipCardInner.classList.add("flip-card-inner");
flipCardFront.classList.add("flip-card-front");
flipCardBack.classList.add("flip-card-back");
boardDiv.appendChild(flipCard);
flipCard.appendChild(flipCardInner);
flipCardInner.appendChild(flipCardFront);
flipCardInner.appendChild(flipCardBack);
flipCardBack.textContent = cardsPaired[i];
}
document.querySelectorAll(".flip-card-inner").forEach((flipCard) => {
flipCard.addEventListener("click", () => {
//Flip card on card click
flipCard.classList.add("flip-card-flip", "disable-click");
//Add card name to array to compare matching cards with conditional later.
nameMatch.push(flipCard.textContent);
//Count each time the cards are clicked/flipped
flipCount++;
//Match the card values if two are flipped
if (flipCount >= 2) {
//Add to totalMoves count
totalMoves++;
totalMovesScore.textContent = totalMoves;
//disable clicks after 2 cards have been flipped
document.querySelectorAll(".flip-card-inner").forEach((flipCard) => {
flipCard.classList.add("disable-click");
});
//If cards flipped do match
if (nameMatch[0] === nameMatch[1]) {
//Add card value to matching cards array
matchingCards.push(flipCard.textContent);
//Enable clicking again, except for matching cards
document.querySelectorAll(".flip-card-inner").forEach((flipCard) => {
if (!matchingCards.includes(flipCard.textContent))
flipCard.classList.remove("disable-click");
});
matchingPairMoves++;
matchingPairsScore.textContent = matchingPairMoves;
//Else they don't match
} else {
//Delay flip back
window.setTimeout(function () {
//Skip every card thats already matched (on matchingCard Array)
document
.querySelectorAll(".flip-card-inner")
.forEach((flipCard) => {
//IF name is in matchingCards, don't not flip back
if (!matchingCards.includes(flipCard.textContent))
flipCard.classList.remove("flip-card-flip", "disable-click");
});
}, 1000);
}
//Display winning modal display
if (matchingPairMoves >= 10 && totalMoves < 30) {
modal.classList.remove("hide1");
console.log("Hey");
winningMessage.textContent = "Grand Prize";
} else if (matchingPairMoves >= 10) {
modal.textContent = "Here's your participation trophy";
modal.classList.remove("hide2");
}
//Reset variables / clear array
flipCount = 0;
nameMatch = [];
}
});
});
}
//Change card array based on difficulty
document.querySelectorAll("button").forEach((button) => {
button.addEventListener("click", () => {
gameReset();
main();
});
});
main();