Skip to content

Commit 95f65a6

Browse files
authored
Merge pull request #1185 from kushpo357/SudokuSolver-kushpo357-branch
Sudoku solver kushpo357 branch
2 parents d0629f5 + 5bec37e commit 95f65a6

File tree

7 files changed

+224
-0
lines changed

7 files changed

+224
-0
lines changed

SudokuSolver/kushpo357/Readme.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Sudoku Solver
2+
3+
A web-based Sudoku Solver that allows users to input a 9x9 Sudoku puzzle and solve it with a single click. Built with HTML, CSS, JavaScript, and TailwindCSS.
4+
5+
## Features
6+
- Interactive 9x9 Sudoku grid for input.
7+
- Validates input to allow only numbers 1-9.
8+
- Solves the puzzle instantly or alerts if no solution exists.
9+
- Reset functionality to clear the grid.
10+
11+
## Tech Stack
12+
- **Frontend**: HTML, TailwindCSS for styling.
13+
- **Logic**: JavaScript for puzzle solving using a backtracking algorithm.
14+
15+
## File Structure
16+
- `index.html`: Core structure of the webpage.
17+
- `styles.css`: Styling for the Sudoku grid and buttons.
18+
- `script.js`: Logic for solving the Sudoku puzzle and interactive functionalities.
19+
20+
## How to Use
21+
1. Open `index.html` in your browser.
22+
2. Enter numbers 1-9 in the grid (leave empty cells as blank).
23+
3. Click **Solve** to see the solution or **Reset** to clear the grid.
24+
25+
## Algorithm
26+
- Uses a backtracking approach to solve the Sudoku puzzle.
27+
- Checks each cell to ensure compliance with Sudoku rules.
28+
29+
## Demo
30+
Initial position
31+
![alt text](image.png)
32+
33+
inputing a sudoku problem
34+
![alt text](image-1.png)
35+
36+
submitting and generating a solution
37+
![alt text](image-2.png)
38+
---
39+
40+

SudokuSolver/kushpo357/image-1.png

30.9 KB
Loading

SudokuSolver/kushpo357/image-2.png

51.4 KB
Loading

SudokuSolver/kushpo357/image.png

22.3 KB
Loading

SudokuSolver/kushpo357/index.html

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6+
<title>Sudoku Solver</title>
7+
<script src="https://cdn.tailwindcss.com"></script>
8+
<link rel="stylesheet" href="styles.css">
9+
</head>
10+
<body class="bg-gray-100 text-gray-800">
11+
<div class="container mx-auto text-center py-8">
12+
<h1 class="text-4xl font-bold mb-4">Sudoku Solver</h1>
13+
<p class="text-lg mb-4">Enter your 9x9 Sudoku puzzle:</p>
14+
<form>
15+
<table id="sudokuTable" class="mx-auto border-collapse">
16+
</table>
17+
<div class="mt-4 flex justify-center space-x-4">
18+
<button id="resetButton" type="reset" class="button bg-gray-200 px-6 py-3 rounded-md shadow-md hover:bg-gray-300">
19+
Reset
20+
</button>
21+
<button id="solveButton" type="button" onclick="solveSudoku()" class="button bg-blue-500 text-white px-6 py-3 rounded-md shadow-md hover:bg-blue-600">
22+
Solve
23+
</button>
24+
</div>
25+
</form>
26+
</div>
27+
<script src="script.js"></script>
28+
</body>
29+
</html>

