-
Notifications
You must be signed in to change notification settings - Fork 1
/
CGOL.py
187 lines (156 loc) · 5.71 KB
/
CGOL.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import pygame
import numpy as np
from pathlib import Path
import os
import sys
import time
#colors
one = (246, 244, 210)
two = (203, 223, 189)
pygame.init()
size = (716, 716)
screen = pygame.display.set_mode(size)
pygame.display.set_caption("Conway's Game of Life")
programIcon = pygame.image.load((os.fspath(Path(__file__).resolve().parent / 'icon.png')))
pygame.display.set_icon(programIcon)
width = 15
height = 15
margin = 2
grid = [[0 for x in range(44)] for y in range(44)]
done = False
clock = pygame.time.Clock()
neighbour=[]
def neighbourr():
global grid,neighbour
neighbour = [0 for col in range(len(grid)) for row in range(len(grid))]
count=0
N = len(grid)
for i in range(len(grid)):
for j in range(len(grid)):
if (i > 0 and grid[i - 1][j] == 1):
neighbour[count]=neighbour[count]+1
if (j > 0 and grid[i][j - 1] == 1):
neighbour[count]=neighbour[count]+1
if (i > 0 and j > 0 and
grid[i - 1][j - 1] == 1):
neighbour[count]=neighbour[count]+1
if (i < N - 1 and grid[i + 1][j] == 1):
neighbour[count]=neighbour[count]+1
if (j < N - 1 and grid[i][j + 1] == 1):
neighbour[count]=neighbour[count]+1
if (i < N - 1 and j < N - 1
and grid[i + 1][j + 1] == 1):
neighbour[count]=neighbour[count]+1
if (i < N - 1 and j > 0
and grid[i + 1][j - 1] == 1):
neighbour[count]=neighbour[count]+1
if (i > 0 and j < N - 1
and grid[i - 1][j + 1] == 1):
neighbour[count]=neighbour[count]+1
count+=1
def cgol():
global grid,neighbour
neighbourr()
N = len(grid)
arr=grid.copy()
for i in range(len(grid)):
for j in range(len(grid[i])):
if grid[i][j] == 1:
if (neighbour[i*len(grid[0]) +j] < 2) or (neighbour[i*len(grid[0]) +j] > 3):
arr[i][j] = 0
else:
if neighbour[i*len(grid[0]) +j] == 3:
arr[i][j] = 1
grid = arr
def cgol_auto():
exitt = True
while exitt:
for event in pygame.event.get():
if event.type == pygame.KEYDOWN:
if event.key == pygame.K_SPACE:
exitt = False
break
count = 0
for i in range(len(grid)):
if(1 not in grid[i]):
count+=1
if(count==len(grid)):
exitt = False
break
cgol()
for row in range(len(grid)):
for column in range(len(grid[row])):
if grid[row][column] == 1:
color = two
else:
color = one
pygame.draw.rect(screen, color, [margin + (margin + width) * column, margin + (margin + height) * row, width, height])
pygame.display.flip()
clock.tick(60)
time.sleep(0.3)
def savegrid():
global grid
np.savetxt((os.fspath(Path(__file__).resolve().parent / "example.txt")),grid)
def loadgrid(index):
global grid
if(index ==0):
grid = np.loadtxt((os.fspath(Path(__file__).resolve().parent / "example.txt"))).tolist()
elif(index ==1):
grid = np.loadtxt(os.fspath(Path(__file__).resolve().parent / "Ship/example.txt")).tolist()
elif(index ==2):
grid = np.loadtxt(os.fspath(Path(__file__).resolve().parent / "Ship2/example.txt")).tolist()
elif(index ==3):
grid = np.loadtxt(os.fspath(Path(__file__).resolve().parent / "Diamond/example.txt")).tolist()
elif(index ==4):
grid = np.loadtxt(os.fspath(Path(__file__).resolve().parent / "A for all/example.txt")).tolist()
elif(index ==5):
grid = np.loadtxt(os.fspath(Path(__file__).resolve().parent / "Martins/example.txt")).tolist()
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
elif event.type == pygame.KEYDOWN:
if event.key == pygame.K_ESCAPE:
pygame.quit()
if event.key == pygame.K_s:
savegrid()
if event.key == pygame.K_l:
loadgrid(0)
if event.key == pygame.K_1:
loadgrid(1)
if event.key == pygame.K_2:
loadgrid(2)
if event.key == pygame.K_3:
loadgrid(3)
if event.key == pygame.K_4:
loadgrid(4)
if event.key == pygame.K_5:
loadgrid(5)
if event.key == pygame.K_RETURN:
cgol_auto()
if event.key == pygame.K_r:
grid = [[0 for x in range(44)] for y in range(44)]
if event.key == pygame.K_RIGHT :
cgol()
if pygame.mouse.get_pressed()[2]:
column = pos[0] // (width + margin)
row = pos[1] // (height + margin)
grid[row][column] = 0
if pygame.mouse.get_pressed()[0]:
column = pos[0] // (width + margin)
row = pos[1] // (height + margin)
grid[row][column] = 1
pos = pygame.mouse.get_pos()
x = pos[0]
y = pos[1]
screen.fill(two)
for row in range(len(grid)):
for column in range(len(grid[row])):
if grid[row][column] == 1:
color = two
else:
color = one
pygame.draw.rect(screen, color, [margin + (margin + width) * column, margin + (margin + height) * row, width, height])
pygame.display.flip()
clock.tick(60)
pygame.quit()