-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
249 lines (217 loc) · 5.69 KB
/
script.js
File metadata and controls
249 lines (217 loc) · 5.69 KB
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
var arr = [[], [], [], [], [], [], [], [], []];
var temp = [[], [], [], [], [], [], [], [], []];
var selectedCell = null;
for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
arr[i][j] = document.getElementById(i * 9 + j);
arr[i][j].onclick = function () {
if (selectedCell) {
selectedCell.classList.remove("selected");
}
selectedCell = this;
selectedCell.classList.add("selected");
};
}
}
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 = [[], [], [], [], [], [], [], [], []];
var solvedBoard = [[], [], [], [], [], [], [], [], []];
// Timer variables and helpers
var timerInterval = null;
var startTime = null;
var elapsedSeconds = 0;
function formatTime(sec) {
var m = Math.floor(sec / 60);
var s = sec % 60;
return m + ":" + (s < 10 ? "0" + s : s);
}
function startTimer() {
stopTimer();
startTime = Date.now();
timerInterval = setInterval(function () {
elapsedSeconds = Math.floor((Date.now() - startTime) / 1000);
var el = document.getElementById("timer");
if (el) el.innerText = formatTime(elapsedSeconds);
}, 1000);
}
function stopTimer() {
if (timerInterval) {
clearInterval(timerInterval);
timerInterval = null;
}
}
function resetTimer() {
stopTimer();
elapsedSeconds = 0;
var el = document.getElementById("timer");
if (el) el.innerText = "0:00";
}
let button = document.getElementById("generate-sudoku");
let solve = document.getElementById("solve");
console.log(arr);
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 () {
const apiUrl =
"https://api.api-ninjas.com/v1/sudokugenerate?difficulty=medium&width=3&height=3";
const apiKey = "O+vc1zGtv/mKz2CwF5GEdQ==w3Xyjt62Ud7jwEDX";
var xhrRequest = new XMLHttpRequest();
xhrRequest.open("get", apiUrl);
xhrRequest.responseType = "json";
xhrRequest.setRequestHeader("X-Api-Key", apiKey);
xhrRequest.setRequestHeader("Content-Type", "application/json");
xhrRequest.onload = function () {
const response = xhrRequest.response;
console.log(response);
initializeTemp(temp);
resetColor();
board = response.puzzle;
solvedBoard = response.solution;
setTemp(board, temp);
setColor(temp);
changeBoard(board);
// enable solve button
solve.disabled = false;
// reset and start timer when new puzzle is loaded
resetTimer();
startTimer();
};
xhrRequest.send();
};
//to be completed by student, function should not return anything
// you can make a call to changeboard(board) function to update the state on the screen
//returns a boolean true of false
function isSafe(board, r, c, no) {
//not repeating in the same row or column
for (var i = 0; i < 9; i++) {
if (board[i][c] == no || board[r][i] == no) {
return false;
}
}
//subgrid
var sx = r - (r % 3);
var sy = c - (c % 3);
for (var x = sx; x < sx + 3; x++) {
for (var y = sy; y < sy + 3; y++) {
if (board[x][y] == no) {
return false;
}
}
}
return true;
}
function solveSudokuHelper(board, r, c) {
//base case
if (r == 9) {
// solved: update board and stop timer
changeBoard(board);
stopTimer();
return true;
}
//other cases
if (c == 9) {
return solveSudokuHelper(board, r + 1, 0);
}
//pre-filled cell, skip it
if (board[r][c] != 0) {
return solveSudokuHelper(board, r, c + 1);
}
//there is 0 in the current location
for (var i = 1; i <= 9; i++) {
if (isSafe(board, r, c, i)) {
board[r][c] = i;
var success = solveSudokuHelper(board, r, c + 1);
if (success == true) {
return true;
}
//backtracking step
board[r][c] = 0;
}
}
return false;
}
function solveSudoku(board) {
return solveSudokuHelper(board, 0, 0);
}
solve.onclick = function () {
changeBoard(solvedBoard);
stopTimer();
};
function checkCompletion() {
for (var i = 0; i < 9; i++) {
for (var j = 0; j < 9; j++) {
if (parseInt(board[i][j]) !== parseInt(solvedBoard[i][j])) {
return false;
}
}
}
return true;
}
document.onkeydown = function (e) {
if (selectedCell) {
var val = parseInt(e.key);
var id = parseInt(selectedCell.id);
var r = Math.floor(id / 9);
var c = id % 9;
if (val > 0 && val < 10) {
selectedCell.innerText = val;
// Ensure row is initialized if we are inputting on a blank board
if (!board[r]) board[r] = [];
board[r][c] = val;
if (val != solvedBoard[r][c]) {
selectedCell.style.color = "red";
} else {
selectedCell.style.color = "green";
if (checkCompletion()) {
stopTimer();
// Small delay to allow UI to update before alert
setTimeout(() => {
alert("You did it!");
}, 100);
}
}
} else if (e.key == "Backspace" || e.key == "Delete") {
selectedCell.innerText = "";
if (board[r]) board[r][c] = 0;
selectedCell.style.color = "black"; // Reset color on clear
}
}
};