Pair game chat gpt
<!DOCTYPE html>
<html>
<head>
<title>Simple Match Game</title>
<style>
button {
font-size: 30px;
padding: 20px;
margin: 10px;
width: 100px;
height: 100px;
cursor: pointer;
}
.hidden {
visibility: hidden;
}
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 40px;
}
#message {
font-weight: bold;
}
</style>
</head>
<body>
<h1>Match A with Apple, B with Ball</h1>
<!-- Buttons -->
<button id="A">A</button>
<button id="Apple">🍎</button>
<button id="B">B</button>
<button id="Ball">🏀</button>
<!-- Score and Message -->
<p>Score: <span id="score">0</span></p>
<p id="message">Click two buttons to match!</p>
<button onclick="restart()">Restart</button>
<script>
let score = 0;
let selected = [];
const pairs = {
A: "Apple",
Apple: "A",
B: "Ball",
Ball: "B"
};
const buttons = ["A", "Apple", "B", "Ball"];
// Add click events to each button
buttons.forEach(id => {
const btn = document.getElementById(id);
btn.onclick = () => selectButton(btn);
});
function selectButton(btn) {
if (btn.classList.contains("hidden") || selected.includes(btn)) return;
selected.push(btn);
if (selected.length === 2) {
setTimeout(checkMatch, 500); // slight delay for better UX
}
}
function checkMatch() {
const [btn1, btn2] = selected;
if (pairs[btn1.id] === btn2.id) {
btn1.classList.add("hidden");
btn2.classList.add("hidden");
score++;
document.getElementById("score").textContent = score;
document.getElementById("message").textContent = "Match found!";
if (score === 2) {
document.getElementById("message").textContent = "You win!";
}
} else {
document.getElementById("message").textContent = "No match, try again!";
}
selected = [];
}
function restart() {
score = 0;
selected = [];
document.getElementById("score").textContent = "0";
document.getElementById("message").textContent = "Click two buttons to match!";
buttons.forEach(id => {
const btn = document.getElementById(id);
btn.classList.remove("hidden");
});
}
</script>
</body>
</html>
Comments
Post a Comment