// Timer functionality let timerInterval = null; let startTime = null; let elapsedTime = 0; let timerRunning = false; function toggleTimer() { const btn = document.getElementById('timerBtn'); if (!timerRunning) { startTime = Date.now() - elapsedTime; timerInterval = setInterval(updateTimer, 100); btn.textContent = 'Stop Timer'; btn.classList.add('running'); timerRunning = true; } else { clearInterval(timerInterval); btn.textContent = 'Resume Timer'; btn.classList.remove('running'); timerRunning = false; } } function updateTimer() { elapsedTime = Date.now() - startTime; const seconds = Math.floor(elapsedTime / 1000); const minutes = Math.floor(seconds / 60); const displaySeconds = seconds % 60; document.getElementById('timer').textContent = `${String(minutes).padStart(2, '0')}:${String(displaySeconds).padStart(2, '0')}`; } function stopTimerForWin() { if (timerRunning) { clearInterval(timerInterval); timerRunning = false; const finalTimeDiv = document.getElementById('finalTime'); const timerDisplay = document.getElementById('timer').textContent; finalTimeDiv.innerHTML = `🎉 Completed in ${timerDisplay}! 🎉`; finalTimeDiv.style.display = 'block'; finalTimeDiv.className = 'final-time'; document.getElementById('timerBtn').style.display = 'none'; } } // Word Search Game with Click-to-Select const gridSize = 15; let grid = []; let selectedCells = []; let foundWords = new Set(); let lockedCells = new Set(); // Cells that are part of found words function createGrid() { grid = Array(gridSize).fill().map(() => Array(gridSize).fill('')); // Place words words.forEach(word => { let placed = false; let attempts = 0; while (!placed && attempts < 100) { const direction = Math.floor(Math.random() * 8); const row = Math.floor(Math.random() * gridSize); const col = Math.floor(Math.random() * gridSize); if (canPlaceWord(word, row, col, direction)) { placeWord(word, row, col, direction); placed = true; } attempts++; } }); // Fill empty cells for (let i = 0; i < gridSize; i++) { for (let j = 0; j < gridSize; j++) { if (grid[i][j] === '') { grid[i][j] = String.fromCharCode(65 + Math.floor(Math.random() * 26)); } } } } function canPlaceWord(word, row, col, direction) { const directions = [ [0, 1], [1, 0], [1, 1], [-1, 1], [0, -1], [-1, 0], [-1, -1], [1, -1] ]; const [dx, dy] = directions[direction]; for (let i = 0; i < word.length; i++) { const newRow = row + (dx * i); const newCol = col + (dy * i); if (newRow < 0 || newRow >= gridSize || newCol < 0 || newCol >= gridSize) { return false; } if (grid[newRow][newCol] !== '' && grid[newRow][newCol] !== word[i]) { return false; } } return true; } function placeWord(word, row, col, direction) { const directions = [ [0, 1], [1, 0], [1, 1], [-1, 1], [0, -1], [-1, 0], [-1, -1], [1, -1] ]; const [dx, dy] = directions[direction]; for (let i = 0; i < word.length; i++) { const newRow = row + (dx * i); const newCol = col + (dy * i); grid[newRow][newCol] = word[i]; } } function renderGrid() { const gridElement = document.getElementById('grid'); gridElement.innerHTML = ''; for (let i = 0; i < gridSize; i++) { for (let j = 0; j < gridSize; j++) { const cell = document.createElement('div'); cell.className = 'cell'; cell.textContent = grid[i][j]; cell.dataset.row = i; cell.dataset.col = j; // Click to select/deselect cell.addEventListener('click', () => handleCellClick(i, j)); gridElement.appendChild(cell); } } } function handleCellClick(row, col) { const cellKey = `${row},${col}`; // Can't interact with locked cells (already found words) if (lockedCells.has(cellKey)) { return; } // Check if clicking same cell to deselect const existingIndex = selectedCells.findIndex(c => c.row === row && c.col === col); if (existingIndex !== -1) { // Deselect this cell selectedCells.splice(existingIndex, 1); updateSelection(); return; } // Add cell to selection selectedCells.push({row, col}); updateSelection(); // Check if we've formed a valid word checkForWord(); } function checkForWord() { const selectedWord = selectedCells.map(cell => grid[cell.row][cell.col]).join(''); const reversedWord = selectedWord.split('').reverse().join(''); // Check if it matches a word we're looking for if (words.includes(selectedWord) && !foundWords.has(selectedWord)) { foundWords.add(selectedWord); markAsFound(); renderWordList(); } else if (words.includes(reversedWord) && !foundWords.has(reversedWord)) { foundWords.add(reversedWord); markAsFound(); renderWordList(); } } function renderWordList() { const wordListElement = document.getElementById('wordList'); wordListElement.innerHTML = ''; words.forEach(word => { const wordItem = document.createElement('div'); wordItem.className = 'word-item' + (foundWords.has(word) ? ' found' : ''); wordItem.textContent = word; wordListElement.appendChild(wordItem); }); document.getElementById('foundCount').textContent = foundWords.size; document.getElementById('totalCount').textContent = words.length; // Check if all words found if (foundWords.size === words.length) { stopTimerForWin(); } } function updateSelection() { document.querySelectorAll('.cell').forEach(cell => { const row = parseInt(cell.dataset.row); const col = parseInt(cell.dataset.col); const cellKey = `${row},${col}`; // Don't change appearance of locked cells if (lockedCells.has(cellKey)) { return; } cell.classList.remove('selected'); }); selectedCells.forEach(({row, col}) => { const cell = document.querySelector(`[data-row="${row}"][data-col="${col}"]`); if (cell) { cell.classList.add('selected'); } }); } function markAsFound() { // Lock these cells permanently selectedCells.forEach(({row, col}) => { const cellKey = `${row},${col}`; lockedCells.add(cellKey); const cell = document.querySelector(`[data-row="${row}"][data-col="${col}"]`); if (cell) { cell.classList.remove('selected'); cell.classList.add('found'); } }); // Clear selection selectedCells = []; } // Initialize createGrid(); renderGrid(); renderWordList();