Created
May 10, 2017 14:35
-
-
Save ashik94vc/921506cd75bc57f1173cdde005f0ca62 to your computer and use it in GitHub Desktop.
Suguru Tile Generator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
import Tkinter | |
root = Tkinter.Tk() | |
class Block: | |
def __init__(self,index): | |
self.index = index | |
self.shapeNo = 0 | |
def isEmpty(self): | |
if(self.shapeNo > 0): | |
return False | |
else: | |
return True | |
def getShapeNum(self): | |
return self.shapeNo | |
def __repr__(self): | |
return "Block:{id:"+str(self.index)+",shape:"+str(self.shapeNo)+"}" | |
class Suguru: | |
def __init__(self,size): | |
self.blocks = [Block(index) for index in xrange(size*size)] | |
self.size = size | |
self.neighbors = [] | |
for block in self.blocks: | |
localNeighbors = [] | |
if block.index % size != size-1: | |
localNeighbors.append(block.index+1) | |
else: | |
localNeighbors.append(-1) | |
if block.index / size != size-1: | |
localNeighbors.append(block.index+size) | |
else: | |
localNeighbors.append(-1) | |
if block.index % size != 0: | |
localNeighbors.append(block.index-1) | |
else: | |
localNeighbors.append(-1) | |
if block.index / 7 != 0: | |
localNeighbors.append(block.index - size) | |
else: | |
localNeighbors.append(-1) | |
self.blocks[block.index].neighbors = localNeighbors | |
self.neighbors.append(localNeighbors) | |
def getNeighbors(self,index): | |
return self.neighbors[index] | |
def getShapeOfNeighbors(self,index): | |
return [self.blocks[id].shapeNo for id in self.neighbors[index] if id != -1] | |
def getCountForShapes(self,shape): | |
return len([block.shapeNo for block in self.blocks if block.shapeNo == shape]) | |
def hasEmptyBlocks(self): | |
for block in self.blocks: | |
if(block.isEmpty()): | |
return True | |
return False | |
def emptyBlockCount(self): | |
count = 0 | |
for block in self.blocks: | |
print block.shapeNo | |
if(block.isEmpty): | |
count += 1 | |
return count | |
def generateUI(self): | |
colors = ["white","red","green","blue","cyan","magenta","yellow"] | |
for row in xrange(self.size): | |
for column in xrange(self.size): | |
index = row*self.size + column | |
shape = self.blocks[index].shapeNo | |
Tkinter.Label(root,bg=colors[shape],text=index,borderwidth=1,width=2).grid(row=row,column=column) | |
def __repr__(self): | |
string = [] | |
for block in self.blocks: | |
string.append("{ index:"+str(block.index)+", shape:"+str(block.shapeNo)+", neighbors:"+str(self.neighbors[block.index])+" }") | |
print string | |
def getWeightedShape(suguru,shapes): | |
shapeWeights = [] | |
maxvalue = suguru.size * suguru.size * 2 | |
for shape in shapes: | |
if shape > 0: | |
shapeWeights.append(suguru.getCountForShapes(shape)) | |
else: | |
shapeWeights.append(maxvalue) | |
minvalue = min(shapeWeights) | |
if(minvalue == maxvalue): | |
return 0 | |
else: | |
shapeIndices = [x for x,shapeWeight in enumerate(shapeWeights) if shapeWeight == minvalue ] | |
finalShapes = [x for i,x in enumerate(shapes) if i in shapeIndices ] | |
choice = random.choice(finalShapes) | |
print str(choice) + " chosen from "+ str(finalShapes) | |
return choice | |
def generateShapes(size,shapes): | |
suguru = Suguru(size) | |
initShapes = random.sample(range(0,(size*size)-1),shapes) | |
shapeCount = 1 | |
for i in initShapes: | |
suguru.blocks[i].shapeNo = shapeCount | |
shapeCount += 1 | |
index = 0 | |
while suguru.hasEmptyBlocks(): | |
if(suguru.blocks[index].isEmpty()): | |
suguru.blocks[index].shapeNo = getWeightedShape(suguru,suguru.getShapeOfNeighbors(index)) | |
print str(suguru.blocks[index].shapeNo) + " is assigned for " + str(index) + " from " + str(suguru.getShapeOfNeighbors(index)) + " obtained from its neighbors " + str(suguru.getNeighbors(index)) | |
if(index == (size*size)-1): | |
index = 0 | |
else: | |
index += 1 | |
suguru.generateUI() | |
raw_input() | |
generateShapes(6,4) | |
root.mainloop( ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment