Last active
May 20, 2018 05:34
-
-
Save sage-git/7b60d1ca280e8f69eb4245a0ccfa027a to your computer and use it in GitHub Desktop.
Lifegame in python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
tested: | |
* python 3.5.2 | |
* ubuntu 16.04 | |
required: | |
* numpy | |
* scipy | |
* opencv | |
control during play | |
* r - reset | |
* q, esc - exit | |
* s - slow down | |
* f - speed up | |
* w - save current state to "save.txt" | |
* l - load state from "save.txt" if exists | |
for autoreset/genrule additional control | |
* - - decrease width and height of the system | |
* + - increase width and height os the system | |
* x - restart with x-shaped initial state | |
* h - restart with one horizontal line at the middle of the system | |
* o - restart with a circle | |
for autoreset/genrule additional control | |
* . - restart with only one alive cell |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from __future__ import print_function | |
import os | |
import sys | |
import numpy as np | |
from scipy import signal | |
import cv2 | |
mask = np.ones((3, 3), dtype=int) | |
def init_state(width, height, init_alive_prob=0.5): | |
N = width*height | |
v = np.array(np.random.rand(N) + init_alive_prob, dtype=int) | |
return v.reshape(height, width) | |
def count_neighbor(F): | |
return signal.correlate2d(F, mask, mode="same", boundary="wrap") | |
def next_generation(F): | |
N = count_neighbor(F) | |
G = (N == 3) + F*(N == 4) | |
return G | |
def print_for_gnuplot(F): | |
np.savetxt(sys.stdout, F, fmt="%d") | |
print() | |
print() | |
def to_image(F, scale=3.0): | |
img = np.array(F, dtype=np.uint8)*180 + 20 | |
W = int(F.shape[1]*scale) | |
H = int(F.shape[0]*scale) | |
img = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST) | |
return img | |
def main(): | |
p = 0.08 | |
F = init_state(100, 100, init_alive_prob=p) | |
ret = 0 | |
wait = 10 | |
while True: | |
img = to_image(F, scale=5.0) | |
cv2.imshow("test", img) | |
ret = cv2.waitKey(wait) | |
F = next_generation(F) | |
if ret == ord('r'): | |
F = init_state(100, 100, init_alive_prob=p) | |
if ret == ord('s'): | |
wait = min(wait*2, 1000) | |
if ret == ord('f'): | |
wait = max(wait//2, 10) | |
if ret == ord('q') or ret == 27: | |
break | |
if ret == ord('w'): | |
np.savetxt("save.txt", F, "%d") | |
if ret == ord('l'): | |
if os.path.exists("save.txt"): | |
F = np.loadtxt("save.txt") | |
cv2.destroyAllWindows() | |
if __name__ == "__main__": | |
main() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from __future__ import print_function | |
import os | |
import sys | |
import numpy as np | |
from scipy import signal | |
import cv2 | |
mask = np.ones((3, 3), dtype=int) | |
def init_state(width, height, init_alive_prob=0.5): | |
N = width*height | |
v = np.array(np.random.rand(N) + init_alive_prob, dtype=int) | |
return v.reshape(height, width) | |
def count_neighbor(F): | |
return signal.correlate2d(F, mask, mode="same", boundary="wrap") | |
def next_generation(F): | |
N = count_neighbor(F) | |
G = np.array(N == 3, dtype=int) + F*np.array(N == 4, dtype=int) | |
return G | |
def to_image(F, scale=3.0): | |
img = np.array(F, dtype=np.uint8)*180 + 20 | |
W = int(F.shape[1]*scale) | |
H = int(F.shape[0]*scale) | |
img = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST) | |
return img | |
def main(): | |
p = 0.08 | |
W = 256 | |
H = 256 | |
F = init_state(W, H, init_alive_prob=p) | |
F1 = F | |
F2 = F1 | |
ret = 0 | |
wait = 10 | |
while True: | |
img = to_image(F, scale=5.0) | |
cv2.imshow("test", img) | |
ret = cv2.waitKey(wait) | |
F2 = F1 | |
F1 = F | |
F = next_generation(F) | |
if np.all(F2 == F): | |
F = init_state(W, H, init_alive_prob=p) | |
print("Reset!") | |
if ret == ord('r'): | |
F = init_state(W, H, init_alive_prob=p) | |
if ret == ord('s'): | |
wait = min(wait*2, 1000) | |
if ret == ord('f'): | |
wait = max(wait//2, 10) | |
if ret == ord('q') or ret == 27: | |
break | |
if ret == ord('w'): | |
np.savetxt("save.txt", F, "%d") | |
if ret == ord('l'): | |
if os.path.exists("save.txt"): | |
F = np.loadtxt("save.txt") | |
F1 = np.zeros_like(F) | |
F2 = np.zeros_like(F) | |
if ret == ord('+'): | |
H += 1 | |
W += 1 | |
M = np.zeros((H, W), dtype=int) | |
M[:H - 1, :W - 1] = F | |
F = M | |
F1 = np.zeros_like(F) | |
F2 = np.zeros_like(F) | |
print(F.shape) | |
if ret == ord('-'): | |
H -= 1 | |
W -= 1 | |
M = np.zeros((H, W), dtype=int) | |
M = F[:H, :W] | |
F = M | |
F1 = np.zeros_like(F) | |
F2 = np.zeros_like(F) | |
print(F.shape) | |
if ret == ord('x'): | |
F = np.eye(H, W, dtype=int) | |
F = F + F[:, -1::-1] | |
F[F > 1] = 1 | |
if ret == ord('h'): | |
F = np.zeros_like(F) | |
F[H//2, :] = 1 | |
if ret == ord('o'): | |
M = np.zeros((H, W), dtype=np.uint8) | |
M = cv2.circle(M, (H//2, W//2), H//4, 255, 1) | |
F = np.array(M, dtype=np.int) | |
F[F > 1] = 1 | |
cv2.destroyAllWindows() | |
if __name__ == "__main__": | |
main() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from __future__ import print_function | |
import os | |
import sys | |
import numpy as np | |
from scipy import signal | |
import cv2 | |
mask = [ | |
[1, 1, 1], | |
[1, 1, 1], | |
[1, 1, 1] | |
] | |
# mask = np.ones((3, 3), dtype=int) | |
survive = [2, 3] | |
birth = [3] | |
def init_state(width, height, init_alive_prob=0.5): | |
N = width*height | |
v = np.array(np.random.rand(N) + init_alive_prob, dtype=int) | |
return v.reshape(height, width) | |
def count_neighbor(F): | |
return signal.correlate2d(F, mask, mode="same", boundary="wrap") | |
def next_generation(F): | |
N = count_neighbor(F) | |
G = np.zeros_like(F) | |
for k in survive: | |
G += (N == k + 1)*F | |
for k in birth: | |
G += (N == k)*(1 - F) | |
return G | |
def to_image(F, scale=3.0): | |
img = np.array(F, dtype=np.uint8)*180 + 20 | |
W = int(F.shape[1]*scale) | |
H = int(F.shape[0]*scale) | |
img = cv2.resize(img, (W, H), interpolation=cv2.INTER_NEAREST) | |
return img | |
def main(): | |
p = 0.5 | |
W = 256 | |
H = 256 | |
F = init_state(W, H, init_alive_prob=p) | |
wait = 10 | |
while True: | |
img = to_image(F, scale=5.0) | |
cv2.imshow("test", img) | |
ret = cv2.waitKey(wait) | |
F = next_generation(F) | |
if ret == ord('r'): | |
F = init_state(W, H, init_alive_prob=p) | |
if ret == ord('s'): | |
wait = min(wait*2, 1000) | |
if ret == ord('f'): | |
wait = max(wait//2, 10) | |
if ret == ord('q') or ret == 27: | |
break | |
if ret == ord('w'): | |
np.savetxt("save.txt", F, "%d") | |
if ret == ord('l'): | |
if os.path.exists("save.txt"): | |
F = np.loadtxt("save.txt") | |
W = F.shape[1] | |
H = F.shape[0] | |
if ret == ord('+'): | |
H += 1 | |
W += 1 | |
M = np.zeros((H, W), dtype=int) | |
M[:H - 1, :W - 1] = F | |
F = M | |
print(F.shape) | |
if ret == ord('-'): | |
H -= 1 | |
W -= 1 | |
M = np.zeros((H, W), dtype=int) | |
M = F[:H, :W] | |
F = M | |
print(F.shape) | |
if ret == ord('x'): | |
F = np.eye(H, W, dtype=int) | |
F = F + F[:, -1::-1] | |
F[F > 1] = 1 | |
if ret == ord('h'): | |
F = np.zeros_like(F) | |
F[H//2, :] = 1 | |
if ret == ord('.'): | |
F = np.zeros_like(F) | |
F[np.random.randint(H), np.random.randint(W)] = 1 | |
if ret == ord('o'): | |
M = np.zeros((H, W), dtype=np.uint8) | |
M = cv2.circle(M, (H//2, W//2), H//4, 255, 1) | |
F = np.array(M, dtype=np.int) | |
F[F > 1] = 1 | |
cv2.destroyAllWindows() | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment