Last active
June 8, 2019 19:48
-
-
Save readmeexe/0444d5e9922034ee69293c580fcec0b2 to your computer and use it in GitHub Desktop.
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
from os import system, name | |
from time import sleep | |
def clear(): | |
if name == 'nt': | |
_ = system('cls') | |
else: | |
_ = system('clear') | |
def gol(width,oldboard): | |
# Takes a list of boolean values and the width of the board | |
# Return the next step | |
# Any live cell with fewer than two live neighbors dies (referred to as underpopulation or exposure). | |
# Any live cell with more than three live neighbors dies (referred to as overpopulation or overcrowding). | |
# Any live cell with two or three live neighbors lives, unchanged, to the next generation. | |
# Any dead cell with exactly three live neighbors will come to life. | |
# http://www.conwaylife.com/wiki/Conway%27s_Game_of_Life | |
newboard = [] | |
field = len(oldboard) | |
for cell in range(field): | |
cell += field | |
cell += width | |
e = ((cell+1) % width) + (width * (cell // width)) % field - width | |
se = ((cell+1) % width) + (width * ((cell // width)+1)) % field - width | |
s = ((cell) % width) + (width * ((cell // width)+1)) % field - width | |
sw = ((cell-1) % width) + (width * ((cell // width)+1)) % field - width | |
w = ((cell-1) % width) + (width * (cell // width)) % field - width | |
nw = ((cell-1) % width) + (width * ((cell // width)-1)) % field - width | |
n = ((cell) % width) + (width * ((cell // width)-1)) % field - width | |
ne = ((cell+1) % width) + (width * ((cell // width)-1)) % field - width | |
neighbors = [oldboard[e],oldboard[se],oldboard[s],oldboard[sw],oldboard[w],oldboard[nw],oldboard[n],oldboard[ne]] | |
count = 0 | |
for neighbor in neighbors: | |
if neighbor == True: | |
count += 1 | |
if count == 2: | |
newboard.append(oldboard[cell-field-width]) | |
elif count == 3: | |
newboard.append(True) | |
else: | |
newboard.append(False) | |
return newboard | |
def displayboard(width,board): | |
clear() | |
screen = '' | |
newline = 0 | |
for cell in board: | |
if cell == True: | |
screen += 'X' | |
else: | |
screen += ' ' | |
newline += 1 | |
if newline == width: | |
screen += '\n' | |
newline = 0 | |
print(screen) | |
sleep(.05) | |
def main(): | |
board = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] | |
width = 10 | |
while True: | |
displayboard(width,board) | |
board = gol(width,board) | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment