-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
171 lines (156 loc) · 4.3 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
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
let testGame = [
["", "2", "3", "4", "5", "7", "6", "9", "8"],
["4", "5", "7", "6", "9", "8", "2", "1", ""],
["6", "", "8", "2", "1", "3", "", "", "7"],
["", "", "5", "8", "", "1", "9", "7", "4"],
["", "", "", "9", "7", "4", "5", "", "2"],
["", "7", "", "", "3", "2", "", "", "1"],
["5", "1", "", "", "", "", "7", "8", ""],
["", "4", "6", "7", "", "9", "", "2", ""],
["7", "", "9", "", "", "5", "3", "4", "6"],
];
let squares = [];
let num = "";
for (let i = 0; i < 9; i++) {
let innerArray = [];
for (let j = 0; j < 9; j++) {
num = testGame[j][i];
innerArray.push(new Square(i * 40, j * 40, 40, num));
}
squares.push(innerArray);
}
let inputSound;
let checkSound;
let alertSound;
function preload() {
inputSound = loadSound("./assets/sounds/Input/Input-04a.mp3");
checkSound = loadSound("./assets/sounds/Input/Input-03.mp3");
alertSound = loadSound("./assets/sounds/Alert/Alert-10.mp3");
montserrat = loadFont(
"./assets/fonts/Montserrat_Alternates/MontserratAlternates-Medium.ttf"
);
}
let timer;
let counter = 300;
let seconds, minutes;
function setup() {
let cnv = createCanvas(800, 360);
cnv.position(width / 2, height / 2, "fixed");
button = createButton("check");
button.position(900, 300);
button.mouseClicked(collectNum);
button.style("font-size", "20px");
button.style("background-color", "white");
button.style("border-color", "darkgray");
timer = createP("timer");
timer.style("font-size", "20px");
setInterval(timeIt, 1000);
}
let s = 'check your solution by clicking "check" ';
function draw() {
background(255);
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
squares[i][j].render();
}
}
textFont(montserrat);
textSize(16);
text(s, 560, 200);
}
function timeIt() {
if (counter > 0) {
counter--;
}
minutes = floor(counter / 60);
seconds = counter % 60;
time = timer.html(minutes + ": " + seconds);
time.position(915, 230);
if (minutes === 0 && seconds === 0) {
s = "Start it over by refreshing the page!";
}
}
function mouseClicked() {
for (let i = 0; i < 9; i++) {
for (let j = 0; j < 9; j++) {
squares[i][j].clicked();
}
}
}
function collectNum() {
checkSound.play();
let collected = [];
for (let i = 0; i < 9; i++) {
let innerCollected = [];
for (let j = 0; j < 9; j++) {
innerCollected.push(squares[j][i].textString);
}
collected.push(innerCollected);
}
let result = checkDouble(collected);
if (checkDouble(collected) === true) {
s = "You solved the Panda Sudoku!";
} else {
s = `Hint: ${result}`;
}
}
function check1D(array) {
const numberContainer = ["1", "2", "3", "4", "5", "6", "7", "8", "9"];
for (let i = 0; i < numberContainer.length; i++) {
if (!array.includes(numberContainer[i])) {
return numberContainer[i];
}
}
return true;
}
function checkDouble(collected) {
for (let i = 0; i < collected.length; i++) {
let res = check1D(collected[i]);
if (res !== true) {
return `row ${i + 1} does not include ${res}`;
}
}
for (let i = 0; i < collected.length; i++) {
let col = [];
for (let j = 0; j < collected.length; j++) {
col.push(collected[j][i]);
}
let res = check1D(collected[i]);
if (res !== true) {
return `column ${i + 1} does not include ${res}`;
}
}
for (let i = 0; i < 3; i++) {
let subA1 = [];
for (let j = 0; j < 3; j++) {
subA1.push(collected[3 * i][j]);
subA1.push(collected[3 * i + 1][j]);
subA1.push(collected[3 * i + 2][j]);
}
let res = check1D(collected[i]);
if (res !== true) {
return `Square ${i + 1}, 1 does not include ${res}`;
}
let subA2 = [];
for (let j = 0; j < collected.length; j++) {
subA2.push(collected[3 * i][j + 3]);
subA2.push(collected[3 * i + 1][j + 3]);
subA2.push(collected[3 * i + 2][j + 3]);
}
res = check1D(collected[i]);
if (res !== true) {
return `Square ${i + 1}, 2 does not include ${res}`;
}
let subA3 = [];
for (let j = 0; j < collected.length; j++) {
subA3.push(collected[3 * i][j + 6]);
subA3.push(collected[3 * i + 1][j + 6]);
subA3.push(collected[3 * i + 2][j + 6]);
}
res = check1D(collected[i]);
if (res !== true) {
return `Square ${i + 1}, 3 does not include ${res}`;
}
}
return true;
}