chibineku Posted May 6, 2010 Share Posted May 6, 2010 I'm trying to write a 6x6 mini sudoku solver, and I wouldn't mind some help with my algorithm.I'll show you my code along with my thinking, to see if you can clear up my messy ideas. function solve() { //gather all the numbers already input var rows = new Array(); //for each row $('#sudoku_frame tr').not('.ignore').each(function(r) { rows[r+1] = new Array(); //for each column in each row $(this).find('td input').each(function(c) { rows[r+1][c+1] = $(this).val(); }); }); At this point, rows is a multidimensional array representing the whole grid: 1 (row) => 1 (col) => 6 2 (col) => 2 3 (col) => '' 4 (col) => '' 5 (col) => '' 6 (col) => '' etc. etc. //define possibles function Sudoku(n) { this.possible = new Array(); for(i=1;i<=n;i++) { this.possible[i] = i; } } //for each row for(var i in rows) { toTry = new Sudoku(6); //for each field for(var k in rows[i]) { //if the field isn't empty if(rows[i][k] != '') { //remove the value from the possible array for(x in toTry.possible) { if(toTry.possible[x] == rows[i][k]) { toTry.possible[x] = ''; } //end if {} } //end removing value } //end if the field isn't empty } //end for each field } //end for each row For each row, I need to check what values are already input and scratch them off a list of possibles, which I can do by creating a new Sudoku object which has, as it's 'possible' property, an array of the possible numbers (where n is the size of the grid). So, if the first number on the first row is known to be 2, the possibles array of the first toTry object looks like: 1 => 1 2 => '' 3 => 3 4 => 4 5 => 5 6 => 6I intend then to check each full column against the possibles list, and scratch off any more. If I end up with one left in the possibles list, I can go ahead and add it to the rows array.I don't know if it's going to be possible to code this using only human solving heuristics - it might require some sort of mathematical approach.What do you guys think - any tips?You can see it, for what it's worth, here. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.