forked from arrayfire/arrayfire-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconway.py
More file actions
77 lines (59 loc) · 2.36 KB
/
conway.py
File metadata and controls
77 lines (59 loc) · 2.36 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
#!/usr/bin/env python
#######################################################
# Copyright (c) 2019, ArrayFire
# All rights reserved.
#
# This file is distributed under 3-clause BSD license.
# The complete license agreement can be obtained at:
# http://arrayfire.com/licenses/BSD-3-Clause
########################################################
import array
from time import time
import arrayfire as af
h_kernel = array.array("f", (1, 1, 1, 1, 0, 1, 1, 1, 1))
reset = 500
game_w = 128
game_h = 128
fps = 30
print("Example demonstrating conway's game of life using arrayfire")
print("The conway_pretty example visualizes all the states in Conway")
print("Red : Cells that have died due to under population")
print("Yellow: Cells that continue to live from previous state")
print("Green : Cells that are new as a result of reproduction")
print("Blue : Cells that have died due to over population")
print("This examples is throttled to 30 FPS so as to be a better visualization")
simple_win = af.Window(512, 512, "Conway's Game of Life - Current State")
pretty_win = af.Window(512, 512, "Conway's Game of Life - Current State with visualization")
simple_win.set_pos(25, 15)
pretty_win.set_pos(600, 25)
frame_count = 0
# Copy kernel that specifies neighborhood conditions
kernel = af.Array(h_kernel, dims=(3, 3))
# Generate the initial state with 0s and 1s
state = (af.randu(game_h, game_w) > 0.4).as_type(af.Dtype.f32)
# tile 3 times to display color
display = af.tile(state, 1, 1, 3, 1)
while not (simple_win.close() or pretty_win.close()):
delay = time()
if not simple_win.close():
simple_win.image(state)
if not pretty_win.close():
pretty_win.image(display)
frame_count += 1
if frame_count % reset == 0:
state = (af.randu(game_h, game_w) > 0.4).as_type(af.Dtype.f32)
neighborhood = af.convolve(state, kernel)
# state == 1 && neighborhood < 2 --> state = 0
# state == 1 && neighborhood > 3 --> state = 0
# state == 0 && neighborhood == 3 --> state = 1
# else state remains un changed
C0 = neighborhood == 2
C1 = neighborhood == 3
A0 = (state == 1) & (neighborhood < 2)
A1 = (state != 0) & (C0 | C1)
A2 = (state == 0) & C1
A3 = (state == 1) & (neighborhood > 3)
display = af.join(2, A0 + A1, A1 + A2, A3).as_type(af.Dtype.f32)
state = state * C0 + C1
while time() - delay < (1.0 / fps):
pass