Skip to content
Snippets Groups Projects
Commit f73a9f79 authored by Gottfried's avatar Gottfried
Browse files

Merge branch 'Mamdasan' into refractoring-(v0.2)

parents 9460037e 30c976d6
No related branches found
No related tags found
No related merge requests found
...@@ -62,7 +62,10 @@ class Simulation: ...@@ -62,7 +62,10 @@ class Simulation:
fig, ax = plt.subplots() fig, ax = plt.subplots()
self.fig = fig self.fig = fig
self.ax = ax self.ax = ax
#self.p = self.ax.imshow(np.zeros_like(game.board())) self.board_shape = game.board().shape
self.p = self.ax.imshow(np.zeros_like(game.board()), \
cmap = plt.cm.gray_r, interpolation='None', \
extent=[0, self.board_shape[1], 0, self.board_shape[0]])
def __call__(self, iterations: int): def __call__(self, iterations: int):
""" """
...@@ -72,14 +75,28 @@ class Simulation: ...@@ -72,14 +75,28 @@ class Simulation:
iterations (int): number of frames to compute iterations (int): number of frames to compute
""" """
return anim.FuncAnimation(self.fig, a = anim.FuncAnimation(self.fig,
self._render, self._render,
init_func = self._init, init_func = self._init,
frames = iterations, frames = iterations,
interval = 200) interval = 200)
# Add a title
plt.title('Game Of Life')
# Show grid lines
plt.grid(which='both', linestyle='--')
plt.xticks(ticks=[i for i in range(self.board_shape[0])], \
labels=[' ' for i in range(self.board_shape[0])])
plt.yticks(ticks=[i for i in range(self.board_shape[1])], \
labels=[' ' for i in range(self.board_shape[1])])
plt.show()
return a
def _init(self): def _init(self):
self.p = self.ax.imshow(self.game.board()) self.p = self.ax.imshow(self.game.board(), \
cmap = plt.cm.gray_r, interpolation='None', \
extent=[0, self.board_shape[1], 0, self.board_shape[0]])
return (self.p,) return (self.p,)
def _render(self, i): def _render(self, i):
......
from gol_library import GameOfLife from gol_library import GameOfLife
from game_library import CliSimulation,Simulation from game_library import CliSimulation, Simulation
import numpy as np import numpy as np
if __name__=="__main__": if __name__=="__main__":
rng = np.random.default_rng() rng = np.random.default_rng()
board = rng.integers(0, 2, size=(10,10),dtype=int) board = rng.integers(0, 2, size=(20,20),dtype=int)
gol = GameOfLife(board) gol = GameOfLife(board)
#sim = CliSimulation(gol)
sim = Simulation(gol) sim = Simulation(gol)
res=sim(50) sim(500)
\ No newline at end of file
...@@ -21,23 +21,28 @@ def oscillators(): ...@@ -21,23 +21,28 @@ def oscillators():
def test_gol_still_lifes(): def test_gol_still_lifes():
gol = GameOfLife()
for l in still_lifes(): for l in still_lifes():
if np.any(l != gol.next(l)): gol = GameOfLife(l)
gol.update()
if not np.all(l == gol.board()):
assert False,"Still life was not still." assert False,"Still life was not still."
def test_gol_oscillators(): def test_gol_oscillators():
gol=GameOfLife()
for l_i,l_o in oscillators(): for l_i,l_o in oscillators():
if np.any(l_o != gol.next(l_i)): gol=GameOfLife(l_i)
gol.update()
if not np.all(l_o == gol.board()):
assert False,"Oscillators did not work." assert False,"Oscillators did not work."
def test_gol_benchmark(benchmark): def test_gol_benchmark(benchmark):
gol=GameOfLife()
board = np.ones((50,50)) board = np.ones((50,50))
benchmark(gol.next,board) def gol_next(board):
gol=GameOfLife(board)
gol.update()
return gol.board()
benchmark(gol_next, board)
if __name__ == "__main__": if __name__ == "__main__":
test_gol_still_lifes() test_gol_still_lifes()
test_gol_oscillators() test_gol_oscillators()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment