Skip to content

Commit 747819c

Browse files
authored
Add files via upload
1 parent d0c92f2 commit 747819c

File tree

1 file changed

+143
-1
lines changed

1 file changed

+143
-1
lines changed

pairsgame/pairsgame.html

Lines changed: 143 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,143 @@
1-
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Pairs Game</title>
7+
<style>
8+
body {
9+
font-family: 'Arial', sans-serif;
10+
background-color: #f4f4f4;
11+
display: flex;
12+
flex-direction: column;
13+
justify-content: center;
14+
align-items: center;
15+
height: 100vh;
16+
margin: 0;
17+
}
18+
19+
.game-container {
20+
display: grid;
21+
grid-template-columns: repeat(4, 100px);
22+
grid-gap: 10px;
23+
margin-top: 20px;
24+
}
25+
26+
.card {
27+
width: 100px;
28+
height: 100px;
29+
background-color: #007bff;
30+
color: white;
31+
font-size: 32px;
32+
display: flex;
33+
justify-content: center;
34+
align-items: center;
35+
cursor: pointer;
36+
border-radius: 10px;
37+
user-select: none;
38+
}
39+
40+
.card.flipped {
41+
background-color: #fff;
42+
color: #007bff;
43+
cursor: default;
44+
}
45+
46+
h1 {
47+
color: #333;
48+
}
49+
</style>
50+
</head>
51+
<body>
52+
<h1>Pairs Game</h1>
53+
<div class="game-container" id="gameContainer"></div>
54+
55+
<script>
56+
const cardsArray = [
57+
{ name: 'A', id: 1 },
58+
{ name: 'B', id: 2 },
59+
{ name: 'C', id: 3 },
60+
{ name: 'D', id: 4 },
61+
{ name: 'A', id: 5 },
62+
{ name: 'B', id: 6 },
63+
{ name: 'C', id: 7 },
64+
{ name: 'D', id: 8 }
65+
];
66+
67+
let firstCard = null;
68+
let secondCard = null;
69+
let lockBoard = false;
70+
let matchedPairs = 0;
71+
72+
function shuffle(array) {
73+
for (let i = array.length - 1; i > 0; i--) {
74+
const j = Math.floor(Math.random() * (i + 1));
75+
[array[i], array[j]] = [array[j], array[i]];
76+
}
77+
return array;
78+
}
79+
80+
function createBoard() {
81+
const gameContainer = document.getElementById('gameContainer');
82+
const shuffledCards = shuffle(cardsArray);
83+
84+
shuffledCards.forEach(card => {
85+
const cardElement = document.createElement('div');
86+
cardElement.classList.add('card');
87+
cardElement.dataset.name = card.name;
88+
cardElement.dataset.id = card.id;
89+
cardElement.addEventListener('click', flipCard);
90+
gameContainer.appendChild(cardElement);
91+
});
92+
}
93+
94+
function flipCard() {
95+
if (lockBoard || this === firstCard) return;
96+
97+
this.classList.add('flipped');
98+
this.textContent = this.dataset.name;
99+
100+
if (!firstCard) {
101+
firstCard = this;
102+
return;
103+
}
104+
105+
secondCard = this;
106+
checkForMatch();
107+
}
108+
109+
function checkForMatch() {
110+
const isMatch = firstCard.dataset.name === secondCard.dataset.name;
111+
isMatch ? disableCards() : unflipCards();
112+
}
113+
114+
function disableCards() {
115+
firstCard.removeEventListener('click', flipCard);
116+
secondCard.removeEventListener('click', flipCard);
117+
resetBoard();
118+
matchedPairs++;
119+
120+
if (matchedPairs === cardsArray.length / 2) {
121+
setTimeout(() => alert('Congratulations! You matched all pairs!'), 500);
122+
}
123+
}
124+
125+
function unflipCards() {
126+
lockBoard = true;
127+
setTimeout(() => {
128+
firstCard.classList.remove('flipped');
129+
secondCard.classList.remove('flipped');
130+
firstCard.textContent = '';
131+
secondCard.textContent = '';
132+
resetBoard();
133+
}, 1000);
134+
}
135+
136+
function resetBoard() {
137+
[firstCard, secondCard, lockBoard] = [null, null, false];
138+
}
139+
140+
createBoard();
141+
</script>
142+
</body>
143+
</html>

0 commit comments

Comments
 (0)