This repository was archived by the owner on Jan 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestAlgo.py
More file actions
145 lines (113 loc) · 4.04 KB
/
TestAlgo.py
File metadata and controls
145 lines (113 loc) · 4.04 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
try:
import Tkinter as tk
except ModuleNotFoundError:
import tkinter as tk
import matplotlib
matplotlib.use('TkAgg')
from matplotlib.backends.backend_tkagg import (FigureCanvasTkAgg, NavigationToolbar2Tk)
# Implement the default Matplotlib key bindings.
from matplotlib.backend_bases import key_press_handler
from matplotlib.figure import Figure
import matplotlib.pyplot as plt
import numpy as np
import time
from random import *
root = tk.Tk()
def _quit():
root.quit() # stops mainloop
root.destroy() # this is necessary on Windows to prevent
# Fatal Python Error: PyEval_RestoreThread: NULL tstate
def drawcells(g, d, t):
for i in range(len(g)):
for j in range(len(g[i])):
if g[i][j] == 1:
d.create_rectangle(j * t, i * t, (j + 1) * t, (i + 1) * t, fill="black")
else:
d.create_rectangle(j * t, i * t, (j + 1) * t, (i + 1) * t, fill="lightgrey")
d.pack()
def updatecells(g, g2, d, t):
for i in range(len(g)):
for j in range(len(g[i])):
if g2[i][j] == 1 and g[i][j] == 0:
d.create_rectangle(j * t, i * t, (j + 1) * t, (i + 1) * t, fill="black")
if g2[i][j] == 0 and g[i][j] == 1:
d.create_rectangle(j * t, i * t, (j + 1) * t, (i + 1) * t, fill="lightgrey")
d.pack()
def count(g, x, y):
resultat = -g[x][y]
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
xx = x + i
yy = y + j
if xx >= len(g):
xx = xx - len(g)
if yy >= len(g[x]):
yy = yy - len(g[x])
resultat = resultat + g[xx][yy]
return resultat
def run1(g):
grille2 = np.zeros(g.shape)
for i in range(len(g)):
for j in range(len(g[i])):
c = count(g, i, j)
if c == 3:
grille2[i][j] = 1
if g[i][j] == 1 and c == 2:
grille2[i][j] = 1
return grille2
def count_living_cells():
c = 0
for i in range(len(root.grid)):
for j in range(len(root.grid[i])):
if root.grid[i][j] == 1:
c += 1
return c
def run_all():
#drawcells(g, dessin, t)
g2 = run1(root.grid)
updatecells(root.grid, g2, root.gof_canvas, root.size)
root.grid = g2
root.living_cells.append(count_living_cells())
root.steps.append(len(root.steps))
root.after(root.delai, run_all)
root.line1.set_ydata(root.living_cells)
root.line1.set_xdata(root.steps)
root.ax.set_xlim(0, len(root.steps))
root.ax.set_ylim(0, root.living_cells[0])
root.fig.canvas.draw()
if root.steps[-1] % 10 == 0:
s = int(1000*(time.time()-root.start_time))
root.label["text"] = "Execution Time : {} milliseconds".format(s)
def init_cells(width, height):
g = np.random.randint(2, size=(height, width))
return g
def init_all():
root.wm_title("Game of Life")
root.label = tk.Label(master=root, text="Execution Time : {} milliseconds".format(int(1000*(time.time()-root.start_time))))
root.label.pack(side=tk.TOP)
button = tk.Button(master=root, text="Quit", command=_quit)
button.pack(side=tk.BOTTOM)
root.width = len(root.grid[0]) * root.size
root.height = len(root.grid) * root.size
root.gof_canvas = tk.Canvas(root, width=root.width, height=root.height)
root.gof_canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
drawcells(root.grid, root.gof_canvas, root.size)
plt.ion()
root.fig = Figure(figsize=(5, 4), dpi=100)
root.living_cells = [count_living_cells()]
root.steps = [0]
root.ax = root.fig.add_subplot(111)
root.line1, = root.ax.plot(root.living_cells)
root.plt_canvas = FigureCanvasTkAgg(root.fig, master=root) # A tk.DrawingArea.
root.plt_canvas.draw()
root.plt_canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=1)
def vie():
root.size = 20
root.delai = 200 #millisecondes
root.grid = init_cells(30, 20)
root.start_time = time.time()
root.again = True
init_all()
root.after(root.delai, run_all)
tk.mainloop()
vie()