-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
78 lines (64 loc) · 1.93 KB
/
main.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
const size = 40;
let sudoku;
function setup() {
createCanvas(size * 9, size * 9);
frameRate(10);
sudoku = new Sudoku();
}
function draw() {
clear();
textSize(size / 2);
for(let i = 0; i < 81; i++) {
let row = Math.floor(i / 9);
let col = i % 9;
let cell = sudoku.grid.getCell(row, col);
stroke(96);
if(cell.focused) {
fill(96);
} else {
noFill();
}
square(col * size, row * size, size);
noStroke();
fill(cell.stored ? 255 : 192);
textFont(cell.stored ? 'Lemonada' : 'Shadows Into Light');
text(cell.value ? cell.value : '', col * size + size / 3, row * size + size / 1.5);
}
stroke(255);
noFill();
square(0, 0, width);
line(width / 3, 0, width / 3, height);
line(width / 3 * 2, 0, width / 3 * 2, height);
line(0, height / 3, width, height / 3);
line(0, height / 3 * 2, width, height / 3 * 2);
}
function mousePressed() {
if(mouseX < 0 - size || mouseX > width + size || mouseY < 0 - size || mouseY > width + size) {
return;
}
for(let i = 0; i < 81; i++) {
let row = Math.floor(i / 9);
let col = i % 9;
let cell = sudoku.grid.getCell(Math.floor(i / 9), i % 9);
if(mouseX >= col * size && mouseX < col * size + size
&& mouseY >= row * size && mouseY < row * size + size) {
if(cell.focused) {
cell.value = (cell.value + 1) % 10;
}
cell.focused = !cell.stored;
} else {
cell.focused = false;
}
}
}
function keyPressed() {
let value = keyCode - 48;
if(value >= 0 && value < 10) {
sudoku.setFocusedCell(value);
} else {
value = keyCode - 96;
if(value >= 0 && value < 10) {
sudoku.setFocusedCell(value);
}
}
}