-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgol.py
More file actions
137 lines (104 loc) · 2.52 KB
/
Copy pathgol.py
File metadata and controls
137 lines (104 loc) · 2.52 KB
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
#!/usr/bin/python3
#############################################################################################
#
# Conway's Game of Life
# *Toroidal Universe
# *Standard B3/S23 rule by default. Adjustable below.
#
#############################################################################################
import os.path
import sys
import time
global h
global w
def main(argv):
#B3/S23
B = [3]
S = [2,3]
if not len(argv) == 2:
print("Usage: ./gol.py <file>")
return 1
#filename from command line
file = argv[1]
#if not os.path.isfile("./" + file):
# setup = open(file, "w")
# for y in range(h):
# for x in range(w):
# setup.write('.')
# setup.write('\n')
#close file
# setup.close()
# print(file + " file created.")
# print("Open the file in a text editor and change full stops to octothorpes before running this program again.")
#else:
#read file
setup = open(file, "r")
lines = setup.readlines()
global w
global h
w = len(lines[0])-1
h = len(lines)
#return to beginning of file
setup.seek(0)
#create board
board = [[0 for x in range(w)] for y in range(h)]
#store file into board array
for y in range(h):
for x in range(w):
data = setup.read(1)
#Don't store newline chars, read off another char
if data == '\n':
data = setup.read(1)
if data == '.':
board[y][x] = 0
elif data == '#':
board[y][x] = 1
#close file
setup.close()
#primary game loop
while True:
try:
draw(board)
board = change(board,B,S)
time.sleep(0.25)
print('\n\n\n')
except:
return 0
def change(board,B,S):
global h
global w
changes = [[0 for x in range(w)] for y in range(h)]
#find what changes need to be made
for y in range(h):
for x in range(w):
#count live neighbors
neigh = 0
for offy in range(-1, 2):
for offx in range(-1, 2):
if not (offy == 0 and offx == 0):
#do not count self as a neighbor
neigh += board[(y + offy) % h][(x + offx) % w]
#program in rule
#birth
if board[y][x] == 0 and neigh in B:
changes[y][x] = 1
#death
if board[y][x] == 1 and not neigh in S:
changes[y][x] = 1
#xor array of changes needed with board and return new board
for y in range(h):
for x in range(w):
board[y][x] = int(bool(board[y][x]) ^ bool(changes[y][x]))
return board
def draw(board):
global h
global w
for y in range(h):
for x in range(w):
if board[y][x] == 1:
print('#', end="")
elif board[y][x] == 0:
print('.', end="")
print('')
if __name__ == "__main__":
main(sys.argv)