CherryKitten
70cee77a95
Currently contains the Unit Tests, which should already be done correctly. Functional tests still need to be added.
64 lines
No EOL
1.9 KiB
JavaScript
64 lines
No EOL
1.9 KiB
JavaScript
const textArea = document.getElementById("text-input");
|
|
const coordInput = document.getElementById("coord");
|
|
const valInput = document.getElementById("val");
|
|
const errorMsg = document.getElementById("error");
|
|
|
|
document.addEventListener("DOMContentLoaded", () => {
|
|
textArea.value =
|
|
"..9..5.1.85.4....2432......1...69.83.9.....6.62.71...9......1945....4.37.4.3..6..";
|
|
fillpuzzle(textArea.value);
|
|
});
|
|
|
|
textArea.addEventListener("input", () => {
|
|
fillpuzzle(textArea.value);
|
|
});
|
|
|
|
function fillpuzzle(data) {
|
|
let len = data.length < 81 ? data.length : 81;
|
|
for (let i = 0; i < len; i++) {
|
|
let rowLetter = String.fromCharCode('A'.charCodeAt(0) + Math.floor(i / 9));
|
|
let col = (i % 9) + 1;
|
|
if (!data[i] || data[i] === ".") {
|
|
document.getElementsByClassName(rowLetter + col)[0].innerText = " ";
|
|
continue;
|
|
}
|
|
document.getElementsByClassName(rowLetter + col)[0].innerText = data[i];
|
|
}
|
|
return;
|
|
}
|
|
|
|
async function getSolved() {
|
|
const stuff = {"puzzle": textArea.value}
|
|
const data = await fetch("/api/solve", {
|
|
method: "POST",
|
|
headers: {
|
|
"Accept": "application/json",
|
|
"Content-type": "application/json"
|
|
},
|
|
body: JSON.stringify(stuff)
|
|
})
|
|
const parsed = await data.json();
|
|
if (parsed.error) {
|
|
errorMsg.innerHTML = `<code>${JSON.stringify(parsed, null, 2)}</code>`;
|
|
return
|
|
}
|
|
fillpuzzle(parsed.solution)
|
|
}
|
|
|
|
async function getChecked() {
|
|
const stuff = {"puzzle": textArea.value, "coordinate": coordInput.value, "value": valInput.value}
|
|
const data = await fetch("/api/check", {
|
|
method: "POST",
|
|
headers: {
|
|
"Accept": "application/json",
|
|
"Content-type": "application/json"
|
|
},
|
|
body: JSON.stringify(stuff)
|
|
})
|
|
const parsed = await data.json();
|
|
errorMsg.innerHTML = `<code>${JSON.stringify(parsed, null, 2)}</code>`;
|
|
}
|
|
|
|
|
|
document.getElementById("solve-button").addEventListener("click", getSolved)
|
|
document.getElementById("check-button").addEventListener("click", getChecked) |