User Tools

Site Tools


boards:knight_s_tour:python

First I'll show the basic algorithm, then I'll show a couple excerpts of code.

Algorithm

  1. Create Territories
  2. Add Borders
  3. Delete Territories
    1. Pick a random territory
    2. If it is an isthmus (i.e. would leave an island) skip it
    3. Delete the territory
  4. Check all territories can be reached
  5. Add continents
  6. create PNGs & XML

Code Details

Adding Chain Continents

  def addChainContinents(self,baseIDSet,value=1): #,length=2):
    '''
    # this function adds continent chains.
    '''
    chains = set()
    for base in baseIDSet:
      ggn = self.getGeoNeighbors(base)
      #print ggn
      for neighbor in ggn:
        chain = frozenset([neighbor, base])        
        chains.add(chain)
 
    print chains
    for chain in chains:
      name = "chain"
      members = set()
      for link in chain:
        name += "." + self.getTerritoryNameFromID(link)
        members.add(link)
      #print "adding",name
      self.addContinent(name,members,bonus=value)
  def getGeoNeighbors(self,territoryID):
    name = self.getTerritoryNameFromID(territoryID)
    (row,col) = name.split("_")     
    #print name,row,col
    ret = []
    for x in self.genGeoNeighborsRC(int(row),int(col)):
      ret.append(x)
    return ret
  def getGeoNeighborsRC(self,row,col):
    ret = []
    neighbor = self.getTerritoryIDFromName(self.getTerritoryName(row,col+1))
    if (neighbor != None):
      ret.append(neighbor)
 
    neighbor = self.getTerritoryIDFromName(self.getTerritoryName(row,col-1))
    if (neighbor != None):
      ret.append(neighbor)
 
    neighbor = self.getTerritoryIDFromName(self.getTerritoryName(row+1,col))
    if (neighbor != None):
      ret.append(neighbor)
 
    neighbor = self.getTerritoryIDFromName(self.getTerritoryName(row-1,col))
    if (neighbor != None):
      ret.append(neighbor)
 
    return ret
 

Creating the PNGs

  def createPNGs(self, filePath, rowHeight, colWidth, 
                          xOrigin, yOrigin, territoriesDeleted):
    '''
    Creates a PNG of a chessboard w/knights in dead squares..
    '''
    imBoard = Image.new("RGBA", ( self.cols*colWidth,self.rows*rowHeight), (0, 0, 0, 0))
    imFill = Image.new("RGB", ( self.cols*colWidth,self.rows*rowHeight), "white")
    imFog = Image.new("RGB", ( self.cols*colWidth,self.rows*rowHeight), "white")
 
    #print "create new RGB image:", self.cols*colWidth,self.rows*rowHeight
 
    # create fill image
    borderSize = 1; #borderSize = 3; #borders are actually twice this.
    for x in range(self.cols*colWidth):
      for y in range(self.rows*rowHeight):
        row = floor(y/rowHeight)
        col = floor(x/colWidth)
        #print x,y
        #print im.getpixel((x,y))
        #print "putpixel",x,y
        if ((row+col) % 2 == 0): #black square
          if (x - col*colWidth) >=  borderSize and (y - row*rowHeight) >= borderSize and ((col+1)*colWidth - x) >  borderSize and ((row+1)*rowHeight - y) > borderSize:
            imFill.putpixel((x,y), (1, 1, 1)) #off color centers
          else:
            imFill.putpixel((x, y), (0, 0, 0)) 
        else: #white square 
          if (x - col*colWidth) >= borderSize and (y - row*rowHeight) >= borderSize and ((col+1)*colWidth - x) >  borderSize and ((row+1)*rowHeight - y) > borderSize:
            imFill.putpixel((x,y), (254, 254, 254)) #off color centers
          else:
            imFill.putpixel((x, y), (255, 255, 255)) 
 
 
    print "pasting"
    #paste in the un/deleted territories to the Board Image
    knightBorder=0
    print "territoriesDeleted", territoriesDeleted
    for col in range(self.cols):
      for row in range(self.rows):
        print territoriesDeleted
        print [ (r,c) for (r,c) in territoriesDeleted]
        found = [(r,c) for (r,c) in territoriesDeleted if r == row if c == col]
        print "found",found
        if found:
          print "found hole!"
          if ((row+col) % 2 == 0): #black square
            tileImage = self.holeDarkImage     
          else:
            tileImage = self.holeLightImage
        else:
          if ((row+col) % 2 == 0): #black square
            tileImage = self.playerDarkImage
            #tileFImage = self.playerDarkFogImage
          else:
            tileImage = self.playerLightImage
            #tileFImage = self.playerLightFogImage
 
        px = col*colWidth + knightBorder
        py = row*rowHeight + knightBorder
        imBoard.paste(tileImage, (px, py))
 
 
 
    #fog now
    for col in range(self.cols):
      for row in range(self.rows):              
        if ((row+col) % 2 == 0): #black square
          tileImage = self.darkFogImage
        else:
          tileImage = self.lightFogImage
 
        px = col*colWidth
        py = row*rowHeight
 
        imFog.paste(tileImage, (px, py))    
 
 
    imBoard.save(filePath + "-BOARD.png")
    imFill.save(filePath + "-FILLFOG.png")
    imFog.save(filePath+ "-FOG.png")
boards/knight_s_tour/python.txt · Last modified: 2014/09/14 23:28 by Ozyman