-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku1.html
197 lines (179 loc) · 5.6 KB
/
sudoku1.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Sudoku Game</title>
<style>
body {
background-color: limegreen;
}
table {
border-collapse: collapse;
border: 2px solid black;
margin: auto;
background-color: white; /* Set the background color of the sudoku box */
}
td {
width: 40px;
height: 40px;
border: 1px solid black;
text-align: center;
font-size: 20px;
cursor: pointer;
}
.highlight {
background-color: #ffff99;
}
.initial {
background-color: lightgrey;
}
</style>
</head>
<body>
<h1 style="text-align: center;">SUDOKU</h1>
<table id="sudokuTable"></table>
<p id="result"></p>
<script>
let sudokuPuzzle;
let selectedCell = null;
function generateSudoku() {
const table = document.getElementById('sudokuTable');
table.innerHTML = '';
sudokuPuzzle = generateRandomSudoku();
for (let i = 0; i < 9; i++) {
const row = document.createElement('tr');
for (let j = 0; j < 9; j++) {
const cell = document.createElement('td');
cell.textContent = sudokuPuzzle[i][j] === 0 ? '' : sudokuPuzzle[i][j];
cell.onclick = () => selectCell(i, j);
if (sudokuPuzzle[i][j] !== 0) {
cell.classList.add('initial');
}
row.appendChild(cell);
}
table.appendChild(row);
}
}
function generateRandomSudoku() {
// Generate a random completed Sudoku board
const board = [];
for (let i = 0; i < 9; i++) {
board.push([]);
for (let j = 0; j < 9; j++) {
board[i].push(0);
}
}
solveSudoku(board);
// Remove random cells to create a puzzle
const puzzle = JSON.parse(JSON.stringify(board)); // Clone the board
for (let i = 0; i < 40; i++) { // Adjust the number of cells to remove for difficulty
const row = Math.floor(Math.random() * 9);
const col = Math.floor(Math.random() * 9);
puzzle[row][col] = 0;
}
return puzzle;
}
function solveSudoku(board) {
const emptyCell = findEmptyCell(board);
if (!emptyCell) {
return true; // Puzzle solved
}
const [row, col] = emptyCell;
for (let num = 1; num <= 9; num++) {
if (isValid(board, row, col, num)) {
board[row][col] = num;
if (solveSudoku(board)) {
return true;
}
board[row][col] = 0; // Backtrack
}
}
return false; // No solution found
}
function isValid(board, row, col, num) {
// Check if the number is not already in the same row, column, or box
return (
!usedInRow(board, row, num) &&
!usedInColumn(board, col, num) &&
!usedInBox(board, row - row % 3, col - col % 3, num)
);
}
function usedInRow(board, row, num) {
return board[row].includes(num);
}
function usedInColumn(board, col, num) {
for (let i = 0; i < 9; i++) {
if (board[i][col] === num) {
return true;
}
}
return false;
}
function usedInBox(board, startRow, startCol, num) {
for (let i = 0; i < 3; i++) {
for (let j = 0; j < 3; j++) {
if (board[startRow + i][startCol + j] === num) {
return true;
}
}
}
return false;
}
function findEmptyCell(board) {
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
if (board[i][j] === 0) {
return [i, j];
}
}
}
return null; // No empty cell found
}
function selectCell(row, col) {
const table = document.getElementById('sudokuTable');
if (selectedCell) {
const [prevRow, prevCol] = selectedCell;
table.rows[prevRow].cells[prevCol].classList.remove('highlight');
}
selectedCell = [row, col];
table.rows[row].cells[col].classList.add('highlight');
}
function fillNumber(number) {
if (selectedCell) {
const [row, col] = selectedCell;
if (sudokuPuzzle[row][col] === 0 || sudokuPuzzle[row][col] !== number) {
const table = document.getElementById('sudokuTable');
table.rows[row].cells[col].textContent = number;
sudokuPuzzle[row][col] = number;
checkSolution();
}
}
}
function checkSolution() {
let isSolutionCorrect = true;
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
if (sudokuPuzzle[i][j] !== 0 && !isValid(sudokuPuzzle, i, j, sudokuPuzzle[i][j])) {
isSolutionCorrect = false;
break;
}
}
if (!isSolutionCorrect) {
break;
}
}
const resultElement = document.getElementById('result');
if (isSolutionCorrect) {
resultElement.textContent = 'Congratulations! You solved the Sudoku puzzle!';
} else {
resultElement.textContent = 'Keep going! Solution is not correct yet.';
}
}
generateSudoku();
</script>
<button onclick="generateSudoku()">Generate New Puzzle</button>
<br><br>
Fill number: <button onclick="fillNumber(1)">1</button> <button onclick="fillNumber(2)">2</button> <button onclick="fillNumber(3)">3</button> <button onclick="fillNumber(4)">4</button> <button onclick="fillNumber(5)">5</button> <button onclick="fillNumber(6)">6</button> <button onclick="fillNumber(7)">7</button> <button onclick="fillNumber(8)">8</button> <button onclick="fillNumber(9)">9</button>
</body>
</html>