Perfect maze generation – tile based version – AS3

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
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!

Rate this post: 1 Star2 Stars3 Stars4 Stars5 Stars (5 votes, average: 5.00 out of 5)
Loading ... Loading ...
If you found this post useful, please consider a small donation.

6 Responses

  1. Maru says:

    This is great and endless all the same time.

  2. Chris says:

    Great stuff, havent been on in over a year glad I came back

  3. JL says:

    Alright!!! AS3 version. Thank you very much.

  4. Darren says:

    how to use?

  5. Razvan says:

    Hi!
    Thanks for the Maze generator! It`s very nice!
    I used it with a bit of papervision.. http://www.webdesignmedia.ro/Maze.swf :P

  6. Jesus says:

    This is the work of the devil!

Leave a Reply