Last active
June 17, 2019 15:00
-
-
Save SuperDoxin/1e9ddab4d6f3d596741c to your computer and use it in GitHub Desktop.
game of life
This file contains hidden or 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/python3 | |
import numpy | |
from functools import reduce | |
import time | |
# I am terribly sorry for what is about to follow | |
initial_state = "random" # possible values: glider, random | |
if initial_state == "glider": | |
state = numpy.zeros((25, 75), dtype=numpy.int8) | |
state[20][20] = 1 | |
state[21][20] = 1 | |
state[22][20] = 1 | |
state[22][21] = 1 | |
state[21][22] = 1 | |
elif initial_state == "random": | |
state = numpy.random.randint(0, 2, (25, 75)) | |
else: | |
raise ValueError("initial_state not in ['glider','random']") | |
def step(prev_state): | |
neighbor_count = sum([sum([numpy.roll(numpy.roll(prev_state, γ, 0), δ, 1) for γ in range(-1, 2)]) for δ in range(-1, 2)]) | |
born = neighbor_count == 3 | |
stays_alive = numpy.logical_and(neighbor_count == 4, prev_state) | |
return numpy.logical_or(born, stays_alive) * 1 | |
while True: | |
print("\x1B[2J\x1B[H") | |
print('\n'.join(''.join('●' if cell else ' ' for cell in row) for row in state)) | |
state = step(state) | |
time.sleep(1 / 10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment