-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlife
executable file
·186 lines (149 loc) · 6.01 KB
/
life
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
176
177
178
179
180
181
182
183
184
185
186
#!/usr/bin/env python3
#
# Basic implementation of Conways Game of Life
#
# - - - - - - - - - - - - - - - - - - - - - - - - -
#
# Q/q - Quit
# Up/Right - Speed up
# Down/Left - Slow down
# W/w - Toggle edge wrapping
# R/r - New random seed
#_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
from random import randint
from time import sleep
import curses
import sys
import signal
class game_of_life:
def __init__(self, screen):
curses.curs_set(0)
self.screen = screen
self.speed = 0.1
self.wrap = False
self.h = curses.LINES - 1
self.w = curses.COLS - 1
self.live_cell = '\u2588'
self.dead_cell = ' '
self.s1 = [[' ' for x in range(self.w)] for y in range(self.h)]
self.s2 = [[' ' for x in range(self.w)] for y in range(self.h)]
self.random_start()
self.screen.nodelay(True)
self.screen.clear()
self.mainloop()
def random_start(self):
for y in range(self.h):
offset = int(self.h * 0.2)
if y >= offset and y <= self.h - offset:
counter = 10
else:
counter = 50
for x in range(self.w):
self.s1[y][x] = self.live_cell if randint(1, counter) == 2 else self.dead_cell
def get_input(self):
inp = self.screen.getch()
curses.flushinp()
if inp in [113, 27, 81]:
sys.exit(0)
elif inp in [114, 82]:
self.random_start()
elif inp in [curses.KEY_DOWN, curses.KEY_LEFT]:
if self.speed + 0.05 <= 0.8:
self.speed += 0.05
elif inp in [curses.KEY_UP, curses.KEY_RIGHT]:
if self.speed - 0.05 >= 0:
self.speed -= 0.05
elif inp in [87, 119]:
self.wrap = not self.wrap
def mainloop(self):
while 1:
self.get_input()
self.screen.erase()
for y in range(self.h):
for x in range(self.w):
self.s2[y][x] = self.update_cell(x, y)
for y in range(self.h):
self.screen.addstr(y, 0, ''.join(self.s1[y]))
self.screen.refresh()
sleep(self.speed)
for y in range(self.h):
for x in range(self.w):
self.s1[y][x] = self.s2[y][x]
def update_cell(self, x, y):
state = self.s1[y][x]
neighbors = self.check_surroundings(x, y)
if self.wrap:
neighbors += self.check_wrap(x, y)
if self.s1[y][x] == self.dead_cell and neighbors == 3:
state = self.live_cell
elif neighbors < 2 or neighbors > 3:
state = self.dead_cell
return state
def check_surroundings(self, x, y):
neighbors = 0
if y - 1 >= 0 and self.s1[y - 1][x] == self.live_cell:
neighbors += 1
elif y - 1 < 0 and self.s1[self.h - 1][x] == self.live_cell:
neighbors += 1
if y + 1 < self.h and self.s1[y + 1][x] == self.live_cell:
neighbors += 1
elif y + 1 >= self.h and self.s1[0][x] == self.live_cell:
neighbors += 1
if x - 1 >= 0 and self.s1[y][x - 1] == self.live_cell:
neighbors += 1
elif x - 1 < 0 and self.s1[y][self.w - 1] == self.live_cell:
neighbors += 1
if x + 1 < self.w and self.s1[y][x + 1] == self.live_cell:
neighbors += 1
elif x + 1 >= self.w and self.s1[y][0] == self.live_cell:
neighbors += 1
if x - 1 >= 0 and y - 1 >= 0 and self.s1[y - 1][x - 1] == self.live_cell:
neighbors += 1
if x + 1 < self.w and y + 1 < self.h and self.s1[y + 1][x + 1] == self.live_cell:
neighbors += 1
if x + 1 < self.w and y - 1 >= 0 and self.s1[y - 1][x + 1] == self.live_cell:
neighbors += 1
if x - 1 >= 0 and y + 1 < self.h and self.s1[y + 1][x - 1] == self.live_cell:
neighbors += 1
return neighbors
def check_wrap(self, x, y):
neighbors = 0
if x - 1 < 0 and y - 1 >= 0 and self.s1[y - 1][self.w - 1] == self.live_cell:
neighbors += 1
elif x - 1 >= 0 and y - 1 < 0 and self.s1[self.h - 1][x - 1] == self.live_cell:
neighbors += 1
elif x - 1 < 0 and y - 1 < 0 and self.s1[self.h - 1][self.w - 1] == self.live_cell:
neighbors += 1
if x + 1 >= self.w and y + 1 < self.h and self.s1[y + 1][0] == self.live_cell:
neighbors += 1
elif x + 1 < self.w and y + 1 >= self.h and self.s1[0][x + 1] == self.live_cell:
neighbors += 1
elif x + 1 >= self.w and y + 1 >= self.h and self.s1[0][0] == self.live_cell:
neighbors += 1
if x + 1 >= self.w and y - 1 >= 0 and self.s1[y - 1][0] == self.live_cell:
neighbors += 1
elif x + 1 < self.w and y - 1 < 0 and self.s1[self.h - 1][x + 1] == self.live_cell:
neighbors += 1
elif x + 1 >= self.w and y - 1 < 0 and self.s1[self.h - 1][0] == self.live_cell:
neighbors += 1
if x - 1 < 0 and y + 1 >= self.h and self.s1[0][self.w - 1] == self.live_cell:
neighbors += 1
elif x - 1 >= 0 and y + 1 >= self.h and self.s1[0][x - 1] == self.live_cell:
neighbors += 1
elif x - 1 < 0 and y + 1 < self.h and self.s1[y + 1][self.w - 1] == self.live_cell:
neighbors += 1
return neighbors
if __name__ == '__main__':
signal.signal(signal.SIGINT, signal.SIG_IGN)
try:
commands = sys.argv[1:]
if len(commands) >= 1:
if commands[0] in ['h','-h','help','--help']:
print('Game of Life\n\nControls:\nQ/q\t\tQuit\nUp/Right\t\tSpeed up\nDown/Left\t\tSlow down\nW/w\t\tToggle edge wrapping\nR/r\t\tNew random seed\n\nlife [h, -h, --help, help]\t\tdisplay this message')
sys.exit(0)
else:
print('Unknown argument: {}'.format(commands[0]))
sys.exit(1)
curses.wrapper(game_of_life)
except:
sys.exit(1)