-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcards.html
More file actions
189 lines (168 loc) · 5.59 KB
/
cards.html
File metadata and controls
189 lines (168 loc) · 5.59 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Coffee Memory Game</title>
<style>
body {
background-color: #fbeee6;
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
margin: 0;
padding: 20px;
}
h1 {
color: #8b4513;
}
.game-board {
display: grid;
grid-template-columns: repeat(4, 100px);
grid-gap: 10px;
margin-top: 20px;
}
.card {
width: 100px;
height: 100px;
perspective: 1000px;
}
.card-inner {
position: relative;
width: 100%;
height: 100%;
transition: transform 0.5s;
transform-style: preserve-3d;
cursor: pointer;
}
.flipped {
transform: rotateY(180deg);
}
.card-front, .card-back {
position: absolute;
width: 100%;
height: 100%;
backface-visibility: hidden;
border-radius: 8px;
}
.card-front {
background-color: #fff;
border: 2px solid #8b4513;
display: flex;
justify-content: center;
align-items: center;
font-size: 2em;
color: #8b4513;
}
.card-back {
background: url('https://via.placeholder.com/100x100?text=Coffee') center/cover;
transform: rotateY(180deg);
border: 2px solid #8b4513;
border-radius: 8px;
}
button {
background: #8b4513;
color: #fff;
border: none;
padding: 10px 20px;
margin: 15px;
cursor: pointer;
border-radius: 5px;
}
button:hover {
background: #5a2d0c;
}
</style>
</head>
<body>
<h1>Coffee Memory Game</h1>
<p id="message">Click the button to preview all cards.</p>
<button id="previewBtn">Preview Cards</button>
<div class="game-board" id="gameBoard"></div>
<script>
// List of coffee types (you can replace emoji with images)
const coffeeCards = ['☕', '🍵', '🥛', '🫖', '☕', '🍵', '🥛', '🫖'];
// Shuffle array using Fisher-Yates algorithm
function shuffle(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
}
let flippedCards = [];
let matchedCount = 0;
let allowClicks = false; // Clicking enabled only after preview
const gameBoard = document.getElementById('gameBoard');
const messageEl = document.getElementById('message');
const previewBtn = document.getElementById('previewBtn');
const shuffledCards = shuffle(coffeeCards.slice()); // Shuffle a copy
// Create card elements (initially face down)
shuffledCards.forEach((coffee) => {
const card = document.createElement('div');
card.classList.add('card');
card.dataset.coffee = coffee;
const cardInner = document.createElement('div');
cardInner.classList.add('card-inner');
const cardFront = document.createElement('div');
cardFront.classList.add('card-front');
cardFront.textContent = coffee;
const cardBack = document.createElement('div');
cardBack.classList.add('card-back');
cardInner.appendChild(cardFront);
cardInner.appendChild(cardBack);
card.appendChild(cardInner);
gameBoard.appendChild(card);
// Add click event listener once preview is done
card.addEventListener('click', () => {
if (allowClicks) {
flipCard(card);
}
});
});
// Preview function: flip all cards to reveal them briefly
previewBtn.addEventListener('click', () => {
allowClicks = false; // disable clicks during preview
messageEl.textContent = "Memorize the positions!";
previewBtn.disabled = true;
const allCardInners = document.querySelectorAll('.card-inner');
allCardInners.forEach(cardInner => cardInner.classList.add('flipped'));
// After 5 seconds, hide the cards and start the game
setTimeout(() => {
allCardInners.forEach(cardInner => cardInner.classList.remove('flipped'));
messageEl.textContent = "Start matching the cards!";
allowClicks = true;
}, 5000);
});
// Flip card function
function flipCard(card) {
// Ignore clicks on already flipped/matched cards
if (card.querySelector('.card-inner').classList.contains('flipped') && flippedCards.includes(card)) return;
card.querySelector('.card-inner').classList.add('flipped');
flippedCards.push(card);
// If two cards are flipped, check for a match
if (flippedCards.length === 2) {
allowClicks = false; // disable clicking during match check
setTimeout(checkForMatch, 800);
}
}
// Check if the two flipped cards match
function checkForMatch() {
const [card1, card2] = flippedCards;
if (card1.dataset.coffee === card2.dataset.coffee) {
matchedCount += 2;
} else {
card1.querySelector('.card-inner').classList.remove('flipped');
card2.querySelector('.card-inner').classList.remove('flipped');
}
flippedCards = [];
allowClicks = true;
// Check if game is over
if (matchedCount === coffeeCards.length) {
setTimeout(() => alert('Congratulations! You matched all the coffee cards!'), 300);
}
}
</script>
</body>
</html>