Sudoku creator/solver with AS3

Read all posts about "" game

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.

Get the most popular Phaser 3 book

Through 202 pages, 32 source code examples and an Android Studio project you will learn how to build cross platform HTML5 games and create a complete game along the way.

Get the book

215 GAME PROTOTYPES EXPLAINED WITH SOURCE CODE
// 1+2=3
// 100 rounds
// 10000000
// 2 Cars
// 2048
// A Blocky Christmas
// A Jumping Block
// A Life of Logic
// Angry Birds
// Angry Birds Space
// Artillery
// Astro-PANIC!
// Avoider
// Back to Square One
// Ball Game
// Ball vs Ball
// Ball: Revamped
// Balloon Invasion
// BallPusher
// Ballz
// Bar Balance
// Bejeweled
// Biggification
// Block it
// Blockage
// Bloons
// Boids
// Bombuzal
// Boom Dots
// Bouncing Ball
// Bouncing Ball 2
// Bouncy Light
// BoxHead
// Breakout
// Bricks
// Bubble Chaos
// Bubbles 2
// Card Game
// Castle Ramble
// Chronotron
// Circle Chain
// Circle Path
// Circle Race
// Circular endless runner
// Cirplosion
// CLOCKS - The Game
// Color Hit
// Color Jump
// ColorFill
// Columns
// Concentration
// Crossy Road
// Crush the Castle
// Cube Jump
// CubesOut
// Dash N Blast
// Dashy Panda
// Deflection
// Diamond Digger Saga
// Don't touch the spikes
// Dots
// Down The Mountain
// Drag and Match
// Draw Game
// Drop Wizard
// DROP'd
// Dudeski
// Dungeon Raid
// Educational Game
// Elasticity
// Endless Runner
// Erase Box
// Eskiv
// Farm Heroes Saga
// Filler
// Flappy Bird
// Fling
// Flipping Legend
// Floaty Light
// Fuse Ballz
// GearTaker
// Gem Sweeper
// Globe
// Goat Rider
// Gold Miner
// Grindstone
// GuessNext
// Helicopter
// Hero Emblems
// Hero Slide
// Hexagonal Tiles
// HookPod
// Hop Hop Hop Underwater
// Horizontal Endless Runner
// Hundreds
// Hungry Hero
// Hurry it's Christmas
// InkTd
// Iromeku
// Jet Set Willy
// Jigsaw Game
// Knife Hit
// Knightfall
// Legends of Runeterra
// Lep's World
// Line Rider
// Lumines
// Magick
// MagOrMin
// Mass Attack
// Math Game
// Maze
// Meeblings
// Memdot
// Metro Siberia Underground
// Mike Dangers
// Mikey Hooks
// Nano War
// Nodes
// o:anquan
// One Button Game
// One Tap RPG
// Ononmin
// Pacco
// Perfect Square!
// Perfectionism
// Phyballs
// Pixel Purge
// PixelField
// Planet Revenge
// Plants Vs Zombies
// Platform
// Platform game
// Plus+Plus
// Pocket Snap
// Poker
// Pool
// Pop the Lock
// Pop to Save
// Poux
// Pudi
// Pumpkin Story
// Puppet Bird
// Pyramids of Ra
// qomp
// Quick Switch
// Racing
// Radical
// Rebuild Chile
// Renju
// Rise Above
// Risky Road
// Roguelike
// Roly Poly
// Run Around
// Rush Hour
// SameGame
// SamePhysics
// Save the Totem
// Security
// Serious Scramblers
// Shrink it
// Sling
// Slingy
// Snowflakes
// Sokoban
// Space Checkers
// Space is Key
// Spellfall
// Spinny Gun
// Splitter
// Spring Ninja
// Sproing
// Stabilize!
// Stack
// Stairs
// Stick Hero
// String Avoider
// Stringy
// Sudoku
// Super Mario Bros
// Surfingers
// Survival Horror
// Talesworth Adventure
// Tetris
// The Impossible Line
// The Moops - Combos of Joy
// The Next Arrow
// Threes
// Tic Tac Toe
// Timberman
// Tiny Wings
// Tipsy Tower
// Toony
// Totem Destroyer
// Tower Defense
// Trick Shot
// Tunnelball
// Turn
// Turnellio
// TwinSpin
// vvvvvv
// Warp Shift
// Way of an Idea
// Whack a Creep
// Wheel of Fortune
// Where's my Water
// Wish Upon a Star
// Word Game
// Wordle
// Worms
// Yanga
// Yeah Bunny
// Zhed
// zNumbers