SudokuSolver/kushpo357/script.js

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
class SudokuSolver {
2+
constructor() {
3+
this.N = 9;
4+
}
5+
6+
isSafe(row, col, val, grid) {
7+
for (let i = 0; i < this.N; i++) {
8+
if (
9+
grid[row][i] === val ||
10+
grid[i][col] === val ||
11+
grid[Math.floor(row / 3) * 3 + Math.floor(i / 3)][
12+
Math.floor(col / 3) * 3 + (i % 3)
13+
] === val
14+
) {
15+
return false;
16+
}
17+
}
18+
return true;
19+
}
20+
21+
solve(grid) {
22+
for (let i = 0; i < this.N; i++) {
23+
for (let j = 0; j < this.N; j++) {
24+
if (grid[i][j] === 0) {
25+
for (let k = 1; k <= 9; k++) {
26+
if (this.isSafe(i, j, k, grid)) {
27+
grid[i][j] = k;
28+
let solvePossible = this.solve(grid);
29+
if (solvePossible) {
30+
return true;
31+
} else {
32+
grid[i][j] = 0;
33+
}
34+
}
35+
}
36+
return false;
37+
}
38+
}
39+
}
40+
return true;
41+
}
42+
43+
solveSudoku(grid) {
44+
return this.solve(grid);
45+
}
46+
}
47+
48+
function initializeSudokuTable() {
49+
const sudokuTable = document.getElementById("sudokuTable");
50+
for (let i = 0; i < 9; i++) {
51+
const row = sudokuTable.insertRow(i);
52+
for (let j = 0; j < 9; j++) {
53+
const cell = row.insertCell(j);
54+
const input = document.createElement("input");
55+
input.type = "text";
56+
input.maxLength = 1;
57+
input.className =
58+
"w-full h-full text-center text-lg border-none focus:ring-2 focus:ring-blue-500";
59+
input.addEventListener("input", validateInput);
60+
cell.appendChild(input);
61+
}
62+
}
63+
}
64+
65+
function validateInput(event) {
66+
const inputValue = event.target.value;
67+
event.target.value = inputValue.replace(/[^1-9]/g, "");
68+
}
69+
70+
function getSudokuPuzzle() {
71+
const puzzle = [];
72+
const rows = document.getElementById("sudokuTable").rows;
73+
for (let i = 0; i < 9; i++) {
74+
const row = rows[i];
75+
const rowData = [];
76+
for (let j = 0; j < 9; j++) {
77+
const input = row.cells[j].querySelector("input");
78+
rowData.push(parseInt(input.value) || 0);
79+
}
80+
puzzle.push(rowData);
81+
}
82+
return puzzle;
83+
}
84+
85+
function displaySolvedPuzzle(solvedPuzzle) {
86+
const rows = document.getElementById("sudokuTable").rows;
87+
for (let i = 0; i < 9; i++) {
88+
const row = rows[i];
89+
for (let j = 0; j < 9; j++) {
90+
const input = row.cells[j].querySelector("input");
91+
input.value = solvedPuzzle[i][j];
92+
}
93+
}
94+
}
95+
96+
function solveSudoku() {
97+
const sudokuSolver = new SudokuSolver();
98+
const puzzle = getSudokuPuzzle();
99+
100+
if (sudokuSolver.solveSudoku(puzzle)) {
101+
displaySolvedPuzzle(puzzle);
102+
} else {
103+
alert("No solution exists.");
104+
}
105+
}
106+
107+
function resetSudokuTable() {
108+
const inputs = document.querySelectorAll("#sudokuTable input");
109+
inputs.forEach((input) => (input.value = ""));
110+
}
111+
112+
initializeSudokuTable();

SudokuSolver/kushpo357/styles.css

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/* General Body Styling */
2+
body {
3+
font-family: Arial, sans-serif;
4+
}
5+
6+
/* Sudoku Table Styling */
7+
#sudokuTable {
8+
margin: 0 auto;
9+
border-collapse: collapse;
10+
}
11+
12+
#sudokuTable td {
13+
border: 1px solid #000; /* Standard Grid Borders */
14+
width: 50px;
15+
height: 50px;
16+
text-align: center;
17+
font-size: 1.5rem;
18+
}
19+
20+
#sudokuTable tr:nth-child(3n) td {
21+
border-bottom: 3px solid #000; /* Thick Line After Every 3rd Row */
22+
}
23+
24+
#sudokuTable td:nth-child(3n) {
25+
border-right: 3px solid #000; /* Thick Line After Every 3rd Column */
26+
}
27+
28+
#sudokuTable tr:last-child td {
29+
border-bottom: none; /* Remove Border for Last Row */
30+
}
31+
32+
#sudokuTable td:last-child {
33+
border-right: none; /* Remove Border for Last Column */
34+
}
35+
36+
/* Button Styling */
37+
.button {
38+
transition: background-color 0.3s ease, box-shadow 0.3s ease;
39+
}
40+
41+
.button:hover {
42+
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
43+
}

0 commit comments

Comments
 (0)