-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
237 lines (218 loc) · 7.7 KB
/
main.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
window.onload = () => {
geneatePastBoard();
}
const formInput = document.getElementById("sudoku-form");
const message = document.getElementById("message-p")
const livesMes = document.getElementById("lives")
const hintsMes = document.getElementById("hints")
let focusElement = 0;
let lives = JSON.parse(localStorage.getItem("lives")) + 1 || 3
localStorage.setItem("lives", JSON.stringify(lives - 1))
let hints = JSON.parse(localStorage.getItem("hints")) + 1 || 3
localStorage.setItem("hints", JSON.stringify(hints - 1))
lives--;
hints--;
let visits = JSON.parse(localStorage.getItem("visits")) || 1
if (visits == 1) {
generateBoard("easy")
lives = 3
hints = 3
localStorage.setItem("hints", JSON.stringify(3))
localStorage.setItem("lives", JSON.stringify(3))
}
visits++
localStorage.setItem("visits", JSON.stringify(visits))
livesMes.innerHTML = lives
hintsMes.innerHTML = hints
document.getElementById("solve-button").addEventListener("click", solve);
document.getElementById("hint").addEventListener("click", giveHint)
document.getElementById("generate").addEventListener("click", readDifficultyAndGenerate);
function geneatePastBoard() {
const board = JSON.parse(localStorage.getItem("no-solution"))
const solution = JSON.parse(localStorage.getItem("with-solution"))
if(board && solution) {
writeValues(board, solution)
}
if (checkWin()) {
document.getElementById("hint").disabled = true;
document.getElementById("solve-button").disabled = true;
}
if (hints == 0) {
document.getElementById("hint").disabled = true;
}
if (lives == 0) {
message.innerHTML = "You have no more lives. You lost!"
for (let i = 0; i < formInput.length; i++) {
formInput[i].readOnly = true;
}
}
}
function giveHint() {
if (formInput[focusElement].value == "") {
hints -= 1;
hintsMes.innerHTML = hints
localStorage.setItem("hints", JSON.stringify(hints))
if (hints == 0) {
document.getElementById("hint").disabled = true;
localStorage.setItem("hints", JSON.stringify(0))
}
formInput[focusElement].value = formInput[focusElement].dataset.solution
formInput[focusElement].readOnly = true
let board = JSON.parse(localStorage.getItem("no-solution"))
board[focusElement] = formInput[focusElement].dataset.solution
localStorage.setItem("no-solution", JSON.stringify(board))
if (checkWin()) {
message.innerHTML = "You win!"
}
}
}
function readDifficultyAndGenerate() {
message.innerHTML = ""
document.getElementById("hint").disabled = false;
document.getElementById("solve-button").disabled = false;
livesMes.innerHTML = 3;
hintsMes.innerHTML = 3;
lives = 3
hints = 3
localStorage.setItem("hints", JSON.stringify(3))
localStorage.setItem("lives", JSON.stringify(3))
clearBoard();
const difficulty = document.getElementById("difficulty").value
generateBoard(difficulty)
}
function clearBoard() {
for (let i = 0; i < 81; i++) {
formInput[i].value = ""
formInput[i].readOnly = false;
}
}
function checkInput() {
currentInput = formInput[focusElement].value
console.log(currentInput)
if (currentInput == 0 || currentInput > 9) {
message.innerHTML = "Your recent input was invalid. It has been removed"
formInput[focusElement].value = ""
return;
}
const correctInput = formInput[focusElement].dataset.solution
if (currentInput == correctInput) {
message.innerHTML = ""
formInput[focusElement].readOnly = true
let board = JSON.parse(localStorage.getItem("no-solution"))
board[focusElement] = correctInput
localStorage.setItem("no-solution", JSON.stringify(board))
if (checkWin()) {
message.innerHTML = "You win!"
}
} else {
formInput[focusElement].value = ""
lives -= 1
localStorage.setItem("lives", JSON.stringify(lives))
livesMes.innerHTML = lives
if (lives == 0) {
message.innerHTML = "You have no more lives. You lost!"
for (let i = 0; i < formInput.length; i++) {
formInput[i].readOnly = true;
}
return;
}
message.innerHTML = "Your recent input was incorrect. It has been removed"
}
}
function generateBoard(difficulty) {
const URL = "https://sugoku.herokuapp.com/board?difficulty="
const requestUrl = URL + difficulty
axios.get(requestUrl)
.then((data) => {
let obtained_board = data.data.board
// Modify the data so that 0 becomes -1
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
if (obtained_board[i][j] == 0) {
obtained_board[i][j] = -1
}
}
}
const real_board = obtained_board.flat()
localStorage.setItem("original", JSON.stringify(real_board))
localStorage.setItem("no-solution", JSON.stringify(real_board))
const is_solvable = sudokuSolver(obtained_board)
if (is_solvable) {
const real_solution = obtained_board.flat()
localStorage.setItem("with-solution", JSON.stringify(real_solution))
writeValues(real_board, real_solution)
} else {
localStorage.clear()
alert("Error occurred, board generated is not solvable, please try again")
}
})
.catch((err) => {
console.error(err);
alert("Error occurred, please try again later!")
})
}
function setDemo() {
const test = [
[3, 9, -1, -1, 5, -1, -1, -1, -1],
[-1, -1, -1, 2, -1, -1, -1, -1, 5],
[-1, -1, -1, 7, 1, 9, -1, 8, -1],
[-1, 5, -1, -1, 6, 8, -1, -1, -1],
[2, -1, 6, -1, -1, 3, -1, -1, -1],
[-1, -1, -1, -1, -1, -1, -1, -1, 4],
[5, -1, -1, -1, -1, -1, -1, -1, -1],
[6, 7, -1, 1, -1, 5, -1, 4, -1],
[1, -1, 9, -1, -1, -1, 2, -1, -1]
]
let demo = test.flat();
sudokuSolver(test);
let demo_sol = test.flat()
writeValues(demo, demo_sol);
}
function setFocus() {
for (let i = 0; i < formInput.length; i++) {
if (formInput[i] === document.activeElement) {
focusElement = i;
}
}
}
function writeValues(nums, solved) {
for (let i = 0; i < nums.length; i++) {
if (nums[i] != -1) {
formInput[i].value = nums[i];
formInput[i].readOnly = true;
const original = JSON.parse(localStorage.getItem("original"))
if (nums[i] == original[i] && nums[i] != -1 && original[i] != -1) {
formInput[i].style.fontWeight = "bold";
}
}
formInput[i].dataset.solution = solved[i]
formInput[i].dataset.index = i;
formInput[i].onkeydown = (e) => {
if (e.which === 38 || e.which === 40) {
e.preventDefault();
}
}
formInput[i].onfocus = setFocus
formInput[i].oninput = checkInput
}
}
function checkWin() {
for (let i = 0; i < formInput.length; i++) {
if (formInput[i].value == "") {
return false;
}
}
return true;
}
function solve() {
document.getElementById("hint").disabled = true;
document.getElementById("solve-button").disabled = true;
for (let i = 0; i < formInput.length; i++) {
if (formInput[i].value == "") {
formInput[i].value = formInput[i].dataset.solution
formInput[i].readOnly = true
}
}
const solution = JSON.parse(localStorage.getItem("with-solution"))
localStorage.setItem("no-solution", JSON.stringify(solution))
}