-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
102 lines (85 loc) · 3.13 KB
/
main.py
File metadata and controls
102 lines (85 loc) · 3.13 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
import copy
import os
import time
from file_handler import make_content_from_file
from file_handler import get_rules_from_file
from ui import display
from ui import menu
import getch
def clear(): return os.system('clear')
'''
Any live cell with fewer than two live neighbours dies, as if by underpopulation.
Any live cell with two or three live neighbours lives on to the next generation.
Any live cell with more than three live neighbours dies, as if by overpopulation.
Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.'''
def step(content,rules):
newcontent = copy.deepcopy(content)
for i in range(1, len(content[:-1])):
row_list = list(content[i])
new_rowlist = list(newcontent[i])
for j in range(1, len(row_list[:-1])):
living_neighbours = 0
if content[i - 1][j - 1] != '_':
living_neighbours += 1
if content[i - 1][j] != '_':
living_neighbours += 1
if content[i - 1][j + 1] != '_':
living_neighbours += 1
if content[i][j - 1] != '_':
living_neighbours += 1
if content[i][j + 1] != '_':
living_neighbours += 1
if content[i + 1][j - 1] != '_':
living_neighbours += 1
if content[i + 1][j] != '_':
living_neighbours += 1
if content[i + 1][j + 1] != '_':
living_neighbours += 1
if row_list[j] == '0':
if rules[living_neighbours][1] == 'd':
new_rowlist[j] = '_'
else:
new_rowlist[j] = '0'
if row_list[j] == '_':
if rules[living_neighbours][2] == 'd':
new_rowlist[j] = '_'
else:
new_rowlist[j] = '0'
newcontent[i] = ''.join(new_rowlist)
return newcontent
def main():
while True:
rules = []
rules = get_rules_from_file()
user_is_smart = False
print(rules)
while user_is_smart == False:
user_choose = menu()
if user_choose == '1':
content = make_content_from_file("random_like_init.txt")
elif user_choose == '2':
content = make_content_from_file("glider.txt")
elif user_choose == '3':
content = make_content_from_file("stable_configurations.txt")
elif user_choose == '4':
content = make_content_from_file("glider_gun.txt")
elif user_choose == '5':
filename = input("Insert path here:")
content = make_content_from_file(filename)
else:
print('Think again!')
continue
user_is_smart = True
display(content)
input('Press enter to start!')
run = True
while run:
try:
display(content)
time.sleep(.1)
content = step(content,rules)
clear()
except KeyboardInterrupt:
break
if __name__ == "__main__":
main()