-
Notifications
You must be signed in to change notification settings - Fork 1
/
sudoku.js
116 lines (96 loc) · 3.11 KB
/
sudoku.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
var actionSet = [0, 0, 0];
var currAction = -1;
var cellLookupTable = [null, 6, 7, 8, 3, 4, 5, 0, 1, 2];
$(document).ready(function() {
// Fill in a starting sudoku grid
fillSudokuGrid("14.7...6985.6.31....3..4..53.....71.2..1.5..4.91.....85..8..9....65.7.8372...1.56");
// This function outlines the core mechanic of Numpad Sudoku
// (will probably move it to a separate function soon)
$(document).keypress(function(e) {
// 1-9 are used to select a block or cell, or to fill a cell
if(e.key >= 1 && e.key <= 9) {
currAction++;
clearHighlight();
if(currAction == 0) {
actionSet[0] = e.key;
highlightBlock(e.key);
}
else if(currAction == 1) {
actionSet[1] = e.key;
highlightCell(actionSet.slice(0, 2));
}
else if(currAction == 2) {
actionSet[2] = e.key;
writeValueToCell(actionSet.slice(0, 2), actionSet[2]);
}
}
// 0 will be used to clear the value of a cell on the grid, or to cancel an action set
else if(e.key == 0) {
currAction++;
if(currAction == 1)
clearActionSet();
if(currAction == 2) {
actionSet[2] = e.key;
writeValueToCell(actionSet.slice(0, 2), actionSet[2]);
}
}
// The period (.) key will be used to cancel an action set
else if(e.key == ".") {
clearActionSet();
}
});
});
function highlightBlock(blockNumber) {
$(".square" + blockNumber).addClass("highlighted");
}
function highlightCell(coordinates) {
var selector = ".square" + coordinates[0];
var block = $(selector);
var index = cellLookupTable[coordinates[1]];
var cell = block[index];
$(cell).addClass("highlighted");
}
function clearHighlight() {
$(".highlighted").removeClass("highlighted");
}
function writeValueToCell(coordinates, value) {
var selector = ".square" + coordinates[0];
var block = $(selector);
var index = cellLookupTable[coordinates[1]];
var cell = block[index];
// Null value will clear the cell when 0 key has been pressed
if(value == 0)
value = null;
// Only change the cell's value if it is not a pre-filled cell (clue)
if(!$(cell).hasClass("clue"))
$(cell).text(value);
clearActionSet();
}
function clearActionSet() {
clearHighlight();
actionSet = new Array(3);
currAction = -1;
}
/**
* Fills the sudoku grid from a string that lists the cells to be filled in
* (clues) and the ones to be left empty (I will call them "blanks").
* Example string (one row only): "1..4.67.9"
* The entire grid is represented in one string, with no separating character
* between the rows or boxes, so the string should be 81 characters long.
*
* Note: format may be changed later, as I have not yet decided how to store
* the puzzles before they are loaded on the page (I do not want to
* randomly generate them every time, as this would require validation
* of whether the puzzles have unique solutions).
*
* @param {string} gridString - string of clues and blanks in format described above
*/
function fillSudokuGrid(gridString) {
$("td").each(function(index) {
currValue = gridString.charAt(index);
if(currValue != '.')
$(this).text(currValue).addClass("clue");
else
$(this).text(null).removeClass("clue");
});
}