-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpyGoL.py
executable file
·97 lines (80 loc) · 2.24 KB
/
pyGoL.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
#!/usr/bin/python3.5
import copy
import time
import os
import random
import shutil
class Cell(object):
def __init__(self, alive):
self._status=alive
def birth(self):
self._status=True
#print("ACellwasborn")
def death(self):
self._status=False
#print("KilledaCell")
def areYouAlive(self):
return self._status
def display(spielfeld):
for lineIndex, line in enumerate(spielfeld):
ausgabe=""
for field in line:
if field.areYouAlive()==True:
ausgabe+=" O"
else:
ausgabe+=" ."
if lineIndex < 10:
print("0" + str(lineIndex) + " " + ausgabe)
continue
print(str(lineIndex) + " " + ausgabe)
width, length = shutil.get_terminal_size()
width = int((width - 3) / 2)
length -= 2
spielfeld=[]
for i in range(length):
eineZeile=[]
for i in range(width):
if random.randint(1,5) == 1:
eineZeile.append(Cell(True))
else:
eineZeile.append(Cell(False))
spielfeld.append(eineZeile)
spielfeldNew=copy.deepcopy(spielfeld)
print("Generation 0")
display(spielfeld)
for generation in range(1,101):
for lineIndex, line in enumerate(spielfeld):
if lineIndex == 0 or len(spielfeld)-1 == lineIndex:
pass
else:
for fieldIndex, field in enumerate(line):
if fieldIndex == 0 or fieldIndex == len(line) -1:
pass
else:
neighbors=0
for i in range(-1,2):
for j in range(-1,2):
if spielfeld[lineIndex+i][fieldIndex+j].areYouAlive() == True:
neighbors+=1
if field.areYouAlive()==True:
neighbors-=1
if neighbors == 2 or neighbors == 3:
spielfeldNew[lineIndex][fieldIndex].birth()
# print("Zelle"+str(lineIndex)+
# ","+
# str(fieldIndex)+
# "bleibtamLeben")
else:
spielfeldNew[lineIndex][fieldIndex].death()
else:
#print("Dead Cell with " + str(neighbors) + " Neighbours")
#print("Zelle "+str(lineIndex)+", "+str(fieldIndex)+" has "
# + str(neighbors) + " neighbours")
if neighbors == 3:
spielfeldNew[lineIndex][fieldIndex].birth()
# print("Zelle "+str(lineIndex)+", "+str(fieldIndex)+" was born.")
os.system('clear')
print("Generation "+str(generation))
display(spielfeldNew)
spielfeld=copy.deepcopy(spielfeldNew)
time.sleep(0.2)