-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
156 lines (120 loc) · 4.01 KB
/
index.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
var arr = [[], [], [], [], [], [], [], [], []]
var temp = [[], [], [], [], [], [], [], [], []]
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 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 () {
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()
}
//to be completed by student
function isSafe(board,row,col,number) {
//check both rows & cols whether same number is present
for (var x=0;x<9;x++) {
if (board[x][col]==number || board[row][x]==number){
return false;
}
}
//check the subgrid in which current value of (row,col) whether it is present or not.
var sx =row-row%3; //starting index of subgrid's x cordinate in which (i,j) exists
var sy=col-col%3; //starting index of subgrid's y-cordinate in which (i,j) exists
for (var i=sx;i<sx+3;i++){
for (var j=sy;j<sy+3;j++){
if (board[i][j]==number){
return false;
}
}
}
return true; //if not found in this subgrid then it's true means we can safe to fill that number into that cell
}
//to be completed by student
function solveSudokuHelper(board,row,col) {
//base case
if (row== 9) { //when we already hit the last cell of board i.e(8,8)
changeBoard(board);
return;
}
if (col== 9) { //when in each row ,col 0 t0 8 ,when at last col,call another row with col value 0
solveSudokuHelper(board, row + 1, 0)
return;
}
if (board[row][col] != 0) {//skip all pre-filled cells i.e already that cells are filled with some values so means next call recursively
solveSudokuHelper(board,row,col + 1);
return;
}
//here we are at inital row & col to fill value from 1 to 9 in the empty cells
for (var number = 1; number <= 9; number++) {
if (isSafe(board,row,col, number)) {
board[row][col] = number;//we asusume this is the correct number temporarily if we are safe to fill that number in that cells
solveSudokuHelper(board,row,col+1);
board[row][col] = 0; //backtracking i.e if the number is not suitable to fill in that position of the cell then back from that position with putting 0 value in that cell
}
}
}
function solveSudoku(board){
solveSudokuHelper(board, 0, 0)
}
solve.onclick = function(){
solveSudoku(board);
}