Do you like my tutorials?

Then consider supporting me on Ko-fi

Talking about Sudoku game, Actionscript 3, Flash and Game development.

At the beginning of this month I published the Sudoku creator/solver with PHP.

Now, after a little hassle due to a strange way AS3 manages arrays (in my opinion), I am ready to post the AS3 version of the solver.

This is what you will get

Results may vary if you don’t have Lucida Console font installed in your system, anyway the complete solved sudoku is in the sudoku array, so feel free to modify the code in order to display the sudoku in a more eye catching way.

Here it is the script:

package {
	import flash.display.Sprite;
	import flash.text.*;
	public class sudoku extends Sprite {
		public function sudoku() {
			var sudoku = new Array(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0);
			solve(sudoku);
		}
		function return_row(cell) {
			return Math.floor(cell / 9);
		}
		function return_col(cell) {
			return cell % 9;
		}
		function return_block(cell) {
			return Math.floor(return_row(cell) / 3) * 3 + Math.floor(return_col(cell) / 3);
		}
		function is_possible_row(number,row,sudoku) {
			var possible = true;
			for (var i=0; i<=8; i++) {
				if (sudoku[row*9+i] == number) {
					possible = false;
					break;
				}
			}
			return possible;
		}
		function is_possible_col(number,col,sudoku) {
			var possible = true;
			for (var i=0; i<=8; i++) {
				if (sudoku[col+9*i] == number) {
					possible = false;
					break;
				}
			}
			return possible;
		}
		function is_possible_block(number,block,sudoku) {
			var possible = true;
			for (var i=0; i<=8; i++) {
				if (sudoku[Math.floor(block/3)*27+i%3+9*Math.floor(i/3)+3*(block%3)] == number) {
					possible = false;
					break;
				}
			}
			return possible;
		}
		function is_possible_number(cell,number,sudoku) {
			var row = return_row(cell);
			var col = return_col(cell);
			var block = return_block(cell);
			return is_possible_row(number,row,sudoku) && is_possible_col(number,col,sudoku) && is_possible_block(number,block,sudoku);
		}
		function is_correct_row(row,sudoku) {
			var right_sequence = new Array(1,2,3,4,5,6,7,8,9);
			var row_temp= new Array();
			for (var i=0; i<=8; i++) {
				row_temp[i] = sudoku[row*9+i];
			}
			row_temp.sort();
			return row_temp.join() == right_sequence.join();
		}
		function is_correct_col(col,sudoku) {
			var right_sequence = new Array(1,2,3,4,5,6,7,8,9);
			var col_temp= new Array();
			for (var i=0; i<=8; i++) {
				col_temp[i] = sudoku[col+i*9];
			}
			col_temp.sort();
			return col_temp.join() == right_sequence.join();
		}
		function is_correct_block(block,sudoku) {
			var right_sequence = new Array(1,2,3,4,5,6,7,8,9);
			var block_temp= new Array();
			for (var i=0; i<=8; i++) {
				block_temp[i] = sudoku[Math.floor(block/3)*27+i%3+9*Math.floor(i/3)+3*(block%3)];
			}
			block_temp.sort();
			return block_temp.join() == right_sequence.join();
		}
		function is_solved_sudoku(sudoku) {
			for (var i=0; i<=8; i++) {
				if (!is_correct_block(i,sudoku) || !is_correct_row(i,sudoku) || !is_correct_col(i,sudoku)) {
					return false;
				}
			}
			return true;
		}
		function determine_possible_values(cell,sudoku) {
			var possible = new Array();
			for (var i=1; i<=9; i++) {
				if (is_possible_number(cell,i,sudoku)) {
					possible.unshift(i);
				}
			}
			return possible;
		}
		function determine_random_possible_value(possible,cell) {
			var random_picked = Math.floor(Math.random() * possible[cell].length);
			return possible[cell][random_picked];
		}
		function scan_sudoku_for_unique(sudoku) {
			var possible = new Array();
			for (var i=0; i<=80; i++) {
				if (sudoku[i] == 0) {
					possible[i] = new Array();
					possible[i] = determine_possible_values(i,sudoku);
					if (possible[i].length==0) {
						return false;
					}
				}
			}
			return possible;
		}
		function remove_attempt(attempt_array,number) {
			var new_array = new Array();
			for (var i=0; i0)) {
						mai = possible[i].length;
						min_choices = i;
					}
				}
			}
			return min_choices;
		}
		function show_sudoku(sudoku,i) {
			var sudokutext:TextField = new TextField();
			var format:TextFormat = new TextFormat();
			var solved = "\n\nSolved in "+i+" steps";
			format.font = "Lucida Console";
			sudokutext.width = 250;
			sudokutext.height = 250;
			sudokutext.defaultTextFormat = format;
			sudokutext.x = 25;
			sudokutext.y = 25;
			addChild(sudokutext);
			for (var i=0; i<=8; i++) {
				for (var j=0; j<=8; j++) {
					sudokutext.appendText(" ");
					sudokutext.appendText(sudoku[i*9+j]);
					sudokutext.appendText(" ");
					if (j!=8) {
						sudokutext.appendText("|");
					}
				}
				if (i!=8) {
					sudokutext.appendText("\n---+---+---+---+---+---+---+---+---\n");
				}
			}
			sudokutext.appendText(solved);
		}
		function solve(sudoku) {
			var saved = new Array();
			var saved_sud = new Array();
			var i=0;
			var next_move;
			var what_to_try;
			var attempt;
			while (!is_solved_sudoku(sudoku)) {
				i+=1;
				next_move = scan_sudoku_for_unique(sudoku);
				if (next_move == false) {
					next_move = saved.pop();
					sudoku = saved_sud.pop();
				}
				what_to_try = next_random(next_move);
				attempt = determine_random_possible_value(next_move,what_to_try);
				if (next_move[what_to_try].length>1) {
					next_move[what_to_try] = remove_attempt(next_move[what_to_try],attempt);
					saved.push(next_move.slice());
					saved_sud.push(sudoku.slice());
				}
				sudoku[what_to_try] = attempt;
			}
			show_sudoku(sudoku,i);
		}
	}
}

Download the source code and let’s make soduku popular once again.

Never miss an update! Subscribe, and I will bother you by email only when a new game or full source code comes out.