我正在使用javascript数独游戏,并且就输入验证而言,我很难过。当每个值输入到sudoku表中时,我有一个onkeyup触发一个函数,我试图使用该函数来确保没有值进入已存在这样的值的行/列/框。这是我第一次尝试这样的项目,所以我提前为noob代码道歉。
这是我到目前为止所得到的:
function myFunction(row) {
var x = 0;
var arraySolution = [
[x, x, 4, 5, x, 3, x, 7, x],
[x, x, x, x, x, x, 3, 1, x],
[3, 5, x, x, x, 9, 2, x, x],
[x, x, x, x, 2, x, 9, 3, 7],
[6, x, 9, x, x, x, 4, x, 8],
[4, 7, 2, x, x, x, x, x, x],
[x, x, 1, x, x, x, x, 5, 2],
[x, 4, 5, x, x, x, x, x, x],
[x, 6, x, 8, x, 1, 7, x, x]
];
var num = row.innerHTML; //get its innerHTML
num = parseInt(num, 10);
if (num < 1 || num > 9) {
row.innerHTML = "";//clear it if num is not what you expect
alert("Please Enter a Number Between 1 and 9.");
}
//Checks Row
for (var b = 0; b < 9; b++) {
if (num != arraySolution[0][b]) {
console.log("This is a valid input");
b++;
}
else if (num = arraySolution[0][b]) {
alert("this is an invalid input");
row.innerHTML = "";
}
}
}
我知道使用x作为占位符可能不是最好的计划,但此时它主要用于测试目的,因此在测试开始之前输入的值不会存在。 在我尝试检查行的地方,我显然试图在索引(0,0)处循环遍历数组 - &gt; (0,8)并检查该行中是否存在两次值。我有这个工作来检查单行,但似乎在尝试添加更多行进行检查时打破了它。
我试图检查多行的一个简短示例(我完全理解我需要做的实际数独逻辑,但我不能得到这个东西来验证)
//Checks Row
for (var b = 0; b < 9; b++) {
if ((num != arraySolution[0][b]) || (num != arraySolution[1][b])) {
console.log("This is a valid input");
b++;
}
else if ((num = arraySolution[0][b]) || (num != arraySolution[1][b])){
alert("this is an invalid input");
row.innerHTML = "";
}
}
答案 0 :(得分:1)
您可以使用此功能验证解决方案:
// returns true if arraySolution is valid, false otherwise
function valid(arraySolution) {
for (var y = 0; y < 9; ++y) {
for (var x = 0; x < 9; ++x) {
var value = arraySolution[y][x];
if (value) {
// Check the line
for (var x2 = 0; x2 < 9; ++x2) {
if (x2 != x && arraySolution[y][x2] == value) {
return false;
}
}
// Check the column
for (var y2 = 0; y2 < 9; ++y2) {
if (y2 != y && arraySolution[y2][x] == value) {
return false;
}
}
// Check the square
var startY = Math.floor(y/3)*3;
for (var y2 = startY; y2 < startY + 3; ++y2) {
var startX = Math.floor(x/3)*3;
for (x2 = startX; x2 < startX + 3; ++x2) {
if ((x2 != x || y2 != y) && arraySolution[y2][x2] == value) {
return false;
}
}
}
}
}
}
return true;
}
答案 1 :(得分:0)
<强> jsBin demo 强>
var x = 0, map = [
[x, x, 4, 5, x, 3, x, 7, x],
[x, x, x, x, x, x, 3, 1, x],
[3, 5, x, x, x, 9, 2, x, x],
[x, x, x, x, 2, x, 9, 3, 7],
[6, x, 9, x, x, x, 4, x, 8],
[4, 7, 2, x, x, x, x, x, x],
[x, x, 1, x, x, x, x, 5, 2],
[x, 4, 5, x, x, x, x, x, x],
[x, 6, x, 8, x, 1, 7, x, x]
];
// This is what the user played
// after every user input update the "user" object.
var user = {};
function numberExists() {
var err = [];
for(var r=0; r<9; r++){ // iterate map rows
if(map[r][user.cell] === user.num){ // current row, specific cell == Match?
err.push(user.num +" exists in Row"+ r +" Cell"+ user.cell);
}
if(r === user.row){ // Is this row selected by the user?
for(var c=0; c<9; c++){ // Iterate all cells of this row
if(map[r][c] === user.num){ // Match?
err.push(user.num +" exists in Row"+ r + " Cell"+ c);
}
}
}
}
if(err.length){
console.log( err.join(" and ") );
} else { // No errors
console.log( "GOOD! Row"+ user.row +" Cell"+ user.cell );
}
}
// TEST /////
user.row = 4;
user.cell = 3;
user.num = 8;
numberExists(); // Trigger search!
将触发(在上面的示例中使用r4,c3,n8)
&#34; 8存在于Row4 Cell8中,8存在于Row8 Cell3&#34;