Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ### Note that there are quite some mistakes in the code, some of which I am aware of, some which I am not.
- ### It should be an implemention of Conway's Game of Life, my "Iterator" is located at the end.
- import random
- import itertools
- class Game():
- ''' Play The Game of Life'''
- def __init__(self, num_x, num_y, rand):
- self.num_x = num_x # num_x/_y sind Höhe und Breite des Spielfeldes
- self.num_y = num_y
- self.rand = int((num_x*num_y)*rand/100) # rand soll die Prozentuelle Anzahl an lebendigen Zellen widerspiegeln
- # rand wird als volle Zahle (z.B 10), als Prozent interpretiert und als absoluten Anteil des gesamtanzahl der Zellen ausgegeben
- def coords_maker(self):
- '''Creates a number of strings. Their name corresponds to their position in a x/y system.'''
- self.num_x += 1 #fügt +1 zu den Argumenten hinzu, da range() ansonsten zu früh aufhört
- self.num_y += 1
- self.coords = {'x'+str(x)+'y'+str(y) : (x,y) for x,y, in itertools.product(range(self.num_x), range(self.num_y))}
- self.cells = dict.fromkeys(self.coords, '-')
- # self.cells nimmt die keys von coords, und setzt sie alle auf '-'
- # self. coords enthält die Koordinatennamen, e.g. x2y1 und die Koordinaten als value in einem Tuple, e.g. (2,1)
- def rand_value(self): #TODO: Den lebenden Zellen den Wert 1 hinzufügen
- '''Takes a random number form the list coords and assigns the value 1 to it'''
- count = 1 #zählt die Wiederholungen
- cells_keys = list(self.cells.keys()) # erstellt eine LISTE (!) der keys von self.coords
- aux = [i for i in range (0, len(cells_keys), 1)] # erstellt List die genauso groß ist wie Ausgangsliste
- while count <= self.rand: # nur so oft zuällig eine Zahl auswählen wie rand groß ist
- count += 1
- posit = random.randrange (0, len(aux), 1) #wählt eine Zahl
- index = aux [posit] #speichert die ausgewählte Zahl
- elem = cells_keys [index] # Gibt den key von self.cell aus der geändert werden soll
- self.cells [elem] = '*' # Setzt den Wert der lebenden Zelle auf 1
- del aux[posit] # löscht Wert aus aux um Wiederholungen zu vermeiden
- def neighbours(self):
- self.net = {}
- for coord in self.coords:
- position = self.coords[coord]
- x,y = position
- neighbours = (
- (x+1, y),#rechts
- (x+1, y+1),#rechts oben
- (x+1, y-1),#rechts unten
- (x-1, y),#links
- (x-1, y+1),#links oben
- (x-1, y-1),#links unten
- (x, y+1),#oben
- (x, y-1))#unten
- self.net [coord] = neighbours
- #[key for key, value in dic.items() if value == val][0]
- # TODO:
- # - Koordinatennamen für Werte in self.net nachschauen.
- # - Mit den Namen evaluieren ob lebendig oder tot
- # - wenn lebendig an einen neuen Tuple anhängen und evaluieren ob die Zentralzelle überlebt, stirbt, usw...
- class NewRound(Game):
- def __init__(self):
- Game.__init__(self, num_x, num_y, rand)
- def __iter__(self):
- def printer (self):
- for key, value in self.coords.items():
- char = self.cells[key] # 1 oder ' '
- x,y = value
- HORIZ=str(x)
- VERT=str(y)
- char = str(char)
- print('\033['+VERT+';'+HORIZ+'f'+char)
- printer()
- print ('\033['+num_x+';'+num_y+'f'+'\n'*10)
- return self
- def next(self):
- for coord in self.net.keys():
- neighbours = self.net [coord]
- living = []
- for tuple in neighbours:
- elem = [key for key, value in self.cells.items() if value == tuple][0]
- if self.cells [elem] == '*':
- living.append (elem)
- population = len (living)
- if self.cells [coord] == '-':
- if population == 3:
- self.cells [coord] = '*'
- else:
- if population < 2:
- self.cells [coord] = '-'
- elif population == 2 or 3:
- self.cells [coord] = '*'
- elif population > 3:
- self.cells [coord] = '-'
- printer()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement