[英]Solving a 4x4 (simplified one) sudoku in javascript
I have a problem where I have to code an automatic solution to a given sudoku which is 4x4 which means the numbers go only as far 1 2 3 4. It's a simplified version of a sudoku since I am still new to programming. 我有一个问题,我必须为给定的数独输入4x4的自动解决方案编写代码,这意味着数字只能达到1 2 34。这是数独的简化版本,因为我还是编程新手。
I have a given template with random sudoku generated from it and I have to write a code to solve the sudoku automatically. 我有一个给定的模板,该模板具有从中生成的随机数独的功能,我必须编写代码以自动解决数独的问题。
Here is what I have in the begining as sudoku that i have to solve 这是我一开始必须解决的数独问题
sudoku[6] = [[" ","2"," "," "],
[" "," ","2"," "],
[" "," "," ","3"],
["4"," "," "," "]
My idea was to insert "1234" into empty " " and then remove the numbers from "1234" when one of the numbers is already present in the column, row and quadrant. 我的想法是在空的“”中插入“ 1234”,然后在列,行和象限中已经存在一个数字时将其从“ 1234”中删除。 So what I wanted to do is with the use of loops go through all the positions in the tables and the moment I find for example "1" alone i will remove the 1 from "1234". 因此,我想做的就是使用循环遍历表中的所有位置,并且当我发现例如“ 1”的那一刻,我就会从“ 1234”中删除1。
Here is the beggning of my code, it appears it doesnt work the moment I reach if, can you guys please tell me what I am doing wrong or why isn't it working when i get to my If. 这是我的代码的开头,看来我到达时不起作用,请告诉我我做错了什么,或者当我进入If时为什么不起作用。 Thank you in advance. 先感谢您。
var sudoku = sudoku[6];
// function to put "1234" into empty space ""
var concatenate = function (s)
{
for (i=0; i<s.length; i++)
for (j=0; j<s.length; j++)
if (sudoku[i][j] === " ")
sudoku[i][j] = "1234";
};
concatenate(sudoku);
// function to solve the sudoku automatically.
var solve = function (t)
{
for (i = 0; i<t.length; i++)
for (j=0; j<t.length; j++)
for (k=j; k<(4+j); k++)
if (sudoku[i][j].length === 1) // this is where it seems to bug, in this if im trying to find the position where we only have one number and not "1234"
var s = sudoku[i][j];
if (sudoku[i][k-j] !== ("1" || "2" || "3" || "4")) // here im finding the position of all position in the sudoku where ive got "1234" so i can remove the number found in the previous if.
{
var index = sudoku[i][k-j].indexOf(s);
var string_new = sudoku[i][k-j].substring(0,index) + sudoku[i][k-j].substring(index+1, 4);
sudoku[i][k-j] = string_new;
}
};
There are known algorithms to solve the sudoku problem, you should take a look. 有解决数独问题的已知算法,您应该看看。
For such a small sudoku like yours, you may choose to implement not taking in account the computational time. 对于像您这样的小数独,您可以选择实现时不考虑计算时间。 (choose the simplest to implement) (选择最简单的实现方法)
For more infos: Sudoku solving algorithms 有关更多信息: Sudoku解决算法
Reguarding your code, the idea (on paper) is not bad, but I really can't understand what you intended to do with that. 保护您的代码,这个想法(在纸上)还不错,但是我真的不明白您打算怎么做。 For example: 例如:
if (sudoku[i][k-j] !== ("1" || "2" || "3" || "4"))
This line has no sense. 这条线没有意义。 The expression ("1" || "2" || "3" || "4") will always evaluates "1". 表达式(“ 1” ||“ 2” ||“ 3” ||“ 4”)将始终为“ 1”。 So you are writing: 所以你在写:
if (sudoku[i][k-j] !== "1")
Moreover sudoku[i][kj] is a string containing "1234" (or part of it), so you should use indexOf to check the presence of a character. 而且sudoku [i] [kj]是一个包含“ 1234”(或其一部分)的字符串,因此您应该使用indexOf检查字符的存在。
And what is the use of the internal for? 内部的用途是什么?
for (k=j; k<(4+j); k++)
Why are you iterating from j to 4+j? 为什么要从j迭代到4 + j? You're always using kj to access the variable (sudoku[i][kj]), that is always between jj and 4+jj. 您始终使用kj来访问变量(sudoku [i] [kj]),该变量始终在jj和4 + jj之间。 So why don't: 那么为什么不:
for (k=0; k<4; k++)
There are more other logic errors in you code... If you want to implement that idea, you should take your time to think what you are writing, or (better) use a known algorithm. 您的代码中还有更多其他逻辑错误...如果您想实现该想法,则应花些时间思考所写的内容,或者(最好)使用一种已知的算法。
You should replace this one: 您应该替换以下一个:
if (sudoku[i][k-j] !== ("1" || "2" || "3" || "4"))
to something like this: 像这样:
if ( sudoku[i][k-j] !== "1" || sudoku[i][k-j] !== "2" || ...
or you can consider using a switch
statement also, but I'm not sure if it's gonna solve the bug that you're struggling with that or not ... 或者您也可以考虑使用switch
语句,但是我不确定它是否会解决您为此而苦苦挣扎的错误...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.