-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsudoku.js
More file actions
343 lines (276 loc) · 8.73 KB
/
sudoku.js
File metadata and controls
343 lines (276 loc) · 8.73 KB
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
var start='';
var end = '';
var bit = '';
var possibleBits;
var expanded = [];
window.document.onkeydown = function (e)
{
if (!e) e = event;
if (e.keyCode == 27)
clearCells();
}
function isNetworkPossible()
{
document.getElementById("save").disabled=true;
network = document.getElementById("network").value;
mask = document.getElementById("mask").value;
var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
r=xmlhttp.responseText;
z=document.getElementById("hint");
z.innerHTML = r;
if(r.indexOf("OK!")> -1)
{
document.getElementById("save").disabled=false;
}
}
}
xmlhttp.open("GET", "isPossible.php?network=" + network + "&mask="+mask, true);
xmlhttp.send();
}
function getPossibleSubnets(net, mask)
{
var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
// Startfield in bin
bs=Hex2Bin8(start);
possibleBits = parseInt(xmlhttp.responseText);
// mark possible space for subnetting in lightred
for(i=1; i< Math.pow(2, possibleBits); i++)
{
z=document.getElementById("cell" + addLeadingZeros(Bin2Hex(bs.substr(0, 8 - bit).toString() + Dec2Bin8n(i,bit)).toUpperCase(),2));
{
z.style.backgroundColor = "#ff8888";
}
}
}
}
xmlhttp.open("GET", "getPossibleMasks.php?mask="+mask+"&subnet=" + net, true);
xmlhttp.send();
}
function expandNetworks(id, lastNetwork_bin, mask, root)
{
if(expanded.indexOf([id])==-1 && expanded[id] != '')
{
ul = document.getElementById("hiddennNetworks" + id);
expanded[id] = ul.innerHTML;
ul.innerHTML = '';
// for every new Subnet
li = document.createElement("li");
li.appendChild(document.createTextNode("loading subnets..."));
ul.appendChild(li);
var xmlhttp;
if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else { // code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
{
ul.innerHTML = '';
ul.innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "getHiddenNetworks.php?mask="+mask+"&lastNetwork_bin=" + lastNetwork_bin + "&root=" + root, true);
xmlhttp.send();
}
else
{
ul = document.getElementById("hiddennNetworks" + id);
ul.innerHTML = '';
ul.innerHTML = expanded[id];
delete expanded[id];
}
}
function selectCell(cell) {
if(start=='')
{
start=cell;
// Startfield in bin
bs=Hex2Bin8(start);
// mark start-filed with special Color
c=document.getElementById("cell" + cell);
c.style.backgroundColor = "red";
// calculate available bits for subnetting
x=("1" + bs).lastIndexOf("1") -1;
bit = 7 - x;
s=document.getElementById("form_network");
s.value=start;
// calculate new binary network
basenetwork = document.getElementById("form_basenetwork").value;
basemask = document.getElementById("form_basemask").value;
part1 = basenetwork.slice( 0, parseInt(basemask) );
c = part1 + bs + basenetwork.slice( parseInt(basemask) + 8, 128);
// get PossibleNetworks via ajax
getPossibleSubnets(c, basemask);
document.getElementById("form_maska").value = (parseInt(document.getElementById("form_basemask").value) + (8));
}
else
{
// Startfield in bin
bs=Hex2Bin8(start);
// cell in Bin
bc=Hex2Bin8(cell);
// action is only required if we are in a possible subnet
if ( bs.substr(0, 8 - possibleBits) == bc.substr(0, 8 - possibleBits) )
{
end=cell;
// calculate available bits for subnetting
x=("1" + bs).lastIndexOf("1") -1;
bit = 7 - x;
// calculate changed bits
y=bit-(bc.substr(8 - bit, bit)+"1").indexOf("1");
// write mask to form
document.getElementById("form_mask").value = (parseInt(document.getElementById("form_basemask").value) + (8-y));
document.getElementById("form_maska").value = (parseInt(document.getElementById("form_basemask").value) + (8-y));
// calculate new binary network
basenetwork = document.getElementById("form_basenetwork").value;
basemask = document.getElementById("form_basemask").value;
part1 = basenetwork.slice( 0, parseInt(basemask) );
c = part1 + bs + basenetwork.slice( parseInt(basemask) + 8, 128);
document.getElementById("form_basenetwork").value = c;
document.getElementById("form_description").value = "";
document.getElementById("form_description").disabled = false;
document.getElementById("form_color").disabled = false;
document.getElementById("form_submit").disabled = false;
}
}
}
function highlightCell(cell) {
if(start != '' && end == '')
{
// Startfield in bin
bs=Hex2Bin8(start);
// cell in Bin
bc=Hex2Bin8(cell);
// calculate available bits for subnetting
x=("1" + bs).lastIndexOf("1") -1;
bit = 7 - x;
// action is only required if we are in a possible subnet
if ( bs.substr(0, 8 - possibleBits) == bc.substr(0, 8 - possibleBits) )
{
// calculate changed bits
y=bit-(bc.substr(8 - bit, bit)+"1").indexOf("1");
document.getElementById("form_maska").value = (parseInt(document.getElementById("form_basemask").value) + (8-y));
// mark possible space for subnetting in lightred
for(i=1; i< Math.pow(2, possibleBits); i++)
{
z=document.getElementById("cell" + addLeadingZeros(Bin2Hex(bs.substr(0, 8 - bit).toString() + Dec2Bin8n(i,bit)).toUpperCase(),2));
z.style.backgroundColor = "#ff8888";
}
// mark selected space for subnetting in red
for(i=1; i< Math.pow(2, y); i++)
{
z=document.getElementById("cell" + addLeadingZeros(Bin2Hex(bs.substr(0, 8 - bit).toString() + Dec2Bin8n(i,bit)).toUpperCase(),2));
z.style.backgroundColor = "red";
}
}
}
}
function clearCell(cell) {
}
function deleteNetework($id)
{
// check if Network has Subnets
// it subnets==true
// askif user wants to delete allso all Subnets
// if yes
window.location = './?action=deleteNetwork&id=620&subnets=1';
return false;
// if no
return true;
}
function onkeypressed(evt, input) {
var code = evt.charCode || evt.keyCode;
if (code == 27) {
clearCells();
}
}
function clearCells() {
// just a Workaround because rest of function does not work propper if selected cell is "00"
location.reload();
return;
// Startfield in bin
bs=Hex2Bin8(start);
// calculate available bits for subnetting
x=("1" + bs).lastIndexOf("1") -1;
bit = 7 - x;
// mark possible space for subnetting in lightblue
if(bit == '0')
{
z=document.getElementById("cell" + start);
z.style.backgroundColor = "white";
}
else
{
for(i=0; i< Math.pow(2, bit); i++)
{
z=document.getElementById("cell" + addLeadingZeros(Bin2Hex(bs.substr(0, 8 - bit).toString() + Dec2Bin8n(i,bit)).toUpperCase(),2));
z.style.backgroundColor = "white";
}
}
start='';
end='';
bit='';
document.getElementById("form_description").disabled=true;
document.getElementById("form_color").disabled=true;
document.getElementById("form_submit").disabled=true;
}
function Hex2Bin8(n)
{
if(!checkHex(n))
return 0;
return addLeadingZeros(parseInt(n,16).toString(2), 8);
}
function Hex2Bin8N(n, c)
{
if(!checkHex(n))
return 0;
return addLeadingZeros(parseInt(n,16).toString(2), c);
}
function Dec2Bin8n(n, c)
{
if(!checkDec(n)||n<0)
return 0;
return addLeadingZeros(n.toString(2), c);
}
function checkHex(n)
{
return/^[0-9A-Fa-f]{1,64}$/.test(n)
}
function addLeadingZeros(number, length) {
var num = '' + number;
while (num.length < length) num = '0' + num;
return num;
}
//Useful Functions
function checkBin(n){return/^[01]{1,64}$/.test(n)}
function checkDec(n){return/^[0-9]{1,64}$/.test(n)}
function pad(s,z){s=""+s;return s.length<z?pad("0"+s,z):s}
function unpad(s){s=""+s;return s.replace(/^0+/,'')}
//Decimal operations
function Dec2Bin(n){if(!checkDec(n)||n<0)return 0;return n.toString(2)}
function Dec2Hex(n){if(!checkDec(n)||n<0)return 0;return n.toString(16)}
//Binary Operations
function Bin2Dec(n){if(!checkBin(n))return 0;return parseInt(n,2).toString(10)}
function Bin2Hex(n){if(!checkBin(n))return 0;return parseInt(n,2).toString(16)}
//Hexadecimal Operations
function Hex2Bin(n){if(!checkHex(n))return 0;return parseInt(n,16).toString(2)}
function Hex2Dec(n){if(!checkHex(n))return 0;return parseInt(n,16).toString(10)}