-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
executable file
·76 lines (55 loc) · 1.82 KB
/
main.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
#!/usr/bin/python3
"""
Game of life implementation in python
"""
import random
import time
import argparse
def initboard(rows, columns):
board = []
for i in range(0, rows):
board.append([])
for i in board:
for _ in range(0, columns):
i.append(round(random.random()))
return board
def printboard(board):
for x in board:
string = ""
for y in x:
if y == 1:
string += "X"
else:
string += " "
print(string)
def nextboard(board):
nextgeneration = board.copy()
# Calculate amount of neighbors
for i in range(0, len(nextgeneration)-1):
for j in range(0, i):
neighbors = [nextgeneration[i-1][j-1], nextgeneration[i-1][j], nextgeneration[i-1][j+1], nextgeneration[i]
[j-1], nextgeneration[i][j+1], nextgeneration[i+1][j-1], nextgeneration[i+1][j], nextgeneration[i+1][j+1]]
aliveneighbors = 0
for neighbor in neighbors:
if neighbor == 1:
aliveneighbors += 1
if nextgeneration[i][j] == 1:
if (aliveneighbors < 2) or (aliveneighbors > 3):
nextgeneration[i][j] = 0
else:
if aliveneighbors == 3:
nextgeneration[i][j] = 1
return nextgeneration
def main():
parser = argparse.ArgumentParser(description="Conways Game of Life")
parser.add_argument("--rows", dest="rows", required=True)
parser.add_argument("--columns", dest="cols", required=True)
args = parser.parse_args()
board = initboard(int(args.rows), int(args.cols))
while True:
time.sleep(1)
nextgen = nextboard(board)
print("---New Gen---")
printboard(nextgen)
if __name__ == '__main__':
main()