This repository was archived by the owner on Jan 22, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackend.js
More file actions
128 lines (109 loc) · 4.09 KB
/
Copy pathbackend.js
File metadata and controls
128 lines (109 loc) · 4.09 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
const selectors = {
boardContainer: document.querySelector('.Bcontainer'),
board: document.querySelector('.board'),
moves: document.querySelector('.moves'),
timer: document.querySelector('.timer'),
start: document.querySelector('button'),
win: document.querySelector('.win'),
};
const state = {
gameStarted: false,
flippedCards: 0,
totalFlips: 0,
totalTime: 0,
loop: null,
};
const shuffle = (array) => {
const clonedArray = [...array];
for (let i = clonedArray.length - 1; i > 0; i--) {
const randomIndex = Math.floor(Math.random() * (i + 1));
[clonedArray[i], clonedArray[randomIndex]] = [clonedArray[randomIndex], clonedArray[i]];
}
return clonedArray;
};
const pickRandom = (array, items) => {
const clonedArray = [...array];
return Array.from({ length: items }, () => clonedArray.splice(Math.floor(Math.random() * clonedArray.length), 1)[0]);
};
const generateGame = () => {
const dimensions = parseInt(selectors.board.getAttribute('data-dimension'), 10);
if (dimensions % 2 !== 0) {
throw new Error('The dimensions of the board must be an even number.');
}
const fruitEmojis = ['🍎', '🍌', '🍇', '🍉', '🍓', '🍒', '🍍', '🥭', '🍑', '🍊'];
const picks = pickRandom(fruitEmojis, (dimensions * dimensions) / 2);
const items = shuffle([...picks, ...picks]);
const cards = `
<div class="board" style="grid-template-columns: repeat(${dimensions}, auto);">
${items
.map(
(item) => `
<div class="card">
<div class="card-front"></div>
<div class="card-back">${item}</div>
</div>
`,
)
.join('')}
</div>
`;
selectors.boardContainer.innerHTML = cards;
};
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);
};
const flipBackCards = () => {
document.querySelectorAll('.card:not(.matched)').forEach((card) => {
card.classList.remove('flipped');
});
state.flippedCards = 0;
};
const flipCard = (card) => {
if (!state.gameStarted) startGame();
if (state.flippedCards < 2 && !card.classList.contains('flipped')) {
card.classList.add('flipped');
state.flippedCards++;
state.totalFlips++;
}
if (state.flippedCards === 2) {
const flippedCards = document.querySelectorAll('.flipped:not(.matched)');
if (flippedCards[0].querySelector('.card-back').innerText === flippedCards[1].querySelector('.card-back').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');
document.getElementById('win').style.display = "flex";
selectors.win.innerHTML = `
You Won!<br>
With <span class="highlight">${state.totalFlips}</span> moves<br>
In <span class="highlight">${state.totalTime}</span> seconds
`;
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();
}
});
};
generateGame();
attachEventListeners();