Index

Part 2: The Cell Class

After Superstrict, which I use for all my BlitzMax code, we will create the Cell class.

Type TCell
End Type

We will add a field called paths, which the four lower bits will determine if a path exists between two cells (1) or a wall exists between the cells (0). We will also create constants for each direction as a bit mask.

	Field paths:Int
	
	Const North:Int = 1
	Const East:Int = 2
	Const South:Int = 4
	Const West:Int = 8

Next I am adding pointers to the cells that exists in each direction. Not strictly necessary for a regular rectangular grid, but this will come in handy for our maze as you can have control over how it is generated. For example, leave a hole in the middle where the ghosts will begin. You would simply leave null the directions where the path should not go. We will use these fields here to outline the border of the maze and restrict the path to only the left half of the grid.

	Field _north:TCell, _east:TCell, _south:TCell, _west:TCell

Next I am adding a method called Destroy(). This will remove all the circular references in the class, which is necessary or else the garbage collector will not properly remove the object when it is no longer needed.

	Method Destroy()
		_north = Null
		_east = Null
		_south = Null
		_west = Null
	End Method

Next we need a way to link two cells together. We will create a method called Link() and pass to it the direction we want to link to. We also need to link a path from the other cell to this one. We will do a bitwise Or on the paths field. We will also return the cell from that direction so it can be added to the list.

	Method Link:TCell(Direction:Int)
		paths :| Direction
		Select Direction
			Case North
				_north.paths :| South
				Return _north
			Case South
				_south.paths :| North
				Return _south
			Case East
				_east.paths :| West
				Return _east
			Case West
				_west.paths :| East
				Return _west
		End Select
	End Method

Next we need a method to get all the directions that can potentially have the wall removed. You can only have the wall removed to an unvisited cell, so first we check if a cell exists in a certain direction, then check if the cell is unvisited. Unvisited cells will have the paths field still at 0. The result will be placed in an array, and the array will be returned. If no direction exists, Null will be returned.

	Method getUnvisited:Int[]()
		Local uv:Int[4]
		Local top:Int = 0
		If _north And _north.paths = 0
			uv[top] = North
			top :+ 1
		End If
		If _east And _east.paths = 0
			uv[top] = East
			top :+ 1
		End If
		If _south And _south.paths = 0
			uv[top] = South
			top :+ 1
		End If
		If _west And _west.paths = 0
			uv[top] = West
			top :+ 1
		End If
		
		If top = 0 Then Return Null
		Return uv[..top]
	End Method

This is all that is required here for generating a random maze. We will next create the maze class where I will show you how to create a random maze, then we will expand both the maze class and the cell class to turn our maze into a "Pac-Man" level.

The complete Cell class

SuperStrict

Type TCell
	Field paths:Int
	
	Const North:Int = 1
	Const East:Int = 2
	Const South:Int = 4
	Const West:Int = 8
	
	Field _north:TCell, _east:TCell, _south:TCell, _west:TCell
	
	Method Destroy()
		_north = Null
		_east = Null
		_south = Null
		_west = Null
	End Method
	
	Method Link:TCell(Direction:Int)
		paths :| Direction
		Select Direction
			Case North
				_north.paths :| South
				Return _north
			Case South
				_south.paths :| North
				Return _south
			Case East
				_east.paths :| West
				Return _east
			Case West
				_west.paths :| East
				Return _west
		End Select
	End Method
	
	Method getUnvisited:Int[]()
		Local uv:Int[4]
		Local top:Int = 0
		If _north And _north.paths = 0
			uv[top] = North
			top :+ 1
		End If
		If _east And _east.paths = 0
			uv[top] = East
			top :+ 1
		End If
		If _south And _south.paths = 0
			uv[top] = South
			top :+ 1
		End If
		If _west And _west.paths = 0
			uv[top] = West
			top :+ 1
		End If
		
		If top = 0 Then Return Null
		Return uv[..top]
	End Method
	
End Type

Index

Created with Tutorial Markup (c)2018 James Chamblin