-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame-of-life.py
68 lines (56 loc) · 1.64 KB
/
game-of-life.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!user/bin/env python
import sys
import random
import time
args = sys.argv
w =45
h = 45
# fillChar = u"\u2588"
fillChar = "#"
empChar = " "
mutation = 0.01
mutation /= 100
alive_perc = 5
alive_perc /= 100
if (len(args) == 3):
w = int(args[1])
h = int(args[2])
def is_alive():
if (random.random() < alive_perc):
return True
return False
def main():
board = [[fillChar if is_alive() else " " for i in range(w)] for j in range(h)]
for row in board:
print("".join(row))
while True:
cboard = board.copy()
for y in range(h):
for x in range(w):
current = cboard[y][x]
no_of_alive = 0
for dx,dy in [(1,1), (1,0), (0,1), (-1,0), (0,-1), (-1, -1), (-1, 1), (1, -1)]:
nx, ny = x+dx, y+dy
if (0<=nx<w and 0<=ny<h):
if cboard[ny][nx] == fillChar:
no_of_alive += 1
# print(no_of_alive, current)
if no_of_alive < 2:
board[y][x] = " "
elif no_of_alive > 3:
board[y][x] = " "
elif current == " " and no_of_alive == 3:
board[y][x] = fillChar
elif current == fillChar and no_of_alive in [2,3]:
board[y][x] = fillChar
if (random.random() < mutation):board[y][x] = fillChar
for i in range(h):
sys.stdout.write("\033[F")
for row in board:
print(" ".join(row))
# print()
time.sleep(0.4)
# input()
pass
if __name__=="__main__":
main()