-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsudoku_gui.py
275 lines (232 loc) · 11.2 KB
/
sudoku_gui.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
from sudoku_alg import valid, solve, find_empty
from copy import deepcopy
from sys import exit
import pygame
import time
import random
pygame.init()
def generate():
'''Randomly generates a Sudoku grid/board'''
while True: #return will interrupt the loop
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
board = [[0 for i in range(9)] for j in range (9)]
# puts one random number, then solves the board to generate a board
for i in range(9):
for j in range(9):
if random.randint(1, 10) >= 5:
board[i][j] = random.randint(1, 9) #plug in random number at random spot
if valid(board, (i, j), board[i][j]):
continue
else:
board[i][j] = 0
partialBoard = deepcopy(board) #copies board without being modified after solve is called
if solve(board):
return partialBoard
class Board:
'''A sudoku board made out of Tiles'''
def __init__(self, window):
self.board = generate()
self.solvedBoard = deepcopy(self.board)
solve(self.solvedBoard)
self.tiles = [[Tile(self.board[i][j], window, i*60, j*60) for j in range(9)] for i in range(9)]
self.window = window
def draw_board(self):
'''Fills the board with Tiles and renders their values'''
for i in range(9):
for j in range(9):
if j%3 == 0 and j != 0: #vertical lines
pygame.draw.line(self.window, (0, 0, 0), ((j//3)*180, 0), ((j//3)*180, 540), 4)
if i%3 == 0 and i != 0: #horizontal lines
pygame.draw.line(self.window, (0, 0, 0), (0, (i//3)*180), (540, (i//3)*180), 4)
self.tiles[i][j].draw((0,0,0), 1)
if self.tiles[i][j].value != 0: #don't draw 0s on the grid
self.tiles[i][j].display(self.tiles[i][j].value, (21+(j*60), (16+(i*60))), (0, 0, 0)) #20,5 are the coordinates of the first tile
#bottom-most line
pygame.draw.line(self.window, (0, 0, 0), (0, ((i+1) // 3) * 180), (540, ((i+1) // 3) * 180), 4)
def deselect(self, tile):
'''Deselects every tile except the one currently clicked'''
for i in range(9):
for j in range(9):
if self.tiles[i][j] != tile:
self.tiles[i][j].selected = False
def redraw(self, keys, wrong, time):
'''Redraws board with highlighted tiles'''
self.window.fill((255,255,255))
self.draw_board()
for i in range(9):
for j in range(9):
if self.tiles[j][i].selected: #draws the border on selected tiles
self.tiles[j][i].draw((50, 205, 50), 4)
elif self.tiles[i][j].correct:
self.tiles[j][i].draw((34, 139, 34), 4)
elif self.tiles[i][j].incorrect:
self.tiles[j][i].draw((255, 0, 0), 4)
if len(keys) != 0: #draws inputs that the user places on board but not their final value on that tile
for value in keys:
self.tiles[value[0]][value[1]].display(keys[value], (21+(value[0]*60), (16+(value[1]*60))), (128, 128, 128))
if wrong > 0:
font = pygame.font.SysFont('Bauhaus 93', 30) #Red X
text = font.render('X', True, (255, 0, 0))
self.window.blit(text, (10, 554))
font = pygame.font.SysFont('Bahnschrift', 40) #Number of Incorrect Inputs
text = font.render(str(wrong), True, (0, 0, 0))
self.window.blit(text, (32, 542))
font = pygame.font.SysFont('Bahnschrift', 40) #Time Display
text = font.render(str(time), True, (0, 0, 0))
self.window.blit(text, (388, 542))
pygame.display.flip()
def visualSolve(self, wrong, time):
'''Showcases how the board is solved via backtracking'''
for event in pygame.event.get(): #so that touching anything doesn't freeze the screen
if event.type == pygame.QUIT:
exit()
empty = find_empty(self.board)
if not empty:
return True
for nums in range(9):
if valid(self.board, (empty[0],empty[1]), nums+1):
self.board[empty[0]][empty[1]] = nums+1
self.tiles[empty[0]][empty[1]].value = nums+1
self.tiles[empty[0]][empty[1]].correct = True
pygame.time.delay(63) #show tiles at a slower rate
self.redraw({}, wrong, time)
if self.visualSolve(wrong, time):
return True
self.board[empty[0]][empty[1]] = 0
self.tiles[empty[0]][empty[1]].value = 0
self.tiles[empty[0]][empty[1]].incorrect = True
self.tiles[empty[0]][empty[1]].correct = False
pygame.time.delay(63)
self.redraw({}, wrong, time)
def hint(self, keys):
'''Shows a random empty tile's solved value as a hint'''
while True: #keeps generating i,j coords until it finds a valid random spot
i = random.randint(0, 8)
j = random.randint(0, 8)
if self.board[i][j] == 0: #hint spot has to be empty
if (j,i) in keys:
del keys[(j,i)]
self.board[i][j] = self.solvedBoard[i][j]
self.tiles[i][j].value = self.solvedBoard[i][j]
return True
elif self.board == self.solvedBoard:
return False
class Tile:
'''Represents each white tile/box on the grid'''
def __init__(self, value, window, x1, y1):
self.value = value
self.window = window
self.rect = pygame.Rect(x1, y1, 60, 60) #dimensions for the rectangle
self.selected = False
self.correct = False
self.incorrect = False
def draw(self, color, thickness):
'''Draws a tile on the board'''
pygame.draw.rect(self.window, color, self.rect, thickness)
def display(self, value, position, color):
'''Displays a number on that tile'''
font = pygame.font.SysFont('lato', 45)
text = font.render(str(value), True, color)
self.window.blit(text, position)
def clicked(self, mousePos):
'''Checks if a tile has been clicked'''
if self.rect.collidepoint(mousePos): #checks if a point is inside a rect
self.selected = True
return self.selected
def main():
'''Runs the main Sudoku GUI/Game'''
screen = pygame.display.set_mode((540, 590))
screen.fill((255, 255, 255))
pygame.display.set_caption("Sudoku")
icon = pygame.image.load("icon.png")
pygame.display.set_icon(icon)
#loading screen when generating grid
font = pygame.font.SysFont('Bahnschrift', 40)
text = font.render("Generating", True, (0, 0, 0))
screen.blit(text, (175, 245))
font = pygame.font.SysFont('Bahnschrift', 40)
text = font.render("Grid", True, (0, 0, 0))
screen.blit(text, (230, 290))
pygame.display.flip()
#initiliaze values and variables
wrong = 0
board = Board(screen)
selected = -1,-1 #NoneType error when selected = None, easier to just format as a tuple whose value will never be used
keyDict = {}
running = True
startTime = time.time()
while running:
elapsed = time.time() - startTime
passedTime = time.strftime("%H:%M:%S", time.gmtime(elapsed))
if board.board == board.solvedBoard: #user has solved the board
for i in range(9):
for j in range(9):
board.tiles[i][j].selected = False
running = False
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit() #so that it doesnt go to the outer run loop
elif event.type == pygame.MOUSEBUTTONUP: #allow clicks only while the board hasn't been solved
mousePos = pygame.mouse.get_pos()
for i in range(9):
for j in range (9):
if board.tiles[i][j].clicked(mousePos):
selected = i,j
board.deselect(board.tiles[i][j]) #deselects every tile except the one currently clicked
elif event.type == pygame.KEYDOWN:
if board.board[selected[1]][selected[0]] == 0 and selected != (-1,-1):
if event.key == pygame.K_1:
keyDict[selected] = 1
if event.key == pygame.K_2:
keyDict[selected] = 2
if event.key == pygame.K_3:
keyDict[selected] = 3
if event.key == pygame.K_4:
keyDict[selected] = 4
if event.key == pygame.K_5:
keyDict[selected] = 5
if event.key == pygame.K_6:
keyDict[selected] = 6
if event.key == pygame.K_7:
keyDict[selected] = 7
if event.key == pygame.K_8:
keyDict[selected] = 8
if event.key == pygame.K_9:
keyDict[selected] = 9
elif event.key == pygame.K_BACKSPACE or event.key == pygame.K_DELETE: # clears tile out
if selected in keyDict:
board.tiles[selected[1]][selected[0]].value = 0
del keyDict[selected]
elif event.key == pygame.K_RETURN:
if selected in keyDict:
if keyDict[selected] != board.solvedBoard[selected[1]][selected[0]]: #clear tile when incorrect value is inputted
wrong += 1
board.tiles[selected[1]][selected[0]].value = 0
del keyDict[selected]
break
#valid and correct entry into cell
board.tiles[selected[1]][selected[0]].value = keyDict[selected] #assigns current grid value
board.board[selected[1]][selected[0]] = keyDict[selected] #assigns to actual board so that the correct value can't be modified
del keyDict[selected]
if event.key == pygame.K_h:
board.hint(keyDict)
if event.key == pygame.K_SPACE:
for i in range(9):
for j in range(9):
board.tiles[i][j].selected = False
keyDict = {} #clear keyDict out
board.visualSolve(wrong, passedTime)
for i in range(9):
for j in range(9):
board.tiles[i][j].correct = False
board.tiles[i][j].incorrect = False #reset tiles
running = False
board.redraw(keyDict, wrong, passedTime)
while True: #another running loop so that the program ONLY closes when user closes program
for event in pygame.event.get():
if event.type == pygame.QUIT:
return
main()
pygame.quit()