Sudoku Game

/* styles.css */ body { font-family: Arial, sans-serif; text-align: center; margin: 20px; } #sudoku-container { display: inline-block; margin-top: 20px; } #sudoku-grid { border-collapse: collapse; } #sudoku-grid td { border: 1px solid #999; width: 40px; height: 40px; text-align: center; } #sudoku-grid input { width: 100%; height: 100%; text-align: center; font-size: 18px; border: none; } #sudoku-grid .bold-border { border-width: 2px; } .controls { margin-top: 10px; } button { margin: 0 5px; padding: 5px 10px; } // script.js // Generate the Sudoku grid function createGrid() { const table = document.getElementById('sudoku-grid'); table.innerHTML = ''; for (let row = 0; row < 9; row++) { const tr = document.createElement('tr'); for (let col = 0; col < 9; col++) { const td = document.createElement('td'); if (row % 3 === 0) td.style.borderTop = '2px solid #000'; if (col % 3 === 0) td.style.borderLeft = '2px solid #000'; if (row === 8) td.style.borderBottom = '2px solid #000'; if (col === 8) td.style.borderRight = '2px solid #000'; const input = document.createElement('input'); input.setAttribute('type', 'text'); input.setAttribute('maxlength', '1'); input.addEventListener('input', () => { input.value = input.value.replace(/[^1-9]/g, ''); }); td.appendChild(input); tr.appendChild(td); } table.appendChild(tr); } } // Get the current state of the grid function getGrid() { const table = document.getElementById('sudoku-grid'); const grid = []; for (let row = 0; row < 9; row++) { const rowData = []; for (let col = 0; col < 9; col++) { const input = table.rows[row].cells[col].firstChild; rowData.push(input.value ? parseInt(input.value) : 0); } grid.push(rowData); } return grid; } // Set the grid with given data function setGrid(grid) { const table = document.getElementById('sudoku-grid'); for (let row = 0; row < 9; row++) { for (let col = 0; col < 9; col++) { const input = table.rows[row].cells[col].firstChild; input.value = grid[row][col] !== 0 ? grid[row][col] : ''; } } } // Clear the grid function clearGrid() { const table = document.getElementById('sudoku-grid'); for (let row = 0; row < 9; row++) { for (let col = 0; col < 9; col++) { const input = table.rows[row].cells[col].firstChild; input.value = ''; } } } // Check if it's safe to place a number function isSafe(grid, row, col, num) { for (let x = 0; x < 9; x++) { if (grid[row][x] === num || grid[x][col] === num) return false; } const startRow = row - (row % 3); const startCol = col - (col % 3); for (let i = 0; i < 3; i++) for (let j = 0; j < 3; j++) if (grid[i + startRow][j + startCol] === num) return false; return true; } // Solve the Sudoku using backtracking function solveSudoku(grid) { for (let row = 0; row < 9; row++) { for (let col = 0; col < 9; col++) { if (grid[row][col] === 0) { for (let num = 1; num <= 9; num++) { if (isSafe(grid, row, col, num)) { grid[row][col] = num; if (solveSudoku(grid)) return true; grid[row][col] = 0; } } return false; } } } return true; } // Generate a new puzzle (for simplicity, using a fixed puzzle) function generatePuzzle() { const puzzle = [ [5, 3, 0, 0, 7, 0, 0, 0, 0], [6, 0, 0, 1, 9, 5, 0, 0, 0], [0, 9, 8, 0, 0, 0, 0, 6, 0], [8, 0, 0, 0, 6, 0, 0, 0, 3], [4, 0, 0, 8, 0, 3, 0, 0, 1], [7, 0, 0, 0, 2, 0, 0, 0, 6], [0, 6, 0, 0, 0, 0, 2, 8, 0], [0, 0, 0, 4, 1, 9, 0, 0, 5], [0, 0, 0, 0, 8, 0, 0, 7, 9], ]; setGrid(puzzle); } // Solve the current puzzle function solvePuzzle() { const grid = getGrid(); if (solveSudoku(grid)) { setGrid(grid); alert('Puzzle Solved!'); } else { alert('No solution exists!'); } } // Initialize the grid on page load window.onload = () => { createGrid(); generatePuzzle(); };