-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgameoflife.py
93 lines (85 loc) · 2.2 KB
/
gameoflife.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
#! python3
from os import system, name
from sys import stdin
from copy import deepcopy
from html import parser
# # # # # FILE INPUT # # # # #
f = open("input.txt")
# # # # # # # # # # # # # # #
input = f.read().splitlines()
global inputArray
inputArray = []
for line in input:
inputArray.append(list(line))
def nextGen(inputArray):
newArray = deepcopy(inputArray)
for lineKey, line in enumerate(inputArray):
for charKey, char in enumerate(line):
if inputArray[lineKey][charKey] == "1" and neighbours(lineKey, charKey) < 2:
newArray[lineKey][charKey] = "0"
elif inputArray[lineKey][charKey] == "1" and neighbours(lineKey, charKey) > 3:
newArray[lineKey][charKey] = "0"
elif inputArray[lineKey][charKey] == "0" and neighbours(lineKey, charKey) == 3:
newArray[lineKey][charKey] = "1"
return newArray
def neighbours(y, x):
counter = 0
try:
if x-1 >= 0 and y-1 >= 0 and inputArray[y-1][x-1] == "1":
counter += 1
except:
pass
try:
if x >= 0 and y-1 >= 0 and inputArray[y-1][x] == "1":
counter += 1
except:
pass
try:
if x+1 >= 0 and y-1 >= 0 and inputArray[y-1][x+1] == "1":
counter += 1
except:
pass
try:
if x-1 >= 0 and y >= 0 and inputArray[y][x-1] == "1":
counter += 1
except:
pass
try:
if x+1 >= 0 and y >= 0 and inputArray[y][x+1] == "1":
counter += 1
except:
pass
try:
if x-1 >= 0 and y+1 >= 0 and inputArray[y+1][x-1] == "1":
counter += 1
except:
pass
try:
if x >= 0 and y+1 >= 0 and inputArray[y+1][x] == "1":
counter += 1
except:
pass
try:
if x+1 >= 0 and y+1 >= 0 and inputArray[y+1][x+1] == "1":
counter += 1
except:
pass
return counter
decode = parser.HTMLParser()
# decode.unescape('█') FULL BLOCK
# decode.unescape('░') LIGHT SHADE
gen = 0
while True:
newPrint = deepcopy(inputArray)
for lineKey, line in enumerate(inputArray):
for charKey, char in enumerate(line):
if char == "1":
newPrint[lineKey][charKey] = decode.unescape('█')
elif char == "0":
newPrint[lineKey][charKey] = decode.unescape('░')
print("Generation: ",str(gen),"\n\n")
print( '\n'.join([''.join(line) for line in newPrint]))
gen+=1
inputArray = nextGen(inputArray)
system("pause")
system('cls' if name=='nt' else 'clear')