LeonMarchetti / tampermonkey-scripts

Scripts for the Tampermonkey browser extension
1 stars 0 forks source link

Web Sudoku #15

Open LeonMarchetti opened 5 months ago

LeonMarchetti commented 5 months ago

Script para jugar Sudoku en https://websudoku.com

LeonMarchetti commented 5 months ago
LeonMarchetti commented 5 months ago

Funció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}`);
}
LeonMarchetti commented 5 months ago

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
LeonMarchetti commented 5 months ago
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);
        });
});
LeonMarchetti commented 5 months ago
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;
LeonMarchetti commented 5 months ago

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(" ")}`);
}
LeonMarchetti commented 5 months ago

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)
    }
}