-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game of Life.py
119 lines (93 loc) · 3.4 KB
/
Game of Life.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
import random
# Game of Life in Python
class GameOfLife:
def __init__(self, board):
self.board = board
self.n = len(board)
def neighbourSum(self, i, j):
neighbours = [self.board[(i-1+self.n)%self.n][(j-1+self.n)%self.n],
self.board[(i-1+self.n)%self.n][j],
self.board[(i-1+self.n)%self.n][(j+1)%self.n],
self.board[i%self.n][(j+1)%self.n],
self.board[(i+1)%self.n][(j+1)%self.n],
self.board[(i+1)%self.n][j],
self.board[(i+1)%self.n][(j-1+self.n)%self.n],
self.board[i][(j-1+self.n)%self.n]]
return sum(neighbours)
def nextPattern(self):
if self.board is None or self.n == 0:
raise ValueError("Board not initialized or has size 0")
new_board = [[0 for j in range(self.n)] for i in range(self.n)]
for i in range(self.n):
for j in range(self.n):
live_neighbours = self.neighbourSum(i, j)
if self.board[i][j] == 1:
if live_neighbours < 2:
new_board[i][j] = 0
elif live_neighbours == 2 or live_neighbours == 3:
new_board[i][j] = 1
elif live_neighbours > 3:
new_board[i][j] = 0
else:
if live_neighbours == 3:
new_board[i][j] = 1
self.board = new_board
def printBoard(self):
for i in range(self.n):
for j in range(self.n):
if self.board[i][j] == 0:
print(" ", end=" ")
else:
print("*", end=" ")
print()
print()
def generate_board(n):
board = []
for i in range(n):
row = []
for j in range(n):
row.append(random.randint(0,1))
board.append(row)
return board
def main():
# Create two initial patterns for the game
n=2
pattern1 = generate_board(n)
pattern2 = generate_board(n)
pattern3 = generate_board(n)
# Ask the user to choose one of the patterns
print("Choose one of the following patterns to start the game:")
#pattern1
obj1 = GameOfLife(pattern1);
print("Pattern 1:")
obj1.printBoard()
#pattern2
obj2 = GameOfLife(pattern2);
print("Pattern 2:")
obj2.printBoard()
#pattern3
obj3= GameOfLife(pattern3);
print("Pattern 3:")
obj3.printBoard()
choice = int(input("Enter the number of the pattern you want to play with (1 or 2 or 3): "))
if choice == 1:
obj1 = GameOfLife(pattern1);
elif choice == 2:
obj1 = GameOfLife(pattern2);
elif choice == 3:
obj1 = GameOfLife(pattern3);
else :
print("Wrong Input! Input should be between 1-3")
# Start the game
print("The game starts now!")
while True:
# Print the current pattern
obj1.printBoard()
# Ask the user if they want to see the next pattern
next_step = input("Do you want to see the next pattern? (y/Y). To end the game enter any other symbol ")
if next_step.lower() != "y":
break
# Calculate and update the next pattern
obj1.nextPattern()
if __name__ == "__main__":
main()