Advertisement
Guest User

Trace road without forks and generate it

a guest
Sep 24th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.46 KB | None | 0 0
  1. import wx
  2. import wx.xrc
  3. import wx.grid
  4. import random
  5.  
  6. ###########################################################################
  7. ## Class MyFrame1
  8. ###########################################################################
  9.  
  10. class MyFrame1 ( wx.Frame ):
  11.    
  12.     def __init__( self, parent ):
  13.         wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 819,267 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )
  14.        
  15.         self.SetSizeHints( wx.DefaultSize, wx.DefaultSize )
  16.        
  17.         bSizer1 = wx.BoxSizer( wx.VERTICAL )
  18.        
  19.         self.m_scrolledWindow1 = wx.ScrolledWindow( self, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, wx.HSCROLL|wx.VSCROLL )
  20.         self.m_scrolledWindow1.SetScrollRate( 5, 5 )
  21.         wSizer1 = wx.WrapSizer( wx.HORIZONTAL )
  22.        
  23.         self.Grid = wx.grid.Grid( self.m_scrolledWindow1, wx.ID_ANY, wx.DefaultPosition, wx.DefaultSize, 0 )
  24.        
  25.         # Grid
  26.         self.Grid.CreateGrid( 10, 10 )
  27.         self.Grid.EnableEditing( True )
  28.         self.Grid.EnableGridLines( True )
  29.         self.Grid.EnableDragGridSize( False )
  30.         self.Grid.SetMargins( 0, 0 )
  31.        
  32.         # Columns
  33.         self.Grid.EnableDragColMove( False )
  34.         self.Grid.EnableDragColSize( True )
  35.         self.Grid.SetColLabelSize( 0 )
  36.         self.Grid.SetColLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )
  37.        
  38.         # Rows
  39.         self.Grid.EnableDragRowSize( True )
  40.         self.Grid.SetRowLabelSize( 0 )
  41.         self.Grid.SetRowLabelAlignment( wx.ALIGN_CENTRE, wx.ALIGN_CENTRE )
  42.        
  43.         # Label Appearance
  44.        
  45.         # Cell Defaults
  46.         self.Grid.SetDefaultCellAlignment( wx.ALIGN_LEFT, wx.ALIGN_TOP )
  47.         wSizer1.Add( self.Grid, 0, wx.ALL, 5 )
  48.        
  49.         self.Button = wx.Button( self.m_scrolledWindow1, wx.ID_ANY, u"Trace it!", wx.DefaultPosition, wx.DefaultSize, 0 )
  50.         wSizer1.Add( self.Button, 0, wx.ALL, 5 )
  51.        
  52.         self.GenerateBtn = wx.Button( self.m_scrolledWindow1, wx.ID_ANY, u"Generate road!", wx.DefaultPosition, wx.DefaultSize, 0 )
  53.         wSizer1.Add( self.GenerateBtn, 0, wx.ALL, 5 )
  54.        
  55.        
  56.         self.m_scrolledWindow1.SetSizer( wSizer1 )
  57.         self.m_scrolledWindow1.Layout()
  58.         wSizer1.Fit( self.m_scrolledWindow1 )
  59.         bSizer1.Add( self.m_scrolledWindow1, 1, wx.EXPAND |wx.ALL, 0 )
  60.        
  61.        
  62.         self.SetSizer( bSizer1 )
  63.         self.Layout()
  64.        
  65.         self.Centre( wx.BOTH )
  66.        
  67.         # Connect Events
  68.         self.Button.Bind( wx.EVT_BUTTON, self.Trace )
  69.         self.GenerateBtn.Bind( wx.EVT_BUTTON, self.Generate )
  70.    
  71.     def __del__( self ):
  72.         pass
  73.  
  74.     #Reset original colors of grid
  75.     def reset( self ):
  76.         for i in range(0, self.Grid.GetNumberRows()):
  77.             for j in range(0, self.Grid.GetNumberCols()):
  78.                 self.Grid.SetCellBackgroundColour(i, j, wx.WHITE)
  79.                 self.Grid.SetCellTextColour(i, j, wx.BLACK)
  80.        
  81.     #Recursive function to find path through road
  82.     def FindPath( self, i, j ):
  83.         #Timer for visualisating work
  84.         #Colorize current cell
  85.         color = wx.Colour(100, 100, 255)
  86.         self.Grid.SetCellBackgroundColour(i, j, color)
  87.         self.Grid.SetCellTextColour(i, j, wx.WHITE)
  88.         self.Grid.ForceRefresh()
  89.         #Down
  90.         if i + 1 < self.Grid.GetNumberRows() and self.Grid.GetCellValue(i + 1, j) == "0" and self.Grid.GetCellBackgroundColour(i + 1, j) != color:
  91.             return self.FindPath(i + 1, j)
  92.         #Up
  93.         elif i - 1 >= 0 and self.Grid.GetCellValue(i - 1, j) == "0" and self.Grid.GetCellBackgroundColour(i - 1, j) != color:
  94.             return self.FindPath(i - 1, j)
  95.         #Left
  96.         elif j - 1 >= 0 and self.Grid.GetCellValue(i, j - 1) == "0" and self.Grid.GetCellBackgroundColour(i, j - 1) != color:
  97.             return self.FindPath(i, j - 1)
  98.         #Right
  99.         elif j + 1 < self.Grid.GetNumberCols() and self.Grid.GetCellValue(i, j + 1) == "0" and self.Grid.GetCellBackgroundColour(i, j + 1) != color:
  100.             return self.FindPath(i, j + 1)
  101.         else: return None
  102.  
  103.     #Function for check for free cells in defined range
  104.     def free( self, i1, i2, j1, j2 ):
  105.         #Go through all cells
  106.         for i in range(i1, i2 + 1):
  107.             for j in range(j1, j2 + 1):
  108.                 #If cell is above grid or a part of road
  109.                 #then way is not free
  110.                 try:
  111.                     if self.Grid.GetCellValue(i, j) == "0":
  112.                         return False
  113.                 except:
  114.                     return False
  115.         return True #Else way is clear
  116.    
  117.     #Recursive function to make a road
  118.     def MakeRoad( self, i, j ):
  119.         self.Grid.SetCellValue(i, j, "0")
  120.        
  121.         #Make random direction
  122.        
  123.         #Set list with directions
  124.         dirs = [n for n in range(1, 5)]  #1 for down, 2 for up, 3 for left, 4 for right
  125.         #Check 2x3 rectangle in cells of 1 below these directions
  126.         #Down
  127.         if self.free(i + 1, i + 2, j - 1, j + 1) == False:
  128.             dirs.remove(1)
  129.         #Up
  130.         if self.free(i - 2, i - 1, j - 1, j + 1) == False:
  131.             dirs.remove(2)
  132.         stop = False
  133.         #Left
  134.         if self.free(i - 1, i + 1, j - 2, j - 1) == False:
  135.             dirs.remove(3)
  136.         #Right
  137.         if self.free(i - 1, i + 1, j + 1, j + 2) == False:
  138.             dirs.remove(4)
  139.            
  140.         if dirs == []:
  141.             return None #If no free directions left generation stops
  142.         else:
  143.             direction = random.choice(dirs) #Else set a random direction from last ones
  144.         #Make a road
  145.         #Down
  146.         if direction == 1:
  147.             return self.MakeRoad(i + 1, j)
  148.         #Up
  149.         elif direction == 2:
  150.             return self.MakeRoad(i - 1, j)
  151.         #Left
  152.         elif direction == 3:
  153.             return self.MakeRoad(i, j - 1)
  154.         #Right
  155.         elif direction == 4:
  156.             return self.MakeRoad(i, j + 1)
  157.        
  158.     # Virtual event handlers, overide them in your derived class
  159.     def Trace( self, event ):
  160.         self.reset()
  161.         #Find beginning of the road
  162.         for i in range(0, self.Grid.GetNumberRows()):
  163.             for j in range(0, self.Grid.GetNumberCols()):
  164.                 if self.Grid.GetCellValue(i, j) == '0':
  165.                     break
  166.             if self.Grid.GetCellValue(i, j) == '0':
  167.                 break
  168.         #Call the function of making path
  169.         #Statment used for check that there is really road in case that last cell isn't equal to 0
  170.         if self.Grid.GetCellValue(i, j) == '0':
  171.             return self.FindPath(i, j)
  172.         else:
  173.             return wx.LogMessage("No road there!") #Log message if there is no road
  174.        
  175.     #Fill all cells with random numbers and call a function to make a road with a parameters as random cell on the top
  176.     #By the way, it's a click's on generate button event
  177.     def Generate( self, event ):
  178.         self.reset()
  179.         for i in range(0, self.Grid.GetNumberRows()):
  180.             for j in range(0, self.Grid.GetNumberCols()):
  181.                 self.Grid.SetCellValue(i, j, str(random.randrange(1, 2)))
  182.         return self.MakeRoad(0, random.randrange(1, self.Grid.GetNumberCols() - 1))
  183.                    
  184.    
  185. app = wx.App(False) #Create example of app's class
  186. frame = MyFrame1(parent=None) #Example of window's frame's class
  187. frame.Show() #Show frame
  188. app.MainLoop() #Show app's window
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement