转换从C到Javascript:数独解决方案生成器

Convert from C to Javascript: Sudoku solution generator

本文关键字:解决方案 Javascript 转换      更新时间:2023-09-26

我用C写了一个生成数独解的程序。它会随机生成数字,不断地填满谜题。如果它试图填充一个位置超过20次,它会将所有内容重置为0并重新开始。

代码在c中工作得很好,但我不能让它在JavaScript中工作。我不想做什么花哨的东西,只是把它显示在网页上。当我运行这个脚本时,它使页面崩溃(我最终得到一个'page not responding'消息)。

我非常感谢任何反馈我在哪里做错了。

C代码在JavaScript下面。

在JavaScript:

function Sudoku2() {
    /* constant */
    var LEN = 9;
    /* Track numbers in use */
    var blockNums = new Array(LEN);
    var rowNums = new Array(LEN);
    var colNums = new Array(LEN);
    /* solution by blocks (e.g. [0][0-8] is block 0) */
    var solutionBlocks = new Array(LEN);
    /* final puzzle solution (rows x cols) */
    var solution = new Array(LEN);
    /* Track where we are */
    var row, col, block, num = 0, iterations = 0;
    /* make arrays 2d */
    for (i=0; i < LEN; i++) {
        blockNums[i] = new Array(LEN);
        rowNums[i] = new Array(LEN);
        colNums[i] = new Array(LEN);
        solutionBlocks[i] = new Array(LEN);
        solution[i] = new Array(LEN);
        solution[i][9] = "<br />";
    }
    /* Generate solution by block */
    for (block = 0; block < LEN; block++) {
        for (i = 0; i < LEN; ) {
            /* Generate a random number */
            num = Math.floor(Math.random()*LEN) + 1;
            /* If iteration is > 20 for any i, the solution is unsolvable */
            if (iterations > 20) {
                /* Reset everything */
                for (j = 0; j < LEN; j++) {
                    for (k = 0; k < LEN; k++) {
                        solution[j][k] = 0;
                        solutionBlocks[j][k] = 0;
                        blockNums[j][k] = 0;
                        rowNums[j][k] = 0;
                        colNums[j][k] = 0;
                    }
                }
                i = 0;
                iterations = 0;
                block = 0;
            }
            /* is number already assigned to block? */
            if (blockNums[block][num - 1] === 0) {
                /* convert block number/position to row and col */
                col = ((block % 3) * 3) + (i % 3);
                row = (i / 3) + ((block / 3) * 3);
                /* if number not assigned to row or col */
                if (rowNums[row][num - 1] === 0 && colNums[col][num - 1] === 0) {
                    /* assign number */
                    solutionBlocks[block][i] = num;
                    solution[row][col] = num;
                    blockNums[block][num - 1] = 1;
                    rowNums[row][num - 1] = 1;
                    colNums[col][num - 1] = 1;
                    iterations = 0;
                    i++;
                /* otherwise, track # of loops */
                } else { iterations++; }
            }
        }
    }

    /* Generate string output */
    var str = solution.toString();
    var screenOutput = document.getElementById("sudoku2");
    screenOutput.innerHTML=str;
}
在C:

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define LEN 9
int main ()
{
    /* Track numbers in use */
    int blockNums[LEN][LEN] = {0};
    int rowNums[LEN][LEN] = {0};
    int colNums[LEN][LEN] = {0};
    /* Track where we are */
    int i = 0, row, col, block, num = 0, iterations = 0;
    /* solution by blocks (e.g. [0][0-8] is block 0) */
    int solutionBlocks[LEN][LEN] = {0};
    /* final puzzle solution (rows x cols) */
    int solution[LEN][LEN] = {0};
    /* initialize random seed */
    srand ( time(NULL) );
    /* Generate solution by block */
    for (block = 0; block < LEN; block++) {
        for (i = 0; i < LEN; ) {
            /* Generate a random number */
            num = (rand() % LEN) + 1;
            /* If iteration is > 20 for any i, the solution is unsolvable */
            if (iterations > 20) {
                int j, k;
                /* Reset everything */
                for (j = 0; j < LEN; j++) {
                    for (k = 0; k < LEN; k++) {
                        solution[j][k] = 0;
                        solutionBlocks[j][k] = 0;
                        blockNums[j][k] = 0;
                        rowNums[j][k] = 0;
                        colNums[j][k] = 0;
                    }
                }
                i = 0;
                iterations = 0;
                block = 0;
            }
            /* is number already assigned to block? */
            switch (blockNums[block][num - 1]) {
                case 0: 
                        /* convert block number/position to row and col */
                        col = ((block % 3) * 3) + (i % 3);
                        row = (i / 3) + ((block / 3) * 3);
                        /* if number not assigned to row or col */
                        if (rowNums[row][num - 1] == 0 && colNums[col][num - 1] == 0) {
                            /* assign number */
                            solutionBlocks[block][i] = num;
                            solution[row][col] = num;
                            blockNums[block][num - 1] = 1;
                            rowNums[row][num - 1] = 1;
                            colNums[col][num - 1] = 1;
                            iterations = 0;
                            i++;
                        /* otherwise, track # of loops */
                        } else { iterations++; }
                        break;
            }
        }
    }
    /* Print solution */
    for(row = 0; row < LEN; row++) {
        printf("   ");
        for(col = 0; col < LEN; col++) {
            printf("%d ", solution[row][col]);
            if (((col + 1) % 3 == 0) && (col != 8))
                printf(" ");
        }
        printf("'n");
        if (((row + 1) % 3 == 0) && (row != 8))
            printf("'n");
    }
    return 0;
}

如果需要,下面是我使用的HTML:

<p id="sudoku2">Click it.</p>
<button type="button" onclick="Sudoku2()">it.</button>

这是JavaScript中不存在的计算错误。

row = (i / 3) + ((block / 3) * 3);

我把它改成:

row = Math.floor((i / 3)) + (Math.floor(block / 3) * 3);

因为我的变量不是明确的整数,在确定行号时的除法会丢掉一些计算。我还添加了一个for循环,其中我使我的数字跟踪数组2D初始化为0。