This repository was archived by the owner on Jan 18, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path3Lights.py
More file actions
324 lines (220 loc) · 8.17 KB
/
3Lights.py
File metadata and controls
324 lines (220 loc) · 8.17 KB
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
#19/05/15
import pygame, random, datetime, time
from pygame.locals import *
from astro_pi import AstroPi
ap = AstroPi()
pygame.init()
pygame.display.set_mode((640, 480))
def handle_event(event):
if event.key == pygame.K_DOWN:
return "DOWN"
elif event.key == pygame.K_UP:
return "UP"
elif event.key == pygame.K_LEFT:
return "LEFT"
elif event.key == pygame.K_RIGHT:
return "RIGHT"
elif event.key == pygame.K_RETURN:
return "RETURN"
#Gets a joystick input with optional timeout
def joystick(timeout=-1):
running = True
startTime = datetime.datetime.now()
while running:
for event in pygame.event.get():
if event.type == KEYUP:
jPressed = handle_event(event)
running = False
endTime = datetime.datetime.now()
totalTime = endTime - startTime
if timeout != -1:
if totalTime.seconds >= timeout:
return "NONE"
return jPressed
#Print the board onto the pi
def print_board(pointer):
lightOn = [155,155,31]
lightOff = [0,0,0]
blueLight = [155,0,155]
pointerColour = [155,155,155]
for row in range(boundaryStart,boundaryEnd):
for col in range(boundaryStart, boundaryEnd):
pos = fullBoard[row][col]
pointerPos = pointer[row][col]
if pos == 1:
ap.set_pixel(row, col, lightOn)
elif pos == 0:
ap.set_pixel(row, col, lightOff)
elif row == 3 and col == boundaryStart:
ap.set_pixel(row, col, blueLight)
if pointerPos == 1:
ap.set_pixel(row, col, pointerColour)
def check_move(position):
global pointerMiddle
if position >= boundaryStart and position < boundaryEnd:
return True
elif position >= boundaryStart-1 and pointerMiddle[1] == 4:
return True
return False
def check_win():
for i in range(boundaryStart, boundaryEnd):
for l in range(boundaryStart, boundaryEnd):
if fullBoard[i][l] == 1:
return False
return True
def change_board(location):
if check_move(location[0]-1):
state = fullBoard[location[0]-1][location[1]]
fullBoard[location[0]-1][location[1]] = 1 - state
if check_move(location[0]+1):
state = fullBoard[location[0]+1][location[1]]
fullBoard[location[0]+1][location[1]] = 1 - state
if check_move(location[1]-1):
state = fullBoard[location[0]][location[1]-1]
fullBoard[location[0]][location[1]-1] = 1 - state
if check_move(location[1]+1):
state = fullBoard[location[0]][location[1]+1]
fullBoard[location[0]][location[1]+1] = 1 - state
state = fullBoard[location[0]][location[1]]
fullBoard[location[0]][location[1]] = 1 - state
def move_pointer(state):
global moves, pointerMiddle
#Move the pointer
direction = joystick(0.67)
if direction == "UP":
if check_move(pointerMiddle[1]-1):
pointerMiddle[1] -= 1
elif direction == "DOWN":
if check_move(pointerMiddle[1]+1):
pointerMiddle[1] += 1
elif direction == "RIGHT":
if check_move(pointerMiddle[0]+1):
pointerMiddle[0] += 1
elif direction == "LEFT":
if check_move(pointerMiddle[0]-1):
pointerMiddle[0] -= 1
elif direction == "RETURN":
moves += 1
if pointerMiddle[0] < boundaryStart:
pygame.quit()
quit()
change_board(pointerMiddle)
#Updates the pointer list
pointer = [[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0]]
pointer[pointerMiddle[0]][pointerMiddle[1]] = 1 * state
if check_move(pointerMiddle[0]-1):
pointer[pointerMiddle[0]-1][pointerMiddle[1]] = 1 * state
if check_move(pointerMiddle[0]+1):
pointer[pointerMiddle[0]+1][pointerMiddle[1]] = 1 * state
if check_move(pointerMiddle[1]-1):
pointer[pointerMiddle[0]][pointerMiddle[1]-1] = 1 * state
if check_move(pointerMiddle[1]+1):
pointer[pointerMiddle[0]][pointerMiddle[1]+1] = 1 * state
print_board(pointer)
if state == 1:
return 0
else:
return 1
global pointerMiddle, fullBoard, boundaryStart, boundaryEnd, moves
moves = 0
pointerMiddle = [3,3]
boundaryStart = 1
boundaryEnd = 6
fullBoard = []
def make_new():
#Allows you to cheat if you are on a computer and works out minimum number of moves
coordinates = []
ordered = []
#Create the board
for i in range(8):
#Make 8 rows so it's easier to calculate positions that need changing
fullBoard.append([])
for l in range(8):
#Make them all off so when puzzle created, solution is availiable
fullBoard[i].append(0)
#Make the puzzle
for i in range(32):
#The boundary means it only creates the puzzle inside the level bounds
randRow = random.randint(boundaryStart,boundaryEnd-1)
randCol = random.randint(boundaryStart,boundaryEnd-1)
randomLoc = [randRow, randCol]
coordinates.append(randomLoc)
#Simulate a user pressing the buttons as then the puzzle is definitely
#Solvable
change_board(randomLoc)
#Initialises the flashing pointer
state = 0
#Clear the board
ap.clear()
red = [155,0,0]
blue = [0,0,155]
#Makes the boar the red colour
#Only need to change the pixels once instead of every click as the only part of
#The board that changes is the boundary. This is more efficient and easier
for row in range(8):
for col in range(8):
if row == 0 and col == 4:
ap.set_pixel(row, col, blue)
else:
ap.set_pixel(row, col, red)
playing = True
ordered = sorted(coordinates)
number = []
count = 0
previous = [-1,-1]
#Find irrelevant moves
for i in ordered:
if i != previous:
previous = i
number.append(count)
count = 1
else:
count += 1
number.append(count)
number.pop(0)
removeFrom = 0
for number in number:
if number % 2 == 0:
for i in range(number):
ordered.pop(removeFrom)
else:
for i in range(number -1):
ordered.pop(removeFrom)
removeFrom += 1
print(ordered)
minMoves = len(ordered)
while playing:
#move_pointer(x) returns the state the pointer needs to be changed to
state = move_pointer(state)
if check_win():
playing = False
#If the user beats the round show that message
ap.show_message("You won!")
return minMoves
#Initialises all the statistics that I will be using
level = 0
startTimeV = datetime.datetime.now()
totalMoves = 0
time = []
moveLevel = []
minMoves = []
level = 1
fileObject = open("3Lights/data.txt","a")
fileObject.write(str(datetime.datetime.now()))
#This will make the 3 rounds and increment the boundary accordingly
#The boundary is a global variable and can therefore be seen in all
#Function. It also updates all statistics
startTime = datetime.datetime.now()
minMoves = make_new()
endTime = datetime.datetime.now()
time.append(endTime-startTime)
#Time taken, level, made in moves, moves made
fileObject.write("\n "+str(endTime-startTime)+","+str(minMoves)+","+str(moves))
fileObject.close()