Dies ist eine alte Version des Dokuments!
code am anfang:(funktioniert)
import GameOfLife as gol import numpy as np from matplotlib import pyplot as plt from matplotlib import animation import random def animate (e): im.set_array(gol.Schritt()) return im, N=50 e=np.random.random((N,N)) print(e) fig = plt.figure() im=plt.imshow(e, animated=True) anim = animation.FuncAnimation(fig, animate, frames=200, interval=100, blit=True) #blit ist ueberschreiben statt alles neu zu machen # plt.show()
import random import numpy as np import matplotlib.pyplot as plt # Größe der Matrix # Die Matrix wird zufällig mit Nullen und Einsen gefüllt def Matrix_füllen(M,zeilen,spalten): for i in range(0, zeilen): for j in range(0, spalten): if random.random() < 0.5: M[(i,j)]= 1 else: M[(i,j)]= 0 return M def Tripleeinheitsmatrix(D,zeilen,spalten): for i in range(0, zeilen): for j in range(0, spalten): if i==j: D[(i,j)] = 1 else: D[(i,j)] = 0 for i in range(0, zeilen): for j in range(0, spalten): if i==(j+1): D[(i,j)] = 1 for i in range(0, zeilen): for j in range(0, spalten): if j==(i+1): D[(i,j)] = 1 D[0,D.shape[1]-1] = 1 D[D.shape[1]-1,0] = 1 return D def Generationen(Matrix_füllen,Tripleeinheitsmatrix): def Matrizenmultiplikation(D,M): z = np.dot(D,M) k = np.dot(z,D) wandelmatrix = k- M return wandelmatrix wandelmatrix= Matrizenmultiplikation(D,M) Matrizenmultiplikation(D,M) #print (wandelmatrix) #Ursprungszelle Tod + genau 3 lebende Nachbarn = lebend #Ursprungszelle Lebend + genau 1 lebenden Nachbarn = tot #Ursprungszelle Lebend + 2 o. 3 Nachbarn = lebend #Ursprungszelle lebend + 4 oder mehr Nachbarn = tot def GameofLife(M,wandelmatrix): for i in range(0, zeilen): for j in range(0, spalten): if M[(i,j)] == 1 and (wandelmatrix[(i,j)]==2 or wandelmatrix[(i,j)]==3) : M[(i,j)]=1 elif M[(i,j)] == 1 and (wandelmatrix[(i,j)]==1 or wandelmatrix[(i,j)] in range(4,9)) : M[(i,j)]=0 elif M[(i,j)] == 0 and wandelmatrix[(i,j)]==3: M[(i,j)]=1 else: M[(i,j)]=0 return M return GameofLife(M,wandelmatrix) #print(M) #plt.imshow(M) #plt.show() def Wiederholungen(t): for i in range(t): Generationen(Matrix_füllen,Tripleeinheitsmatrix) def Schritt(): return Generationen(Matrix_füllen,Tripleeinheitsmatrix) t= int(input('Gib die Zeitschritte ein: ')) zeilen = int(input('Gib die Anzahl der Zeilen/Spalten ein: ')) spalten = zeilen M = np.zeros ((zeilen,spalten)) D = np.zeros ((zeilen,spalten)) wandelmatrix = np.zeros ((zeilen,spalten)) Matrix_füllen(M,zeilen,spalten) Tripleeinheitsmatrix(D,zeilen,spalten) Generationen(Matrix_füllen,Tripleeinheitsmatrix) #Wiederholungen(t)
import random import numpy as np import matplotlib.pyplot as plt # Größe der Matrix # Die Matrix wird zufällig mit Nullen und Einsen gefüllt def Tripleeinheitsmatrix(zeilen,spalten): tem = np.zeros ((zeilen,spalten)) for i in range(0, zeilen): for j in range(0, spalten): if i==j: tem[(i,j)] = 1 else: tem[(i,j)] = 0 for i in range(0, zeilen): for j in range(0, spalten): if i==(j+1): tem[(i,j)] = 1 for i in range(0, zeilen): for j in range(0, spalten): if j==(i+1): tem[(i,j)] = 1 tem[0,tem.shape[1]-1] = 1 tem[tem.shape[1]-1,0] = 1 return tem def BerechneAnzahlNachbarn(zustand): z = np.dot(tem, zustand) k = np.dot(z, tem) anzahlNachbarn = k - zustand return anzahlNachbarn def BerechneNeuenZustand(zustand, anzahlNachbarn): #Ursprungszelle Tod + genau 3 lebende Nachbarn = lebend #Ursprungszelle Lebend + genau 1 lebenden Nachbarn = tot #Ursprungszelle Lebend + 2 o. 3 Nachbarn = lebend #Ursprungszelle lebend + 4 oder mehr Nachbarn = tot for i in range(0, zeilen): for j in range(0, spalten): if zustand[(i,j)] == 1 and (anzahlNachbarn[(i,j)] in range(2) or anzahlNachbarn[(i,j)] in range(4,9)) : zustand[(i,j)]=0 elif zustand[(i,j)] == 0 and anzahlNachbarn[(i,j)]==3: zustand[(i,j)]=1 return zustand def Schritt(): global zustand anzahlNachbarn = BerechneAnzahlNachbarn(zustand) return BerechneNeuenZustand(zustand, anzahlNachbarn) def Wiederhole(t): for i in range(t): Schritt() #t= int(input('Gib die Zeitschritte ein: ')) def Initialisiere(z, s): global zustand, tem, zeilen, spalten zeilen = z spalten = s zustand = np.round(np.random.random((zeilen,spalten))).astype(int) tem = Tripleeinheitsmatrix(zeilen,spalten) #Wiederholungen(t)
import GameOfLife as gol import numpy as np from matplotlib import pyplot as plt from matplotlib import animation import random def animate (e): im.set_array(gol.Schritt()) return im, zeilen = int(input('Gib die Anzahl der Zeilen/Spalten ein: ')) spalten = zeilen gol.Initialisiere(zeilen, spalten) fig = plt.figure() im = plt.imshow(gol.zustand, animated=True, cmap=plt.get_cmap('PiYG_r')) anim = animation.FuncAnimation(fig, animate, frames=200, interval=100) #blit ist boese # # fuer mint gruen cmap=YlGn_r plt.show()