def CreateInitial(self): self.Log("\nLoad initial\n") #flush board del self.board.Polyominoes[:] self.board.LookUp = {} self.board = TT.Board(TT.BOARDHEIGHT, TT.BOARDWIDTH) bh = TT.BOARDHEIGHT bw = TT.BOARDWIDTH TT.GLUEFUNC = { 'N': 1, 'E': 1, 'S': 1, 'W': 1, } #initial #CreateTiles(board) colorb = "#000" colorl = "#fff" NumTiles = 10 for i in range(NumTiles): #bottom tiles #colorb = str(colorb[0]+chr(ord(colorb[1])+1)+colorb[2:]) colorb = "#" + str(hex(random.randint(0, 16))[2:]) + str( hex(random.randint(0, 16))[2:]) + str( hex(random.randint(0, 16))[2:]) if len(colorb) > 4: colorb = colorb[:4] p = TT.Polyomino( TT.Tile(chr(ord('A') + i), 0, bh - i - 2, ['N', 'E', 'S', 'W'], colorb)) self.board.Add(p) #left tiles #colorl = str(colorl[0]+chr(ord(colorl[1])-1)+colorl[2:]) colorl = "#" + str(hex(random.randint(0, 16))[2:]) + str( hex(random.randint(0, 16))[2:]) + str( hex(random.randint(0, 16))[2:]) if len(colorl) > 4: colorl = colorl[:4] p = TT.Polyomino( TT.Tile(chr(ord('a') + i), i + 1, bh - 1, ['S', 'W', 'N', 'E'], colorl)) self.board.Add(p) self.board.SetGrid() self.RedrawCanvas()
def getfile(self): fname = tkFileDialog.askopenfilename() #print "doing stuff with "+fname try: tree = ET.parse(fname) treeroot = tree.getroot() self.Log("\nLoad " + fname + "\n") gluefun = treeroot[0] #flush board del self.board.Polyominoes[:] self.board.LookUp = {} TT.GLUEFUNC = {} self.board = TT.Board(TT.BOARDHEIGHT, TT.BOARDWIDTH) for fun in gluefun: TT.GLUEFUNC[fun.find('Labels').attrib['L1']] = int( fun.find('Strength').text) for tt in treeroot[1:]: ntlocx = 0 ntlocy = 0 if tt[0].find('Location') != None: ntlocx = int(tt[0].find('Location').attrib['x']) ntlocy = int(tt[0].find('Location').attrib['y']) ntcol = "#555555" if tt[0].find('Color') != None: ntcol = "#" + tt[0].find('Color').text ntnort = " " if tt[0].find('NorthGlue') != None: ntnort = tt[0].find('NorthGlue').text nteast = " " if tt[0].find('EastGlue') != None: nteast = tt[0].find('EastGlue').text ntsouth = " " if tt[0].find('SouthGlue') != None: ntsouth = tt[0].find('SouthGlue').text ntwest = " " if tt[0].find('WestGlue') != None: ntwest = tt[0].find('WestGlue').text ntlab = "X" if tt[0].find('label') != None: ntlab = tt[0].find('label').text ntile = TT.Tile(ntlab, ntlocx, ntlocy, [ntnort, nteast, ntsouth, ntwest], ntcol) self.board.Add(TT.Polyomino(ntile)) self.board.SetGrid() self.RedrawCanvas() #board.GridDraw() #print TT.GLUEFUNC except: print "Problem with file: " + fname print sys.exc_info()[0]
def initialize(): global stuckSetFile global startSetFile global redEscSetFile #Clear log file file = open("Log/log.txt", "w") file.write("") stuckFile = open(stuckSetFile, "r") startFile = open(startSetFile, "r") redEscFile = open(redEscSetFile, "r") # for l in startFile.readlines(): # startingPositions.add(l[:8]) # for l in stuckFile.readlines(): # stuckPositions.add(l[:8]) for l in redEscFile.readlines(): redEscapedPositions.add(l[:4]) print "Number of starting positions: ", len(startingPositions) blueGlues = ["N", "N", "N", "N"] redGlues = ["S", "S", "S", "S"] bluePoly = TT.Polyomino(1, 0, 0, blueGlues, "#0000ff") redPoly = TT.Polyomino(0, board.Rows - 1, board.Cols - 1, redGlues, "#ff0000") board.Add(redPoly) board.Add(bluePoly) # Add the rest of the tiles to the blue square bluePoly.Tiles.append( TT.Tile(bluePoly, 1, 1, 0, blueGlues, "#0000ff", False)) bluePoly.Tiles.append( TT.Tile(bluePoly, 1, 0, 1, blueGlues, "#0000ff", False)) bluePoly.Tiles.append( TT.Tile(bluePoly, 1, 1, 1, blueGlues, "#0000ff", False))
def parseFile(filename): tree = ET.parse(filename) treeroot = tree.getroot() #self.Log("\nLoad "+filename+"\n") #default size of board, changes if new board size data is read from the file rows = 15 columns = 15 boardSizeExits = False glueFuncExists = False previewTilesExist = False tileDataExists = False CommandsExists = False #check if the xml attributes are found if tree.find("GlueFunction") != None: glueFuncExists = True if tree.find("PreviewTiles") != None: previewTilesExist = True if tree.find("BoardSize") != None: boardSizeExists = True if tree.find("TileData") != None: tileDataExists = True if tree.find("Commands") != None: CommandsExists = True #data set that will be passed back to tumblegui tile_set_data = {"glueFunc": {}, "prevTiles": [], "tileData": []} if boardSizeExists: rows = treeroot[0].attrib["height"] columns = treeroot[0].attrib["width"] #add glue function to the data set if glueFuncExists: glueFuncTree = treeroot[1] for fun in glueFuncTree: tile_set_data["glueFunc"][fun.find('Labels').attrib['L1']] = ( fun.find('Strength').text) #add preview tiles to the data set if previewTilesExist: prevTilesTree = treeroot[2] for prev in prevTilesTree: newPrevTile = {} newPrevTile["color"] = "#555555" newPrevTile["northGlue"] = " " newPrevTile["southGlue"] = " " newPrevTile["westGlue"] = " " newPrevTile["eastGlue"] = " " newPrevTile["label"] = "X" newPrevTile["concrete"] = " " if prev.find('Color') != None: if prev.find('Concrete').text == "True": newPrevTile["color"] = "#686868" else: newPrevTile["color"] = "#" + prev.find('Color').text if prev.find('NorthGlue') != None: newPrevTile["northGlue"] = prev.find('NorthGlue').text if prev.find('EastGlue') != None: newPrevTile["eastGlue"] = prev.find('EastGlue').text if prev.find('SouthGlue') != None: newPrevTile["southGlue"] = prev.find('SouthGlue').text if prev.find('WestGlue') != None: newPrevTile["westGlue"] = prev.find('WestGlue').text if prev.find('label') != None: newPrevTile["label"] = prev.find('label').text if prev.find('Concrete') != None: newPrevTile["concrete"] = prev.find('Concrete').text tile_set_data["prevTiles"].append(newPrevTile) #add tile data to the data set, these are the tiles that will actually be loaded onto the plane if tileDataExists: tileDataTree = treeroot[3] for tile in tileDataTree: newTile = {} newTile["location"] = {'x': 0, 'y': 0} newTile["color"] = "#555555" newTile["northGlue"] = " " newTile["southGlue"] = " " newTile["westGlue"] = " " newTile["eastGlue"] = " " newTile["label"] = "X" newTile["concrete"] = " " if tile.find('Location') != None: newTile["location"]["x"] = int( tile.find('Location').attrib['x']) newTile["location"]["y"] = int( tile.find('Location').attrib['y']) if tile.find('Color') != None: if tile.find('Concrete').text == "True": newTile["color"] = "#686868" else: newTile["color"] = "#" + tile.find('Color').text if tile.find('NorthGlue') != None: newTile["northGlue"] = tile.find('NorthGlue').text if tile.find('EastGlue') != None: newTile["eastGlue"] = tile.find('EastGlue').text if tile.find('SouthGlue') != None: newTile["southGlue"] = tile.find('SouthGlue').text if tile.find('WestGlue') != None: newTile["westGlue"] = tile.find('WestGlue').text if tile.find('label') != None: newTile["label"] = tile.find('label').text if tile.find('Concrete') != None: newTile["concrete"] = tile.find('Concrete').text tile_set_data["tileData"].append(newTile) board = TT.Board(int(rows), int(columns)) glueFunc = tile_set_data["glueFunc"] prevTiles = tile_set_data["prevTiles"] prevTileList = [] for tile in tile_set_data["tileData"]: if tile["concrete"] != "True": glues = [ tile["northGlue"], tile["eastGlue"], tile["southGlue"], tile["westGlue"] ] board.Add( TT.Polyomino(0, tile["location"]["x"], tile["location"]["y"], glues, tile["color"])) else: glues = [] board.AddConc( TT.Tile(None, 0, tile["location"]["x"], tile["location"]["y"], glues, tile["color"], "True")) for prevTile in prevTiles: prevGlues = [ prevTile["northGlue"], prevTile["eastGlue"], prevTile["southGlue"], prevTile["westGlue"] ] prevTileList.append( TT.Tile(None, 0, 0, 0, prevGlues, prevTile["color"], prevTile["concrete"])) commands = [] if CommandsExists: listOfCommands = treeroot[4] print(listOfCommands) for c in listOfCommands: print(c) print("NAME: ", c.attrib["name"], " FILENAME: ", c.attrib["filename"]) commands.append((c.attrib["name"], c.attrib["filename"])) data = [board, glueFunc, prevTileList, commands] return data