数独验证器未返回正确的布尔值
我正在编写一个数独验证器,但在最后遇到了问题。我对编码很陌生,所以请耐心等待。函数 sudokuIsValid 应该接受一个谜题(一个由 9 个数组组成的数组)并检查每个列、行和 3x3 网格以查看它是否有效。函数includes1To9 可以单独工作,getColumn 可以工作,getRow 可以工作,getSection 也可以工作。所以根据我的理解,这只是最后一个函数sudokuIsValid。我不确定它出了什么问题,可能是一些小问题。我只是不认识其他编码的人,所以我没有太多人可以问。我知道这段代码可能会写得更好。我还恳请您不要试图向我介绍新的操作员或类似的事情。您可以解释一下为什么这不起作用吗?我不明白为什么 sudokuIsValid 没有按应有的方式返回。感谢您的帮助。这是我的代码:
let puzzle = [[ 8,9,5, 7,4,2, 1,3,6 ],
[ 2,7,1, 9,6,3, 4,8,5 ],
[ 4,6,3, 5,8,1, 7,9,2 ],
[ 9,3,4, 6,1,7, 2,5,8 ],
[ 5,1,7, 2,3,8, 9,6,4 ],
[ 6,8,2, 4,5,9, 3,7,1 ],
[ 1,5,9, 8,7,4, 6,2,3 ],
[ 7,4,6, 3,2,5, 8,1,9 ],
[ 3,2,8, 1,9,6, 5,4,7 ]];
//puzzle 2
let puzzleTwo = [[ 8,9,5,7,4,2,1,3,6 ],
[ 8,7,1,9,6,3,4,8,5 ],
[ 4,6,3,5,8,1,7,9,2 ],
[ 9,3,4,6,1,7,2,5,8 ],
[ 5,1,7,2,3,8,9,6,4 ],
[ 6,8,2,4,5,9,3,7,1 ],
[ 1,5,9,8,7,4,6,2,3 ],
[ 7,4,6,3,2,5,8,1,9 ],
[ 3,2,8,1,9,6,5,4,7 ]];
//DO NOT EDIT ABOVE
function getRow(puzzle, row) {
return puzzle[row]
};
function getColumn(puzzle, col) {
let column = []
for (let i = 0; i < puzzle.length; i++){
column.push(puzzle[i][col])
}
return column
};
function getSection(puzzle, x, y) {
x *= 3
y *= 3
let cell = []
for (let i = y; i < y + 3; i++){
for (let j=x;j< x + 3; j++){
cell.push(puzzle[i][j])
}
}
return cell
};
function includes1To9(arr) {
for (i = 0; i < arr.length; i++){
for (j = 0; j < arr.length; j++){
if (j != i){
if (arr[i] === arr[j]){
return false
}
}
}
}
return true
};
function sudokuIsValid(puzzle) {
let valid = []
for (let i=0;i<9;i++) {
valid.push(getRow(puzzle, i))
valid.push(getColumn(puzzle,i))
}
for (let i=0;i<3;i++){
for (let j=0; j<3; j++){
valid.push(getSection(puzzle, i, j))
}
}
for (let i=0; i < valid.length; i++) {
if (includes1To9(valid[i]) === false){
return false
} else {
return true
}
}
};
console.log(sudokuIsValid(puzzleTwo)) // returns true. But should return false because the first column has two 8's.
console.log(includes1To9([8,8,4,9,5,6,1,7,3])) // returns false, works as it should. This is also the first column of puzzleTwo which should make sudokuIsValid return false.
I'm writing a sudoku validator and I've run into a problem at the very end of it. I'm very new to coding, so please be patient with me. The function sudokuIsValid is supposed to take a puzzle (an array of 9 arrays) and check each column, row, and 3x3 grid to see if it is valid. The function includes1To9 works on its own, getColumn works, getRow works, and getSection works. So from my understanding, it's just the last function sudokuIsValid. I'm not sure what is wrong with it, probably something minute. I just don't know anyone else who codes, so I don't have many people to ask. I understand that this code could likely be written better. I also kindly ask you do not try to introduce me to new operators or things like that.. May you please just explain why this does not work? I don't understand why sudokuIsValid is not returning the way it should as it is. Thanks for your help. Here's my code:
let puzzle = [[ 8,9,5, 7,4,2, 1,3,6 ],
[ 2,7,1, 9,6,3, 4,8,5 ],
[ 4,6,3, 5,8,1, 7,9,2 ],
[ 9,3,4, 6,1,7, 2,5,8 ],
[ 5,1,7, 2,3,8, 9,6,4 ],
[ 6,8,2, 4,5,9, 3,7,1 ],
[ 1,5,9, 8,7,4, 6,2,3 ],
[ 7,4,6, 3,2,5, 8,1,9 ],
[ 3,2,8, 1,9,6, 5,4,7 ]];
//puzzle 2
let puzzleTwo = [[ 8,9,5,7,4,2,1,3,6 ],
[ 8,7,1,9,6,3,4,8,5 ],
[ 4,6,3,5,8,1,7,9,2 ],
[ 9,3,4,6,1,7,2,5,8 ],
[ 5,1,7,2,3,8,9,6,4 ],
[ 6,8,2,4,5,9,3,7,1 ],
[ 1,5,9,8,7,4,6,2,3 ],
[ 7,4,6,3,2,5,8,1,9 ],
[ 3,2,8,1,9,6,5,4,7 ]];
//DO NOT EDIT ABOVE
function getRow(puzzle, row) {
return puzzle[row]
};
function getColumn(puzzle, col) {
let column = []
for (let i = 0; i < puzzle.length; i++){
column.push(puzzle[i][col])
}
return column
};
function getSection(puzzle, x, y) {
x *= 3
y *= 3
let cell = []
for (let i = y; i < y + 3; i++){
for (let j=x;j< x + 3; j++){
cell.push(puzzle[i][j])
}
}
return cell
};
function includes1To9(arr) {
for (i = 0; i < arr.length; i++){
for (j = 0; j < arr.length; j++){
if (j != i){
if (arr[i] === arr[j]){
return false
}
}
}
}
return true
};
function sudokuIsValid(puzzle) {
let valid = []
for (let i=0;i<9;i++) {
valid.push(getRow(puzzle, i))
valid.push(getColumn(puzzle,i))
}
for (let i=0;i<3;i++){
for (let j=0; j<3; j++){
valid.push(getSection(puzzle, i, j))
}
}
for (let i=0; i < valid.length; i++) {
if (includes1To9(valid[i]) === false){
return false
} else {
return true
}
}
};
console.log(sudokuIsValid(puzzleTwo)) // returns true. But should return false because the first column has two 8's.
console.log(includes1To9([8,8,4,9,5,6,1,7,3])) // returns false, works as it should. This is also the first column of puzzleTwo which should make sudokuIsValid return false.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是由于您的代码中存在一个小逻辑错误,
sudokuIsValid
中的最终 for 循环是错误的具体来说,
每当单行或单列时,您返回的
true
部分的所有数字都会将最后一个
for
更改为:并且它对我有用
This is due to a small logic error in your code, the final for loop in the
sudokuIsValid
is wrongspecificaly
Your returning
true
whenever a single row or column, section has all the numberschange the last
for
to:and it worked for me