Advertisement
mixster

Untitled

Jul 15th, 2011
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.11 KB | None | 0 0
  1. #!/usr/bin/python
  2. import sys, os, math#, pygame
  3. #from pygame.locals import *
  4.  
  5. running = True
  6. fps = 20
  7.  
  8. class GeneType:
  9.   BLD = 1
  10.   TRM = 2
  11.   SPR = 3
  12.   VAL = [BLD, TRM, SPR]
  13.  
  14.   def isValid(self, typ):
  15.     for t in self.VAL:
  16.       if (t == typ):
  17.     return True
  18.     return False
  19.  
  20. class GenePool(object):
  21.   def __init__(self):
  22.     self.nuid = 0
  23.     self.genes = []
  24.     self.mutations = {}
  25.     self.terminal = self.addGene(GeneType.TRM, {})
  26.     self.base = self.addGene(GeneType.BLD, {})
  27.  
  28.   def addGene(self, typ, neigh):
  29.     i = self.nuid
  30.     self.nuid += 1
  31.     self.genes.append(Gene(typ, i, neigh, self))
  32.     self.mutations[i] = {}
  33.     for g in self.mutations.keys():
  34.       self.mutations[g][i] = 1
  35.       self.mutations[i][g] = 1
  36.     return i
  37.  
  38.   def isGeneValid(self, gene):
  39.     for g in self.genes:
  40.       if g.uid == gene.uid:
  41.     return True
  42.     return False
  43.  
  44.   def validateNeigh(self, neigh):
  45.     n = {}
  46.     for d in Dir.VAL:
  47.       n[d] = self.terminal
  48.     for (k, v) in neigh.items():
  49.       if not k in n:
  50.     print 'Neighbour has invalid direction: ' + str(k) +'; ignoring'
  51.       else:
  52.     n[k] = v
  53.     return n
  54.  
  55. class Dir:
  56.   UP = 1
  57.   RI = 2
  58.   DO = 3
  59.   LE = 4
  60.   VAL = [UP, RI, DO, LE]
  61.  
  62.   def isValid(self, di):
  63.     for d in self.VAL:
  64.       if d == di:
  65.     return True
  66.     return False
  67.  
  68.   def applyDir(self, d, p):
  69.     if d == self.UP:
  70.       return (p[0], p[1] - 1)
  71.     if d == self.RI:
  72.       return (p[0] + 1, p[1])
  73.     if d == self.DO:
  74.       return (p[0], p[1] + 1)
  75.     if d == self.LE:
  76.       return (p[0] - 1, p[1])
  77.  
  78. class Gene(object):
  79.   def __init__(self, typ, uid, neigh, pool):
  80.     if GeneType().isValid(typ):
  81.       self.typ = typ
  82.     else:
  83.       print 'Invalid gene type:', typ, '; Setting to terminal'
  84.       self.typ = GeneType.TRM
  85.     self.uid = uid
  86.     if self.typ == GeneType.TRM:
  87.       self.nbr = {}
  88.     else:
  89.       self.nbr = pool.validateNeigh(neigh)
  90.  
  91. class Creature(object):
  92.   def __init__(self, origin = (0, 0), gp = None):
  93.     if gp == None:
  94.       self.genepool = GenePool()
  95.     else:
  96.       self.genepool = gp
  97.     self.body = {}
  98.     self.pos = origin
  99.  
  100.   def build(self):
  101.     queue = []
  102.     queue.append((self.genepool.genes[self.genepool.base], self.pos))   # Gotta start somewhere
  103.     while len(queue) > 0:   # While there's still building to do
  104.       g = queue.pop(0)   # Get the oldest gene
  105.       if not g[0].typ == GeneType.TRM:   # If it isn't a terminal type
  106.     if not g[1][1] in self.body:   # If it's on a new row
  107.       self.body[g[1][1]] = {}   # Add new dict for the row
  108.     if not g[1][0] in self.body[g[1][1]]:   # If the row and column are unique, ie the position hasn't been assigned to another gene already
  109.       self.body[g[1][1]][g[1][0]] = g[0]   # Set the row, column to this gene
  110.       for (d, n) in g[0].nbr.items():   # Go through all of this gene's neighbours
  111.         queue.append((self.genepool.genes[n], Dir().applyDir(d, g[1])))   # And add them with the appropriate adjustment to the position
  112.  
  113.   def buildLimit(self, lim):
  114.     queue = []
  115.     queue.append((self.genepool.genes[self.genepool.base], self.pos))   # Gotta start somewhere
  116.     while (len(queue) > 0) and (lim > 0):   # While there's still building to do and we haven't reached the limit
  117.       g = queue.pop(0)   # Get the oldest gene
  118.       if not g[0].typ == GeneType.TRM:   # If it isn't a terminal type
  119.     if not g[1][1] in self.body:   # If it's on a new row
  120.       self.body[g[1][1]] = {}   # Add new dict for the row
  121.     if not g[1][0] in self.body[g[1][1]]:   # If the row and column are unique, ie the position hasn't been assigned to another gene already
  122.       self.body[g[1][1]][g[1][0]] = g[0]   # Set the row, column to this gene
  123.       lim -= 1   # Lower the limit since we've expanded
  124.       for (d, n) in g[0].nbr.items():   # Go through all of this gene's neighbours
  125.         queue.append((self.genepool.genes[n], Dir().applyDir(d, g[1])))   # And add them with the appropriate adjustment to the position
  126.  
  127.  
  128.   def printBody(self):
  129.     x1, y1, x2, y2 = 0, 0, 0, 0
  130.     for y in self.body.keys():
  131.       if y < y1:
  132.     y1 = y
  133.       elif y > y2:
  134.     y2 = y
  135.       for x in self.body[y].keys():
  136.     if x < x1:
  137.       x1 = x
  138.     elif x > x2:
  139.       x2 = x
  140.  
  141.     x2 -= x1 - 1
  142.     y2 -= y1 - 1
  143.  
  144.     p = []
  145.     for y in xrange(y2):
  146.       p.append([])
  147.       for x in xrange(x2):
  148.     p[y].append(' ')
  149.  
  150.     for y in self.body.keys():
  151.       for x in self.body[y].keys():
  152.     p[y - y1][x - x1] = 'X'
  153.  
  154.     for l in p:
  155.       print ''.join(l)
  156.  
  157. """
  158. pygame.init()
  159. size = (640, 480)
  160. screen = pygame.display.set_mode(size)
  161. pygame.display.set_caption('Patterning')
  162. pygame.mouse.set_visible(0)
  163. clock = pygame.time.Clock()
  164. """
  165.  
  166. print 'Starting'
  167.  
  168. while running:
  169.   running = False
  170.  
  171. p = GenePool()
  172. u = p.addGene(GeneType.BLD, {})
  173. l = p.addGene(GeneType.BLD, {})
  174. m = p.addGene(GeneType.BLD, {})
  175. n = p.addGene(GeneType.BLD, {})
  176. b = p.addGene(GeneType.BLD, {})
  177.  
  178. p.genes[p.base].nbr[Dir.DO] = b
  179. p.genes[u].nbr[Dir.DO] = n
  180. p.genes[n].nbr[Dir.DO] = b
  181. p.genes[b].nbr[Dir.RI] = l
  182. p.genes[b].nbr[Dir.LE] = l
  183. p.genes[l].nbr[Dir.RI] = u
  184. p.genes[l].nbr[Dir.LE] = u
  185.  
  186. c = Creature((0, 0), p)
  187. c.buildLimit(188)
  188. c.printBody()
  189. print 'Terminating'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement