-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
151 lines (129 loc) · 4.48 KB
/
script.js
File metadata and controls
151 lines (129 loc) · 4.48 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
const cards = ["🐶", "🐱", "🐰", "🦊", "🐻", "🐼", "🦁", "🐨"];
const gameBoard = document.getElementById('game-board');
const popup = document.getElementById('popup');
const overlay = document.getElementById('overlay');
const restartButton = document.getElementById('restart-game');
const closePopupButton = document.getElementById('close-popup');
const timerDisplay = document.getElementById('timer');
const popupTitle = document.getElementById('popup-title');
const popupMessage = document.getElementById('popup-message');
const startGameButton = document.getElementById('start-game');
const timeInput = document.getElementById('time-input');
const startScreen = document.getElementById('start-screen');
let flippedCards = [];
let matchedPairs = 0;
let cardPairs = [...cards, ...cards];
let timeLeft;
let timerInterval;
const shuffleCards = () => {
cardPairs.sort(() => Math.random() - 0.5);
};
const createCards = () => {
gameBoard.innerHTML = '';
cardPairs.forEach((emoji, index) => {
const card = document.createElement('div');
card.classList.add('card', 'relative', 'cursor-pointer', 'w-16', 'h-16', 'md:w-24', 'md:h-24');
card.dataset.index = index;
card.dataset.emoji = emoji;
const front = document.createElement('div');
front.classList.add('front', 'text-3xl', 'md:text-4xl');
front.textContent = emoji;
const back = document.createElement('div');
back.classList.add('back', 'text-2xl', 'md:text-3xl');
back.textContent = '❓';
card.appendChild(front);
card.appendChild(back);
card.addEventListener('click', flipCard);
gameBoard.appendChild(card);
});
};
const startTimer = () => {
timerInterval = setInterval(() => {
timeLeft--;
updateTimerDisplay();
if (timeLeft <= 0) {
clearInterval(timerInterval);
endGame(false);
}
}, 1000);
};
const updateTimerDisplay = () => {
const minutes = Math.floor(timeLeft / 60);
const seconds = timeLeft % 60;
timerDisplay.textContent = `Time Left: ${minutes}:${seconds.toString().padStart(2, '0')}`;
if (timeLeft <= 10) {
timerDisplay.classList.add('pulse');
}
};
const endGame = (isWin) => {
clearInterval(timerInterval);
if (isWin) {
popupTitle.textContent = '🎉You Won!🎉';
popupMessage.textContent = 'Congratulations! You\'ve matched all the cards.';
} else {
popupTitle.textContent = '⏰Time\'s Up!⏰';
popupMessage.textContent = 'You ran out of time. Better luck next time!';
}
timerDisplay.classList.remove('pulse');
showPopup();
};
const initGame = () => {
const userTime = parseInt(timeInput.value, 10);
if (isNaN(userTime) || userTime < 1 || userTime > 3) {
alert('Please enter a valid time between 1 and 3 minutes.');
return;
}
shuffleCards();
createCards();
flippedCards = [];
matchedPairs = 0;
timeLeft = userTime * 60;
updateTimerDisplay();
startTimer();
popup.classList.add('hidden');
overlay.classList.add('hidden');
startScreen.classList.add('hidden');
};
function flipCard(event) {
const selectedCard = event.target.closest('.card');
if (flippedCards.length >= 2 || selectedCard.classList.contains('flipped') || selectedCard.classList.contains('matched')) return;
selectedCard.classList.add('flipped');
flippedCards.push(selectedCard);
if (flippedCards.length === 2) {
setTimeout(checkMatch, 500);
}
}
function checkMatch() {
const [card1, card2] = flippedCards;
if (card1.dataset.emoji === card2.dataset.emoji) {
card1.classList.add('matched');
card2.classList.add('matched');
matchedPairs++;
if (matchedPairs === cards.length) {
endGame(true);
}
} else {
card1.classList.remove('flipped');
card2.classList.remove('flipped');
}
flippedCards = [];
}
function showPopup() {
popup.classList.remove('hidden');
overlay.classList.remove('hidden');
}
const flipAllCards = () => {
const allCards = document.querySelectorAll('.card');
allCards.forEach(card => {
card.classList.add('flipped');
});
};
startGameButton.addEventListener('click', initGame);
restartButton.addEventListener('click', () => {
startScreen.classList.remove('hidden');
initGame();
});
closePopupButton.addEventListener('click', () => {
popup.classList.add('hidden');
overlay.classList.add('hidden');
});