Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import copy
- os.chdir("../Configurations")
- from tilingObjects.Tile import Tile
- from tiling.FieldFrame import FieldFrame
- from tiling.Field import Field
- import time
- class Solution15_0_0(object):
- CONFIGURATION = "15-0-0.txt"
- def __init__(self):
- i = 1
- for idx, each_line in enumerate(open(self.CONFIGURATION, 'r')):
- if idx == 0:
- width, height, scale = map(int, each_line.strip().split("\t"))
- self.field = Field(width, height)
- self.frame = FieldFrame(self.field, scale)
- else:
- frequency, width, height = map(int, each_line.strip().split("\t"))
- for idx in range(frequency):
- self.field.addTile(Tile(i, width, height))
- i += 1
- globalTiles = self.field.getTiles()
- #localTiles, duplicates = self.checkForDuplicates(globalTiles)
- xCoordinate = 0
- yCoordinate = 0
- fieldRightWidth = self.field.getWidth()
- fieldRightHeight = self.field.getHeight()
- fieldBelowWidth = self.field.getWidth()
- fieldBelowHeight = self.field.getHeight()
- print "global", globalTiles
- #print "local", localTiles
- self.methods(globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight)
- if self.field.solved(): print "SOLVED"
- self.frame.root.mainloop()
- def methods(self, globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight):
- globalTiles = self.sortGlobal(globalTiles)
- localTiles, duplicates = self.checkForDuplicates(globalTiles)
- print "Start methods"
- time.sleep(0.5)
- if globalTiles == []:
- self.field.solved()
- print "SOLVED"
- else:
- if fieldRightWidth == 0:
- if fieldBelowHeight == 0:
- print "test1"
- #BELOW
- else:
- #Width
- print "Aloha"
- for i, tile in enumerate(localTiles):
- if fieldBelowWidth == tile.getWidth():
- self.field.placeTile(tile, xCoordinate, yCoordinate)
- self.frame.repaint(self.field)
- localTiles.pop(i)
- globalTiles = localTiles + duplicates
- xCoordinate = xCoordinate + tile.getWidth()
- fieldRightWidth = fieldRightWidth -tile.getWidth()
- fieldRightHeight = tile.getHeight()
- fieldBelowHeight = fieldRightHeight - tile.getHeight()
- fieldBelowWidth = fieldRightWidth + tile.getWidth()
- return self.methods(globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight)
- if fieldBelowWidth == tile.getHeight():
- tile.flip()
- self.field.placeTile(tile, xCoordinate, yCoordinate)
- self.frame.repaint(self.field)
- localTiles.pop(i)
- globalTiles = localTiles + duplicates
- xCoordinate = xCoordinate + tile.getWidth()
- fieldRightWidth = fieldRightWidth -tile.getWidth()
- fieldRightHeight = tile.getHeight()
- fieldBelowHeight = fieldRightHeight - tile.getHeight()
- fieldBelowWidth = fieldRightWidth + tile.getWidth()
- return self.methods(globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight)
- #Height
- for i, tile in enumerate(localTiles):
- if fieldBelowHeight == tile.getHeight():
- self.field.placeTile(tile, xCoordinate, yCoordinate)
- self.frame.repaint(self.field)
- localTiles.pop(i)
- globalTiles = localTiles + duplicates
- xCoordinate = xCoordinate + tile.getWidth()
- fieldRightWidth = fieldRightWidth -tile.getWidth()
- fieldRightHeight = tile.getHeight()
- fieldBelowHeight = fieldRightHeight - tile.getHeight()
- fieldBelowWidth = fieldRightWidth + tile.getWidth()
- print globalTiles
- return self.methods(globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight)
- if fieldBelowHeight == tile.getWidth():
- self.field.placeTile(tile, xCoordinate, yCoordinate)
- self.frame.repaint(self.field)
- localTiles.pop(i)
- globalTiles = localTiles + duplicates
- xCoordinate = xCoordinate + tile.getWidth()
- fieldRightWidth = fieldRightWidth -tile.getWidth()
- fieldRightHeight = tile.getHeight()
- fieldBelowHeight = fieldRightHeight - tile.getHeight()
- fieldBelowWidth = fieldRightWidth + tile.getWidth()
- print globalTiles
- return self.methods(globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight)
- #biggest
- for i, tile in enumerate(localTiles):
- self.field.placeTile(tile, xCoordinate, yCoordinate)
- self.frame.repaint(self.field)
- localTiles.pop(i)
- globalTiles = localTiles + duplicates
- xCoordinate = xCoordinate + tile.getWidth()
- fieldRightWidth = fieldRightWidth -tile.getWidth()
- fieldRightHeight = tile.getHeight()
- fieldBelowHeight = fieldRightHeight - tile.getHeight()
- fieldBelowWidth = fieldRightWidth + tile.getWidth()
- print globalTiles
- return self.methods(globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight)
- #RECHTS
- else:
- #Width
- for i, tile in enumerate(localTiles):
- if fieldRightWidth == tile.getWidth():
- self.field.placeTile(tile, xCoordinate, yCoordinate)
- self.frame.repaint(self.field)
- localTiles.pop(i)
- globalTiles = localTiles + duplicates
- xCoordinate = xCoordinate + tile.getWidth()
- fieldRightWidth = fieldRightWidth -tile.getWidth()
- fieldRightHeight = tile.getHeight()
- fieldBelowHeight = fieldRightHeight - tile.getHeight()
- fieldBelowWidth = fieldRightWidth + tile.getWidth()
- print "RightWidth", tile.getID()
- return self.methods(globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight)
- if fieldRightWidth == tile.getHeight():
- tile.flip()
- self.field.placeTile(tile, xCoordinate, yCoordinate)
- self.frame.repaint(self.field)
- localTiles.pop(i)
- globalTiles = localTiles + duplicates
- xCoordinate = xCoordinate + tile.getWidth()
- fieldRightWidth = fieldRightWidth -tile.getWidth()
- fieldRightHeight = tile.getHeight()
- fieldBelowHeight = fieldRightHeight - tile.getHeight()
- fieldBelowWidth = fieldRightWidth + tile.getWidth()
- print "RightWidth", tile.getID()
- return self.methods(globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight)
- #Height
- for i, tile in enumerate(localTiles):
- if fieldRightHeight == tile.getHeight():
- self.field.placeTile(tile, xCoordinate, yCoordinate)
- self.frame.repaint(self.field)
- localTiles.pop(i)
- globalTiles = localTiles + duplicates
- xCoordinate = xCoordinate + tile.getWidth()
- fieldRightWidth = fieldRightWidth -tile.getWidth()
- fieldRightHeight = tile.getHeight()
- fieldBelowHeight = fieldRightHeight - tile.getHeight()
- fieldBelowWidth = fieldRightWidth + tile.getWidth()
- print "fieldRightWidth", fieldRightWidth
- print "fieldRightHeight", fieldRightHeight
- print "RightHeight", tile.getID()
- return self.methods(globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight)
- if fieldRightHeight == tile.getWidth():
- tile.flip()
- self.field.placeTile(tile, xCoordinate, yCoordinate)
- self.frame.repaint(self.field)
- localTiles.pop(i)
- globalTiles = localTiles + duplicates
- xCoordinate = xCoordinate + tile.getWidth()
- fieldRightWidth = fieldRightWidth -tile.getWidth()
- fieldRightHeight = tile.getHeight()
- fieldBelowHeight = fieldRightHeight - tile.getHeight()
- fieldBelowWidth = fieldRightWidth + tile.getWidth()
- print "fieldRightWidth", fieldRightWidth
- print "fieldRightHeight", fieldRightHeight
- print "RightHeight", tile.getID()
- return self.methods(globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight)
- #Biggest
- for i, tile in enumerate(localTiles):
- self.field.placeTile(tile, xCoordinate, yCoordinate)
- self.frame.repaint(self.field)
- localTiles.pop(i)
- globalTiles = localTiles + duplicates
- xCoordinate = xCoordinate + tile.getWidth()
- fieldRightWidth = fieldRightWidth -tile.getWidth()
- fieldRightHeight = tile.getHeight()
- fieldBelowHeight = fieldRightHeight - tile.getHeight()
- fieldBelowWidth = fieldRightWidth + tile.getWidth()
- print "Biggest:", tile.getID()
- return self.methods(globalTiles, xCoordinate, yCoordinate, fieldRightWidth, fieldRightHeight, fieldBelowWidth, fieldBelowHeight)
- def sortGlobal(self, globalTiles):
- listID = []
- local = []
- localID =[]
- for i, tile in enumerate(globalTiles):
- listID.append(tile.getID())
- listID.sort()
- for id in listID:
- for i, tile in enumerate(globalTiles):
- if id == tile.getID():
- local.append(tile)
- localID.append(tile.getID())
- return local
- def checkForDuplicates(self,globalTiles):
- localTiles = []
- duplicates = []
- localTilesID = []
- duplicatesID = []
- for i, tile in enumerate(globalTiles):
- previous = globalTiles[i-1]
- if tile.compareToHeight(previous) == 0 and tile.compareToWidth(previous) == 0 and localTiles != []:
- duplicates.append(tile)
- duplicatesID.append(tile.getID())
- else:
- localTiles.append(tile)
- localTilesID.append(tile.getID())
- return localTiles, duplicates
- Solution15_0_0()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement