Skip to content

GameΒ #5447

Description

@samihtmlcss
<title>Code Battle - Puzzle Game</title>

Code Battle - Memory Puzzle

Score: 0

Save Score
<script src="script.js"></script>

const emojis = ['πŸ’»','⚑','πŸ”₯','πŸš€','🧠','πŸ–±οΈ','⌨️','🧩'];
let cards = [...emojis, ...emojis];
cards.sort(() => 0.5 - Math.random());

const grid = document.getElementById("gameGrid");
let first = null, second = null, lock = false, score = 0;

cards.forEach((emoji, index) => {
const card = document.createElement("div");
card.className = "card";
card.dataset.emoji = emoji;
card.innerText = "?";
card.onclick = () => flip(card);
grid.appendChild(card);
});

function flip(card) {
if (lock || card.innerText !== "?") return;
card.innerText = card.dataset.emoji;
if (!first) {
first = card;
} else {
second = card;
check();
}
}

function check() {
lock = true;
if (first.dataset.emoji === second.dataset.emoji) {
score += 10;
document.getElementById("score").innerText = score;
first = second = null;
lock = false;
} else {
setTimeout(() => {
first.innerText = "?";
second.innerText = "?";
first = second = null;
lock = false;
}, 1000);
}
}

function saveScore() {
alert("Score saved locally: " + score);
}
body {
margin: 0;
font-family: Arial;
background: linear-gradient(to right, #1e3c72, #2a5298);
color: white;
text-align: center;
}

h1 {
margin-top: 20px;
}

.grid {
display: grid;
grid-template-columns: repeat(4, 100px);
gap: 10px;
justify-content: center;
margin-top: 30px;
}

.card {
width: 100px;
height: 100px;
background: white;
color: black;
font-size: 24px;
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
border-radius: 8px;
user-select: none;
}

#scoreBox {
margin-top: 20px;
}

button {
padding: 10px 20px;
background: gold;
border: none;
font-weight: bold;
cursor: pointer;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions