Automatically solve online Sudoku in the same way as you fish

background

After discovering that they were bossed by the CPU, basically what everyone wanted to do was to catch fish. People like me with no skills could not catch fish and play MOBA games, so they could only choose small games like Sudoku that did not have very urgent time requirements. game. However, sometimes it takes me a long time to find out that it was wrong from the beginning, which makes me very distressed. It seems that it is necessary to fish again on this fishing operation.

platform

The online Sudoku web game discovered by accident [Online Sudoku (sudoku-cn.com)]

plan

Install the Grease Monkey plug-in in the browser and create a new Grease Monkey script with the following content:

// ==UserScript==
// @name         右键自动解答数独
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  try to take over the world!
// @author       You
// @match        https://sudoku-cn.com/
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    let board = [];
    let over = false;

    let hang=[];
    let lie=[];
    let mid=[];


    function getQuestion() {
        hang=[];
        lie=[];
        mid=[];
        for(let i=0;i<9;i++){
            hang.push([false,false,false,false,false,false,false,false,false]);
            lie.push([false,false,false,false,false,false,false,false,false]);
            mid.push([false,false,false,false,false,false,false,false,false]);
        }

        board=[];
        let str="";
        for (let i = 0; i < 9; i++) {

            board.push([]);
            for (let j = 0; j < 9; j++) {
                let div = document.querySelector("#vc_" + j + "_" + i);
                if (div.innerHTML.trim().length != 0&&div.className=="r1") {
                    board[i].push(div.innerHTML.trim());
                    str+=div.innerHTML.trim();
                    hang[i][board[i][j].charCodeAt(0)-49]=true;
                    lie[j][board[i][j].charCodeAt(0)-49]=true;
                    mid[Math.floor(i/3)*3+Math.floor(j/3)][board[i][j].charCodeAt(0)-49]=true;
                } else {
                    str+="_";
                    board[i].push("_");
                }
            }
        }
/*         console.log("源:",str); */
    }

    function dfs(i,j) {
        if (over)
        {
            return;
        }
        if (i >= 9 || j >= 9)
        {
            over = true;
            return;
        }

        if (board[i][j] != '_')
        {
            if (j == 9 - 1)
            {
                dfs(i + 1, 0);
            }
            else
            {
                dfs(i, j + 1);
            }
            return;
        }

        for(let number = 1; number <= 9; number++)
        {
            if (hang[i][number - 1] || lie[j][ number - 1] || mid[Math.floor(i / 3) * 3 +Math.floor( j / 3)][ number - 1])
            {
                continue;
            }
            else
            {
                board[i][j] =String.fromCharCode(number + 48);
                hang[i][number - 1] = true;
                lie[j][ number - 1] = true;
                mid[Math.floor(i / 3) * 3 + Math.floor(j / 3)][ number - 1] = true;

                if (j == 9 - 1)
                {
                    dfs(i + 1, 0);
                }
                else
                {
                    dfs(i, j + 1);
                }

                if (over)
                {
                    return;
                }
                else
                {
                    board[i][j] = '_';
                    hang[i][ number - 1] = false;
                    lie[j][ number - 1] = false;
                    mid[Math.floor(i / 3) * 3 +Math.floor( j / 3)][ number - 1] = false;
                }
            }
        }
    }

    function setAnswer() {
        let index = 0;
        for (let i = 0; i < 9; i++) {
            for (let j = 0; j < 9; j++) {
                let div = document.querySelector("#vc_" + j + "_" + i);
                div.innerHTML = board[i][j];
            }
        }
    }

    document.oncontextmenu = (e) => {
        e.preventDefault();
        over = false;
        getQuestion();
        dfs(0,0);
        setAnswer();
    }
})();

As shown in the code, I canceled the default right-click event of the mouse and replaced it with automatically solving the problem when right-clicking and filling in the answer.

test

Directly advanced + difficult questions

Click the right mouse button to solve directly

 

If it's all green, then there's no problem.

At this point, the goal of fishing and fishing has been achieved.

Guess you like

Origin blog.csdn.net/qq_36694133/article/details/133030910