从C转换为Javascript:Sudoku解决方案生成器

时间:2014-05-08 20:29:38

标签: javascript c sudoku

我在C中编写了一个生成数独拼图解决方案的程序。它随机生成数字,随着时间的推移填充拼图。如果它试图将一个点填充超过20次,它会将所有内容重置为0并重新开始。

代码在C中运行得非常好。但我无法在JavaScript中使用它。我不想尝试任何与它有关的事情,只需将其显示在网页上即可。当我运行脚本时,它会崩溃页面(我最终得到的页面没有响应'消息)。

我非常感谢有关我在哪里出错的任何反馈。

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>

1 个答案:

答案 0 :(得分:1)

这是JavaScript中的计算错误,但在C中并不存在。

我有:

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

我把它改为:

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

因为我的变量不是明确的整数,所以确定行号的除法是放弃了一些计算。我还添加了一个for循环,我将数字跟踪数组2D以将所有内容初始化为0。