-
Notifications
You must be signed in to change notification settings - Fork 1
/
grid example.py
360 lines (295 loc) · 10.2 KB
/
grid example.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#20x20 grid
import pygame
import random
import time
#default values of grid size and starting pattern
patternSelection = 5
size = 16
#Default screen size
WINDOW_SIZE = [800, 800]
grid = []
tracker = []
pattern = []
def rotate_and_center(ds, x, y, image, degrees):
#rotating a square
rotated = pygame.transform.rotate(image, degrees)
rect = rotated.get_rect()
ds.blit(rotated, (x - rect.center[0], y - rect.center[1]))
def printGrid(grid):
#pretty printing of grid
for row in grid:
print(row)
print("\n")
def drawGrid(grid):
# Draw the grid
for row in range(len(grid)):
for column in range(len(grid)):
if grid[row][column] == 0:
img = pygame.image.load('curves_v.bmp')
color = WHITE
if grid[row][column] == 1:
img = pygame.image.load('curves_h.bmp')
color = BLACK
pygame.draw.rect(screen, color,[(WIDTH) * column, (HEIGHT) * row, WIDTH, HEIGHT]) #backup in case of image not loading
screen.blit(pygame.transform.scale(img, (int(WIDTH), int(HEIGHT))), (int(HEIGHT * column), int(HEIGHT * row)))
def updateSquare(row,column):
#draw single square
if grid[row][column] == 0:
img = pygame.image.load('curves_v.bmp')
color = WHITE
if grid[row][column] == 1:
img = pygame.image.load('curves_h.bmp')
color = BLACK
pygame.draw.rect(screen, color,[(WIDTH) * column, (HEIGHT) * row, WIDTH, HEIGHT]) # backup in case of image not loading
screen.blit(pygame.transform.scale(img, (int(WIDTH), int(HEIGHT))), (int(HEIGHT * column), int(HEIGHT * row)))
def createGrid(size):
#create grid of a given size with random 1's & 0's
global grid
grid = []
for i in range(size):
row = []
for x in range(size):
row.append(random.randrange(2))
grid.append(row)
def createTracker():
#create a grid of references to keep track of which elements have been changed
global grid
global tracker
for rowIndex in range(0,len(grid)):
row = []
for cellIndex in range(0, len(grid)):
row.append([rowIndex,cellIndex])
tracker.append(row)
def stripTracker(pattern):
#strip the tracking grid of any references where the element is already the correct value
global grid
global tracker
correct = []
if tracker != []:
for rowIndex, row in enumerate(tracker):
for refIndex, ref in enumerate(row):
if grid[ref[0]][ref[1]] == pattern[ref[0]][ref[1]]:
correct.append(ref)
for rowIndex, row in enumerate(tracker):
newRow = [e for e in row if e not in correct]
tracker[rowIndex] = newRow
if tracker[rowIndex] == []:
del tracker[rowIndex]
def createPattern(patternNumber):
#create a grid with a set pattern
global grid
global pattern
value = 1
if patternNumber == 0:
# create a grid of all 0's
for i in range(0,len(grid)):
row = []
for x in range(0,len(grid)):
row.append(0)
pattern.append(row)
return pattern
elif patternNumber == 1:
# create a grid of all 1's
for i in range(0, len(grid)):
row = []
for x in range(0, len(grid)):
row.append(1)
pattern.append(row)
return pattern
elif patternNumber == 2:
#create a grid of alternating 1's and 0's
firstValue = 1
for i in range(0, len(grid)):
row = []
value = firstValue
for x in range(0, len(grid)):
if value == 1:
row.append(value)
value = 0
else:
row.append(value)
value = 1
pattern.append(row)
if firstValue == 1:
firstValue = 0
else:
firstValue = 1
return pattern
elif patternNumber == 3:
#create a grid of alternating 1's and 0's starting on 1
for i in range(0, len(grid)):
row = []
value = 1
for x in range(0, len(grid)):
if value == 1:
row.append(value)
value = 0
else:
row.append(value)
value = 1
pattern.append(row)
return pattern
elif patternNumber == 4:
#create a grid of alternating lines of 1's and 0's
for i in range(0, len(grid)):
row = []
if value == 1:
for x in range(0, len(grid)):
row.append(value)
value = 0
else:
for x in range(0, len(grid)):
row.append(value)
value = 1
pattern.append(row)
return pattern
elif patternNumber == 5:
#create a grid of alternating 1's and 0's starting on 0
for i in range(0, len(grid)):
row = []
value = 0
for x in range(0, len(grid)):
if value == 1:
row.append(value)
value = 0
else:
row.append(value)
value = 1
pattern.append(row)
return pattern
elif patternNumber == 6:
#create a grid of alternating 11's and 00's
firstValue = 1
for i in range(0, len(grid)):
row = []
value = firstValue
repeat = True
for x in range(0, len(grid)):
if value == 1 and repeat == False:
row.append(value)
value = 0
repeat = True
elif value == 1 and repeat == True:
row.append(value)
repeat = False
elif value == 0 and repeat == False:
row.append(value)
value = 1
repeat = True
elif value == 0 and repeat == True:
row.append(value)
repeat = False
pattern.append(row)
if firstValue == 1:
firstValue = 0
else:
firstValue = 1
return pattern
elif patternNumber == 7:
#create a grid of alternating 11's and 00's starting with 11
for i in range(0, len(grid)):
row = []
value = 1
repeat = True
for x in range(0, len(grid)):
if value == 1 and repeat == False:
row.append(value)
value = 0
repeat = True
elif value == 1 and repeat == True:
row.append(value)
repeat = False
elif value == 0 and repeat == False:
row.append(value)
value = 1
repeat = True
elif value == 0 and repeat == True:
row.append(value)
repeat = False
pattern.append(row)
return pattern
def nextPattern(patternNumber):
#loops through all of the patterns
global pattern
randomFlag = True
if randomFlag == True:
r = list(range(0, 7+1))
r.remove(patternNumber)
patternNumber = random.choice(r)
else:
patternNumber += 1
if patternNumber == 8:
patternNumber = 0
pattern = []
print("Pattern No.: " + str(patternNumber))
pattern = createPattern(patternNumber)
stripTracker(pattern)
return patternNumber
def randomReplace():
#randomly replace elements until a pattern has been achieved
global grid
global tracker
global pattern
global patternSelection
global screen
global WIDTH
global HEIGHT
global degrees
if tracker != []:
selectedRow = random.randint(0, len(tracker) - 1)
selectedCell = random.randint(0,len(tracker[selectedRow]) - 1)
contents = tracker[selectedRow].pop(selectedCell)
grid[contents[0]][contents[1]] = pattern[contents[0]][contents[1]]
updateSquare(contents[0],contents[1])
#----------------------
#testing rotations - not working yet
'''
img = pygame.image.load('curves_v.bmp')
img = pygame.transform.scale(img, (int(WIDTH), int(HEIGHT)))
# rotate the image around 360 degrees but centralise it to x and y
rotate_and_center(screen, int(HEIGHT * contents[1]), int(HEIGHT * contents[0]), img, degrees)
# increment the rotation. reset rotation if degrees is greater than 359 (not necessary but cleaner IMO)
if degrees < 90:
degrees += 10
else:
degrees = 0
'''
if len(tracker[selectedRow]) == 0:
tracker.pop(selectedRow)
else:
createTracker()
patternSelection = nextPattern(patternSelection)
#-----------------------------------------------------------#
# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
#setup for initial run
createGrid(size)
createTracker()
pattern = createPattern(patternSelection)
stripTracker(pattern)
printGrid(grid)
# Initialize pygame
pygame.init()
# Set the HEIGHT and WIDTH of the screen
screen = pygame.display.set_mode(WINDOW_SIZE)
WIDTH = WINDOW_SIZE[0] / len(grid)
HEIGHT = WINDOW_SIZE[0] / len(grid)
# Set title of screen
pygame.display.set_caption("Grid Pattern")
# Loop until the user clicks the close button.
done = False
# Used to manage how fast the screen updates
clock = pygame.time.Clock()
#draw the grid once pygame is setup
drawGrid(grid)
#for use with rotation
degrees = 0
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
randomReplace()
pygame.display.flip()
clock.tick(60)
pygame.quit()