Hier werden die Unterschiede zwischen zwei Versionen gezeigt.
Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
ws1920:sechster_termin_9.1 [2020/01/09 15:11] richard.wonneberger angelegt |
ws1920:sechster_termin_9.1 [2020/01/09 17:54] (aktuell) richard.wonneberger |
||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
- | code am anfang:(funktioniert) | ||
+ | Sechster Termin(erster termin des neuen Jahres)(9.1.20) | ||
+ | |||
+ | __**Was wir heute gemacht haben:**__ | ||
+ | |||
+ | - Programm funktioniert!!! | ||
+ | (zusammenführen von animation und gameoflife-code) | ||
+ | |||
+ | - Code aufgeräumt (Variablennamen geändert usw.; Aufteilung in Bibliothek und Ausführungsdatei) | ||
+ | |||
+ | - Colourmap geändert zu schwarz-weiß | ||
+ | |||
+ | - Achsen entfernt | ||
+ | |||
+ | - Wiki sortiert | ||
+ | |||
+ | - alle haben sich mit dem Code vertraut gemacht | ||
+ | |||
+ | __**Ziele für die nächste Woche:**__ | ||
+ | |||
+ | - Interagierbar machen | ||
+ | |||
+ | - Anfangskonfigurationen ändern | ||
+ | |||
+ | - Stefans Tipps | ||
+ | |||
+ | - Git angucken? | ||
+ | |||
+ | - zeitschritte wieder integrieren (gerade ist es durch funcanimation so lang bis mensch das Fenster schließt) | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | |||
+ | 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: ')) | ||
+ | spalten = zeilen | ||
+ | |||
+ | gol.Initialisiere(zeilen, spalten) | ||
+ | |||
+ | 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> | ||
+ | |||
+ | Bibliothek(aufgeraeumt): | ||
<code> | <code> | ||
Zeile 11: | Zeile 86: | ||
# Die Matrix wird zufällig mit Nullen und Einsen gefüllt | # 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): | + | def Tripleeinheitsmatrix(zeilen,spalten): |
+ | tem = np.zeros ((zeilen,spalten)) | ||
for i in range(0, zeilen): | for i in range(0, zeilen): | ||
for j in range(0, spalten): | for j in range(0, spalten): | ||
if i==j: | if i==j: | ||
- | D[(i,j)] = 1 | + | tem[(i,j)] = 1 |
else: | else: | ||
- | D[(i,j)] = 0 | + | tem[(i,j)] = 0 |
| | ||
for i in range(0, zeilen): | for i in range(0, zeilen): | ||
for j in range(0, spalten): | for j in range(0, spalten): | ||
if i==(j+1): | if i==(j+1): | ||
- | D[(i,j)] = 1 | + | tem[(i,j)] = 1 |
for i in range(0, zeilen): | for i in range(0, zeilen): | ||
for j in range(0, spalten): | for j in range(0, spalten): | ||
if j==(i+1): | if j==(i+1): | ||
- | D[(i,j)] = 1 | + | tem[(i,j)] = 1 |
| | ||
- | D[0,D.shape[1]-1] = 1 | + | tem[0,tem.shape[1]-1] = 1 |
- | D[D.shape[1]-1,0] = 1 | + | tem[tem.shape[1]-1,0] = 1 |
- | return D | + | return tem |
| | ||
- | def Generationen(Matrix_füllen,Tripleeinheitsmatrix): | + | def BerechneAnzahlNachbarn(zustand): |
- | def Matrizenmultiplikation(D,M): | + | z = np.dot(tem, zustand) |
- | z = np.dot(D,M) | + | k = np.dot(z, tem) |
- | k = np.dot(z,D) | + | anzahlNachbarn = k - zustand |
- | wandelmatrix = k- M | + | |
| | ||
- | return wandelmatrix | + | return anzahlNachbarn |
- | + | ||
- | wandelmatrix= Matrizenmultiplikation(D,M) | + | |
- | Matrizenmultiplikation(D,M) | + | def BerechneNeuenZustand(zustand, anzahlNachbarn): |
- | #print (wandelmatrix) | + | |
- | + | ||
- | | + | |
#Ursprungszelle Tod + genau 3 lebende Nachbarn = lebend | #Ursprungszelle Tod + genau 3 lebende Nachbarn = lebend | ||
#Ursprungszelle Lebend + genau 1 lebenden Nachbarn = tot | #Ursprungszelle Lebend + genau 1 lebenden Nachbarn = tot | ||
Zeile 63: | Zeile 125: | ||
#Ursprungszelle lebend + 4 oder mehr Nachbarn = tot | #Ursprungszelle lebend + 4 oder mehr Nachbarn = tot | ||
| | ||
- | | + | for i in range(0, zeilen): |
- | def GameofLife(M,wandelmatrix): | + | for j in range(0, spalten): |
- | for i in range(0, zeilen): | + | if zustand[(i,j)] == 1 and (anzahlNachbarn[(i,j)] in range(2) or anzahlNachbarn[(i,j)] in range(4,9)) : |
- | for j in range(0, spalten): | + | zustand[(i,j)]=0 |
- | if M[(i,j)] == 1 and (wandelmatrix[(i,j)]==2 or wandelmatrix[(i,j)]==3) : | + | elif zustand[(i,j)] == 0 and anzahlNachbarn[(i,j)]==3: |
- | M[(i,j)]=1 | + | zustand[(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 zustand |
- | + | ||
- | return GameofLife(M,wandelmatrix) | + | |
- | #print(M) | + | def Schritt(): |
- | #plt.imshow(M) | + | global zustand |
- | #plt.show() | + | anzahlNachbarn = BerechneAnzahlNachbarn(zustand) |
+ | return BerechneNeuenZustand(zustand, anzahlNachbarn) | ||
| | ||
- | def Wiederholungen(t): | + | def Wiederhole(t): |
for i in range(t): | for i in range(t): | ||
- | Generationen(Matrix_füllen,Tripleeinheitsmatrix) | + | Schritt() |
- | def Schritt(): | + | #t= int(input('Gib die Zeitschritte ein: ')) |
- | return Generationen(Matrix_füllen,Tripleeinheitsmatrix) | + | def Initialisiere(z, s): |
+ | global zustand, tem, zeilen, spalten | ||
- | t= int(input('Gib die Zeitschritte ein: ')) | + | zeilen = z |
- | zeilen = int(input('Gib die Anzahl der Zeilen/Spalten ein: ')) | + | spalten = s |
- | spalten = zeilen | + | |
+ | zustand = np.round(np.random.random((zeilen,spalten))).astype(int) | ||
+ | tem = Tripleeinheitsmatrix(zeilen,spalten) | ||
+ | #Wiederholungen(t) | ||
+ | </code> | ||
- | 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) | ||
- | |||
- | </code> |