-
Notifications
You must be signed in to change notification settings - Fork 23
/
script.js
384 lines (304 loc) · 8.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// 2D matrix
// program to implement stack data structure
class Stack {
constructor() {
this.items = [];
}
// add element to the stack
add(element) {
return this.items.push(element);
}
// remove element from the stack
remove() {
if (this.items.length > 0) {
return this.items.pop();
}
}
// view the last element
peek() {
return this.items[this.items.length - 1];
}
// check if the stack is empty
isEmpty() {
return this.items.length == 0;
}
// the size of the stack
size() {
return this.items.length;
}
// empty the stack
clear() {
this.items = [];
}
}
var arr = [[], [], [], [], [], [], [], [], []]
var temp = [[], [], [], [], [], [], [], [], []]
var stack = new Stack();
for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
arr[i][j] = document.getElementById(i * 9 + j);
}
}
function initializeTemp(temp) {
for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
temp[i][j] = false;
}
}
}
function setTemp(board, temp) {
for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
if (board[i][j] != 0) {
temp[i][j] = true;
}
}
}
}
function setColor(temp) {
for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
if (temp[i][j] == true) {
arr[i][j].style.color = "#DC3545";
}
}
}
}
function resetColor() {
for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
arr[i][j].style.color = "green";
}
}
}
var board = [[], [], [], [], [], [], [], [], []]
let container = document.getElementById('container')
let button = document.getElementById('generate-sudoku')
let solve = document.getElementById('solve')
let min = document.getElementById('min')
let sec = document.getElementById('sec')
let divs = document.getElementById('container').querySelectorAll('div');
let value = 1;
let number = document.getElementById('numbers').querySelectorAll('div');
let selectedcolor = document.getElementById('01');
selectedcolor.style.border = '2px solid black'
let timerr;
console.log(number)
number.forEach(function (i) {
i.onclick = function () {
changecolor(i);
value = Number(i.innerText);
}
})
divs.forEach(function (div) {
div.onclick = function () {
if (!Number.isInteger(value)) {
console.log("Select a number")
}
else {
stack.add(div.id);
div.innerText = value;
console.log(div.innerText)
}
}
})
let changecolor = function (i) {
selectedcolor.style.border = 'none';
i.style.border = '2px solid black';
selectedcolor = i;
}
function changeBoard(board) {
for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
if (board[i][j] != 0) {
arr[i][j].innerText = board[i][j]
}
else
arr[i][j].innerText = ''
}
}
}
button.onclick = function () {
var xhrRequest = new XMLHttpRequest()
xhrRequest.onload = function () {
var response = JSON.parse(xhrRequest.response)
console.log(response)
initializeTemp(temp)
resetColor()
board = response.board
setTemp(board, temp)
setColor(temp)
changeBoard(board)
}
xhrRequest.open('get', 'https://sugoku.herokuapp.com/board?difficulty=easy')
//we can change the difficulty of the puzzle the allowed values of difficulty are easy, medium, hard and random
xhrRequest.send()
timerr = setInterval(timer, 1000)
}
function timer() {
if (Number(sec.innerText) < 9) {
sec.innerText = "0" + (Number(sec.innerText) + 1);
}
else {
sec.innerText = Number(sec.innerText) + 1;
}
if (Number(sec.innerText) >= 60) {
sec.innerText = "0o0";
if (Number(min.innerText) < 9) {
min.innerText = "0" + (Number(min.innerText) + 1);
}
else {
min.innerText = Number(min.innerText) + 1;
}
}
function isPossible(board, sr, sc, val) {
// check for not repeating in same row
for (var row = 0; row < 9; row++) {
if (board[row][sc] == val) {
return false;
}
}
// check for not repeating in same column
for (var col = 0; col < 9; col++) {
if (board[sr][col] == val) {
return false;
}
}
// sub-grid
var r = sr - (sr % 3);
var c = sc - (sc % 3);
for (var cr = r; cr < r + 3; cr++) {
for (var cc = c; cc < c + 3; cc++) {
if (board[cr][cc] == val) {
return false;
}
}
}
return true;
}
function solveSudokuHelper(board, sr, sc) { // board, starting row, starting col
// base case
if (sr == 9) { // all rows filled
changeBoard(board);
return;
}
if (sc == 9) { // last column
solveSudokuHelper(board, sr + 1, 0) // move to next row
return;
}
//pre-filled cell, skip it
if (board[sr][sc] != 0) {
solveSudokuHelper(board, sr, sc + 1);
return;
}
//there is 0 in the current location
for (var i = 1; i <= 9; i++) {
if (isPossible(board, sr, sc, i)) { // check
board[sr][sc] = i;
solveSudokuHelper(board, sr, sc + 1);
}
// backtracking
board[sr][sc] = 0;
}
}
function solveSudoku(board) {
solveSudokuHelper(board, 0, 0)
}
// solve button
solve.onclick = function () {
clearInterval(timerr);
solveSudoku(board)
}
/*Dark-Mode*/
function darkMode() {
var element = document.body;
element.classList.toggle("dark-mode");
}
}
//Focus change Notification
function showNotification() {
if (!document.hidden) {
const options = {
body: "Don't give up, champ!",
};
if ('Notification' in window) {
if (Notification.permission === 'granted') {
new Notification('Important Message', options);
alert("Don't give up, champ!");
} else if (Notification.permission !== 'denied') {
Notification.requestPermission().then(permission => {
if (permission === 'granted') {
new Notification('Important Message', options);
}
});
}
}
}
}
/* Focus Change */
window.onload = function () {
var preTitle = document.title;
var postTitle = "Don't give up ! 🥺";
document.addEventListener("visibilitychange", function () {
showNotification();
// console.log("Changed");
var page_Active = !document.hidden;
// console.log("page_Active" + page_Active);
// console.log("!document.hidden" + !document.hidden);
if (!page_Active) {
document.title = postTitle;
} else {
document.title = preTitle;
}
});
}
function save() {
const username = document.getElementById("username").value;
console.log(username, "save");
for (let i = 0; i < 81; i++) {
board[~~(i / 9)][i % 9] = document.getElementById(i).innerHTML;
}
localStorage.setItem(username, JSON.stringify([board, min.innerHTML, sec.innerHTML]));
console.log(min.innerHTML, sec.innerHTML)
}
function resume() {
container.style.opacity = 1;
button.disabled = false;
solve.disabled = false;
number.forEach(function (i) {
i.style.pointerEvents = 'auto';
})
divs.forEach(function (div) {
div.style.pointerEvents = 'auto';
})
const username = document.getElementById("username").value;
console.log(username, JSON.parse(localStorage.getItem(username)));
board = JSON.parse(localStorage.getItem(username))[0];
min.innerHTML = JSON.parse(localStorage.getItem(username))[1];
sec.innerHTML = JSON.parse(localStorage.getItem(username))[2];
timerr = setInterval(timer, 1000)
changeBoard(board)
console.log(board);
}
//undo - redo feature
let undoBtn = document.getElementById("undo");
let redoBtn = document.getElementById("redo");
undoBtn.addEventListener('click', function () {
var i = stack.peek() / 9;
var j = stack.peek() % 9;
document.getElementById(stack.peek()).innerText = null;
stack.remove();
})
function pause() {
container.style.opacity = 0.3;
save();
button.disabled = true;
solve.disabled = true;
number.forEach(function (i) {
i.style.pointerEvents = 'none';
})
divs.forEach(function (div) {
div.style.pointerEvents = 'none';
})
clearInterval(timerr)
}