Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
ws1920:siebter_gruppentermin_16.01.20 [2020/01/16 17:35] richard.wonneberger |
ws1920:siebter_gruppentermin_16.01.20 [2020/01/23 15:01] (aktuell) hannariegel |
||
---|---|---|---|
Zeile 8: | Zeile 8: | ||
+ | [[versuch fuer naechstes mal]] | ||
+ | |||
+ | Ausführungsdatei: | ||
+ | <code> | ||
+ | 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: ')) | ||
+ | modus = int(input('Gib den Modus 1 oder 2 ein: ')) | ||
+ | spalten = zeilen | ||
+ | |||
+ | gol.Initialisiere(zeilen, spalten, modus) | ||
+ | |||
+ | fig = plt.figure(figsize=(1920,1080), dpi=1, frameon=False) | ||
+ | |||
+ | |||
+ | |||
+ | #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, frames=200, interval=100) | ||
+ | #blit ist boese | ||
+ | # | ||
+ | # fuer mint gruen cmap=YlGn | ||
+ | plt.show() | ||
+ | |||
+ | </code> | ||
+ | |||
+ | GameOfLife: (Code mit den Funktionen) | ||
+ | <code> | ||
+ | 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, modus): | ||
+ | global zustand, tem, zeilen, spalten | ||
+ | |||
+ | zeilen = z | ||
+ | spalten = s | ||
+ | #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 | ||
+ | | ||
+ | tem = Tripleeinheitsmatrix(zeilen,spalten) | ||
+ | #Wiederholungen(t) | ||
+ | </code> | ||
+ | |||
+ | VersuchModus | ||
+ | <code> | ||
+ | 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: ')) | ||
+ | modus = int(input('Gib den Modus 1 oder 2 ein: ')) | ||
+ | spalten = zeilen | ||
+ | |||
+ | gol.Initialisiere(zeilen, spalten, modus) | ||
+ | |||
+ | fig = plt.figure(figsize=(1920,1080), dpi=1, frameon=False) | ||
+ | |||
+ | |||
+ | |||
+ | #fig position ändern | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | plt.axis('off') | ||
+ | |||
+ | while True: | ||
+ | if input() == int(1): | ||
+ | break | ||
+ | else: | ||
+ | | ||
+ | im = plt.imshow(gol.zustand, animated=True, cmap=plt.get_cmap('gray')) | ||
+ | anim = animation.FuncAnimation(fig, animate, frames=2, interval=100) | ||
+ | |||
+ | |||
+ | </code> |