Perfect maze generation – tile based version – AS3

Read all posts about "" game

After publishing Perfect maze generation – tile based version written in Php, I got some emails asking for an AS3 version.

Pedro Taranto made the AS3 porting in less than a day, and here’s what you’ll get:

Click on the maze to generate a new one

And this is the source code:

package br.com.pedrotaranto
{
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	[SWF(backgroundColor = "#FFFFFF", width = '800', height = '600', frameRate = "30")]
	public class TiledMazeGen extends Sprite
	{
		//constants
		private const MAZE_WIDTH		: uint = 20;
		private const MAZE_HEIGHT		: uint = 20;
		private const TILE_SIZE			: uint = 10;
		private const START_COLOR		: uint = 0xFF0000;
		private const FINISH_COLOR		: uint = 0x00FF00;
		private const WALL_COLOR		: uint = 0x000000;
		private const WALKABLE_COLOR	: uint = 0xDDDDDD;
		
		//directions
		private const NORTH				: String = "N";
		private const SOUTH				: String = "S";
		private const EAST				: String = "E";
		private const WEST				: String = "W";
		
		//variables
		private var _width		: uint;
		private var _height		: uint;
		private var _maze		: Array;
		private var _moves		: Array;
		private var _start		: Point;
		private var _finish		: Point;
		private var _container	: Sprite;
		
		public function TiledMazeGen () : void
		{
			stage.align		= StageAlign.TOP_LEFT;
			stage.scaleMode	= StageScaleMode.NO_SCALE;
			stage.addEventListener(MouseEvent.CLICK, _generate);
			
			_width	= MAZE_WIDTH * 2 + 1;
			_height	= MAZE_HEIGHT * 2 + 1;
			
			_start	= new Point(1, 1);
			//_finish = new Point(_height - 2, _width - 2);
			
			_container = new Sprite();
			
			_generate();
		}
		
		private function _generate ( event : MouseEvent = null ) : void
		{
			_initMaze();
			_createMaze();
			_drawMaze();
		}
		
		private function _initMaze () : void
		{
			_maze	= new Array(_width);
			
			for ( var x : int = 0; x < _height; x++ )
			{
				_maze[x] = new Array(_height);
				
				for ( var y : int = 0; y < _width; y++ )
				{
					_maze[x][y] = true;
				}
			}
			
			_maze[_start.x][_start.y] = false;
		}
		
		private function _createMaze () : void
		{
			var back				: int;
			var move				: int;
			var possibleDirections	: String;
			var pos					: Point = _start.clone();
			
			_moves = new Array();
			_moves.push(pos.y + (pos.x * _width));
			
			while ( _moves.length )
			{
				possibleDirections = "";
				
				if ((pos.x + 2 < _height ) && (_maze[pos.x + 2][pos.y] == true) && (pos.x + 2 != false) && (pos.x + 2 != _height - 1) )
				{
					possibleDirections += SOUTH;
				}
				
				if ((pos.x - 2 >= 0 ) && (_maze[pos.x - 2][pos.y] == true) && (pos.x - 2 != false) && (pos.x - 2 != _height - 1) )
				{
					possibleDirections += NORTH;
				}
				
				if ((pos.y - 2 >= 0 ) && (_maze[pos.x][pos.y - 2] == true) && (pos.y - 2 != false) && (pos.y - 2 != _width - 1) )
				{
					possibleDirections += WEST;
				}
				
				if ((pos.y + 2 < _width ) && (_maze[pos.x][pos.y + 2] == true) && (pos.y + 2 != false) && (pos.y + 2 != _width - 1) )
				{
					possibleDirections += EAST;
				}
				
				if ( possibleDirections.length > 0 )
				{
					move = _randInt(0, (possibleDirections.length - 1));
					
					switch ( possibleDirections.charAt(move) )
					{
						case NORTH: 
							_maze[pos.x - 2][pos.y] = false;
							_maze[pos.x - 1][pos.y] = false;
							pos.x -=2;
							break;
						
						case SOUTH: 
							_maze[pos.x + 2][pos.y] = false;
							_maze[pos.x + 1][pos.y] = false;
							pos.x +=2;
							break;
						
						case WEST: 
							_maze[pos.x][pos.y - 2] = false;
							_maze[pos.x][pos.y - 1] = false;
							pos.y -=2;
							break;
						
						case EAST: 
							_maze[pos.x][pos.y + 2] = false;
							_maze[pos.x][pos.y + 1] = false;
							pos.y +=2;
							break;        
					}
					
					_moves.push(pos.y + (pos.x * _width));
				}
				else
				{
					back = _moves.pop();
					pos.x = int(back / _width);
					pos.y = back % _width;
				}
			}
		}
		
		private function _drawMaze () : void
		{
			var tile : Sprite;
			
			if ( contains(_container) )
			{
				removeChild(_container)
			}
			
			_container = new Sprite();
			addChild(_container);
			
			for ( var x : int = 0; x < _height; x++ )
			{
				for ( var y : int = 0; y < _width; y++ )
				{
					tile	= (_maze[x][y] == true) ? _drawTile(WALL_COLOR) : _drawTile(WALKABLE_COLOR);
					tile.x	= x * TILE_SIZE;
					tile.y	= y * TILE_SIZE;
					
					_container.addChild(tile);
				}
			}
			
			//start tile
			tile = _drawTile(START_COLOR);
			tile.x = _start.x * TILE_SIZE;
			tile.y = _start.y * TILE_SIZE;
			_container.addChild(tile);
			
			//finish tile
			/*tile = _drawTile(FINISH_COLOR);
			tile.x = _finish.x * TILE_SIZE;
			tile.y = _finish.y * TILE_SIZE;
			_container.addChild(tile);*/
		}
		
		private function _drawTile ( color : uint ) : Sprite
		{
			var tile : Sprite = new Sprite();
				tile.graphics.beginFill(color);
				tile.graphics.drawRect(0, 0, TILE_SIZE, TILE_SIZE);
				tile.graphics.endFill();
			
			return tile;
		}
		
		private function _randInt ( min : int, max : int ) : int 
		{
			return int((Math.random() * (max - min + 1)) + min);
		}
	}
}

Download the source and enjoy!

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