Open LeonMarchetti opened 5 months ago
input.value
: Valor de la celdae.key
: Tecla presionada. Voy a filtrar cuando se teclea un númeroinput.parentElement.cellIndex
: Número de columnainput.parentElement.parentElement
: La fila de la celda, elemento <tr>
input.parentElement.parentElement.rowIndex
: Número de filaFunción para loggear debug y errores
function log(text, logLevel) {
var logFunction = console.log;
switch (logLevel) {
case "debug":
case "D":
logFunction = console.debug;
break;
case "error":
case "E":
logFunction = console.error;
break;
}
logFunction(`[${GM_info.script.name}] ${text}`);
}
Al cargar el script construir las listas de celdas para cada combinación fila/columna y para las cajas.
document.querySelector("form[name=board]") // <form>
.childNodes[1] // <table>
.firstChild // <tbody>
.childNodes[1] // <tr>
.childNodes[1] // <td> .stat.halfbig
.text // string:"6:00"
table.firstChild
.childNodes[n] // <tr>
.firstChild // <td> .g0
.firstChild // <input>
let rows = table.firstChild.childNodes; // NodeList<HTMLTableRowElement>
// HTMLInputElement[]
{
"rows": Array.from(rows)
.map(row => Array.from(row.childNodes)
.map(td => td-firstChild)),
"columns":
}
// Columna "x"
Array.from(rows).map(row => row.childNodes[x].firstChild)
Array.from(Array(9).keys()).map(colNo => Array.from(rows).map(row => row.childNodes[colNo].firstChild)
Array.from(Array(9).keys()).map(x => ~~(x/3)*3).toString()
// "0,0,0,3,3,3,6,6,6"
for (x = 0; x < 3; x++) { for (y = 0; y < 3; y++) { console.log(x%3*3+y%3)}}
// "0, ... ,8"
x | y | box (top-left corner) | rows | columns |
---|---|---|---|---|
0 | 0 | 0,0 | 0,1,2 | 0,1,2 |
0 | 1 | 0,3 | 0,1,2 | 3,4,5 |
0 | 2 | 0,6 | 0,1,2 | 6,7,8 |
1 | 0 | 3,0 | 3,4,5 | 0,1,2 |
1 | 1 | 3,3 | 3,4,5 | 3,4,5 |
1 | 2 | 3,6 | 3,4,5 | 6,7,8 |
2 | 0 | 6,0 | 6,7,8 | 0,1,2 |
2 | 1 | 6,3 | 6,7,8 | 3,4,5 |
2 | 2 | 6,6 | 6,7,8 | 6,7,8 |
function clearRepeatedValues() {
// ...
let rowNo = 4;
let colNo = 5;
store.rows[rowNo]
.concat(store.columns[colNo])
.concat(getBoxCells(rowNo, colNo))
.forEach(cell => { /*...*/ });
}
boxes = Array.from(Array(9)).map(x => []);
rows.forEach(row => {
let rowNo = row.rowIndex;
Array
.from(rows[rowNo].childNodes)
.forEach(td => {
let colNo = td.cellIndex;
boxes[colNo].push(td.firstChild);
});
});
let messages = [
`value = "${valor}"`,
`length = ${valor.length}`,
`e.key = "${e.key}"`,
`columna = ${input.parentElement.cellIndex}`,
`rowSpan = "${input.parentElement.rowSpan}"`,
`rowIndex = "${input.parentElement.parentElement.rowIndex}"`
];
console.log(messages.join("\n"));
console.log(input);
console.log(input.parentElement.parentElement);
log("TODO");
let cells = [];
let boxCornerRow = ~~(rowNo / 3) * 3;
let boxCornerCol = ~~(colNo / 3) * 3;
let rows = input.parentElement.offsetParent.firstChild.childNodes;
for (let row = 0; row < 3; row++) {
for (let col = 0; col < 3; col++) {
cells.push(rows[row + boxCornerRow]
.childNodes[col + boxCornerCol]
.firstChild);
}
}
log("cells");
console.log(cells);
return;
Ver por qué se elimina un valor que no está en la misma fila, columna o caja, pero en store
aparece que sí.
Parece que calcula mal la caja de la celda: La celda [8,4] obtiene la caja 3 en lugar de la 7.
function test(cell = null) {
// TODO
log("TEST");
log("cell");
console.log(cell);
log("store");
console.log(store);
let row = cell.parentElement.parentElement;
let rowNo = row.rowIndex;
let colNo = cell.parentElement.cellIndex;
let storeCells = store.getCells(rowNo, colNo);
console.log(storeCells);
let values = [];
storeCells.forEach(cell => {
if (cell.value.length == 1) {
values.push(cell.value);
}
});
console.log(`Values: ${values.join(" ")}`);
}
31/08/2023
Probé agregando una llamada recursiva a clearClues()
pero ahora hace que no se ejecute el evento de presión de tecla.
/**
* Get object with row, column and box's index of a selected cell
*
* @param {HTMLInputElement} cell Selected cell
* @returns {{"row": number, "column": number, "box": number}}
*/
function getCellLocation(cell) {
let row = cell.parentElement.parentElement.rowIndex;
let column = cell.parentElement.cellIndex;
return {
row: row,
column: column,
box: ~~(row / 3) * 3 + ~~(column / 3)
}
}
Script para jugar Sudoku en https://websudoku.com