Advertisement
Guest User

Game of Life

a guest
Jun 22nd, 2012
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.43 KB | None | 0 0
  1. ### Note that there are quite some mistakes in the code, some of which I am aware of, some which I am not.
  2. ### It should be an implemention of Conway's Game of Life, my "Iterator" is located at the end.
  3.  
  4.  
  5. import random
  6. import itertools
  7.            
  8. class Game():
  9.         ''' Play The Game of Life'''
  10.        
  11.         def __init__(self, num_x, num_y, rand):
  12.                 self.num_x = num_x  # num_x/_y sind Höhe und Breite des Spielfeldes
  13.                 self.num_y = num_y
  14.                 self.rand = int((num_x*num_y)*rand/100) # rand soll die Prozentuelle Anzahl an lebendigen Zellen widerspiegeln
  15.                 # rand wird als volle Zahle (z.B 10), als Prozent interpretiert und als absoluten Anteil des gesamtanzahl der Zellen ausgegeben
  16.                
  17.         def coords_maker(self):
  18.             '''Creates a number of strings. Their name corresponds to their position in a x/y system.'''
  19.                
  20.             self.num_x += 1 #fügt +1 zu den Argumenten hinzu, da range() ansonsten zu früh aufhört
  21.             self.num_y += 1
  22.             self.coords = {'x'+str(x)+'y'+str(y) : (x,y) for x,y, in itertools.product(range(self.num_x), range(self.num_y))}
  23.             self.cells = dict.fromkeys(self.coords, '-')
  24.                 # self.cells nimmt die keys von coords, und setzt sie alle auf '-'
  25.                 # self. coords enthält die Koordinatennamen, e.g. x2y1 und die Koordinaten als value in einem Tuple, e.g. (2,1)
  26.                
  27.         def rand_value(self): #TODO: Den lebenden Zellen den Wert 1 hinzufügen
  28.             '''Takes a random number form the list coords and assigns the value 1 to it'''
  29.            
  30.             count = 1 #zählt die Wiederholungen
  31.             cells_keys = list(self.cells.keys()) # erstellt eine LISTE (!) der keys von self.coords
  32.             aux = [i for i in range (0, len(cells_keys), 1)] # erstellt List die genauso groß ist wie Ausgangsliste
  33.            
  34.             while count <= self.rand: # nur so oft zuällig eine Zahl auswählen wie rand groß ist
  35.                 count += 1
  36.                 posit = random.randrange (0, len(aux), 1)   #wählt eine  Zahl
  37.                 index = aux [posit] #speichert die ausgewählte Zahl
  38.                 elem = cells_keys [index]   # Gibt den key von self.cell aus der geändert werden soll
  39.                 self.cells [elem] = '*' # Setzt den Wert der lebenden Zelle auf 1
  40.                 del aux[posit]  # löscht Wert aus aux um Wiederholungen zu vermeiden
  41.                          
  42.            
  43.         def neighbours(self):
  44.             self.net = {}
  45.             for coord in self.coords:
  46.                 position = self.coords[coord]
  47.                 x,y = position
  48.                 neighbours = (
  49.                 (x+1, y),#rechts
  50.                 (x+1, y+1),#rechts oben
  51.                 (x+1, y-1),#rechts unten
  52.                 (x-1, y),#links
  53.                 (x-1, y+1),#links oben
  54.                 (x-1, y-1),#links unten
  55.                 (x, y+1),#oben
  56.                 (x, y-1))#unten
  57.                 self.net [coord] = neighbours  
  58.                 #[key for key, value in dic.items() if value == val][0]
  59.  
  60. # TODO:
  61. # - Koordinatennamen für Werte in self.net nachschauen.
  62. # - Mit den Namen evaluieren ob lebendig oder tot
  63. # - wenn lebendig an einen neuen Tuple anhängen und evaluieren ob die Zentralzelle überlebt, stirbt, usw...
  64.                
  65.                                                
  66. class NewRound(Game):
  67.  
  68.     def __init__(self):
  69.         Game.__init__(self, num_x, num_y, rand)
  70.        
  71.        
  72.     def __iter__(self):
  73.         def printer (self):
  74.             for key, value in self.coords.items():  
  75.                 char = self.cells[key]  # 1 oder ' '
  76.                 x,y = value
  77.                 HORIZ=str(x)
  78.                 VERT=str(y)
  79.                 char = str(char)
  80.                 print('\033['+VERT+';'+HORIZ+'f'+char)  
  81.             printer()
  82.             print ('\033['+num_x+';'+num_y+'f'+'\n'*10)
  83.            
  84.         return self
  85.        
  86.     def next(self):
  87.            
  88.         for coord in self.net.keys():
  89.             neighbours = self.net [coord]
  90.             living = []
  91.             for tuple in neighbours:
  92.                 elem = [key for key, value in self.cells.items() if value == tuple][0]
  93.                 if self.cells [elem] == '*':
  94.                     living.append (elem)
  95.             population = len (living)
  96.                    
  97.             if self.cells [coord] == '-':
  98.                 if population == 3:
  99.                     self.cells [coord] = '*'
  100.             else:
  101.                 if population < 2:
  102.                     self.cells [coord] = '-'
  103.                 elif population == 2 or 3:
  104.                     self.cells [coord] = '*'
  105.                 elif population > 3:
  106.                     self.cells [coord] = '-'
  107.                            
  108.         printer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement