Cannot assign new value to array element - javascript -
i relatively new programming , new javascript/html/css (read on few tutorials) , having weird problem first script. (trying make sudoku solver) array puzzle contains: "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 ] = . , on. puzzle represents unsolved sudoku puzzle digits 1-9 rep solved cells , . unsolved cells.
below code 1 of functions , there 4 alert messages in it. problem lies near message 4 puzzle[c+(9*r)] = nums[0];
shows puzzle[c+9*r]
still contains . , not value of num[0] (in first case 6). declared outside of functions:
var puzzle = new array(); var options = new array(); var nums = new array(); var r = 0; var c = 0; var num=0;
function checknumber works fine *
message 1: "puz conatians ."
- message 2: "nums length 1"
- message 3: "nums[0] 6"
- message 4: = "puzzle ." should "puzzle 6"
any appreciated! please let me know if need further clarify =]
function makeoptionsarray() { // loop through 9x9 grid of sudoku r = row c = column (r = 0; r < 9; r++) { (c = 0; c < 9; c++) { nums.length = 0; // reset array each new cell if (puzzle[c + (9 * r)] == '.') { // if unknown cell alert("puz contains " + puzzle[c + 9 * r]); // message 1 (num = 1; num <= 9; num++) { if (checknumber(num, r, c)) { // check digits 1-9 nums.push(num); // keep track of possible values //for cell based on other known values on board } } options[c + (9 * r)] = nums; // used part & contain possible values unsolved cell alert("nums length " + nums.length); // message 2 if (nums.length == 1) { // if 1 possible number add sudoku alert("nums[0] " + nums[0]); // message 3 puzzle[c + (9 * r)] = nums[0]; document.getelementbyid(c + 9 * r).innerhtml = nums[0]; alert("puzzle " + puzzle[c + 9 * r]); //message 4 makeoptionsarray(); // call function again updated puzzle (one less unknown cell) } if (nums.length == 0) { //if no possible options no solutions puzzle alert("no solutions"); return true; } else { continue; } } } } }
Comments
Post a Comment