Advertisement
Guest User

Untitled

a guest
Jan 26th, 2016
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 12.81 KB | None | 0 0
  1. import os
  2. import copy
  3. os.chdir("../Configurations")
  4.  
  5. from tilingObjects.Tile import Tile
  6. from tiling.FieldFrame import FieldFrame
  7. from tiling.Field import Field
  8. import time
  9.  
  10. class Solution15_0_0(object):
  11.     CONFIGURATION = "15-0-0.txt"
  12.    
  13.     def __init__(self):
  14.         i = 1
  15.         for idx, each_line in enumerate(open(self.CONFIGURATION, 'r')):
  16.             if idx == 0:
  17.                 width, height, scale = map(int, each_line.strip().split("\t"))
  18.                 self.field = Field(width, height)
  19.                 self.frame = FieldFrame(self.field, scale)
  20.             else:
  21.                 frequency, width, height = map(int, each_line.strip().split("\t"))
  22.                 for idx in range(frequency):
  23.                     self.field.addTile(Tile(i, width, height))
  24.                     i += 1
  25.        
  26.         globalTiles = self.field.getTiles()
  27.         #localTiles, duplicates = self.checkForDuplicates(globalTiles)
  28.         xCoordinate = 0
  29.         yCoordinate = 0
  30.         fieldRightWidth = self.field.getWidth()
  31.         fieldRightHeight = self.field.getHeight()
  32.         fieldBelowWidth = self.field.getWidth()
  33.         fieldBelowHeight = self.field.getHeight()
  34.         print "global", globalTiles
  35.         #print "local", localTiles
  36.        
  37.        
  38.        
  39.         self.methods(globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight)
  40.        
  41.         if self.field.solved(): print "SOLVED"
  42.         self.frame.root.mainloop()
  43.    
  44.     def methods(self, globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight):
  45.         globalTiles = self.sortGlobal(globalTiles)
  46.         localTiles, duplicates = self.checkForDuplicates(globalTiles)
  47.         print "Start methods"
  48.        
  49.         time.sleep(0.5)
  50.         if globalTiles == []:
  51.             self.field.solved()
  52.             print "SOLVED"
  53.        
  54.         else:
  55.            
  56.             if fieldRightWidth == 0:
  57.                
  58.                
  59.                
  60.                
  61.                 if fieldBelowHeight == 0:
  62.                     print "test1"
  63.                    
  64.                    
  65.                 #BELOW
  66.                 else:
  67.                     #Width
  68.                     print "Aloha"
  69.                     for i, tile in enumerate(localTiles):
  70.                         if fieldBelowWidth == tile.getWidth():
  71.                             self.field.placeTile(tile, xCoordinate, yCoordinate)
  72.                             self.frame.repaint(self.field)
  73.                             localTiles.pop(i)
  74.                             globalTiles = localTiles + duplicates
  75.                             xCoordinate = xCoordinate + tile.getWidth()
  76.                             fieldRightWidth = fieldRightWidth -tile.getWidth()
  77.                             fieldRightHeight = tile.getHeight()
  78.                             fieldBelowHeight = fieldRightHeight - tile.getHeight()
  79.                             fieldBelowWidth = fieldRightWidth + tile.getWidth()
  80.                             return self.methods(globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight)
  81.                
  82.                         if fieldBelowWidth == tile.getHeight():
  83.                             tile.flip()
  84.                             self.field.placeTile(tile, xCoordinate, yCoordinate)
  85.                             self.frame.repaint(self.field)
  86.                             localTiles.pop(i)
  87.                             globalTiles = localTiles + duplicates
  88.                             xCoordinate = xCoordinate + tile.getWidth()
  89.                             fieldRightWidth = fieldRightWidth -tile.getWidth()
  90.                             fieldRightHeight = tile.getHeight()
  91.                             fieldBelowHeight = fieldRightHeight - tile.getHeight()
  92.                             fieldBelowWidth = fieldRightWidth + tile.getWidth()
  93.                             return self.methods(globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight)
  94.                     #Height
  95.                     for i, tile in enumerate(localTiles):
  96.                         if fieldBelowHeight == tile.getHeight():
  97.                             self.field.placeTile(tile, xCoordinate, yCoordinate)
  98.                             self.frame.repaint(self.field)
  99.                             localTiles.pop(i)
  100.                             globalTiles = localTiles + duplicates
  101.                             xCoordinate = xCoordinate + tile.getWidth()
  102.                             fieldRightWidth = fieldRightWidth -tile.getWidth()
  103.                             fieldRightHeight = tile.getHeight()
  104.                             fieldBelowHeight = fieldRightHeight - tile.getHeight()
  105.                             fieldBelowWidth = fieldRightWidth + tile.getWidth()
  106.                             print globalTiles
  107.                             return self.methods(globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight)
  108.                            
  109.                         if fieldBelowHeight == tile.getWidth():
  110.                             self.field.placeTile(tile, xCoordinate, yCoordinate)
  111.                             self.frame.repaint(self.field)
  112.                             localTiles.pop(i)
  113.                             globalTiles = localTiles + duplicates
  114.                             xCoordinate = xCoordinate + tile.getWidth()
  115.                             fieldRightWidth = fieldRightWidth -tile.getWidth()
  116.                             fieldRightHeight = tile.getHeight()
  117.                             fieldBelowHeight = fieldRightHeight - tile.getHeight()
  118.                             fieldBelowWidth = fieldRightWidth + tile.getWidth()
  119.                             print globalTiles
  120.                             return self.methods(globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight)
  121.                     #biggest
  122.                     for i, tile in enumerate(localTiles):
  123.                         self.field.placeTile(tile, xCoordinate, yCoordinate)
  124.                         self.frame.repaint(self.field)
  125.                         localTiles.pop(i)
  126.                         globalTiles = localTiles + duplicates
  127.                         xCoordinate = xCoordinate + tile.getWidth()
  128.                         fieldRightWidth = fieldRightWidth -tile.getWidth()
  129.                         fieldRightHeight = tile.getHeight()
  130.                         fieldBelowHeight = fieldRightHeight - tile.getHeight()
  131.                         fieldBelowWidth = fieldRightWidth + tile.getWidth()
  132.                         print globalTiles
  133.                         return self.methods(globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight)
  134.            
  135.             #RECHTS      
  136.             else:
  137.                 #Width
  138.                 for i, tile in enumerate(localTiles):
  139.                     if fieldRightWidth == tile.getWidth():
  140.                         self.field.placeTile(tile, xCoordinate, yCoordinate)
  141.                         self.frame.repaint(self.field)
  142.                         localTiles.pop(i)
  143.                         globalTiles = localTiles + duplicates
  144.                         xCoordinate = xCoordinate + tile.getWidth()
  145.                         fieldRightWidth = fieldRightWidth -tile.getWidth()
  146.                         fieldRightHeight = tile.getHeight()
  147.                         fieldBelowHeight = fieldRightHeight - tile.getHeight()
  148.                         fieldBelowWidth = fieldRightWidth + tile.getWidth()
  149.                         print "RightWidth", tile.getID()
  150.                         return self.methods(globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight)
  151.                
  152.                     if fieldRightWidth == tile.getHeight():
  153.                         tile.flip()
  154.                         self.field.placeTile(tile, xCoordinate, yCoordinate)
  155.                         self.frame.repaint(self.field)
  156.                         localTiles.pop(i)
  157.                         globalTiles = localTiles + duplicates
  158.                         xCoordinate = xCoordinate + tile.getWidth()
  159.                         fieldRightWidth = fieldRightWidth -tile.getWidth()
  160.                         fieldRightHeight = tile.getHeight()
  161.                         fieldBelowHeight = fieldRightHeight - tile.getHeight()
  162.                         fieldBelowWidth = fieldRightWidth + tile.getWidth()
  163.                         print "RightWidth", tile.getID()
  164.                         return self.methods(globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight)
  165.                 #Height
  166.                 for i, tile in enumerate(localTiles):
  167.                     if fieldRightHeight == tile.getHeight():
  168.                         self.field.placeTile(tile, xCoordinate, yCoordinate)
  169.                         self.frame.repaint(self.field)
  170.                         localTiles.pop(i)
  171.                         globalTiles = localTiles + duplicates
  172.                         xCoordinate = xCoordinate + tile.getWidth()
  173.                         fieldRightWidth = fieldRightWidth -tile.getWidth()
  174.                         fieldRightHeight = tile.getHeight()
  175.                         fieldBelowHeight = fieldRightHeight - tile.getHeight()
  176.                         fieldBelowWidth = fieldRightWidth + tile.getWidth()
  177.                         print "fieldRightWidth", fieldRightWidth
  178.                         print "fieldRightHeight", fieldRightHeight
  179.                         print "RightHeight", tile.getID()
  180.                         return self.methods(globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight)
  181.                    
  182.                     if fieldRightHeight == tile.getWidth():
  183.                         tile.flip()
  184.                         self.field.placeTile(tile, xCoordinate, yCoordinate)
  185.                         self.frame.repaint(self.field)
  186.                         localTiles.pop(i)
  187.                         globalTiles = localTiles + duplicates
  188.                         xCoordinate = xCoordinate + tile.getWidth()
  189.                         fieldRightWidth = fieldRightWidth -tile.getWidth()
  190.                         fieldRightHeight = tile.getHeight()
  191.                         fieldBelowHeight = fieldRightHeight - tile.getHeight()
  192.                         fieldBelowWidth = fieldRightWidth + tile.getWidth()
  193.                         print "fieldRightWidth", fieldRightWidth
  194.                         print "fieldRightHeight", fieldRightHeight
  195.                         print "RightHeight", tile.getID()
  196.                         return self.methods(globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight)
  197.                 #Biggest
  198.                 for i, tile in enumerate(localTiles):
  199.                     self.field.placeTile(tile, xCoordinate, yCoordinate)
  200.                     self.frame.repaint(self.field)
  201.                     localTiles.pop(i)
  202.                     globalTiles = localTiles + duplicates
  203.                     xCoordinate = xCoordinate + tile.getWidth()
  204.                     fieldRightWidth = fieldRightWidth -tile.getWidth()
  205.                     fieldRightHeight = tile.getHeight()
  206.                     fieldBelowHeight = fieldRightHeight - tile.getHeight()
  207.                     fieldBelowWidth = fieldRightWidth + tile.getWidth()
  208.                     print "Biggest:", tile.getID()
  209.                     return self.methods(globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight)
  210.            
  211.        
  212.     def sortGlobal(self, globalTiles):
  213.         listID = []
  214.         local = []
  215.         localID =[]
  216.         for i, tile in enumerate(globalTiles):
  217.             listID.append(tile.getID())
  218.         listID.sort()
  219.        
  220.         for id in listID:
  221.             for i, tile in enumerate(globalTiles):
  222.                 if id == tile.getID():
  223.                      local.append(tile)
  224.                      localID.append(tile.getID())
  225.        
  226.         return local
  227.    
  228.     def checkForDuplicates(self,globalTiles):
  229.         localTiles = []
  230.         duplicates = []
  231.         localTilesID = []
  232.         duplicatesID = []
  233.         for i, tile in enumerate(globalTiles):
  234.             previous = globalTiles[i-1]
  235.            
  236.             if tile.compareToHeight(previous) == 0 and tile.compareToWidth(previous) == 0 and localTiles != []:
  237.                 duplicates.append(tile)
  238.                 duplicatesID.append(tile.getID())
  239.                
  240.             else:
  241.                 localTiles.append(tile)
  242.                 localTilesID.append(tile.getID())
  243.        
  244.        
  245.         return localTiles, duplicates
  246.    
  247. Solution15_0_0()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement