Spaces:
Running
Running
File size: 6,280 Bytes
200da92 636c063 200da92 |
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 190 191 192 193 194 |
document.addEventListener('DOMContentLoaded', () => {
// Game state
let gameState = {
score: 0,
level: 1,
moves: 0,
boardSize: 4,
tiles: [],
matchedPairs: 0,
firstSelected: null,
secondSelected: null,
waiting: false,
hintActive: false
};
// DOM elements
const gameBoard = document.querySelector('custom-game-board');
const scorePanel = document.querySelector('custom-score-panel');
const newGameBtn = document.getElementById('newGameBtn');
const hintBtn = document.getElementById('hintBtn');
// Initialize game
function initGame() {
// Add visual feedback
newGameBtn.innerHTML = '<span class="animate-pulse">Creating...</span>';
setTimeout(() => {
gameState = {
score: 0,
level: 1,
moves: 0,
boardSize: 4,
tiles: generateTiles(4),
matchedPairs: 0,
firstSelected: null,
secondSelected: null,
waiting: false,
hintActive: false
};
// Set up event listener for tile clicks
gameBoard.addEventListener('tile-click', (e) => {
handleTileClick(e.detail.index);
});
updateScorePanel();
renderBoard();
// Add success animation
newGameBtn.innerHTML = 'New Puzzle';
newGameBtn.classList.add('bg-green-500');
setTimeout(() => {
newGameBtn.classList.remove('bg-green-500');
newGameBtn.classList.add('bg-purple-600');
}, 1000);
// Play sound for feedback
if (typeof Audio !== 'undefined') {
const audio = new Audio('https://assets.mixkit.co/sfx/preview/mixkit-positive-interface-beep-221.mp3');
audio.volume = 0.3;
audio.play();
}
}, 500);
}
// Generate tiles for the board
function generateTiles(size) {
const totalPairs = (size * size) / 2;
const symbols = [];
// Generate unique symbols
for (let i = 0; i < totalPairs; i++) {
symbols.push(String.fromCharCode(65 + i));
symbols.push(String.fromCharCode(65 + i));
}
// Shuffle the symbols
return shuffleArray(symbols);
}
// Fisher-Yates shuffle algorithm
function shuffleArray(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;
}
// Render the game board
function renderBoard() {
gameBoard.setAttribute('size', gameState.boardSize);
gameBoard.setAttribute('tiles', JSON.stringify(gameState.tiles));
}
// Update score panel
function updateScorePanel() {
scorePanel.setAttribute('score', gameState.score);
scorePanel.setAttribute('level', gameState.level);
scorePanel.setAttribute('moves', gameState.moves);
}
// Handle tile click
function handleTileClick(index) {
if (gameState.waiting || gameState.tiles[index] === null || index === gameState.firstSelected) {
return;
}
if (gameState.firstSelected === null) {
gameState.firstSelected = index;
gameBoard.setAttribute('selected', index.toString());
} else {
gameState.secondSelected = index;
gameState.moves++;
checkForMatch();
}
}
// Check if the selected tiles match
function checkForMatch() {
const firstIndex = gameState.firstSelected;
const secondIndex = gameState.secondSelected;
if (gameState.tiles[firstIndex] === gameState.tiles[secondIndex]) {
// Match found
gameState.tiles[firstIndex] = null;
gameState.tiles[secondIndex] = null;
gameState.matchedPairs++;
gameState.score += 10 * gameState.level;
// Check for level completion
if (gameState.matchedPairs === (gameState.boardSize * gameState.boardSize) / 2) {
setTimeout(() => {
levelUp();
}, 1000);
}
gameBoard.setAttribute('matched', `${firstIndex},${secondIndex}`);
} else {
// No match
gameState.waiting = true;
setTimeout(() => {
gameBoard.removeAttribute('selected');
gameState.waiting = false;
}, 1000);
}
gameState.firstSelected = null;
gameState.secondSelected = null;
updateScorePanel();
}
// Level up the game
function levelUp() {
gameState.level++;
gameState.boardSize = Math.min(8, gameState.boardSize + 2); // Max size 8x8
gameState.tiles = generateTiles(gameState.boardSize);
gameState.matchedPairs = 0;
updateScorePanel();
renderBoard();
}
// Give hint
function giveHint() {
if (gameState.hintActive) return;
gameState.hintActive = true;
hintBtn.classList.add('bg-purple-600');
// Find first unmatched pair
let firstUnmatched = null;
for (let i = 0; i < gameState.tiles.length; i++) {
if (gameState.tiles[i] !== null) {
if (firstUnmatched === null) {
firstUnmatched = i;
} else if (gameState.tiles[i] === gameState.tiles[firstUnmatched]) {
gameBoard.setAttribute('hint', `${firstUnmatched},${i}`);
break;
}
}
}
setTimeout(() => {
gameBoard.removeAttribute('hint');
gameState.hintActive = false;
hintBtn.classList.remove('bg-purple-600');
}, 2000);
}
// Event listeners
newGameBtn.addEventListener('click', initGame);
hintBtn.addEventListener('click', giveHint);
// Initialize the game
initGame();
}); |