-
Notifications
You must be signed in to change notification settings - Fork 1
/
gameOfLife.py
150 lines (126 loc) · 3.24 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
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
import time, os, platform
delay = 0.25
def clear():
if platform.system() == "Windows":
os.system('cls')
else:
os.system('clear')
boardSize = {
"cols": 5,
"rows": 5,
}
world = []
row = 0
for _ in range(boardSize['rows']):
world.append([])
for _ in range(boardSize['cols']):
world[row].append(". ")
row += 1
def findNeighbors(y, x):
neighbors = ["", "", "", "", "", "", "", "", ""]
# Neighbor 1
try:
neighbors[0] = world[y-1][x-1]
except IndexError:
neighbors[0] = ". "
# Neighbor 2
try:
neighbors[1] = world[y-1][x]
except IndexError:
neighbors[1] = ". "
# Neighbor 3
try:
neighbors[2] = world[y-1][x+1]
except IndexError:
neighbors[2] = ". "
# Neighbor 4
try:
neighbors[3] = world[y][x-1]
except IndexError:
neighbors[3] = ". "
# Neighbor 5
try:
neighbors[4] = world[y][x]
except IndexError:
neighbors[4] = ". "
# Neighbor 6
try:
neighbors[5] = world[y][x+1]
except IndexError:
neighbors[5] = ". "
# Neighbor 7
try:
neighbors[6] = world[y+1][x-1]
except IndexError:
neighbors[6] = ". "
# Neighbor 8
try:
neighbors[7] = world[y+1][x]
except IndexError:
neighbors[7] = ". "
# Neighbor 9
try:
neighbors[8] = world[y+1][x+1]
except IndexError:
neighbors[8] = ". "
return neighbors.count("# ")
def killOrNot(y, x):
global world
if findNeighbors(y, x) == 0:
world[y][x] = ". "
else:
if world[y][x] == "# ":
if findNeighbors(y, x) < 2 or findNeighbors(y, x) > 3:
world[y][x] = ". "
elif findNeighbors(y, x) == 2 or findNeighbors(y, x) == 3:
world[y][x] = "# "
elif world[y][x] == ". ":
if findNeighbors(y, x) < 2 or findNeighbors(y, x) > 3:
world[y][x] = ". "
elif findNeighbors(y, x) == 3:
world[y][x] = "# "
def printWorld(iterate: int):
print(f"Current Iteration: {str(iterate)}")
world[2][2] = '# '
row = 0
col = 0
toPrint = ""
for _ in range(len(world)):
for _ in range(len(world[row])):
toPrint += world[row][col]
col += 1
col = 0
row += 1
toPrint += "\n"
print(toPrint)
def startGame(times):
iteration = 0
if times == 0:
while True:
printWorld(iteration)
x = 0
y = 0
for _ in range(len(world)):
for _ in range(len(world[y])):
killOrNot(y, x)
x += 1
x = 0
y += 1
iteration += 1
time.sleep(delay)
clear()
else:
for _ in range(times):
printWorld(iteration)
x = 0
y = 0
for _ in range(len(world)):
for _ in range(len(world[y])):
killOrNot(y, x)
x += 1
x = 0
y += 1
iteration += 1
time.sleep(delay)
startGame(0)
# print(world)