Add 2 files
Browse files- README.md +5 -3
- index.html +721 -19
README.md
CHANGED
@@ -1,10 +1,12 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
colorFrom: blue
|
5 |
colorTo: blue
|
6 |
sdk: static
|
7 |
pinned: false
|
|
|
|
|
8 |
---
|
9 |
|
10 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
1 |
---
|
2 |
+
title: sudoku1
|
3 |
+
emoji: 🐳
|
4 |
colorFrom: blue
|
5 |
colorTo: blue
|
6 |
sdk: static
|
7 |
pinned: false
|
8 |
+
tags:
|
9 |
+
- deepsite
|
10 |
---
|
11 |
|
12 |
+
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
index.html
CHANGED
@@ -1,19 +1,721 @@
|
|
1 |
-
<!
|
2 |
-
<html>
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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>Advanced 9×9 Sudoku</title>
|
7 |
+
<script src="https://cdn.tailwindcss.com"></script>
|
8 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
|
9 |
+
<style>
|
10 |
+
.cell {
|
11 |
+
width: 50px;
|
12 |
+
height: 50px;
|
13 |
+
text-align: center;
|
14 |
+
font-size: 1.5rem;
|
15 |
+
font-weight: bold;
|
16 |
+
position: relative;
|
17 |
+
transition: all 0.2s;
|
18 |
+
}
|
19 |
+
|
20 |
+
.cell:focus {
|
21 |
+
outline: none;
|
22 |
+
background-color: #e2e8f0;
|
23 |
+
transform: scale(1.05);
|
24 |
+
z-index: 10;
|
25 |
+
}
|
26 |
+
|
27 |
+
.cell.prefilled {
|
28 |
+
background-color: #f0f4f8;
|
29 |
+
color: #1e40af;
|
30 |
+
}
|
31 |
+
|
32 |
+
.cell.error {
|
33 |
+
color: #dc2626;
|
34 |
+
animation: shake 0.5s;
|
35 |
+
}
|
36 |
+
|
37 |
+
.cell.highlight {
|
38 |
+
background-color: #dbeafe;
|
39 |
+
}
|
40 |
+
|
41 |
+
.cell.same-number {
|
42 |
+
background-color: #bfdbfe;
|
43 |
+
}
|
44 |
+
|
45 |
+
.cell:nth-child(3n) {
|
46 |
+
border-right: 2px solid #4a5568;
|
47 |
+
}
|
48 |
+
|
49 |
+
.cell:nth-child(9n) {
|
50 |
+
border-right: 1px solid #cbd5e0;
|
51 |
+
}
|
52 |
+
|
53 |
+
.row:nth-child(3n) .cell {
|
54 |
+
border-bottom: 2px solid #4a5568;
|
55 |
+
}
|
56 |
+
|
57 |
+
.row:last-child .cell {
|
58 |
+
border-bottom: none;
|
59 |
+
}
|
60 |
+
|
61 |
+
.row .cell:last-child {
|
62 |
+
border-right: none;
|
63 |
+
}
|
64 |
+
|
65 |
+
@keyframes shake {
|
66 |
+
0%, 100% { transform: translateX(0); }
|
67 |
+
20%, 60% { transform: translateX(-5px); }
|
68 |
+
40%, 80% { transform: translateX(5px); }
|
69 |
+
}
|
70 |
+
|
71 |
+
.number-btn.active {
|
72 |
+
background-color: #3b82f6;
|
73 |
+
color: white;
|
74 |
+
}
|
75 |
+
|
76 |
+
.timer {
|
77 |
+
font-family: monospace;
|
78 |
+
}
|
79 |
+
</style>
|
80 |
+
</head>
|
81 |
+
<body class="bg-gray-100 min-h-screen flex items-center justify-center p-4">
|
82 |
+
<div class="bg-white rounded-xl shadow-2xl p-6 max-w-4xl w-full">
|
83 |
+
<div class="flex flex-col md:flex-row gap-8">
|
84 |
+
<!-- Game Board -->
|
85 |
+
<div class="flex-1">
|
86 |
+
<div class="flex justify-between items-center mb-4">
|
87 |
+
<h1 class="text-3xl font-bold text-indigo-700">
|
88 |
+
<i class="fas fa-th mr-2"></i>Advanced Sudoku
|
89 |
+
</h1>
|
90 |
+
<div class="timer text-xl font-mono bg-gray-200 px-3 py-1 rounded">
|
91 |
+
<i class="fas fa-clock mr-2"></i><span id="time">00:00</span>
|
92 |
+
</div>
|
93 |
+
</div>
|
94 |
+
|
95 |
+
<!-- Sudoku Board -->
|
96 |
+
<div class="border-2 border-gray-800 rounded mb-4" id="sudoku-board">
|
97 |
+
<!-- Rows will be generated by JavaScript -->
|
98 |
+
</div>
|
99 |
+
|
100 |
+
<!-- Number Pad -->
|
101 |
+
<div class="grid grid-cols-9 gap-1 mb-4">
|
102 |
+
<button class="number-btn bg-gray-200 hover:bg-gray-300 h-10 rounded flex items-center justify-center font-bold" data-number="1">1</button>
|
103 |
+
<button class="number-btn bg-gray-200 hover:bg-gray-300 h-10 rounded flex items-center justify-center font-bold" data-number="2">2</button>
|
104 |
+
<button class="number-btn bg-gray-200 hover:bg-gray-300 h-10 rounded flex items-center justify-center font-bold" data-number="3">3</button>
|
105 |
+
<button class="number-btn bg-gray-200 hover:bg-gray-300 h-10 rounded flex items-center justify-center font-bold" data-number="4">4</button>
|
106 |
+
<button class="number-btn bg-gray-200 hover:bg-gray-300 h-10 rounded flex items-center justify-center font-bold" data-number="5">5</button>
|
107 |
+
<button class="number-btn bg-gray-200 hover:bg-gray-300 h-10 rounded flex items-center justify-center font-bold" data-number="6">6</button>
|
108 |
+
<button class="number-btn bg-gray-200 hover:bg-gray-300 h-10 rounded flex items-center justify-center font-bold" data-number="7">7</button>
|
109 |
+
<button class="number-btn bg-gray-200 hover:bg-gray-300 h-10 rounded flex items-center justify-center font-bold" data-number="8">8</button>
|
110 |
+
<button class="number-btn bg-gray-200 hover:bg-gray-300 h-10 rounded flex items-center justify-center font-bold" data-number="9">9</button>
|
111 |
+
</div>
|
112 |
+
|
113 |
+
<!-- Controls -->
|
114 |
+
<div class="flex gap-2">
|
115 |
+
<button id="new-game" class="flex-1 bg-indigo-600 hover:bg-indigo-700 text-white py-2 px-4 rounded flex items-center justify-center">
|
116 |
+
<i class="fas fa-plus-circle mr-2"></i>New Game
|
117 |
+
</button>
|
118 |
+
<button id="check" class="flex-1 bg-green-600 hover:bg-green-700 text-white py-2 px-4 rounded flex items-center justify-center">
|
119 |
+
<i class="fas fa-check mr-2"></i>Check
|
120 |
+
</button>
|
121 |
+
<button id="hint" class="flex-1 bg-yellow-500 hover:bg-yellow-600 text-white py-2 px-4 rounded flex items-center justify-center">
|
122 |
+
<i class="fas fa-lightbulb mr-2"></i>Hint
|
123 |
+
</button>
|
124 |
+
<button id="solve" class="flex-1 bg-purple-600 hover:bg-purple-700 text-white py-2 px-4 rounded flex items-center justify-center">
|
125 |
+
<i class="fas fa-magic mr-2"></i>Solve
|
126 |
+
</button>
|
127 |
+
</div>
|
128 |
+
</div>
|
129 |
+
|
130 |
+
<!-- Game Info -->
|
131 |
+
<div class="md:w-64 flex flex-col gap-4">
|
132 |
+
<div class="bg-gray-100 p-4 rounded-lg">
|
133 |
+
<h3 class="font-bold text-lg mb-2 text-indigo-700">
|
134 |
+
<i class="fas fa-info-circle mr-2"></i>How to Play
|
135 |
+
</h3>
|
136 |
+
<ul class="text-sm space-y-2">
|
137 |
+
<li>Fill the grid so each row, column, and 3×3 box contains 1-9</li>
|
138 |
+
<li>Click a cell and select a number</li>
|
139 |
+
<li>Use <span class="font-bold">Check</span> to validate your solution</li>
|
140 |
+
<li>Stuck? Get a <span class="font-bold">Hint</span></li>
|
141 |
+
</ul>
|
142 |
+
</div>
|
143 |
+
|
144 |
+
<div class="bg-gray-100 p-4 rounded-lg">
|
145 |
+
<h3 class="font-bold text-lg mb-2 text-indigo-700">
|
146 |
+
<i class="fas fa-trophy mr-2"></i>Difficulty
|
147 |
+
</h3>
|
148 |
+
<div class="flex flex-col gap-2">
|
149 |
+
<button class="difficulty-btn active bg-green-100 hover:bg-green-200 text-green-800 py-1 px-3 rounded text-left" data-difficulty="easy">
|
150 |
+
<i class="fas fa-seedling mr-2"></i>Easy
|
151 |
+
</button>
|
152 |
+
<button class="difficulty-btn bg-yellow-100 hover:bg-yellow-200 text-yellow-800 py-1 px-3 rounded text-left" data-difficulty="medium">
|
153 |
+
<i class="fas fa-tree mr-2"></i>Medium
|
154 |
+
</button>
|
155 |
+
<button class="difficulty-btn bg-red-100 hover:bg-red-200 text-red-800 py-1 px-3 rounded text-left" data-difficulty="hard">
|
156 |
+
<i class="fas fa-fire mr-2"></i>Hard
|
157 |
+
</button>
|
158 |
+
</div>
|
159 |
+
</div>
|
160 |
+
|
161 |
+
<div class="bg-gray-100 p-4 rounded-lg flex-1">
|
162 |
+
<h3 class="font-bold text-lg mb-2 text-indigo-700">
|
163 |
+
<i class="fas fa-history mr-2"></i>Stats
|
164 |
+
</h3>
|
165 |
+
<div class="space-y-2 text-sm">
|
166 |
+
<div>Games Played: <span id="games-played" class="font-bold">0</span></div>
|
167 |
+
<div>Games Won: <span id="games-won" class="font-bold">0</span></div>
|
168 |
+
<div>Best Time: <span id="best-time" class="font-bold">--:--</span></div>
|
169 |
+
</div>
|
170 |
+
</div>
|
171 |
+
</div>
|
172 |
+
</div>
|
173 |
+
</div>
|
174 |
+
|
175 |
+
<script>
|
176 |
+
// Game state
|
177 |
+
let board = Array(9).fill().map(() => Array(9).fill(0));
|
178 |
+
let solution = Array(9).fill().map(() => Array(9).fill(0));
|
179 |
+
let selectedCell = null;
|
180 |
+
let selectedNumber = null;
|
181 |
+
let timer = null;
|
182 |
+
let seconds = 0;
|
183 |
+
let difficulty = 'easy';
|
184 |
+
let gamesPlayed = 0;
|
185 |
+
let gamesWon = 0;
|
186 |
+
let bestTime = null;
|
187 |
+
|
188 |
+
// DOM elements
|
189 |
+
const sudokuBoard = document.getElementById('sudoku-board');
|
190 |
+
const numberButtons = document.querySelectorAll('.number-btn');
|
191 |
+
const difficultyButtons = document.querySelectorAll('.difficulty-btn');
|
192 |
+
const timeDisplay = document.getElementById('time');
|
193 |
+
const newGameBtn = document.getElementById('new-game');
|
194 |
+
const checkBtn = document.getElementById('check');
|
195 |
+
const hintBtn = document.getElementById('hint');
|
196 |
+
const solveBtn = document.getElementById('solve');
|
197 |
+
|
198 |
+
// Initialize the game
|
199 |
+
function initGame() {
|
200 |
+
// Clear previous board
|
201 |
+
sudokuBoard.innerHTML = '';
|
202 |
+
|
203 |
+
// Generate a new Sudoku puzzle
|
204 |
+
generateSudoku();
|
205 |
+
|
206 |
+
// Create the board UI
|
207 |
+
createBoardUI();
|
208 |
+
|
209 |
+
// Reset timer
|
210 |
+
resetTimer();
|
211 |
+
startTimer();
|
212 |
+
|
213 |
+
// Update stats
|
214 |
+
updateStats();
|
215 |
+
}
|
216 |
+
|
217 |
+
// Generate a Sudoku puzzle
|
218 |
+
function generateSudoku() {
|
219 |
+
// Generate a complete solution
|
220 |
+
generateSolution();
|
221 |
+
|
222 |
+
// Copy solution to board
|
223 |
+
for (let i = 0; i < 9; i++) {
|
224 |
+
for (let j = 0; j < 9; j++) {
|
225 |
+
board[i][j] = solution[i][j];
|
226 |
+
}
|
227 |
+
}
|
228 |
+
|
229 |
+
// Remove numbers based on difficulty
|
230 |
+
let cellsToRemove;
|
231 |
+
switch(difficulty) {
|
232 |
+
case 'easy':
|
233 |
+
cellsToRemove = 40;
|
234 |
+
break;
|
235 |
+
case 'medium':
|
236 |
+
cellsToRemove = 50;
|
237 |
+
break;
|
238 |
+
case 'hard':
|
239 |
+
cellsToRemove = 60;
|
240 |
+
break;
|
241 |
+
default:
|
242 |
+
cellsToRemove = 40;
|
243 |
+
}
|
244 |
+
|
245 |
+
// Remove numbers
|
246 |
+
let removed = 0;
|
247 |
+
while (removed < cellsToRemove) {
|
248 |
+
const row = Math.floor(Math.random() * 9);
|
249 |
+
const col = Math.floor(Math.random() * 9);
|
250 |
+
|
251 |
+
if (board[row][col] !== 0) {
|
252 |
+
board[row][col] = 0;
|
253 |
+
removed++;
|
254 |
+
}
|
255 |
+
}
|
256 |
+
}
|
257 |
+
|
258 |
+
// Generate a complete Sudoku solution
|
259 |
+
function generateSolution() {
|
260 |
+
// Reset solution
|
261 |
+
solution = Array(9).fill().map(() => Array(9).fill(0));
|
262 |
+
|
263 |
+
// Fill diagonal 3x3 boxes first (they are independent)
|
264 |
+
fillDiagonalBoxes();
|
265 |
+
|
266 |
+
// Solve the rest of the puzzle
|
267 |
+
solveSudoku(solution);
|
268 |
+
}
|
269 |
+
|
270 |
+
// Fill diagonal 3x3 boxes
|
271 |
+
function fillDiagonalBoxes() {
|
272 |
+
for (let box = 0; box < 9; box += 3) {
|
273 |
+
fillBox(box, box);
|
274 |
+
}
|
275 |
+
}
|
276 |
+
|
277 |
+
// Fill a 3x3 box
|
278 |
+
function fillBox(row, col) {
|
279 |
+
const nums = [1, 2, 3, 4, 5, 6, 7, 8, 9];
|
280 |
+
shuffleArray(nums);
|
281 |
+
|
282 |
+
let index = 0;
|
283 |
+
for (let i = 0; i < 3; i++) {
|
284 |
+
for (let j = 0; j < 3; j++) {
|
285 |
+
solution[row + i][col + j] = nums[index++];
|
286 |
+
}
|
287 |
+
}
|
288 |
+
}
|
289 |
+
|
290 |
+
// Shuffle an array
|
291 |
+
function shuffleArray(array) {
|
292 |
+
for (let i = array.length - 1; i > 0; i--) {
|
293 |
+
const j = Math.floor(Math.random() * (i + 1));
|
294 |
+
[array[i], array[j]] = [array[j], array[i]];
|
295 |
+
}
|
296 |
+
}
|
297 |
+
|
298 |
+
// Solve Sudoku using backtracking
|
299 |
+
function solveSudoku(grid) {
|
300 |
+
for (let row = 0; row < 9; row++) {
|
301 |
+
for (let col = 0; col < 9; col++) {
|
302 |
+
if (grid[row][col] === 0) {
|
303 |
+
for (let num = 1; num <= 9; num++) {
|
304 |
+
if (isValid(grid, row, col, num)) {
|
305 |
+
grid[row][col] = num;
|
306 |
+
if (solveSudoku(grid)) {
|
307 |
+
return true;
|
308 |
+
}
|
309 |
+
grid[row][col] = 0;
|
310 |
+
}
|
311 |
+
}
|
312 |
+
return false;
|
313 |
+
}
|
314 |
+
}
|
315 |
+
}
|
316 |
+
return true;
|
317 |
+
}
|
318 |
+
|
319 |
+
// Check if a number is valid in a position
|
320 |
+
function isValid(grid, row, col, num) {
|
321 |
+
// Check row
|
322 |
+
for (let x = 0; x < 9; x++) {
|
323 |
+
if (grid[row][x] === num) return false;
|
324 |
+
}
|
325 |
+
|
326 |
+
// Check column
|
327 |
+
for (let x = 0; x < 9; x++) {
|
328 |
+
if (grid[x][col] === num) return false;
|
329 |
+
}
|
330 |
+
|
331 |
+
// Check 3x3 box
|
332 |
+
const boxRow = Math.floor(row / 3) * 3;
|
333 |
+
const boxCol = Math.floor(col / 3) * 3;
|
334 |
+
|
335 |
+
for (let i = 0; i < 3; i++) {
|
336 |
+
for (let j = 0; j < 3; j++) {
|
337 |
+
if (grid[boxRow + i][boxCol + j] === num) return false;
|
338 |
+
}
|
339 |
+
}
|
340 |
+
|
341 |
+
return true;
|
342 |
+
}
|
343 |
+
|
344 |
+
// Create the board UI
|
345 |
+
function createBoardUI() {
|
346 |
+
for (let i = 0; i < 9; i++) {
|
347 |
+
const row = document.createElement('div');
|
348 |
+
row.className = 'row flex';
|
349 |
+
|
350 |
+
for (let j = 0; j < 9; j++) {
|
351 |
+
const cell = document.createElement('input');
|
352 |
+
cell.className = 'cell border border-gray-300';
|
353 |
+
cell.type = 'text';
|
354 |
+
cell.maxLength = 1;
|
355 |
+
cell.dataset.row = i;
|
356 |
+
cell.dataset.col = j;
|
357 |
+
|
358 |
+
// Set value if it's a prefilled cell
|
359 |
+
if (board[i][j] !== 0) {
|
360 |
+
cell.value = board[i][j];
|
361 |
+
cell.classList.add('prefilled');
|
362 |
+
cell.readOnly = true;
|
363 |
+
}
|
364 |
+
|
365 |
+
// Event listeners
|
366 |
+
cell.addEventListener('focus', handleCellFocus);
|
367 |
+
cell.addEventListener('input', handleCellInput);
|
368 |
+
cell.addEventListener('keydown', handleCellKeyDown);
|
369 |
+
cell.addEventListener('click', handleCellClick);
|
370 |
+
|
371 |
+
row.appendChild(cell);
|
372 |
+
}
|
373 |
+
|
374 |
+
sudokuBoard.appendChild(row);
|
375 |
+
}
|
376 |
+
}
|
377 |
+
|
378 |
+
// Handle cell focus
|
379 |
+
function handleCellFocus(e) {
|
380 |
+
const cell = e.target;
|
381 |
+
const row = parseInt(cell.dataset.row);
|
382 |
+
const col = parseInt(cell.dataset.col);
|
383 |
+
|
384 |
+
// Clear previous highlights
|
385 |
+
clearHighlights();
|
386 |
+
|
387 |
+
// Highlight current cell
|
388 |
+
cell.classList.add('highlight');
|
389 |
+
|
390 |
+
// Highlight same number cells
|
391 |
+
if (cell.value) {
|
392 |
+
highlightSameNumbers(cell.value);
|
393 |
+
}
|
394 |
+
|
395 |
+
// Highlight row, column, and box
|
396 |
+
highlightRelatedCells(row, col);
|
397 |
+
|
398 |
+
// Update selected cell
|
399 |
+
selectedCell = cell;
|
400 |
+
}
|
401 |
+
|
402 |
+
// Handle cell input
|
403 |
+
function handleCellInput(e) {
|
404 |
+
const cell = e.target;
|
405 |
+
const value = cell.value;
|
406 |
+
|
407 |
+
// Only allow numbers 1-9
|
408 |
+
if (value && !/^[1-9]$/.test(value)) {
|
409 |
+
cell.value = '';
|
410 |
+
return;
|
411 |
+
}
|
412 |
+
|
413 |
+
// Clear error state
|
414 |
+
cell.classList.remove('error');
|
415 |
+
|
416 |
+
// Update same number highlights
|
417 |
+
if (value) {
|
418 |
+
highlightSameNumbers(value);
|
419 |
+
} else {
|
420 |
+
clearSameNumberHighlights();
|
421 |
+
}
|
422 |
+
}
|
423 |
+
|
424 |
+
// Handle cell key down (for navigation)
|
425 |
+
function handleCellKeyDown(e) {
|
426 |
+
const cell = e.target;
|
427 |
+
const row = parseInt(cell.dataset.row);
|
428 |
+
const col = parseInt(cell.dataset.col);
|
429 |
+
|
430 |
+
let newRow = row;
|
431 |
+
let newCol = col;
|
432 |
+
|
433 |
+
switch(e.key) {
|
434 |
+
case 'ArrowUp':
|
435 |
+
newRow = Math.max(0, row - 1);
|
436 |
+
break;
|
437 |
+
case 'ArrowDown':
|
438 |
+
newRow = Math.min(8, row + 1);
|
439 |
+
break;
|
440 |
+
case 'ArrowLeft':
|
441 |
+
newCol = Math.max(0, col - 1);
|
442 |
+
break;
|
443 |
+
case 'ArrowRight':
|
444 |
+
newCol = Math.min(8, col + 1);
|
445 |
+
break;
|
446 |
+
default:
|
447 |
+
return;
|
448 |
+
}
|
449 |
+
|
450 |
+
if (newRow !== row || newCol !== col) {
|
451 |
+
const newCell = document.querySelector(`.cell[data-row="${newRow}"][data-col="${newCol}"]`);
|
452 |
+
if (newCell && !newCell.classList.contains('prefilled')) {
|
453 |
+
newCell.focus();
|
454 |
+
}
|
455 |
+
}
|
456 |
+
}
|
457 |
+
|
458 |
+
// Handle cell click
|
459 |
+
function handleCellClick(e) {
|
460 |
+
const cell = e.target;
|
461 |
+
if (selectedNumber && !cell.classList.contains('prefilled')) {
|
462 |
+
cell.value = selectedNumber;
|
463 |
+
cell.dispatchEvent(new Event('input'));
|
464 |
+
}
|
465 |
+
}
|
466 |
+
|
467 |
+
// Highlight related cells (row, column, box)
|
468 |
+
function highlightRelatedCells(row, col) {
|
469 |
+
// Highlight row and column
|
470 |
+
for (let i = 0; i < 9; i++) {
|
471 |
+
const rowCell = document.querySelector(`.cell[data-row="${row}"][data-col="${i}"]`);
|
472 |
+
const colCell = document.querySelector(`.cell[data-row="${i}"][data-col="${col}"]`);
|
473 |
+
|
474 |
+
if (rowCell) rowCell.classList.add('highlight');
|
475 |
+
if (colCell) colCell.classList.add('highlight');
|
476 |
+
}
|
477 |
+
|
478 |
+
// Highlight 3x3 box
|
479 |
+
const boxRow = Math.floor(row / 3) * 3;
|
480 |
+
const boxCol = Math.floor(col / 3) * 3;
|
481 |
+
|
482 |
+
for (let i = 0; i < 3; i++) {
|
483 |
+
for (let j = 0; j < 3; j++) {
|
484 |
+
const cell = document.querySelector(`.cell[data-row="${boxRow + i}"][data-col="${boxCol + j}"]`);
|
485 |
+
if (cell) cell.classList.add('highlight');
|
486 |
+
}
|
487 |
+
}
|
488 |
+
}
|
489 |
+
|
490 |
+
// Highlight cells with the same number
|
491 |
+
function highlightSameNumbers(num) {
|
492 |
+
const cells = document.querySelectorAll('.cell');
|
493 |
+
cells.forEach(cell => {
|
494 |
+
if (cell.value === num) {
|
495 |
+
cell.classList.add('same-number');
|
496 |
+
}
|
497 |
+
});
|
498 |
+
}
|
499 |
+
|
500 |
+
// Clear all highlights
|
501 |
+
function clearHighlights() {
|
502 |
+
const cells = document.querySelectorAll('.cell');
|
503 |
+
cells.forEach(cell => {
|
504 |
+
cell.classList.remove('highlight', 'same-number');
|
505 |
+
});
|
506 |
+
}
|
507 |
+
|
508 |
+
// Clear same number highlights
|
509 |
+
function clearSameNumberHighlights() {
|
510 |
+
const cells = document.querySelectorAll('.cell');
|
511 |
+
cells.forEach(cell => {
|
512 |
+
cell.classList.remove('same-number');
|
513 |
+
});
|
514 |
+
}
|
515 |
+
|
516 |
+
// Start timer
|
517 |
+
function startTimer() {
|
518 |
+
if (timer) clearInterval(timer);
|
519 |
+
|
520 |
+
seconds = 0;
|
521 |
+
updateTimerDisplay();
|
522 |
+
|
523 |
+
timer = setInterval(() => {
|
524 |
+
seconds++;
|
525 |
+
updateTimerDisplay();
|
526 |
+
}, 1000);
|
527 |
+
}
|
528 |
+
|
529 |
+
// Reset timer
|
530 |
+
function resetTimer() {
|
531 |
+
if (timer) clearInterval(timer);
|
532 |
+
seconds = 0;
|
533 |
+
updateTimerDisplay();
|
534 |
+
}
|
535 |
+
|
536 |
+
// Update timer display
|
537 |
+
function updateTimerDisplay() {
|
538 |
+
const minutes = Math.floor(seconds / 60);
|
539 |
+
const remainingSeconds = seconds % 60;
|
540 |
+
timeDisplay.textContent = `${minutes.toString().padStart(2, '0')}:${remainingSeconds.toString().padStart(2, '0')}`;
|
541 |
+
}
|
542 |
+
|
543 |
+
// Update stats
|
544 |
+
function updateStats() {
|
545 |
+
document.getElementById('games-played').textContent = gamesPlayed;
|
546 |
+
document.getElementById('games-won').textContent = gamesWon;
|
547 |
+
|
548 |
+
if (bestTime !== null) {
|
549 |
+
const minutes = Math.floor(bestTime / 60);
|
550 |
+
const seconds = bestTime % 60;
|
551 |
+
document.getElementById('best-time').textContent = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
|
552 |
+
} else {
|
553 |
+
document.getElementById('best-time').textContent = '--:--';
|
554 |
+
}
|
555 |
+
}
|
556 |
+
|
557 |
+
// Check if the board is complete and correct
|
558 |
+
function checkSolution() {
|
559 |
+
let isComplete = true;
|
560 |
+
let isCorrect = true;
|
561 |
+
|
562 |
+
// Check each cell
|
563 |
+
for (let i = 0; i < 9; i++) {
|
564 |
+
for (let j = 0; j < 9; j++) {
|
565 |
+
const cell = document.querySelector(`.cell[data-row="${i}"][data-col="${j}"]`);
|
566 |
+
|
567 |
+
if (!cell.value) {
|
568 |
+
isComplete = false;
|
569 |
+
continue;
|
570 |
+
}
|
571 |
+
|
572 |
+
const num = parseInt(cell.value);
|
573 |
+
if (num !== solution[i][j]) {
|
574 |
+
cell.classList.add('error');
|
575 |
+
isCorrect = false;
|
576 |
+
}
|
577 |
+
}
|
578 |
+
}
|
579 |
+
|
580 |
+
if (!isComplete) {
|
581 |
+
alert('The puzzle is not complete yet!');
|
582 |
+
return false;
|
583 |
+
}
|
584 |
+
|
585 |
+
if (!isCorrect) {
|
586 |
+
alert('There are errors in your solution. Please check the highlighted cells.');
|
587 |
+
return false;
|
588 |
+
}
|
589 |
+
|
590 |
+
return true;
|
591 |
+
}
|
592 |
+
|
593 |
+
// Provide a hint
|
594 |
+
function giveHint() {
|
595 |
+
if (!selectedCell || selectedCell.classList.contains('prefilled')) {
|
596 |
+
alert('Please select an empty cell first!');
|
597 |
+
return;
|
598 |
+
}
|
599 |
+
|
600 |
+
const row = parseInt(selectedCell.dataset.row);
|
601 |
+
const col = parseInt(selectedCell.dataset.col);
|
602 |
+
|
603 |
+
selectedCell.value = solution[row][col];
|
604 |
+
selectedCell.classList.remove('error');
|
605 |
+
selectedCell.dispatchEvent(new Event('input'));
|
606 |
+
|
607 |
+
// Check if the puzzle is complete
|
608 |
+
if (isPuzzleComplete()) {
|
609 |
+
handleWin();
|
610 |
+
}
|
611 |
+
}
|
612 |
+
|
613 |
+
// Solve the puzzle
|
614 |
+
function solvePuzzle() {
|
615 |
+
if (confirm('Are you sure you want to see the solution?')) {
|
616 |
+
for (let i = 0; i < 9; i++) {
|
617 |
+
for (let j = 0; j < 9; j++) {
|
618 |
+
const cell = document.querySelector(`.cell[data-row="${i}"][data-col="${j}"]`);
|
619 |
+
if (!cell.classList.contains('prefilled')) {
|
620 |
+
cell.value = solution[i][j];
|
621 |
+
cell.classList.remove('error');
|
622 |
+
}
|
623 |
+
}
|
624 |
+
}
|
625 |
+
|
626 |
+
clearInterval(timer);
|
627 |
+
alert('Puzzle solved!');
|
628 |
+
}
|
629 |
+
}
|
630 |
+
|
631 |
+
// Check if puzzle is complete
|
632 |
+
function isPuzzleComplete() {
|
633 |
+
for (let i = 0; i < 9; i++) {
|
634 |
+
for (let j = 0; j < 9; j++) {
|
635 |
+
const cell = document.querySelector(`.cell[data-row="${i}"][data-col="${j}"]`);
|
636 |
+
if (!cell.value) return false;
|
637 |
+
}
|
638 |
+
}
|
639 |
+
return true;
|
640 |
+
}
|
641 |
+
|
642 |
+
// Handle win
|
643 |
+
function handleWin() {
|
644 |
+
clearInterval(timer);
|
645 |
+
|
646 |
+
// Update stats
|
647 |
+
gamesPlayed++;
|
648 |
+
gamesWon++;
|
649 |
+
|
650 |
+
if (bestTime === null || seconds < bestTime) {
|
651 |
+
bestTime = seconds;
|
652 |
+
}
|
653 |
+
|
654 |
+
updateStats();
|
655 |
+
|
656 |
+
alert(`Congratulations! You solved the puzzle in ${timeDisplay.textContent}!`);
|
657 |
+
}
|
658 |
+
|
659 |
+
// Event listeners
|
660 |
+
numberButtons.forEach(btn => {
|
661 |
+
btn.addEventListener('click', () => {
|
662 |
+
// Clear active state from all buttons
|
663 |
+
numberButtons.forEach(b => b.classList.remove('active'));
|
664 |
+
|
665 |
+
// Set active state on clicked button
|
666 |
+
btn.classList.add('active');
|
667 |
+
|
668 |
+
// Update selected number
|
669 |
+
selectedNumber = btn.dataset.number;
|
670 |
+
|
671 |
+
// If a cell is selected, fill it
|
672 |
+
if (selectedCell && !selectedCell.classList.contains('prefilled')) {
|
673 |
+
selectedCell.value = selectedNumber;
|
674 |
+
selectedCell.dispatchEvent(new Event('input'));
|
675 |
+
|
676 |
+
// Check if the puzzle is complete
|
677 |
+
if (isPuzzleComplete()) {
|
678 |
+
setTimeout(() => {
|
679 |
+
if (checkSolution()) {
|
680 |
+
handleWin();
|
681 |
+
}
|
682 |
+
}, 100);
|
683 |
+
}
|
684 |
+
}
|
685 |
+
});
|
686 |
+
});
|
687 |
+
|
688 |
+
difficultyButtons.forEach(btn => {
|
689 |
+
btn.addEventListener('click', () => {
|
690 |
+
// Clear active state from all buttons
|
691 |
+
difficultyButtons.forEach(b => b.classList.remove('active'));
|
692 |
+
|
693 |
+
// Set active state on clicked button
|
694 |
+
btn.classList.add('active');
|
695 |
+
|
696 |
+
// Update difficulty
|
697 |
+
difficulty = btn.dataset.difficulty;
|
698 |
+
});
|
699 |
+
});
|
700 |
+
|
701 |
+
newGameBtn.addEventListener('click', () => {
|
702 |
+
gamesPlayed++;
|
703 |
+
updateStats();
|
704 |
+
initGame();
|
705 |
+
});
|
706 |
+
|
707 |
+
checkBtn.addEventListener('click', () => {
|
708 |
+
if (checkSolution()) {
|
709 |
+
handleWin();
|
710 |
+
}
|
711 |
+
});
|
712 |
+
|
713 |
+
hintBtn.addEventListener('click', giveHint);
|
714 |
+
|
715 |
+
solveBtn.addEventListener('click', solvePuzzle);
|
716 |
+
|
717 |
+
// Initialize the game
|
718 |
+
initGame();
|
719 |
+
</script>
|
720 |
+
<p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - <a href="https://enzostvs-deepsite.hf.space?remix=sekopst/sudoku1" style="color: #fff;text-decoration: underline;" target="_blank" >🧬 Remix</a></p></body>
|
721 |
+
</html>
|