__**Was wir heute gemacht:**__ - neue Anfangskonfigurationen genereriert: jetzt können Bilder und Datein eingelesen in Modus 3 und 4 Game of life: import random import numpy as np import matplotlib.pyplot as plt from PIL import Image from math import floor, ceil import time # 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 start_time = time.time() anzahlNachbarn = BerechneAnzahlNachbarn(zustand) print('Berechne Nachbarn:', time.time() - start_time) start_time = time.time() ergebnis = BerechneNeuenZustand(zustand, anzahlNachbarn) print('Berechne neuen Zustand:', time.time() - start_time) return ergebnis def Wiederhole(t): for i in range(t): Schritt() def MachQuadrat(pixel): breite = pixel.shape[0] hoehe = pixel.shape[1] size = max(breite, hoehe) links = floor((size - breite) / 2) rechts = ceil((size - breite) / 2) oben = floor((size - hoehe) / 2) unten = ceil((size - hoehe) / 2) pad_width = ((links, rechts), (oben, unten)) pixel = np.pad(pixel, pad_width, mode='constant', constant_values=0) return pixel #t= int(input('Gib die Zeitschritte ein: ')) def Initialisiere(): global zustand, tem, zeilen, spalten modus = int(input('Gib den Modus 1, 2, 3 oder 4 ein: ')) if modus==1 or modus==2: spalten = zeilen = int(input('Gib die Anzahl der Zeilen/Spalten ein: ')) #zustand = np.zeros if modus==1: zustand = np.round(np.random.random((zeilen,spalten))).astype(int) elif modus==2: zustand=np.zeros((zeilen,spalten)) for i in range(0,zeilen): for j in range (0,spalten): if j%2==0: zustand[(i,j)]=0 else: zustand[(i,j)]=1 if zustand[(int(zeilen/2),int(spalten/2))]==0: zustand[(int(zeilen/2),int(spalten/2))]=1 zustand[(int(zeilen/2),int(spalten/2))]=1 else: zustand[(int(zeilen/2),int(spalten/2))]=0 zustand[(int((zeilen/2)+1),int(spalten/2))]=0 elif modus==3: DateiName = input('was willste oeffnen? ') Datei = open(DateiName) DateiAlsListe = [] for line in Datei: DateiAlsListe.append([int(c) for c in list(line.rstrip())]) DateiAlsMatrix = np.array(DateiAlsListe) zustand = MachQuadrat(DateiAlsMatrix) zeilen = spalten = zustand.shape[0] elif modus==4: dateiname = input('Gib den Bildpfad ein: ') bild = Image.open(dateiname) bild = bild.convert('1') bild.thumbnail((500,500)) pixel = np.array(bild) zustand = MachQuadrat(pixel) zeilen = spalten = zustand.shape[0] tem = Tripleeinheitsmatrix(zeilen, spalten) #Wiederholungen(t) Ausführungsdatei: import GameOfLife as gol import numpy as np from matplotlib import pyplot as plt from matplotlib import animation import random import time def animate(e): im.set_array(gol.zustand) gol.Schritt() return im, gol.Initialisiere() #fig = plt.figure(figsize=(1920,1080), dpi=1, frameon=False) fig = plt.figure() #fig position ändern plt.axis('off') #figure(num=None, figsize=(8, 6), dpi=80, facecolor='w', edgecolor='k') #plt.figure(figsize=(1,1)) im = plt.imshow(gol.zustand, animated=True, cmap=plt.get_cmap('gray')) anim = animation.FuncAnimation(fig, animate, interval=200) #blit ist boese # # fuer mint gruen cmap=YlGn plt.show()