-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemory-match.html
186 lines (181 loc) · 5.84 KB
/
memory-match.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Memory Match Game</title>
<style>
body {
text-align: center;
font-family: 'Poppins', sans-serif;
margin: 0;
padding: 0;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
min-height: 100vh;
background: linear-gradient(135deg, #1e3c72, #2a5298);
color: #fff;
}
.game-board {
display: grid;
grid-gap: 15px;
justify-content: center;
margin-top: 20px;
padding: 10px;
width: 90vw;
max-width: 600px;
}
.card {
background: #ffffff33;
display: flex;
align-items: center;
justify-content: center;
font-size: 24px;
cursor: pointer;
border-radius: 10px;
box-shadow: 2px 2px 10px rgba(0, 0, 0, 0.2);
aspect-ratio: 1 / 1;
transition: 0.3s;
}
.card:hover {
background: #ffffff55;
}
.flipped {
background: #ffffff;
color: #333;
}
.controls {
margin: 15px;
display: flex;
gap: 15px;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
label {
font-size: 18px;
font-weight: bold;
}
select, button {
padding: 12px;
font-size: 16px;
border-radius: 5px;
border: none;
cursor: pointer;
transition: 0.3s;
}
select {
background: #ffffff;
color: #333;
}
button {
background: #ff9800;
color: white;
}
button:hover {
background: #e68900;
}
#winScreen {
display: none;
position: fixed;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(0, 0, 0, 0.9);
color: white;
padding: 25px;
text-align: center;
border-radius: 10px;
box-shadow: 0px 0px 10px rgba(0, 0, 0, 0.5);
}
#restartBtn {
margin-top: 15px;
cursor: pointer;
padding: 12px;
background: #ff9800;
border: none;
border-radius: 5px;
color: white;
font-size: 16px;
}
</style>
</head>
<body>
<h1>Memory Match Game</h1>
<div class="controls">
<label for="gridSize">Grid Size:</label>
<select id="gridSize">
<option value="8" selected>8 Grid</option>
<option value="12">12 Grid</option>
<option value="16">16 Grid</option>
<option value="24">24 Grid</option>
<option value="36">36 Grid</option>
</select>
<button onclick="startGame()">Start Game</button>
</div>
<div class="game-board" id="gameBoard"></div>
<div id="winScreen">
<h2>You Win!</h2>
<button id="restartBtn" onclick="startGame()">Restart</button>
</div>
<script>
let symbols = [];
let selectedCards = [];
let matchedPairs = 0;
let gridSize = 8;
const gameBoard = document.getElementById("gameBoard");
const gridSizeSelect = document.getElementById("gridSize");
const winScreen = document.getElementById("winScreen");
function startGame() {
gameBoard.innerHTML = "";
winScreen.style.display = "none";
selectedCards = [];
matchedPairs = 0;
gridSize = parseInt(gridSizeSelect.value);
let cols = Math.ceil(Math.sqrt(gridSize));
if (cols % 2 !== 0) cols += 1;
let rows = Math.ceil(gridSize / cols);
gameBoard.style.gridTemplateColumns = `repeat(${cols}, 1fr)`;
const uniqueSymbols = ['🍎', '🍌', '🍇', '🍉', '🥕', '🌽', '🍓', '🍊', '🍒', '🍍', '🥑', '🥭', '🍈', '🥥', '🍋', '🍏', '🍐', '🥝'];
symbols = uniqueSymbols.slice(0, gridSize / 2);
symbols = [...symbols, ...symbols].sort(() => Math.random() - 0.5);
symbols.forEach((symbol, index) => {
const card = document.createElement("div");
card.classList.add("card");
card.dataset.index = index;
card.dataset.symbol = symbol;
card.addEventListener("click", flipCard);
gameBoard.appendChild(card);
});
}
function flipCard() {
if (selectedCards.length < 2 && !this.classList.contains("flipped")) {
this.textContent = this.dataset.symbol;
this.classList.add("flipped");
selectedCards.push(this);
if (selectedCards.length === 2) {
setTimeout(checkMatch, 500);
}
}
}
function checkMatch() {
if (selectedCards[0].dataset.symbol === selectedCards[1].dataset.symbol) {
matchedPairs++;
selectedCards = [];
if (matchedPairs === symbols.length / 2) {
setTimeout(() => winScreen.style.display = "block", 300);
}
} else {
selectedCards.forEach(card => {
card.textContent = "";
card.classList.remove("flipped");
});
selectedCards = [];
}
}
startGame();
</script>
</body>
</html>