Sudoku Game Explain
Sudoku Game Explain
DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sudoku Solver 1</title>
<link
href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css"
rel="stylesheet">
<style>
body {
min-height: 100vh;
background: linear-gradient(to bottom, #87ceeb, #f0e68c);
font-family: system-ui, -apple-system, sans-serif;
}
.grid-container {
width: min(90vw, 500px);
aspect-ratio: 1;
margin: 0 auto;
}
.grid {
display: grid;
grid-template-columns: repeat(9, 1fr);
grid-template-rows: repeat(9, 1fr);
gap: 1px;
background: #2f4f4f;
padding: 2px;
border-radius: 8px;
width: 100%;
height: 100%;
box-shadow:
0 4px 6px -1px rgba(0, 0, 0, 0.1),
0 2px 4px -1px rgba(0, 0, 0, 0.06);
}
.cell {
width: 100%;
height: 100%;
display: flex;
align-items: center;
justify-content: center;
background: white;
font-size: clamp(14px, 3vw, 24px);
border: none;
text-align: center;
appearance: none;
outline: none;
}
.cell:focus {
background: #e8f4ff;
box-shadow: inset 0 0 0 2px #3b82f6;
}
.pre-filled {
background: #f0f0f0;
font-weight: bold;
}
.btn {
background: #2f4f4f;
color: white;
padding: 0.5rem 1rem;
border-radius: 0.375rem;
font-weight: 500;
transition: all 0.2s;
border: none;
cursor: pointer;
}
.btn:hover {
background: #3d6363;
transform: translateY(-1px);
}
.btn:active {
transform: translateY(0);
}
<script>
const gridElement = document.getElementById('grid');
gridElement.appendChild(input);
}
// Check column
for (let r = 0; r < 9; r++) {
if (grid[r][col] === num) return false;
}
function solveSudoku() {
const grid = getGrid();
if (!isGridValid(grid)) {
alert('Invalid grid! Please fix it before solving.');
return;
}
if (solve(grid)) {
setGrid(grid);
} else {
alert('No solution exists!');
}
}
function resetGrid() {
const inputs = document.querySelectorAll('.cell');
inputs.forEach(input => {
input.value = '';
});
}
</script>
</body>
</html>
1. HTML Structure
• <html> aur <head>
• HTML ka basic structure diya gaya hai, jisme page ka title "Sudoku
Solver" rakha gaya hai.
• Tailwind CSS link ki gayi hai, jo page ke design ke liye use hoti hai.
• Internal CSS diya gaya hai grid aur cells ko style karne ke liye.
• Body Section
2. CSS (Styling)
• Grid aur Cells
• grid-container: Ek square container hai jo maximum 500px width aur
height ka hai.
• grid: CSS Grid system ka use karke 9x9 cells banayi gayi hain.
• cell: Har cell ek <input> field hai jo number (1-9) accept karti hai.
• Focus Effect: Jab cell par click karo, toh wo highlight hoti hai.
• Pre-filled Cells: Grid ke kuch sections ko light grey background
diya gaya hai.
3. JavaScript Functionality
A. Grid Creation
• for loop se 81 cells dynamically banayi gayi hain.
• Row aur Column ke indexes ke basis par kuch cells ko light grey highlight kiya
gaya hai (e.g., center, corners).
• Har cell ke liye ek input listener lagaya gaya hai jo ensure karta hai ki sirf valid
numbers (1-9) hi input kiye ja sakte hain.
B. Helper Functions
1. getGrid():
• Yeh function har cell ka current value read karke ek 9x9 matrix (array)
return karta hai.
2. setGrid(grid, preFilled):
• Yeh function ek diya gaya 9x9 matrix grid ko render karta hai.
• Agar preFilled = true ho, toh pre-filled cells ko disabled aur bold kar
deta hai.
3. isGridValid(grid):
C. Backtracking Algorithm
• solve(grid):
• Sudoku ko recursively solve karta hai.
• Empty Spot (value = 0) find karta hai.
• Har possible number (1-9) ko check karta hai ki kya wo valid hai.
• Agar valid ho, toh number place karta hai aur recursive call karta hai.
• Agar kisi stage par solution fail ho, toh backtrack karta hai.
D. Button Functions
1. solveSudoku():
4. Highlighted Features
1. Responsive Design:
• Small screens ke liye grid aur font size adjust hota hai.
2. User-Friendly Input:
Code Ka Flow
1. Page load hone par empty 9x9 grid render hoti hai.
2. User apne numbers input karta hai.
3. "Solve Sudoku" button click karne par:
• Validation hoti hai (invalid grid ko solve nahi kiya jaata).
• Backtracking Algorithm se solution calculate hota hai.
4. "Reset Grid" button poori grid ko reset kar deta hai.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sudoku Solver</title>
<!-- Tailwind CSS for styling -->
<link
href="https://cdnjs.cloudflare.com/ajax/libs/tailwindcss/2.2.19/tailwind.min.css"
rel="stylesheet">
<style>
/* Overall page styling */
body {
min-height: 100vh;
background: linear-gradient(to bottom, #87ceeb, #f0e68c); /* Gradient
background */
font-family: system-ui, -apple-system, sans-serif; /* Font style */
}
/* Button styling */
.btn {
background: #2f4f4f; /* Dark background */
color: white; /* White text */
padding: 0.5rem 1rem; /* Padding for size */
border-radius: 0.375rem; /* Rounded corners */
font-weight: 500; /* Bold text */
transition: all 0.2s; /* Smooth transition */
border: none; /* No border */
cursor: pointer; /* Pointer cursor */
}
<script>
// Select the grid container
const gridElement = document.getElementById('grid');
function solveSudoku() {
const grid = getGrid(); // Get current grid
if (!isGridValid(grid)) {
alert('Invalid grid! Fix it first.');
return;
}
if (solve(grid)) {
setGrid(grid);
} else {
alert('No solution exists!');
}
}
function resetGrid() {
const inputs = document.querySelectorAll('.cell');
inputs.forEach(input => input.value = ''); // Clear all cells
}
</script>
</body>
</html>