我对编程比较陌生,我是javascript / html / css的新手(阅读一些教程),我的第一个脚本遇到了一个奇怪的问题。 (试图制作数独求解器)
数组拼图包含:“3.542.81.4879.15.6.29.5637485.793.416132.8957.74.6528.2413.9.655.867.192.965124.8”
其中puzzle [0] = 3 puzzle [1] =。等等。
puzzle代表一个未解决的数独谜题,数字1-9 rep解决了细胞和。未解决的细胞
下面是我的一个函数的代码,其中有4条警报消息。我的问题出在MESSAGE 4附近,puzzle[c+(9*r)] = nums[0];
显示puzzle[c+9*r]
仍然包含。而不是num [0]的值(在第一种情况下为6)。
在函数之外声明:
var puzzle = new Array();
var options = new Array();
var nums = new Array();
var r = 0;
var c = 0;
var num=0;
函数checkNumber工作正常*
消息1:“puz conatians。”
任何帮助将不胜感激!如果我需要进一步澄清某些事情,请告诉我=]
function makeOptionsArray() {
// loop through 9x9 grid of sudoku r = row c = column
for (r = 0; r < 9; r++) {
for (c = 0; c < 9; c++) {
nums.length = 0; // reset array for each new cell
if (puzzle[c + (9 * r)] == '.') { // if unknown cell
alert("puz contains " + puzzle[c + 9 * r]); // MESSAGE 1
for (num = 1; num <= 9; num++) {
if (checkNumber(num, r, c)) { // check digits 1-9
nums.push(num); // keep track of possible values
//for this cell based on other known values on the board
}
}
options[c + (9 * r)] = nums; // used for another part & will contain all possible values for an unsolved cell
alert("nums length is " + nums.length); // MESSAGE 2
if (nums.length == 1) { // if only 1 possible number add to sudoku
alert("nums[0] is " + nums[0]); // MESSAGE 3
puzzle[c + (9 * r)] = nums[0];
document.getElementById(c + 9 * r).innerHTML = nums[0];
alert("puzzle is " + puzzle[c + 9 * r]); //MESSAGE 4
makeOptionsArray(); // call function again with updated puzzle (one less unknown cell)
}
if (nums.length == 0) { //if no possible options then no solutions for puzzle
alert("No solutions");
return true;
} else {
continue;
}
}
}
}
}