-
Notifications
You must be signed in to change notification settings - Fork 0
/
sudoku.html
427 lines (357 loc) · 9.86 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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
<html>
<head>
<style>
body {
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
width: 100%;
height: 100%;
margin: 0;
overflow: hidden;
}
#user_input {
width: 100px;
height: 100px;
position: absolute;
display: none;
text-align: center;
caret-color: transparent;
font-family: "David";
font-size: 20px;
color: blue;
}
#user_input::-webkit-inner-spin-button {
-webkit-appearance: none;
margin: 0;
}
#canvas {
background: gray;
color: white;
border: 1px solid black;
}
#options {
flex-direction: column;
display: flex;
padding: 10px;
align-items: center;
}
#options .inputs {
display: flex;
padding: 10px;
}
#options button {
width: 300px;
height: 25px;
}
#options .inputs .field {
padding: 10px;
display: flex;
justify-content: center;
flex-direction: row;
}
#options .inputs .field input {
width: 50px;
height: 25px;
}
#options .inputs .field span {
padding: 0px 10px 0px 0px;
display: inline-flex;
align-items: center;
}
</style>
</head>
<body>
<div id="options">
<div class="inputs">
<div class="field">
<span>Revealed Numbers:</span><input type="number" id="revealed" value="20" />
</div>
</div>
<button id="start">Start New Game</button>
</div>
<canvas id="canvas" width="600" height="600"></canvas>
<input type="number" id="user_input"></input>
<script>
const TILES_GAP = 1;
const BOXES_GAP = 6;
const EMPTY = -1;
const REAL_VALUE = 1;
const USER_INPUT = 2;
const canvas = document.getElementById("canvas");
const canvas2d = canvas.getContext("2d");
const input = document.getElementById("user_input");
let frameWidth = canvas.width;
let frameHeight = canvas.height;
let tiles;
let gridSize = 9;
function randomInt(max) {
return Math.floor(Math.random() * max);
}
function initializeEmptyTile() {
return {
value: EMPTY,
userInput: EMPTY,
isRevealed: false
};
}
function initializeEmptyRow() {
let row = []
for (let x = 0; x < gridSize; x++) {
row.push(initializeEmptyTile());
}
return row;
}
function initializeEmptyGrid() {
let result = []
for (let y = 0; y < gridSize; y++) {
result.push(initializeEmptyRow());
}
return result;
}
function getValue(x, y, valueType) {
if (valueType == REAL_VALUE) {
return tiles[y][x].value;
} else if (valueType == USER_INPUT) {
return tiles[y][x].userInput
} else {
console.log("Invalid value type");
}
}
function existInRow(y, number, valueType) {
for (let x = 0; x < gridSize; x++) {
if (getValue(x, y, valueType) == number) {
return true;
}
}
return false;
}
function existInColumn(x, number, valueType) {
for (let y = 0; y < gridSize; y++) {
if (getValue(x, y, valueType) == number) {
return true;
}
}
return false;
}
function existInBox(boxX, boxY, number, valueType) {
for (let y = boxY * 3; y < (boxY * 3) + 3; y++) {
for (let x = boxX * 3; x < (boxX * 3) + 3; x++) {
if (getValue(x, y, valueType) == number) {
return true;
}
}
}
return false;
}
function isValidNumber(x, y, number, valueType) {
let boxX = Math.floor(x / 3);
let boxY = Math.floor(y / 3);
return (
!existInRow(y, number, valueType) &&
!existInColumn(x, number, valueType) &&
!existInBox(boxX, boxY, number, valueType)
);
}
function fillNumbers(tileNumber) {
let y = Math.floor(tileNumber / gridSize);
let x = tileNumber % gridSize;
if (tileNumber >= gridSize * gridSize) {
return true;
}
if (tiles[y][x].value != EMPTY) {
return true;
}
let seed = randomInt(9) + 1;
for (let i = 0; i < 9; i++) {
let candidate = ((seed + i) % 9) + 1;
if (!isValidNumber(x, y, candidate, REAL_VALUE)) {
continue;
}
tiles[y][x].value = candidate;
if (fillNumbers(tileNumber + 1)) {
return true;
}
tiles[y][x].value = EMPTY;
}
return false;
}
function revealNumbers(desiredNumbers) {
let revealedCount = 0;
while (revealedCount < desiredNumbers) {
let x = randomInt(9);
let y = randomInt(9);
if (tiles[y][x].isRevealed) {
continue;
}
tiles[y][x].isRevealed = true;
tiles[y][x].userInput = tiles[y][x].value;
revealedCount = revealedCount + 1;
}
}
function initialize(revealedCount) {
canvas.height = gridSize * 70;
canvas.width = gridSize * 70;
frameWidth = canvas.width;
frameHeight = canvas.height;
tiles = initializeEmptyGrid();
fillNumbers(0);
revealNumbers(revealedCount);
}
function boxWidth() {
let boxColumn = Math.floor(gridSize / 3);
return (frameWidth - (BOXES_GAP * (boxColumn + 1))) / boxColumn;
}
function boxHeight() {
let boxRow = Math.floor(gridSize / 3);
return (frameHeight - (BOXES_GAP * (boxRow + 1))) / boxRow;
}
function boxLeft(x) {
return (x * boxWidth()) + ((x + 1) * BOXES_GAP);
}
function boxTop(y) {
return (y * boxHeight()) + ((y + 1) * BOXES_GAP);
}
function tileWidth() {
return Math.ceil((boxWidth() - (TILES_GAP * (3 + 1))) / 3);
}
function tileHeight() {
return Math.ceil((boxHeight() - (TILES_GAP * (3 + 1))) / 3);
}
function tileLeft(x) {
let relativeX = x % 3;
let boxX = Math.floor(x / 3);
return boxLeft(boxX) + (relativeX * tileWidth()) + ((relativeX + 1) * TILES_GAP);
}
function tileTop(y) {
let relativeY = y % 3
let boxY = Math.floor(y / 3);
return boxTop(boxY) + (relativeY * tileHeight()) + ((relativeY + 1) * TILES_GAP);
}
function drawTile(x, y) {
canvas2d.fillStyle = "white";
canvas2d.fillRect(tileLeft(x), tileTop(y), tileWidth(), tileHeight());
if (tiles[y][x].isRevealed) {
canvas2d.font = "20px David";
canvas2d.fillStyle = "black";
canvas2d.textAlign = "center";
canvas2d.textBaseline = "middle";
canvas2d.fillText(tiles[y][x].value, tileLeft(x) + (tileWidth() / 2), tileTop(y) + (tileHeight() / 2));
} else if (tiles[y][x].userInput != EMPTY) {
canvas2d.font = "20px David";
canvas2d.fillStyle = "blue";
canvas2d.textAlign = "center";
canvas2d.textBaseline = "middle";
canvas2d.fillText(tiles[y][x].userInput, tileLeft(x) + (tileWidth() / 2), tileTop(y) + (tileHeight() / 2));
}
}
function draw() {
canvas2d.clearRect(0, 0, frameWidth, frameHeight);
for (let y = 0; y < gridSize; y++) {
for (let x = 0; x < gridSize; x++) {
drawTile(x, y);
}
}
}
function check() {
for (let y = 0; y < gridSize; y++) {
for (let x = 0; x < gridSize; x++) {
if (tiles[y][x].userInput == EMPTY) {
return false;
}
let userInputValue = tiles[y][x].userInput
tiles[y][x].userInput = EMPTY;
let isValid = isValidNumber(x, y, userInputValue, USER_INPUT)
tiles[y][x].userInput = userInputValue;
if (!isValid) {
return false;
}
}
}
return true;
}
function hideUserInput() {
input.attributes["x"] = ""
input.attributes["y"] = ""
input.style.display = "none"
}
function showUserInput(canvasRect, x, y) {
input.style.left = (canvasRect.left + tileLeft(x)) + "px";
input.style.top = (canvasRect.top + tileTop(y)) + "px";
input.style.width = tileWidth() + TILES_GAP * 2 + "px"
input.style.height = tileHeight() + TILES_GAP * 2 + "px"
if (tiles[y][x].userInput == EMPTY) {
input.value = "";
} else {
input.value = tiles[y][x].userInput;
}
input.style.display = "initial"
input.attributes["x"] = x
input.attributes["y"] = y
input.focus();
input.select();
}
function handleClick(event) {
let rect = event.target.getBoundingClientRect();
let mouseX = event.clientX - rect.left;
let mouseY = event.clientY - rect.top;
let boxX = Math.floor(mouseX / (frameWidth / 3));
let boxY = Math.floor(mouseY / (frameHeight / 3));
let relativeMouseX = mouseX - boxLeft(boxX);
let relativeMouseY = mouseY - boxTop(boxY);
let tileX = (boxX * 3) + Math.floor(relativeMouseX / (boxWidth() / 3));
let tileY = (boxY * 3) + Math.floor(relativeMouseY / (boxHeight() / 3));
if (!tiles[tileY][tileX].isRevealed) {
showUserInput(rect, tileX, tileY);
}
}
function handleInputChange(event) {
let x = input.attributes.x
let y = input.attributes.y
let userInput = parseInt(input.value);
console.log(userInput)
if (!isNaN(userInput)) {
if (userInput < 1 || userInput > 9) {
alert("Invalid number: " + userInput);
return;
}
tiles[y][x].userInput = userInput;
} else {
tiles[y][x].userInput = EMPTY;
}
draw();
if (check()) {
alert("win");
startNewGame();
}
}
function startNewGame() {
revealedCount = document.getElementById("revealed").valueAsNumber
if (revealedCount < 0 || revealedCount > 80) {
alert("Invalid number of revealed numbers");
return;
}
initialize(revealedCount);
draw();
}
function main() {
startNewGame();
document.getElementById("start").onclick = function(event) {
startNewGame();
};
canvas.onclick = function(event) {
event.stopPropagation();
handleClick(event);
};
input.onblur = function(event) {
hideUserInput();
}
input.onchange = function(event) {
handleInputChange(event);
}
}
main();
</script>
</body>
</html>