-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlife.py
175 lines (148 loc) · 4.56 KB
/
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
from time import sleep
import numpy as np
import curses
import os
win = curses.initscr()
curses.noecho()
curses.cbreak()
win.keypad(True)
def genPoints():
width, height = os.get_terminal_size()
return np.zeros((height - 3, width))
def draw(point):
if point == 0: return ' '
if point == 1: return 'x'
if point == 2: return '█'
if point == 3: return '▒'
return ' '
def getRow(row, col=0):
if col == len(row) -1:
return draw(row[col])
else:
return draw(row[col]) + getRow(row, col + 1)
def drawScreen(win, points, row=0):
win.addstr(row + 2, 0, getRow(points[row]))
if (row == len(points)-1):
win.refresh()
else:
drawScreen(win, points, row + 1)
def runCycle(points):
height = len(points)
width = len(points[0])
result = genPoints()
for i in range(height):
for j in range(width):
n = 0
alive = points[i][j] == 1
left = j != 0
right = j != width - 1
up = i != 0
down = i != height - 1
# count neighbours
if left and up: n += points[i-1][j-1]
if up: n += points[i-1][j]
if up and right: n += points[i-1][j+1]
if left: n += points[i][j-1]
if right: n += points[i][j+1]
if down and left: n += points[i+1][j-1]
if down: n += points[i+1][j]
if down and right: n += points[i+1][j+1]
# Rule 1: Any live cell with fewer than two live neighbours dies, as if by underpopulation.
# Rule 2: Any live cell with two or three live neighbours lives on to the next generation.
# Rule 3: Any live cell with more than three live neighbours dies, as if by overpopulation.
if alive and (n == 2 or n == 3):
result[i][j] = 1
# Rule 4: Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
elif not alive and n == 3:
result[i][j] = 1
return result
def removeCursor(points, xy):
x, y = xy
point = points[y][x]
point -= 2
if point < 0: raise IndexError()
points[y][x] = point
def addCursor(points, xy):
x, y = xy
point = points[y][x]
point += 2
if point > 3: raise IndexError()
points[y][x] = point
def h(points, x, y):
x, y = xy
if x == 0: x = len(points[0]) - 1
else: x -= 1
return x, y
def j(points, x, y):
x, y = xy
if y == len(points) - 1: y = 0
else: y += 1
return x, y
def k(points, x, y):
if y == 0: y = len(points) - 1
else: y -= 1
return x, y
def l(points, x, y):
if x == len(points[0]) - 1: x = 0
else: x += 1
return x, y
def togglePoint(points, xy):
x, y = xy
point = points[y][x]
if point == 3: point = 2
elif point == 2: point = 3
else: raise IndexError()
points[y][x] = point
movements = {
ord('h'): h,
ord('j'): j,
ord('k'): k,
ord('l'): l,
curses.KEY_LEFT: h,
curses.KEY_RIGHT: l,
curses.KEY_DOWN: j,
curses.KEY_UP: k,
}
points = genPoints()
try:
while True:
win.addstr(0, 0, "Build mode: h, j, k, and l for movement, [Space] and x to toggle cells, q to simulate", curses.A_REVERSE)
points[0][0] = 2
xy = (0, 0)
drawScreen(win, points)
ch = win.getch()
while ch != ord('\n'):
if ch == ord('q'): break
elif ch == 32 or ch == ord('x'): togglePoint(points, xy)
elif ch == curses.KEY_RESIZE: break
else:
removeCursor(points, xy)
xy = movements[ch](points, xy[0], xy[1])
addCursor(points, xy)
drawScreen(win, points)
ch = win.getch()
removeCursor(points, xy)
win.clear()
win.addstr(0, 0, "Running Life: Press 'q' to return to build mode", curses.A_REVERSE)
win.addstr(1, 0, 'Generation: 1')
live = runCycle(points)
win.nodelay(True)
gen = 1
while True:
ch = win.getch()
if ch is not None and ch == ord('q'): break
if ch is not None and ch == curses.KEY_RESIZE: gen += 1; runCycle(live)
win.addstr(1, 12, str(gen))
drawScreen(win, live)
sleep(0.1)
gen += 1
live = runCycle(live)
win.nodelay(False)
win.addstr(1, 12, '0' + ' '*5)
except KeyboardInterrupt:
curses.nocbreak()
win.keypad(False)
curses.echo()
curses.endwin()
print()
exit(0)