Skip to content

Commit

Permalink
finished seed
Browse files Browse the repository at this point in the history
  • Loading branch information
thearn committed Feb 28, 2014
1 parent 2ea42f4 commit ea96f39
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
![Alt text](http://i.imgur.com/6B84SNI.png "Screenshot")

Fast Python implementation of Conway's game of life and other cellular automata.
Fast Python implementation of Conway's game of life [and other cellular automata](http://www.conwaylife.com/wiki/Cellular_automaton#Well-known_Life-like_cellular_automata).

Requires Python 2.7 (will check Python 3 soon), with Numpy and Matplotlib.

Expand All @@ -14,6 +14,8 @@ To run [Replicator](http://www.conwaylife.com/wiki/Replicator_(CA)):
$ python replicator.py
```

Other cellular automata implementations are run the same way.

End each program using a keyboard interrupt (ctrl-c).

The game grid is encoded as a simple `m` by `n` array (default 100x100 in the code) of zeros and ones.
Expand Down
2 changes: 1 addition & 1 deletion conway.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

def conway(state, k):
"""
Iterates one step using the rules for CGOL.
Conway's game of life state transition
"""
# computes sums around each pixel
b = fft_convolve2d(state,k).round()
Expand Down
2 changes: 1 addition & 1 deletion replicator.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

def replicator(state, k):
"""
'Replicator' cellular automaton iteration
'Replicator' cellular automaton state transition
"""
b = fft_convolve2d(state,k).round()
c = np.zeros(b.shape)
Expand Down
39 changes: 39 additions & 0 deletions seed.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import numpy as np
import time
from lib import fft_convolve2d
import matplotlib.pyplot as plt
plt.ion()


def seed(state, k):
"""
'Seed' cellular automaton state transition
"""
b = fft_convolve2d(state,k).round()
c = np.zeros(b.shape)
# checks the values, and sets alive vs. dead state
c[np.where(b==2)] = 1

# return new state
return c

if __name__ == "__main__":
# set up board
m,n = 100,100
A = np.zeros((m,n))
A[20,20] = 1
A[20,21] = 1

# construct convolution kernel
k = np.zeros((m, n))
k[m/2-1 : m/2+2, n/2-1 : n/2+2] = np.array([[1,1,1],[1,0,1],[1,1,1]])

# plot each frame
plt.figure()
img_plot = plt.imshow(A, interpolation="nearest", cmap = plt.cm.gray)
plt.show()
for i in xrange(1,5000):
A = seed(A, k)
img_plot.set_data(A)
plt.draw()
time.sleep(0.05)

0 comments on commit ea96f39

Please sign in to comment.