RandomGenerationMaze

Random Map Generation

  • Role: Engineer
  • Team size: 1
  • Engine: Flash
  • Available: Click Here

Description

This simple prototype is a proof of concept for dynamically generated maps based off of a given grid. This grid, 6 x 10, draws a main path from start to end, while providing many additional branches. This code is inspired by Rogue-Like games such as the Binding of Isaac.

Code

Main

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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
package Core
{
	import flash.geom.Point;
 
	import starling.core.Starling;
	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.events.Touch;
	import starling.events.TouchEvent;
	import starling.events.TouchPhase;
	import starling.text.TextField;
	import starling.utils.Color;
 
	public class Main extends Sprite
	{
		public static const BOARD_WIDTH:int = 6;
		public static const BOARD_HEIGHT:int = 10;
 
		private var board:Array = [];
		private var boardHolder:Sprite;
 
		private var startingPoint:Location;
		private var endingPoint:Location;
		private var selectedRoom:Location
 
		private var endReached:Boolean;
		private var path:Array = [];
		private var adjacentRooms:int = 0;
 
		private var minimumRooms:int = 20;
		private var currentRoomCount:int = 0;
		private var maximumAttempts:int = 5;
		private var currentAttempt:int = 0;
 
		//UI
		private var resetButton:Sprite;
		private var stepButton:Sprite;
 
		private var leadNodes:Array = [];
 
		public function Main()
		{
			super();
 
			createBoard();
			createPaths();
			createUI();
		}
		private function createUI():void
		{
			resetButton = new Sprite();
			addChild(resetButton);
 
			var resetBG:Quad = new Quad(Atlas.wTenPercent * 1, Atlas.hTenPercent * .75, Color.BLACK);
			resetButton.addChild(resetBG);
 
			var resetText:TextField = new TextField(resetBG.width, resetBG.height, "Reset", "Verdana" , 20, Color.WHITE);
			resetButton.addChild(resetText);
 
			resetButton.pivotX = resetButton.width/2;
			resetButton.pivotY = resetButton.height/2;
			resetButton.x = Atlas.wTenPercent * 1;
			resetButton.y = Atlas.hTenPercent * 9;
			resetButton.addEventListener(TouchEvent.TOUCH, resetBoard);
 
			stepButton = new Sprite();
			addChild(stepButton);
 
			var stepBG:Quad = new Quad(Atlas.wTenPercent * 1, Atlas.hTenPercent * .75, Color.BLACK);
			stepButton.addChild(stepBG);
 
			var stepText:TextField = new TextField(stepBG.width, stepBG.height, "Step", "Verdana" , 20, Color.WHITE);
			stepButton.addChild(stepText);
 
			stepButton.pivotX = stepButton.width/2;
			stepButton.pivotY = stepButton.height/2;
			stepButton.x = Atlas.wTenPercent * 5;
			stepButton.y = Atlas.hTenPercent * 9;
			stepButton.addEventListener(TouchEvent.TOUCH, addRoom);
		}
		private function createBoard():void
		{
			var gap:Number = Atlas.wTenPercent * .3;
 
			boardHolder = new Sprite;
			addChild(boardHolder);
 
			var count:int = 0;
 
			//Create all potential location points
			for (var i:int = 0; i < BOARD_HEIGHT; i++)
			{
				for (var j:int = 0; j < BOARD_WIDTH; j++) 				{ 					var location:Location = new Location(); 					boardHolder.addChild(location); 					location.x = (location.width/2 + gap) * j; 					location.y = (location.width/2 + gap) * i; 					 					location.setGridLocation(count); 					location.setRowColumn(new Point(i,j)); 					 					board.push(location); 					 					count++; 				} 			} 			 			boardHolder.pivotX = boardHolder.width/2; 			boardHolder.pivotY = boardHolder.height/2; 			boardHolder.x = Atlas.stageCenter.x; 			boardHolder.y = Atlas.stageCenter.y; 		} 		private function addRoom($e:TouchEvent):void 		{ 			var touch:Touch = $e.getTouch(this, TouchPhase.BEGAN); 			if (touch) 			{ 				determineLocationType(); 			} 		} 		private function createPaths():void 		{ 			var randomStartingPosition:int = Math.round(Math.random() * (BOARD_WIDTH -1)); 			 			startingPoint = board[randomStartingPosition]; 			startingPoint.changeColor("StartingPoint"); 			leadNodes.push(startingPoint); 			 			determineLocationType(); 			 		} 		private function determineLocationType():void 		{ 			/*trace("Determine Location Type"); 			trace("*********************");*/ 			 			if(!endReached) 			{ 				var locationArray:Array = checkNeighbors(leadNodes[0]) 				 				var randomRoom:int; 					 				if(locationArray.length > 0)
				{
					leadNodes.splice(0,1);
 
					var randomNumOfAdjacentRooms:int = 1 + Math.round(Math.random() * (locationArray.length -1));
 
					for( var i:int = 0; i < randomNumOfAdjacentRooms; i++) 					{ 						//trace("Creating Room"); 						 						currentAttempt = 0; 						 						randomRoom = Math.round(Math.random() * (locationArray.length -1)); 						selectedRoom = locationArray[randomRoom]; 						selectedRoom.changeColor("Room"); 						path.push(selectedRoom); 						 						currentRoomCount++; 						 						leadNodes.push(selectedRoom); 						 						locationArray.splice(randomRoom, 1); 					} 					 					//trace("Complete"); 				} 				else 				{ 					//trace("No more path possible"); 					leadNodes.splice(0,1); 					 					if(leadNodes.length > 0)
					{
						//trace("Falling to next branch");
						determineLocationType()
					}
					else
					{
						if(currentAttempt < maximumAttempts)
						{
							if(currentRoomCount < minimumRooms)
							{
								randomRoom = Math.round(Math.random() * (path.length -1))
								//selectedRoom = board[path[randomRoom].getGridLocation()];
								selectedRoom = path[randomRoom];
								leadNodes.push(selectedRoom);
								determineLocationType()
								currentAttempt++;
								//trace("Current Attempts: " + currentAttempt);
							}
							else
							{
								selectedRoom.changeColor("EndingPoint");
								endReached = true;
							}
						}
						else
						{
							selectedRoom.changeColor("EndingPoint");
							endReached = true;
						}
					}
				}
			}			
		}
		private function checkNeighbors(location:Location):Array
		{
			//trace("Checking Location: " + location.getGridLocation());
 
			var locationArray:Array = [];
			adjacentRooms = 0;
 
			var leftLocation:Location = board[location.getGridLocation() - 1];
			var rightLocation:Location = board[location.getGridLocation() + 1];
			var topLocation:Location = board[location.getGridLocation() - BOARD_WIDTH];
			var bottomLocation:Location = board[location.getGridLocation() + BOARD_WIDTH];
 
			var traceString:String = "Bordered on ";
 
			if(leftLocation)
			{
				//trace("Left: " + leftLocation.getGridLocation());
 
				checkAdjacency(leftLocation)
 
				if(leftLocation.getRowColumn().y != BOARD_WIDTH-1 && !leftLocation.getRoom() && leftLocation.getAdjacentNumber() < 2)
				{
					locationArray.push(leftLocation);
				}
				if(leftLocation.getEndPoint())
				{
					endReached = true;
				}
				if(leftLocation.getRoom())
				{
					adjacentRooms++;
					traceString += "Left,";
				}
			}
			if(rightLocation)
			{
				//trace("Right: " + rightLocation.getGridLocation());
 
				checkAdjacency(rightLocation)
 
				if(rightLocation.getRowColumn().y != 0 && !rightLocation.getRoom() && rightLocation.getAdjacentNumber() < 2)
				{
					locationArray.push(rightLocation);
				}
				if(rightLocation.getEndPoint())
				{
					endReached = true;
				}
				if(rightLocation.getRoom())
				{
					adjacentRooms++;
					traceString += "Right,";
				}
			}
			if(topLocation)
			{
				//trace("Top: " + topLocation.getGridLocation());
 
				checkAdjacency(topLocation);
 
				if(topLocation.getRowColumn().x != 0 && !topLocation.getRoom() && topLocation.getAdjacentNumber() < 2)
				{
					locationArray.push(topLocation);
				}
				if(topLocation.getEndPoint())
				{
					endReached = true;
				}
				if(topLocation.getRoom())
				{
					adjacentRooms++;
					traceString += "Top,";
				}
			}
			if(bottomLocation)
			{
				//trace("Down: " + bottomLocation.getGridLocation());
				checkAdjacency(bottomLocation);
 
				if(bottomLocation.getRowColumn().x != BOARD_HEIGHT -1 && !bottomLocation.getRoom() && bottomLocation.getAdjacentNumber() < 2)
				{
					locationArray.push(bottomLocation);
				}
				if(bottomLocation.getEndPoint())
				{
					endReached = true;
				}
				if(bottomLocation.getRoom())
				{
					adjacentRooms++;
					traceString += "Down,";
				}
			}
 
			//trace(traceString);
			//trace("-----------");
 
			return locationArray;
		}
		private function checkAdjacency(location:Location):void
		{
			var leftLocation:Location = board[location.getGridLocation() - 1];
			var rightLocation:Location = board[location.getGridLocation() + 1];
			var topLocation:Location = board[location.getGridLocation() - BOARD_WIDTH];
			var bottomLocation:Location = board[location.getGridLocation() + BOARD_WIDTH];
 
			adjacentRooms = 0;
 
			if(leftLocation)
			{
				if(leftLocation.getRoom())
				{
					adjacentRooms++;
				}
			}
			if(rightLocation)
			{
				if(rightLocation.getRoom())
				{
					adjacentRooms++;
				}
			}
			if(topLocation)
			{
				if(topLocation.getRoom())
				{
					adjacentRooms++;
				}
			}
			if(bottomLocation)
			{
				if(bottomLocation.getRoom())
				{
					adjacentRooms++;
				}
			}
 
			location.setAdjacentNumber(adjacentRooms);
		}
		private function resetBoard($e:TouchEvent):void
		{
			var touch:Touch = $e.getTouch(this, TouchPhase.BEGAN);
			if (touch)
			{
				boardHolder.dispose();
				removeChild(boardHolder);
				boardHolder = null;
 
				startingPoint = null;
				endingPoint = null;
				endReached = false;
 
				board = [];
				currentRoomCount = 0;
				currentAttempt = 0;
 
				createBoard();
				createPaths();
			}
		}
	}
}

Location

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
package Core
{
	import flash.geom.Point;
 
	import starling.display.Quad;
	import starling.display.Sprite;
	import starling.utils.Color;
 
	public class Location extends Sprite
	{
		private var display:Quad
		private var gridLocation:int;
		private var room:Boolean;
		private var rowColumn:Point;
		private var endingLocation:Boolean;
		private var numberOfAdjacentNeighbors:int = 0;
 
		public function Location()
		{
			super();
 
			display = new Quad(20,20, Color.BLACK);
			display.pivotX = display.width/2;
			display.pivotY = display.height/2;
			addChild(display);
 
			visible = false;
		}
		public function changeColor(whatType:String):void
		{
			visible = true;
 
			switch(whatType)
			{
				case "StartingPoint":
					display.color = Color.GREEN;
					break;
				case "EndingPoint":
					display.color = Color.RED;
					endingLocation = true;
					break;
				case "Room":
					display.color = Color.BLACK;
					break;
			}
 
			room = true;
		}
		public function setGridLocation(gridLocation:int):void
		{
			this.gridLocation = gridLocation;
		}
		public function getGridLocation():int
		{
			return gridLocation;
		}
		public function setRowColumn(placement:Point):void
		{
			this.rowColumn = placement;
		}
		public function getRowColumn():Point
		{
			return rowColumn;
		}
		public function getRoom():Boolean
		{
			return room;
		}
		public function getEndPoint():Boolean
		{
			return endingLocation;
		}
		public function setAdjacentNumber(adjacentNumber:int):void
		{
			this.numberOfAdjacentNeighbors = adjacentNumber;
		}
		public function getAdjacentNumber():int
		{
			return numberOfAdjacentNeighbors;
		}
	}
}

 

 

 

Categories:

Code Examples, Experimental, Flash