Full code Download: Http://xiazai.bitsCN.com/201407/tools/python-migong.rar
Recently, the generation algorithm of the maze was studied, and then a simple online maze game was done. The game address and the corresponding open source project address can be found through the links above. The open source project does not contain the server-side code, because the server-side code is simply too simple. The following is a simple introduction to the random Maze generation algorithm. Once you understand it, you'll see how simple the algorithm is.
1. Divide the maze map into multiple rooms, each with four walls.
2. Let "people" from the map at any point a start, began wandering in the maze. Move in any direction from the 1/2/3/4 direction of room A. In the process of walking from room A to room B, tear down the walls between A/b rooms.
3. If the room opposite the direction x has passed, select another direction. If all the directions of the room have been passed, then return to the previous room to see if there are still optional roads.
4. Walk to really no way to go, the explanation has gone through all the rooms, the maze is also generated.
The following is the python implementation of the algorithm (core part)
def gen_map (self, max_x=10, max_y=10): "" "Generate Maze" "" self.max_x, self.max_y = max_x, max_y # set map Size Self.mmap = [None for J in Range (self.max_y)] for I in range (self.max_x)] # Generate original Map self.solution = [] # Maze Solution block_stack = [Block (self, 0, 0)] # Starting from 0, 0 The maze is generated (this is also used as a starting point) and the starting point is placed in the stack while block_stack:block = Block_stack.pop () #取出当前所在的房间 next_block = Block.get_next_block () # Get the next room to go if next_block: # If you succeed in getting the next pass, put the room back 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: # Go to the end, the path in the stack is the solution for O in block_stack:self.so Lution.append ((o.x, O.Y)) def get_next_block_pos (self, direction): "" "gets the room number of 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 Next room to go" "Directions = List (range (4)) Random.shuffle (directions) # randomly get a A direction to go for direction indirections: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: # room number within the permitted range continue if self.mmap.mmap[x][y]: # If already traversed continue self.walls[direction] = False return B Lock (Self.mmap, x, y, direction) return None # no room found available
Note: Because the maze path generated by this method is not too many branches, coffeescript version in the process of creating a maze of random processing, the corresponding algorithm is a little more complicated.