-
Notifications
You must be signed in to change notification settings - Fork 0
/
homeScreen.py
406 lines (360 loc) · 14.8 KB
/
homeScreen.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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
try: from cmu_cs3_graphics import *
except: from cmu_graphics import *
import random
import copy
from runAppWithScreens import *
##################################
# homeScreen
##################################
def homeScreen_onScreenStart(app):
app.width = 1200
app.height = 800
app.largeButtonWidth = 600
app.largeButtonHeight = 75
app.smallButtonWidth = 120
app.smallButtonHeight = 50
app.level = None
app.board = None
app.inputTextMode = False
app.inputText = ''
app.boardFile = None
appStarted(app)
def homeScreen_onKeyPress(app, key):
if app.inputTextMode:
if key == 'backspace':
app.inputText = app.inputText[:-1]
elif key != 'enter':
app.inputText += key
elif key == 'enter':
app.inputTextMode = False
app.boardFile = f'tp-starter-files/boards/{app.inputText}.png.txt'
app.inputText = ''
newBoard = readFile(app.boardFile)
app.board = boardTo2DList(newBoard)
app.legalsBoard = findInitialLegalsBoard(app)
app.solutionBoard = solveSudoku(app, copy.deepcopy(app.board))
app.initialVals = findInitialVals(app.board)
app.boardEditMode = False
setActiveScreen('playScreen')
elif key == 'e':
app.level = 'easy'
createBoard(app)
setActiveScreen('playScreen')
elif key == 'm':
app.level = 'medium'
createBoard(app)
setActiveScreen('playScreen')
elif key == 'h':
app.level = 'hard'
createBoard(app)
setActiveScreen('playScreen')
elif key == 'v':
app.level = 'evil'
createBoard(app)
setActiveScreen('playScreen')
elif key == 'c':
app.level = 'custom'
createBoard(app)
setActiveScreen('playScreen')
elif key == 'u':
app.inputTextMode = True
elif key == 'r' and app.board!=None: setActiveScreen('playScreen')
elif key == 'g': setActiveScreen('helpScreen')
elif key == 'p': setActiveScreen('prefScreen')
def homeScreen_redrawAll(app):
drawLabel('sudoku', app.width/2, 150, size=140, font = 'monospace',)
drawRect(app.width/2 - app.largeButtonWidth/2, 250, app.largeButtonWidth, 2*app.largeButtonHeight, fill = 'gainsboro')
# go to play
drawLabel('new game', app.width/2, 250 + app.largeButtonHeight/2, size=48, font = 'monospace')
drawRect(app.width/2 - 2*app.smallButtonWidth - 36, 325, app.smallButtonWidth, app.smallButtonHeight, fill = 'whiteSmoke')
drawRect(app.width/2 - app.smallButtonWidth - 12, 325, app.smallButtonWidth, app.smallButtonHeight, fill = 'whiteSmoke')
drawRect(app.width/2 + 12, 325, app.smallButtonWidth, app.smallButtonHeight, fill = 'whiteSmoke')
drawRect(app.width/2 + app.smallButtonWidth + 36, 325, app.smallButtonWidth, app.smallButtonHeight, fill = 'whiteSmoke')
drawLabel('easy(e)', app.width/2 - 1.5*app.smallButtonWidth - 36, 350, size=20, font = 'monospace')
drawLabel('medium(m)', app.width/2 - 0.5*app.smallButtonWidth - 12, 350, size=20, font = 'monospace')
drawLabel('hard(d)', app.width/2 + 0.5*app.smallButtonWidth + 12, 350, size=20, font = 'monospace')
drawLabel('evil(v)', app.width/2 + 1.5*app.smallButtonWidth + 36, 350, size=20, font = 'monospace')
# create game
drawRect(app.width/2 - app.largeButtonWidth/2, 420, 2*app.smallButtonWidth + 48, app.largeButtonHeight, fill = 'gainsboro')
drawLabel('create (c)', app.width/2 - app.smallButtonWidth - 36, 420 + app.largeButtonHeight/2, size=30, font = 'monospace')
# go to preferences
drawRect(app.width/2 - app.smallButtonWidth - 24, 610, 2*app.smallButtonWidth + 48, app.largeButtonHeight, fill = 'gainsboro')
drawLabel('preferences (p)', app.width/2, 610 + app.largeButtonHeight/2, size=30, font = 'monospace')
# upload game
drawRect(app.width/2 + 12, 420, 2*app.smallButtonWidth + 48, app.largeButtonHeight, fill = 'gainsboro')
drawLabel('upload (u)', app.width/2 + 36 + app.smallButtonWidth, 420 + app.largeButtonHeight/2, size=30, font = 'monospace')
if app.inputTextMode:
drawRect(app.width/2 - app.largeButtonWidth/2, 610, app.largeButtonWidth, app.largeButtonHeight, fill = 'gainsboro')
drawLabel('write level-number:', app.width/2, 635, size=20, font = 'monospace')
drawLabel(app.inputText, app.width/2, 665, size=20, font = 'monospace')
# resume board
drawRect(app.width/2 - app.largeButtonWidth/2, 515, 2*app.smallButtonWidth + 48, app.largeButtonHeight, fill = 'gainsboro')
drawLabel('resume (r)', app.width/2 - app.smallButtonWidth - 36, 515 + app.largeButtonHeight/2, size=30, font = 'monospace')
# go to help
drawRect(app.width/2 + 12, 515, 2*app.smallButtonWidth + 48, app.largeButtonHeight, fill = 'gainsboro')
drawLabel('guide (g)', app.width/2 + 36 + app.smallButtonWidth, 515 + app.largeButtonHeight/2, size=30, font = 'monospace')
def homeScreen_onMousePress(app, mouseX, mouseY):
# new game
if ((app.width/2 - 2*app.smallButtonWidth - 36 <= mouseX <= app.width/2 - app.smallButtonWidth - 36) and
(325 <= mouseY <= 325 + app.smallButtonHeight)):
app.level = 'easy'
createBoard(app)
setActiveScreen('playScreen')
elif ((app.width/2 - app.smallButtonWidth - 12 <= mouseX <= app.width/2 - 12) and
(325 <= mouseY <= 325 + app.smallButtonHeight)):
app.level = 'medium'
createBoard(app)
setActiveScreen('playScreen')
elif ((app.width/2 + 12 <= mouseX <= app.width/2 + app.smallButtonWidth + 12) and
(325 <= mouseY <= 325 + app.smallButtonHeight)):
app.level = 'hard'
createBoard(app)
setActiveScreen('playScreen')
elif ((app.width/2 + app.smallButtonWidth + 36 <= mouseX <= app.width/2 + 2*app.smallButtonWidth + 36) and
(325 <= mouseY <= 325 + app.smallButtonHeight)):
app.level = 'evil'
createBoard(app)
setActiveScreen('playScreen')
# create
if ((app.width/2 - app.largeButtonWidth/2 <= mouseX <= app.width/2 - app.largeButtonWidth/2 + 2*app.smallButtonWidth + 48) and
(420 <= mouseY <= 420 + app.largeButtonHeight)):
app.level = 'custom'
createBoard(app)
setActiveScreen('playScreen')
# upload
elif ((app.width/2 + 12 <= mouseX <= app.width/2 + 60 + 2*app.smallButtonWidth) and
(420 <= mouseY <= 420 + app.largeButtonHeight)):
app.inputTextMode = True
# resume
elif ((app.width/2 - app.largeButtonWidth/2 <= mouseX <= app.width/2 - app.largeButtonWidth/2 + 2*app.smallButtonWidth + 48) and
(515 <= mouseY <= 515 + app.largeButtonHeight)):
if app.board != None:
setActiveScreen('playScreen')
# guide
if ((app.width/2 + 12 <= mouseX <= app.width/2 + 60 + 2*app.smallButtonWidth) and
(515 <= mouseY <= 515 + app.largeButtonHeight)):
setActiveScreen('helpScreen')
# preferences
if ((app.width/2 - app.smallButtonWidth <= mouseX <= app.width/2 + app.smallButtonWidth) and
(610 <= mouseY <= 610 + app.largeButtonHeight)):
setActiveScreen('prefScreen')
##################################
# FILE-RELATED FUNCTIONS
##################################
def chooseRandomNumber(app):
if app.level == 'evil':
randomBoardNumber = random.choice(range(25)) + 1
else:
randomBoardNumber = random.choice(range(50)) + 1
if randomBoardNumber < 10:
randomBoardNumber = '0' + str(randomBoardNumber)
return str(randomBoardNumber)
# readFile from Term Project Guide
def readFile(path):
with open(path, "rt") as f:
return f.read()
# writeFile from Term Project Guide
def writeFile(path, contents):
with open(path, "wt") as f:
f.write(contents)
def boardTo2DList(board):
L = []
for line in board.splitlines():
M = line.split(' ')
L.append(M)
return L
def change2DListToTextBoard(board):
rows = len(board)
textBoard = ''
textRows = []
for row in range(rows):
textRow = ' '.join(board[row])
textRows.append(textRow)
textBoard = '\n'.join(textRows)
return textBoard
def saveFile(app):
solutionInTextMode = change2DListToTextBoard(app.board)
writeFile(f'solutions-folder/{app.boardFile}', solutionInTextMode)
##################################
# CREATE BOARDS
##################################
def createBoard(app):
if app.level != 'custom':
randomNumber = chooseRandomNumber(app)
app.boardFile = f'tp-starter-files/boards/{app.level}-{randomNumber}.png.txt'
newBoard = readFile(app.boardFile)
app.board = boardTo2DList(newBoard)
app.legalsBoard = findInitialLegalsBoard(app)
app.solutionBoard = solveSudoku(app, copy.deepcopy(app.board))
app.initialVals = findInitialVals(app.board)
app.boardEditMode = False
else:
app.board = [['0' for v in range(app.cols)] for w in range(app.rows)]
app.boardEditMode = True
app.legalsBoard = findInitialLegalsBoard(app)
app.solutionBoard = [['0' for v in range(app.cols)] for w in range(app.rows)]
app.initialVals = set()
def appStarted(app):
app.selection = None
app.hoverSelection = None
app.hoverNumber = None
app.isGameOver = False
app.paused = False
app.mistakes = 0
app.showLegals = False
app.legalsEditMode = False
app.autoPlayOn = False
app.contestMode = False
app.fontColor = 'black'
app.selectionColorNum = 'pink'
app.hoverColorNum = 'lavenderBlush'
app.selectionColor = 'lightBlue'
app.hoverColor = 'aliceBlue'
app.hintColor = 'lightGreen'
app.incorrectColor = 'maroon'
def findInitialVals(board):
initialVals = set()
for row in range(len(board)):
for col in range(len(board[0])):
initialNum = board[row][col]
if (initialNum != '0'):
initialVals.add((row, col))
return initialVals
def findInitialLegalsBoard(app):
board = [[None for v in range(app.rows)] for y in range(app.cols)]
for row in range(app.rows):
for col in range(app.cols):
block = row//3 * 3 + col//3
cellLegals = Legals(app.board, row, col, block)
board[row][col] = cellLegals
if app.board[row][col] != '0':
board[row][col].legals = {app.board[row][col]}
return board
##################################
# FIND LEGALS
##################################
class Legals():
def __init__(self, board, row, col, block):
self.board = board
self.row = row
self.col = col
self.block = block
self.illegals = findValuesinRow(self.board, self.row) | findValuesinCol(self.board, self.col) | findValuesinBlock(self.board, self.block)
self.legals = {'1','2','3','4','5','6','7','8','9'} - self.illegals
self.manualLegals = set()
self.manualIllegals = set()
self.shownLegals = (self.legals | self.manualLegals) - self.manualIllegals
def findValues(L):
seen = set()
for value in L:
if value != '0':
seen.add(value)
return seen
def findValuesinRow(board, row):
return findValues(board[row])
def findValuesinCol(board, col):
rows = len(board)
values = [board[row][col] for row in range(rows)]
return findValues(values)
def findValuesinBlock(board, block):
blockSize = 3
startRow = block // blockSize * blockSize
startCol = block % blockSize * blockSize
values = []
for drow in range(blockSize):
for dcol in range(blockSize):
row, col = startRow + drow, startCol + dcol
values.append(board[row][col])
return findValues(values)
##################################
# FIND BOARD SOLUTION
##################################
# Adapted from solveMiniSudoku (8.11) and isLegalSudoku (4.5)
def solveSudoku(app, board):
if findNextEmptyCellFromHere(app, board, -1, app.cols) == None:
return board
else:
# rewrite find next singleton
if findNextSingletonCellForSolver(app, board) != None:
row, col = findNextSingletonCellForSolver(app, board)
else:
row, col = findNextEmptyCellFromHere(app, board, -1, app.cols)
cellLegals = app.legalsBoard[row][col]
for i in cellLegals.legals:
board[row][col] = i
if isBoardLegal(app, board):
solution = solveSudoku(app, board)
if solution != None:
return solution
board[row][col] = '0'
"""
for i in range(1,10):
board[row][col] = str(i)
if isBoardLegal(app, board):
solution = solveSudoku(app, board)
if solution != None:
return solution
board[row][col] = '0'
"""
return None
def findNextEmptyCellFromHere(app, board, givenRow, givenCol):
for row in range(givenRow, app.rows):
for col in range(app.cols):
if board[row][col] == '0' and not (row == givenRow and col <= givenCol):
return row, col
return None
def findNextSingletonCellForSolver(app, board):
for row in range(app.rows):
for col in range(app.cols):
if board[row][col] == '0':
cellLegals = app.legalsBoard[row][col]
if len(cellLegals.shownLegals) == 1:
return row, col
return None
def findNextSingletonCell(app, board, givenRow, givenCol):
app.selection = findNextEmptyCellFromHere(app, app.board, givenRow, givenCol)
if app.selection == None:
return None
row, col = app.selection
cellLegals = app.legalsBoard[row][col]
if len(cellLegals.shownLegals) == 1:
for value in cellLegals.shownLegals:
return row, col, value
else:
return findNextSingletonCell(app, app.board, row, col)
return None
def isBoardLegal(app, board):
for row in range(app.rows):
if not isLegalRow(board, row):
return False
for col in range(app.cols):
if not isLegalCol(board, col):
return False
blocks = app.rows
for block in range(blocks):
if not isLegalBlock(board, block):
return False
return True
def areLegalValues(L):
seen = set()
for value in L:
if value != '0' and value in seen:
return False
seen.add(value)
return True
def isLegalRow(board, row):
return areLegalValues(board[row])
def isLegalCol(board, col):
rows = len(board)
values = [board[row][col] for row in range(rows)]
return areLegalValues(values)
def isLegalBlock(board, block):
blockSize = 3
startRow = block // blockSize * blockSize
startCol = block % blockSize * blockSize
values = []
for drow in range(blockSize):
for dcol in range(blockSize):
row, col = startRow + drow, startCol + dcol
values.append(board[row][col])
return areLegalValues(values)