Skip to content

Commit 0d2c9dc

Browse files
committed
Adding conway example
1 parent 940d879 commit 0d2c9dc

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

examples/graphics/conway.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
#!/usr/bin/python
2+
3+
#######################################################
4+
# Copyright (c) 2015, ArrayFire
5+
# All rights reserved.
6+
#
7+
# This file is distributed under 3-clause BSD license.
8+
# The complete license agreement can be obtained at:
9+
# http://arrayfire.com/licenses/BSD-3-Clause
10+
########################################################
11+
12+
import arrayfire as af
13+
import array
14+
from time import time
15+
16+
h_kernel = array.array('f', (1, 1, 1, 1, 0, 1, 1, 1, 1))
17+
reset = 500
18+
game_w = 128
19+
game_h = 128
20+
fps = 30
21+
22+
print("Example demonstrating conway's game of life using arrayfire")
23+
print("The conway_pretty example visualizes all the states in Conway")
24+
print("Red : Cells that have died due to under population" )
25+
print("Yellow: Cells that continue to live from previous state" )
26+
print("Green : Cells that are new as a result of reproduction" )
27+
print("Blue : Cells that have died due to over population" )
28+
print("This examples is throttled to 30 FPS so as to be a better visualization")
29+
30+
simple_win = af.Window(512, 512, "Conway's Game of Life - Current State")
31+
pretty_win = af.Window(512, 512, "Conway's Game of Life - Current State with visualization")
32+
33+
simple_win.set_pos(25, 15)
34+
pretty_win.set_pos(600, 25)
35+
frame_count = 0
36+
37+
# Copy kernel that specifies neighborhood conditions
38+
kernel = af.Array(h_kernel, dims=(3,3))
39+
40+
# Generate the initial state with 0s and 1s
41+
state = af.cast(af.randu(game_h, game_w) > 0.4, af.Dtype.f32)
42+
43+
# tile 3 times to display color
44+
display = af.tile(state, 1, 1, 3, 1)
45+
46+
while (not simple_win.close()) and (not pretty_win.close()):
47+
delay = time()
48+
if (not simple_win.close()): simple_win.image(state)
49+
if (not pretty_win.close()): pretty_win.image(display)
50+
51+
frame_count += 1
52+
if (frame_count % reset == 0):
53+
state = af.cast(af.randu(game_h, game_w) > 0.4, af.Dtype.f32)
54+
55+
neighborhood = af.convolve(state, kernel)
56+
57+
# state == 1 && neighborhood < 2 --> state = 0
58+
# state == 1 && neighborhood > 3 --> state = 0
59+
# state == 0 && neighborhood == 3 --> state = 1
60+
# else state remains un changed
61+
62+
C0 = neighborhood == 2
63+
C1 = neighborhood == 3
64+
A0 = (state == 1) & (neighborhood < 2)
65+
A1 = (state != 0) & (C0 | C1)
66+
A2 = (state == 0) & C1
67+
A3 = (state == 1) & (neighborhood > 3)
68+
69+
display = af.cast(af.join(2, A0 + A1, A1 + A2, A3), af.Dtype.f32)
70+
71+
state = state * C0 + C1
72+
73+
while(time() - delay < (1.0 / fps)):
74+
pass

examples/graphics/plot3.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
########################################################
1111

1212
import arrayfire as af
13-
import math
1413

1514
af.info()
1615

examples/graphics/surface.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
########################################################
1111

1212
import arrayfire as af
13-
import math
1413

1514
af.info()
1615

0 commit comments

Comments
 (0)