-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.html
151 lines (133 loc) · 3.62 KB
/
sudoku.html
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
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Sudoku">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Sudoku</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="row">
<div class="col-md-offset-3 col-md-6 col-md-offset-3">
<form id="sudoku" class="">
<table class="table table-condensed table-bordered table-hover "></table>
<input type="button" class="btn btn-success pull-right btn-block" value="Solve">
<input type="button" class="btn btn-danger pull-right btn-block" value="Reset">
</form>
</div>
</div>
</div>
</body>
</html>
<script>function solve(matrix) {
var i, j, b, k;
for (i = 0; i <= 8; i++) {
for (j = 0; j <= 8; j++) {
//check for legitimate values
if (!matrix[i][j]) {
for (k = 1; k <= 9; k++) {
if (insert(matrix, i, j, k)) {
matrix[i][j] = k;
b = solve(matrix);
if (b) { return true; }
matrix[i][j] = 0;
}
}
return false;
}
}
}
return true;
}
function insert(matrix, i, j, k) {
//check column and rows
var a, b;
for (a = 0; a <= 8; a++) {
if (a != i && matrix[a][j] == k) {
return false;
}
}
for (a = 0; a <= 8; a++) {
if (a != j && matrix[i][a] == k) {
return false;
}
}
//check the 3 by 3 squares
var y = Math.floor((i / 3)) * 3,
x = Math.floor((j / 3)) * 3;
for (a = 0; a < 3; a++) {
for (b = 0; b < 3; b++) {
if (a != i && b != j && matrix[y + a][x + b] == k) {
return false;
}
}
}
return true;
}
function test() {
var form = document.querySelector('form#sudoku'),
matrix,
holder = [],
i, j, k, z;
for (i = 0; i < 81; i++) {
//get the form values
holder[i] = form[i].value;
matrix = [];
k = -1;
// from 1 dimensional to matrix
for (j = 0; j < holder.length; j++) {
if (j % 9 === 0) {
k++;
matrix[k] = [];
}
matrix[k].push(holder[j]);
}
}
solve(matrix);
//display the solved sudoku numbers
z = 0;
for (i = 0; i < matrix.length; i++) {
for (j = 0; j < matrix[i].length; j++) {
//display the solved sudoku numbers
form[z].value = matrix[i][j];
z++;
}
}
}
function reset() {
var form = document.querySelector('form#sudoku'),
i;
for (i = 0; i < 81; i++) {
//get the form values
form[i].value ='';
}
}
function populateTable() {
var i, j, tr, td, input,
table = document.querySelector('form#sudoku table'),
tbody = document.createElement('tbody');
table.appendChild(tbody);
for (i = 0; i < 9; i++) {
tr = document.createElement('tr');
tbody.appendChild(tr);
for (j = 0; j < 9; j++) {
td = document.createElement('td');
//td.className += " text-center";
td.style = " font-weight: 700";
tr.appendChild(td);
input = document.createElement('input');
input.type = 'text';
input.size = 1;
input.style = " text-align: center";
td.appendChild(input);
}
}
document.querySelector('input[value=Solve]')
.addEventListener('click', test);
document.querySelector('input[value=Reset]')
.addEventListener('click', reset);
}
window.onload = populateTable();
</script>