User Tools

Site Tools


boards:knight_s_tour:python

This is an old revision of the document!


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
 
boards/knight_s_tour/python.1410751644.txt.gz · Last modified: 2014/09/14 23:27 by Ozyman