Python implementation of the generation of random maze algorithm core code sharing (including game complete code) _python

Source: Internet
Author: User

Complete code Download: Http://xiazai.jb51.net/201407/tools/python-migong.rar

A recent study of the next Maze generation algorithm, and then made a simple online maze game. The game address and the corresponding open source project address can be found through the link 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 generation algorithm of the random maze. Once you understand it, you'll find out how simple the algorithm is.

1. Divide the maze map into multiple rooms, each with four walls.
2. Let "person" from a map any point a set off, began wandering in the maze. Move from One Direction in the 1/2/3/4 direction of room A. In the process of walking from room A to room B, tear down the wall between A/A and the room.
3. If the room opposite the direction X has passed, choose a different direction. If all directions of the room have been passed, then return to the previous room to see if there are alternative roads.
4. Walk to really no way to go, the explanation has gone through all the rooms, the maze is also generated well.

Here is the python implementation of the algorithm (the core part)

def gen_map (self, max_x=10, max_y=10): "" Create 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] # from 0, 0 to create a maze (at the same time as a starting point), the starting point into the stack while block_stack:block = Block_stack.pop () #取出当前所在的房间 next_block = Block.get_n Ext_block () # Get the next room to go if next_block: # If you succeed in getting the next pass, put the walking 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_stac K:self.solution.append ((o.x, O.Y)) def get_next_block_pos (self, direction): "" "" "" "" "" "" "" x = self.x y = s elf.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) # Get a random direction for direction in directions: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 license continue if SELF.MMAP.MMAP[X][Y]: # If you've been through continue
 Self.walls[direction] = False return blocks (Self.mmap, x, y, direction) return None # no room found available

Note: Because the number of the maze path generated by this method is not too many, the coffeescript version of the maze in the process of generating the random processing, the corresponding algorithm is a little bit more complicated.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.