-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
190 lines (155 loc) · 5.83 KB
/
script.js
File metadata and controls
190 lines (155 loc) · 5.83 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
let greet = "Hello Everyone";
console.log(greet);
const card_fronts = {
"card 1": "./images/loveall.png",
"card 2": "./images/loveallheart.png",
"card 3": "./images/loveboy.png",
"card 4": "./images/lovegirl.png",
"card 5": "./images/lovemusic.png",
"card 6": "./images/loverainbow.png",
};
const selectors = {
boardContainer: document.querySelector(".board-container"),
board: document.querySelector(".board"),
moves: document.querySelector(".moves"),
timer: document.querySelector(".timer"),
start: document.querySelector("button"),
win: document.querySelector(".win"),
cards: document.querySelectorAll(".card"),
};
const state = {
gameStarted: false,
flippedCards: 0,
picks: null,
totalFlips: 0,
totalTime: 0,
loop: null,
};
//shuffle cards
const shuffle = (array) => {
const clonedArray = [...array];
for (let index = clonedArray.length - 1; index > 0; index--) {
const randomIndex = Math.floor(Math.random() * (index + 1));
const original = clonedArray[index];
clonedArray[index] = clonedArray[randomIndex];
clonedArray[randomIndex] = original;
}
return clonedArray;
};
const pickRandom = (array, items) => {
const clonedArray = [...array];
const randomPicks = [];
for (let index = 0; index < items; index++) {
const randomIndex = Math.floor(Math.random() * clonedArray.length);
randomPicks.push(clonedArray[randomIndex]);
clonedArray.splice(randomIndex, 1);
}
return randomPicks;
};
// starts game and keeps stats
const startGame = () => {
state.gameStarted = true;
selectors.start.classList.add("disabled");
state.loop = setInterval(() => {
state.totalTime++;
selectors.moves.innerText = `${state.totalFlips} moves`;
selectors.timer.innerText = `time: ${state.totalTime} sec`;
}, 1000);
};
// onclick event for button to start game
selectors.start.onclick = startGame;
// resets cards if a pair doesn't match
const flipBackCards = () => {
document.querySelectorAll(".card:not(.matched)").forEach((card) => {
card.classList.remove("flipped");
});
state.flippedCards = 0;
};
// generate board
function generateGame() {
const card = ["card 1", "card 2", "card 3", "card 4", "card 5", "card 6"];
state.picks = pickRandom(card, 6);
const items = shuffle([...state.picks, ...state.picks]);
for (var i = 0; i < items.length; i++) {
selectors.cards[i].innerHTML = `
<div class="card-front"></div>
<div class="card-back" style="background-image: url('${card_fronts[items[i]]
}')">${items[i]}
</div>`;
selectors.cards[i].class
}
}
function flipCard(card) {
state.flippedCards++;
state.totalFlips++;
if (!state.gameStarted) {
startGame();
}
if (state.flippedCards <= 2) {
card.classList.add("flipped");
}
if (state.flippedCards === 2) {
const flippedCards = document.querySelectorAll(".flipped:not(.matched)");
if (flippedCards[0].innerText === flippedCards[1].innerText) {
flippedCards[0].classList.add("matched");
flippedCards[1].classList.add("matched");
}
setTimeout(() => {
flipBackCards();
}, 1000);
}
if (!document.querySelectorAll(".card:not(.flipped)").length) {
setTimeout(() => {
selectors.boardContainer.classList.add("flipped");
selectors.win.innerHTML = `
<span class="win-text">
You won!<br />
with <span class="highlight">${state.totalFlips}</span> moves<br />
under <span class="highlight">${state.totalTime}</span> seconds
<button class="replay">Play again</button>
</span>
`;
clearInterval(state.loop);
}, 1000);
}
}
const attachEventListeners = () => {
document.addEventListener("click", (event) => {
const eventTarget = event.target;
const eventParent = eventTarget.parentElement;
if (
eventTarget.className.includes("card") &&
!eventParent.className.includes("flipped")
) {
flipCard(eventParent);
} else if (
eventTarget.nodeName === "BUTTON" &&
!eventTarget.className.includes("disabled")
) {
startGame();
}
if (eventTarget.className.includes("replay")) {
selectors.boardContainer.classList.remove("flipped");
document.querySelectorAll(".card").forEach((card) => {
card.classList.remove("flipped", "matched");
});
replayGame();
}
});
};
// a replay function to Play again
const replayGame = () => {
state.gameStarted = false;
state.flippedCards = 0;
state.totalFlips = 0;
state.totalTime = 0;
clearInterval(state.loop);
state.loop = null;
state.picks = null;
generateGame();
selectors.moves.innerText = `${state.totalFlips} moves`;
selectors.timer.innerText = `time: ${state.totalTime} sec`;
selectors.start.classList.remove("disabled");
};
generateGame();
attachEventListeners();