-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsudokuscript.js
183 lines (155 loc) · 3.42 KB
/
sudokuscript.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
const submit=document.getElementById('submit');
const invalidInputMsg = document.getElementById('invalid-input-msg');
submit.addEventListener('click',answer);
function answer(){
var A= new Array();
for (let i=0;i<81;i++){
//taking the input values in a 1D array
A[i]=Number(document.getElementsByTagName('input')[i].value);
const inputValue = Number(document.getElementsByTagName('input')[i].value);
if (!isNaN(inputValue) && (inputValue === 0 || (inputValue >= 1 && inputValue <= 9))) {
A[i] = inputValue;
} else {
A[i] = 0;
invalidInputMsg.style.display = 'block';
return;
}
// If all inputs are valid, hide the invalid input message
invalidInputMsg.style.display = 'none';
}
//Giving output a different color
for(let i=0;i<81;i++){
if(A[i]==0){
document.getElementsByTagName('input')[i].style.color='rgb(89, 89, 231)';
}
}
//converting 1D input array to a 2D array/board
var board=[];
while(A.length>0){
board.push(A.splice(0,9));
}
let N = board.length;
let inputValid= checkInput(board);
if(inputValid){
if (solveSudoku(board, N))
{
print(board);
}
else
{
alert('No Solution');
}
}
}
function isZero(board){
let sum=0;
for(let i=0;i<9;i++){
for(let j=0;j<9;j++){
sum=sum+board[i][j];
}
}
return sum==0;
}
function checkInput(board){
//check if there is no input
if(isZero(board)){
document.getElementById('alert-msg').style.display='block';
return false
}
document.getElementById('alert-msg').style.display='none';
return true;
}
function isSafe(board, row, col, value)
{
//check if row has the value already
for(let j = 0; j < board.length; j++)
{
if (board[row][j] == value)
{
return false;
}
}
//check if column has the value already
for(let i = 0; i < board.length; i++)
{
if (board[i][col] == value)
{
return false;
}
}
//check if the 3*3 matrix has the value already
let sqrt = Math.floor(Math.sqrt(board.length));
let boxRowStart = row - row % sqrt;
let boxColStart = col - col % sqrt;
for(let i= boxRowStart;i<boxRowStart+sqrt;i++)
{
for(let j = boxColStart;j< boxColStart+sqrt;j++)
{
if (board[i][j] == value)
{
return false;
}
}
}
// If none of above conditions satisfied, it's safe
return true;
}
function solveSudoku(board, n)
{
let row = -1;
let col = -1;
let isEmpty = true;
for(let i = 0; i < n; i++)
{
for(let j = 0; j < n; j++)
{
if (board[i][j] == 0)
{
row = i;
col = j;
// We still have some remaining
// missing values in Sudoku
isEmpty = false;
break;
}
}
if (!isEmpty)
{
break;
}
}
// No empty space left
if (isEmpty)
{
return true;
}
// Else for each-row backtrack
for(let num = 1; num <= n; num++)
{
if (isSafe(board, row, col, num))
{
board[row][col] = num;
if (solveSudoku(board, n))
{
return true;
}
else
{
board[row][col] = 0;
}
}
}
return false;
}
function print(board){
//changing 2D back to 1D
const output = new Array();
for(let i=0;i<9;i++){
for(let j=0;j<9;j++){
output.push(board[i][j]);
}
}
for(let k=0;k<81;k++){
document.getElementsByTagName('input')[k].value=output[k];
}
}