This article mainly introduces the python Implementation of random Maze generation algorithm core code sharing, this article contains a simple maze game complete code, need friends can refer to the complete code download: http://xiazai.bitsCN.com/201407/tools/python-migong.rar
Recently I studied the generation algorithm of the lower maze, and then made a simple online maze game. The game address and the corresponding open-source project address can be found through the above link. Open-source projects do not contain server-side code, because the server-side code is too simple. The following describes the algorithm used to generate a random maze. Once understood, you will find how simple this algorithm is.
1. Divide the maze map into multiple rooms, each with four walls.
2. Let the "man" start from any point on the map and start wandering in the maze. Choose one of the 1/2/3/4 directions of Room A to move forward. During the process from Room A to Room B, the wall between room A and Room B is pushed down.
3. If the room opposite the Direction x has passed, select another direction. If the room in all directions has passed, return to the previous room to see if there is any road available.
4. When there is no way to go, it means that all the rooms have passed and the Maze has been generated.
The following is the python Implementation of the algorithm (core part)
Def gen_map (self, max_x = 10, max_y = 10): "generate a maze" "self. max_x, self. max_y = max_x, max_y # Set the map size self. mmap = [[None for j in range (self. max_y)] for I in range (self. max_x)] # generate the original map self. solution = [] # maze solution block_stack = [Block (self, 0, 0)] # generate a maze from 0 to 0 (this is also the starting point ), place the starting point in the stack while block_stack: block = block_stack.pop () # retrieve next_block = block from the current room. get_next_block () # obtain the next room to go if next_block: # if the next exit is obtained successfully, put the room that has passed through into the stack block_stack.append (block) block_stack.append (next_block) if next_block.x = self. max_x-1 and next_block.y = self. max_y-1: # the path in the stack is the solution for o in block_stack: self. solution. append (o. x, o. y) def get_next_block_pos (self, direction): "Get the room number in the specified direction" x = self. x y = self. y if direction = 0: # Top y-= 1 elif direction = 1: # Right x + = 1 if direction = 2: # Bottom y + = 1 if direction = 3: # Left x-= 1 return x, y def get_next_block (self ): "Get the next room to go" directions = list (range (4) random. shuffle (directions) # obtain a random direction for direction in directions ctions: x, y = self. get_next_block_pos (direction) if x> = self. mmap. max_x or x <0 or y> = self. mmap. max_y or y <0: # the room number is allowed. mmap. mmap [x] [y]: # If you have already passed the continue self. wils [direction] = False return Block (self. mmap, x, y, direction) return None # No available rooms found
Note: because the number of branches of the Maze Road generated using this method is not too large, the coffeescript version adds Random Processing during the process of generating the maze, and the corresponding algorithm is a little more complex.