-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
248 lines (206 loc) · 8.72 KB
/
index.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
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
<!DOCTYPE html>
<head>
<!-- <meta name="viewport" content="width=device-width, initial-scale=1"> -->
<link href='http://fonts.googleapis.com/css?family=Rock+Salt' rel='stylesheet' type='text/css'>
<link href="sudoku.css" rel="stylesheet" type="text/css">
<link rel="apple-touch-icon" sizes="57x57" href="/apple-touch-icon-57x57.png">
<link rel="apple-touch-icon" sizes="114x114" href="/apple-touch-icon-114x114.png">
<link rel="apple-touch-icon" sizes="72x72" href="/apple-touch-icon-72x72.png">
<link rel="apple-touch-icon" sizes="144x144" href="/apple-touch-icon-144x144.png">
<link rel="apple-touch-icon" sizes="60x60" href="/apple-touch-icon-60x60.png">
<link rel="apple-touch-icon" sizes="120x120" href="/apple-touch-icon-120x120.png">
<link rel="apple-touch-icon" sizes="76x76" href="/apple-touch-icon-76x76.png">
<link rel="apple-touch-icon" sizes="152x152" href="/apple-touch-icon-152x152.png">
<link rel="icon" type="image/png" href="/favicon-196x196.png" sizes="196x196">
<link rel="icon" type="image/png" href="/favicon-160x160.png" sizes="160x160">
<link rel="icon" type="image/png" href="/favicon-96x96.png" sizes="96x96">
<link rel="icon" type="image/png" href="/favicon-16x16.png" sizes="16x16">
<link rel="icon" type="image/png" href="/favicon-32x32.png" sizes="32x32">
<meta name="msapplication-TileColor" content="#2d89ef">
<meta name="msapplication-TileImage" content="/mstile-144x144.png">
<script type="text/javascript" src="lib/vue.min.js"></script>
<script type="text/javascript" src="lib/set.js"></script>
<script type="text/javascript" src="lib/math2.js"></script>
<script type="text/javascript" src="solver.js"></script>
<script type="text/javascript" src="sudokus.js"></script>
<title>Sudoku Colors</title>
</head>
<body>
<h2 class="title">Sudoku Colors</h2>
<div id="sudoku1"
class="sudoku"
v-on="keypress : update,
keydown : beforeRemove,
keydown : moveWithKeys,
keyup: afterRemove">
</div>
<div class="buttons">
<button class="solveAction" v-on="click : newSudoku">new</button>
<button class="solveAction"
v-on="click: solve"
v-attr="disabled : state != 'initial' && 'disabled'">solve</button>
<button class="solveAction"
v-on="click : reset"
>reset</button>
</div>
<section>
<p>
Each cell is colored dynamically, based on the number of filled in cells in its row (red), column (blue) and quadrant (green). This gives each sudoku a unique color pattern.
Also whenever a number is added, the colors change accordingly. So when there are no more valid options, everything is white and you have completed the sudoku.
</p>
<a href="https://twitter.com/share" class="twitter-share-button" data-via="remcoder" data-dnt="true">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>
<div id="fb-root"></div>
<script>(function(d, s, id) {
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js#xfbml=1&appId=185975018212030&version=v2.0";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>
<div class="fb-like" data-href="" data-layout="button_count" data-action="like" data-show-faces="false" data-share="true"></div>
</section>
<a href="https://github.com/remcoder/sudoku-colors"><img style="position: absolute; top: 0; right: 0; border: 0;" src="https://camo.githubusercontent.com/52760788cde945287fbb584134c4cbc2bc36f904/68747470733a2f2f73332e616d617a6f6e6177732e636f6d2f6769746875622f726962626f6e732f666f726b6d655f72696768745f77686974655f6666666666662e706e67" alt="Fork me on GitHub" data-canonical-src="https://s3.amazonaws.com/github/ribbons/forkme_right_white_ffffff.png"></a>
</body>
<script>
var title = document.querySelector(".title");
var flickerStart = new Date();
title.classList.add("glowing");
(function flicker() {
var duration = new Date() - flickerStart;
if (duration > 4000) {
title.classList.add("glowing");
}
else {
var r3 = Math.random() * Math.random() * Math.random();
title.classList.toggle('glowing', r3 * duration / 4000 > 0.10 );
setTimeout(flicker, 32);
}
}());
var table = document.querySelector("#sudoku1");
var solver = new SudokuSolver(table);
var button = document.querySelectorAll(".solveAction")[2];
var tempValue;
var arrowKeys = {
37 : { r: 0,c:-1},
38 : { r:-1,c: 0},
39 : { r: 0,c: 1},
40 : { r: 1,c: 0}
};
var vm = new Vue({
el: 'body',
data: {
state : "initial",
examples: sudokus
},
methods : {
init: function(s) {
this.$data.state = 'initial';
solver.init(s);
},
newSudoku : function() {
do {
var sudoku = randomSudoku();
} while (solver.sudokuString == sudoku)
location.hash = "#" + sudoku;
},
update : function(evt) {
// console.log(evt.keyCode);
if ( evt.keyCode < 49 || evt.keyCode > 57 ) {
evt.preventDefault();
return;
}
function validSelection() {
return window.getSelection().toString() == evt.target.value;
}
// cell already contains a value (unless the content of the input is selected)
if ( evt.target.value.length && !validSelection() ) {
evt.preventDefault();
return;
}
var cell = evt.target.parentNode;
var value = String.fromCharCode(evt.keyCode);
var row = +cell.id[4];
var column = +cell.id[5];
solver.updateCell(+value, row, column);
// focus next input
var cell = evt.target.parentNode;
do {
cell = cell.nextElementSibling
if (!cell)
cell = evt.target.parentNode.parentNode.firstElementChild;
var input = cell.querySelector("input");
} while (!input);
this.focusCell(cell);
},
focusCell: function(cell) {
var input = cell.querySelector("input");
setTimeout(function() {
if (input.value) // select the contents of the textbox
input.setSelectionRange(0, input.value.length);
else
input.focus();
},0);
},
beforeRemove : function(evt) {
// console.log(evt.keyCode)
if (evt.keyCode == 8 || evt.keyCode == 46)
tempValue = evt.target.value;
},
afterRemove : function(evt) {
// console.log(evt.keyCode)
if (evt.keyCode == 8 || evt.keyCode == 46 && tempValue) {
if (evt.target.value.length < tempValue.length) {
// console.log(tempValue, "deleted");
var cell = evt.target.parentNode;
var row = cell.parentNode;
solver.updateCell(null, row.rowIndex, cell.cellIndex);
}
tempValue = null;
}
},
moveWithKeys : function(evt) {
console.log(evt.keyCode);
if (!(evt.keyCode in arrowKeys)) return;
var cell = evt.target.parentNode;
var row = +cell.id[4];
var column = +cell.id[5];
var dir = arrowKeys[evt.keyCode];
console.log(dir);
do {
row += dir.r;
column += dir.c;
row = (row + 9) % 9;
column = (column + 9) % 9;
var targetCell = solver._cell(row,column);
} while (!targetCell.querySelector('input'))
this.focusCell(targetCell);
},
solve: function() {
solver.init(); // restart this sudoku to initial state
this.$data.state = 'solving';
var before = new Date();
solver.solve();
var after = new Date();
this.$data.state = 'animating';
console.log(after - before + " ms");
solver.animate(function() {
this.$data.state = 'solved';
}.bind(this));
},
reset: function() {
this.$data.state = 'initial';
solver.init();
}
}
});
window.onload = window.onhashchange = function() {
if (!location.hash)
location.hash = "#" + randomSudoku();
else
vm.init(location.hash.substring(1));
}
function randomSudoku() {
return sudokus[Math.floor(Math.random() * sudokus.length)];
}
</script>