gpt4 book ai didi

javascript - 从 C 转换为 Javascript : Sudoku solution generator

转载 作者:太空宇宙 更新时间:2023-11-04 03:46:56 25 4
gpt4 key购买 nike

我用 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>

最佳答案

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

我有:

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

我把它改成:

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

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

关于javascript - 从 C 转换为 Javascript : Sudoku solution generator,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23551907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com