forked from Spandan-Bhattacharya/Solve_it
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
288 lines (241 loc) · 9 KB
/
script.js
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
let currentDifficulty = 0; // 0: easy, 1: medium, 2: hard, 3: super hard
const difficulties = [
[
[[2,0,0,0,1,0,3,8], [3,1,6,0,0,7,0,2], [0,4,5,0,0,0,8,0], [1,0,0,2,6,4,7,5],[0,0,4,7,5,0,0,0] , [5,2,0,0,7,0,6,0] , [0,7,1,3,0,0,0,6] , [4,6,0,0,8,0,0,0]],
[[0,0,8,7,0,0,0,0], [0,0,2,6,7,5,0,1], [7,6,0,4,0,3,1,0], [0,1,3,8,0,7,0,4],[0,0,7,2,0,0,3,0] , [4,0,6,0,5,2,7,0] , [6,0,4,3,1,0,2,0] , [0,2,0,0,3,6,4,0]],
// Add more easy puzzles
],
[
[[0,0,6,0,0,0,8,0] , [4,5,0,8,0,0,0,0] , [0,0,8,0,0,2,4,0] , [7,0,4,6,3,1,0,0] , [0,6,0,4,0,0,0,0] , [0,3,0,2,0,4,0,5] , [6,0,1,0,4,0,0,3] , [3,0,0,7,0,0,6,0]],
[[1,8,6,5,0,0,0,0] , [0,0,2,0,5,0,0,0] , [0,0,3,0,1,7,6,0] , [0,1,0,6,0,0,0,0] , [2,6,0,0,0,0,0,0] , [4,0,0,1,3,0,0,2] , [0,5,0,0,6,4,0,0] , [6,3,1,4,7,0,0,5]],
// Add more medium puzzles
],
[
[[4,0,5,3,0,0,0,8] , [0,0,0,2,0,1,0,0] , [2,0,0,0,7,0,0,4] , [0,0,3,0,8,0,0,0] , [0,6,1,0,0,0,0,0] , [0,3,0,4,0,5,0,1] , [0,2,4,1,0,0,0,7] , [0,0,0,0,0,2,0,0]],
[[3,0,0,1,6,0,0,0] , [8,0,0,4,0,0,0,0] , [0,1,0,6,0,2,0,8] , [0,0,0,0,0,0,7,0] , [0,0,0,0,2,4,0,6] , [0,0,0,0,0,8,5,0] , [0,7,0,0,0,0,0,0] , [0,0,6,0,0,0,2,1]]
// Add more hard puzzles
],
];
let lives = 3;
let totalSeconds = 600; // 10 minutes in seconds
let timerInterval;
document.addEventListener('DOMContentLoaded', function () {
const gridSize = 8;
const solveButton = document.getElementById("solve-btn");
solveButton.addEventListener('click', solveSudoku);
const sudokuGrid = document.getElementById("sudoku-grid");
//creating sudoku grid and input cells
for (let row = 0; row < gridSize; row++) {
const newRow = document.createElement("tr");
for (let col = 0; col < gridSize; col++) {
const cell = document.createElement("td");
const input = document.createElement("input");
input.type = "text";
input.className = "cell";
input.id = `cell-${row}-${col}`;
input.dataset.row = row;
input.dataset.col = col;
input.addEventListener("keydown", function (e) {
if (e.key === "ArrowUp" || e.key === "ArrowDown") {
e.preventDefault();
}
});
input.addEventListener("input", handleInput);
cell.appendChild(input);
newRow.appendChild(cell);
}
sudokuGrid.appendChild(newRow);
}
changeDifficulty(0);
updateTimer();
});
function changeDifficulty(difficulty) {
currentDifficulty = difficulty;
resetGrid();
}
function board() {
const gridSize = 8;
const sudokuArray = [];
//fill the sudoku array
for (let row = 0; row < gridSize; row++) {
sudokuArray[row] = [];
for (let col = 0; col < gridSize; col++) {
const cellId = `cell-${row}-${col}`;
const cellValue = document.getElementById(cellId).value;
sudokuArray[row][col] = cellValue !== "" ? parseInt(cellValue) : 0;
}
}
return sudokuArray;
}
function handleInput(event) {
let row = event.srcElement.dataset.row;
let col = event.srcElement.dataset.col;
let b = [];
b = board();
console.log(b);
const inputValue = event.target.value.replace(/[^0-9]/g, '');
event.target.value = inputValue;
const parsedValue = parseInt(inputValue);
if (!isNaN(parsedValue) && parsedValue >= 1 && parsedValue <= 8 && isValidMove(b, row, col, parsedValue)) {
event.target.classList.remove("output-cell");
event.target.style.color = "black";
} else {
event.target.classList.add("output-cell");
event.target.style.color = "red";
if (!isNaN(parsedValue)) {
document.getElementById(`h-${lives}`).style.display="none";
lives--;
}
if (lives<=0) {
gameOver();
}
}
}
function gameOver() {
alert("Game Over!");
resetGrid();
}
async function solveSudoku() {
const gridSize = 8;
const sudokuArray = board();
//Indentifying user-input and mark
for (let row = 0; row < gridSize; row++) {
for (let col = 0; col < gridSize; col++) {
const cellId = `cell-${row}-${col}`;
const cell = document.getElementById(cellId);
if (sudokuArray[row][col] !== 0) {
cell.classList.add("user-input");
} else {
cell.classList.add("output-cell");
}
}
}
//Solving the soduko and dosplaying the solution
if (solveSudokuHelper(sudokuArray)) {
for (let row = 0; row < gridSize; row++) {
for (let col = 0; col < gridSize; col++) {
const cellId = `cell-${row}-${col}`;
const cell = document.getElementById(cellId);
//filling in solved valued and applying animations
if (!cell.classList.contains("user-input")) {
cell.value = sudokuArray[row][col];
cell.classList.add("solved");
await sleep(60); //delay for visualisation
}
}
}
} else {
alert("No solution exists for the given Sudoku puzzle.");
resetGrid();
}
}
function solveSudokuHelper(board) {
const gridSize = 8;
for (let row = 0; row < gridSize; row++) {
for (let col = 0; col < gridSize; col++) {
if(board[row][col]>8||board[row][col]<0){
return false;
}
if (board[row][col] === 0) {
for (let num = 1; num <= 8; num++) {
if (isValidMove(board, row, col, num)) {
board[row][col] = num;
//recursively check if there is a solution
if (solveSudokuHelper(board)) {
return true; //solved
}
board[row][col] = 0; //Backtrack
}
}
return false; //No valid number found
}
}
}
return true;//All cekks filled
}
function isValidMove(board, row, col, num) {
if(num>8||num<1){
return false;
}
const gridSize = 8;
//Check row and column for conflicts
for (let i = 0; i < gridSize; i++) {
if ((i != col && board[row][i] === num) || (i != row && board[i][col] === num)) {
return false;//Conflict found
}
}
//Check the 3*3 subgrid conflicts
const startRow = Math.floor(row / 2) * 2;
const startCol = Math.floor(col / 4) * 4;
for (let i = startRow; i < startRow + 2; i++) {
for (let j = startCol; j < startCol + 4; j++) {
if (!(i == row && j == col) && board[i][j] == num) {
return false; //Conflict found
}
}
}
for (let i = 0; i < 8; i++) {
for (let j = 0; j < 8; j++) {
if (board[i][j] == 0) {
return true;
}
}
}
complete();
return true; //No conflicts found
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function resetGrid() {
clearInterval(timerInterval);
totalSeconds = (3 - currentDifficulty) * 400;
updateTimer();
timerInterval = setInterval(updateTimer, 1000);
lives = 3;
document.getElementById(`h-1`).style.display="";
document.getElementById(`h-2`).style.display="";
document.getElementById(`h-3`).style.display="";
const gridSize = 8;
console.log(currentDifficulty);
const puzzle = difficulties[currentDifficulty][Math.floor(Math.random() * difficulties[currentDifficulty].length)];
for (let i = 0; i < gridSize; i++) {
for (let j = 0; j < gridSize; j++) {
const field = document.getElementById(`cell-${i}-${j}`);
if (field) {
field.value = puzzle[i][j] !== 0 ? puzzle[i][j] : "";
field.classList.remove("user-input");
field.style.color = '#1e25e8';
// field.classList.remove("output-cell");
}
}
}
}
function gridReset() {
document.getElementById("resetButton").addEventListener("click", (e) => {
e.preventDefault(); // Prevents the form from being submitted and page refresh
resetGrid();
});
}
function complete() {
alert("Congratulation !!! You Solved The Game");
clearInterval(timerInterval);
}
function updateTimer() {
let timerElement = document.getElementById('timer');
let minutes, seconds;
console.log(totalSeconds);
minutes = Math.floor(totalSeconds / 60);
seconds = totalSeconds % 60;
timerElement.textContent = `${minutes < 10 ? '0' : ''}${minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
if (totalSeconds <= 0) {
clearInterval(timerInterval);
alert("Time's UP");
resetGrid();
} else {
totalSeconds--;
}
}
timerInterval = setInterval(updateTimer, 1000); // Update the timer every second
// Call gridReset to attach the event listener to the resetButton
gridReset();