Re: sudoku game in actionscript

Posted 18 September 2008 - 05:11 AM

Hi all,

I've just stumbled across this page while searching for something else and it reminded me of a sudoku flash program I did a while ago. It isn't finished. It's just that I need help with thinking about how to fill an empty 9x9 sudoku grid correctly. My prog gets stuck after about 20 numbers filled. Basically, I think that a backtracking when stuck algorithm might work, but I haven't given it any time since I left it ages ago. What might jolt me is some idea of the 'proper' way to fill in the grid. My .fla and .swf files aren't allowed to be uploaded, so I've just copied over my AS below. Any pointers is greatly appreciated. Cheers.

_global.resetBHV = function() {
// reset arrays Block, Horizontal and Vertical
// means that for each of those arrays,
// it is still OK to choose values 1-9
// if that array number's position contains a 1,
// ok to choose that number, eitherwise, not ok

_global.findQuanOfPossibleValues = function(ThisArray:Array, index:Number):Number {
// adds up all the 1's in ThisArray[index][] to find the number of possible 1-9's to choose from
var QuanOfPossibleValues:Number = 0;

_global.updateAnswerArray2 = function(AnswerArray:Array, chosenSmallBox:Number):Array {
// updates AnswerArray to reflect all the cumulative total possible 1-9 values it can have based
// on all the indirect Horizontal, Vertical and Block small boxes associated with chosenSmallBox

if (i == 0) {
V_Start = temp1_Num; // start number for same column
V_End = temp2_Num; // end number for same column
} else {
H_Start = temp1_Num; // start number for same row
H_End = temp2_Num; // end number for same row
} // if
*/

After that point you just choose a certain number of times to change out numbers, choose randomly between 1 and 9 and swap all of the numbers on the screen.

Say you got 1 and 4, all the 1s would be changed to a holder like 0 and all the 4s to 1s then all the 0s to 4s (this makes sure that you don't get more of the single number than you are supposed to have.

While this is obviously not the only way to generate a soduko, it does take a lot of the brute force out of the equation; all you have to do is randomize it. (granted after a while people will catch on to the pattern used above).