это игра математическая "Жизнь" или я ошибаюсь?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
# encoding: utf-8
from Tkinter import *
import random
root = Tk()
root.title("Djumandji!")
class Zelle(Label):
TOT = 0
LEBT = 1
def __init__(self,parent):
Label.__init__(self,parent,relief="raised",width=2,borderwidth=1)
self.bind("<Button-1>", self.toggle)
self.displaygeneration(Zelle.TOT)
def toggle(self,event):
self.displaygeneration(1-self.generation)
def setzeNeueGeneration(self,Nachbaren):
if self.generation==Zelle.LEBT and (Nachbaren>3 or Nachbaren<2):
self.nextgeneration = Zelle.TOT
elif self.generation==Zelle.TOT and Nachbaren==3:
self.nextgeneration = Zelle.LEBT
else:
self.nextgeneration = self.generation
def schrittToNextgeneration(self):
self.displaygeneration(self.nextgeneration)
def displaygeneration(self,newgeneration):
self.generation = newgeneration
if self.generation==Zelle.LEBT:
self["bg"] = "#FF9900"
else:
self["bg"] = "#009999"
class Grid:
def __init__(self,parent,sizex,sizey):
self.sizex = sizex
self.sizey = sizey
self.Zellen = []
for a in range(0,self.sizex):
rowZellen = []
for b in range(0,self.sizey):
c = Zelle(parent)
c.grid(row=b, column=a)
rowZellen.append(c)
self.Zellen.append(rowZellen)
if sizex>10 and sizey>10:
#beginnt mit zufaelligen Zahlen
for i in range(500):
self.Zellen[random.randint(1,39)][random.randint(1,39)].displaygeneration(Zelle.LEBT)
print i
def schritt(self):
Zellen = self.Zellen
for x in range(0,self.sizex):
if x==0: x_unten = self.sizex-1
else: x_unten = x-1
if x==self.sizex-1: x_oben = 0
else: x_oben = x+1
for y in range(0,self.sizey):
if y==0: y_unten = self.sizey-1
else: y_unten = y-1
if y==self.sizey-1: y_oben = 0
else: y_oben = y+1
sum = Zellen[x_unten][y].generation + Zellen[x_oben][y].generation + Zellen[x][y_unten].generation + Zellen[x][y_oben].generation + Zellen[x_unten][y_unten].generation + Zellen[x_oben][y_oben].generation + Zellen[x_unten][y_oben].generation + Zellen[x_oben][y_unten].generation
Zellen[x][y].setzeNeueGeneration(sum)
for row in Zellen:
for Zelle in row:
Zelle.schrittToNextgeneration()
def clear(self):
for row in self.Zellen:
for Zelle in row:
Zelle.displaygeneration(Zelle.TOT)
def laufen(root, counter):
if counter > 0:
grid.schritt()
counter -=1
root.after(100, laufen, root, counter)
if __name__ == "__main__":
frame = Frame(root)
frame.pack()
grid = Grid(frame,40,40)
bottomFrame = Frame(root)
bottomFrame.pack(side=BOTTOM)
buttonSchritt = Button(bottomFrame,text="Schritt machen",command=grid.schritt)
buttonSchritt.pack(side=LEFT)
buttonClear = Button(bottomFrame,text="Clear",command=grid.clear)
buttonClear.pack(side=LEFT,after=buttonSchritt)
buttonLaufen = Button(bottomFrame,text="Laufen lassen",command=lambda counter=100, root=root: laufen(root, counter))
buttonLaufen.pack(side=LEFT,after=buttonSchritt)
root.mainloop()