-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSudoku.py
381 lines (299 loc) · 12.4 KB
/
Sudoku.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
import random
from collections import defaultdict
from copy import deepcopy
import pygame
# Start the game
pygame.init()
# Colors
black = (0, 0, 0)
white = (211, 211, 211)
red = (255, 0, 0)
green = (21, 119, 40)
orange = (200, 100, 0)
# Fonts
font26 = pygame.font.Font(None, 26)
font20 = pygame.font.Font(None, 20)
class Game:
def __init__(self, diff='medium'):
# Set up parameters
self.time = 0
self.diff = diff # Game difficulty
self.board, self.solution = self.GeneratedBoard(self.diff)
self.temp = defaultdict(str)
self.solved = set()
# Create window
self.window = pygame.display.set_mode(size=(550, 550))
self.window.fill(white)
pygame.display.set_caption("Sudoku")
pygame.display.flip()
# Draw GUI and place numbers
self.drawGrid(placeNums=True)
# Start the game
self.run()
# Color buttons according to difficulty selection
def colorButtons(self, choice):
colors = [white, white, white]
diffToIndex = {'easy': 0, 'medium': 1, 'hard': 2}
colors[diffToIndex[choice]] = green
easyText = font26.render("Easy", True, black, colors[0])
easyRect = easyText.get_rect(center=(215, 520))
self.window.blit(easyText, easyRect)
medText = font26.render("Medium", True, black, colors[1])
medRect = medText.get_rect(center=(275, 520))
self.window.blit(medText, medRect)
hardText = font26.render("Hard", True, black, colors[2])
hardRect = hardText.get_rect(center=(335, 520)) # (350, 520)
self.window.blit(hardText, hardRect)
# Run the game, and display game on the GUI
def run(self):
# Set up buttons
restartText = font26.render("Restart", True, black, orange)
restartRect = restartText.get_rect(center=(80, 520))
self.window.blit(restartText, restartRect)
self.colorButtons(self.diff)
# Run
clock = pygame.time.Clock()
selected = False
run = True
while run:
events = pygame.event.get()
keys = pygame.key.get_pressed()
for event in events:
if event.type == pygame.QUIT:
run = False
if event.type == pygame.MOUSEBUTTONUP:
# remove previous selection
if selected:
self.deselect(x, y)
# get new selection
y, x = pygame.mouse.get_pos()
easy = 190 <= y <= 232 and 510 <= x <= 530
medium = 240 <= y <= 308 and 510 <= x <= 530
hard = 316 <= y <= 360 and 510 <= x <= 530
restart = 50 <= y <= 110 and 510 <= x <= 530
if easy:
self.diff = 'easy'
self.newGame()
elif medium:
self.diff = 'medium'
self.newGame()
elif hard:
self.diff = 'hard'
self.newGame()
elif restart:
self.newGame(sameBoard=True)
else:
x, y = x // 50 - 1, y // 50 - 1
if 0 <= x < 9 and 0 <= y < 9 and self.board[x][y] == '.':
self.select(x, y)
selected = True
if selected:
# Check for user input
for num, pressed in enumerate(keys[49:58]):
if pressed:
self.pencil(num + 1, x, y)
# Check for delete
if keys[pygame.K_DELETE] and self.temp[(x, y)] != '':
self.clearCell(x, y)
self.select(x, y)
# Confirm selection
if keys[pygame.K_RETURN] and self.temp[(x, y)] == self.solution[x][y]:
self.write(self.temp[(x, y)], x, y)
selected = False
if keys[pygame.K_LEFT] and y > 0:
i = y - 1
while i > 0 and self.board[x][i] != '.':
i -= 1
if self.board[x][i] == '.':
self.deselect(x, y)
self.select(x, i)
y = i
pygame.time.wait(150) # debounce
if keys[pygame.K_RIGHT] and y < 8:
i = y + 1
while i < 8 and self.board[x][i] != '.':
i += 1
if self.board[x][i] == '.':
self.deselect(x, y)
self.select(x, i)
y = i
pygame.time.wait(150) # debounce
if keys[pygame.K_DOWN] and x < 8:
i = x + 1
while i < 8 and self.board[i][y] != '.':
i += 1
if self.board[i][y] == '.':
self.deselect(x, y)
self.select(i, y)
x = i
pygame.time.wait(150) # debounce
if keys[pygame.K_UP] and x > 0:
i = x - 1
while i > 0 and self.board[i][y] != '.':
i -= 1
if self.board[i][y] == '.':
self.deselect(x, y)
self.select(i, y)
x = i
pygame.time.wait(150) # debounce
if keys[pygame.K_SPACE]:
if selected:
self.clearCell(x, y)
self.solve()
clock.tick()
self.time += clock.get_rawtime()
mins = str((self.time // 1000) // 60)
secs = str((self.time // 1000) % 60)
if len(secs) == 1:
secs = '0' + secs
pygame.draw.rect(self.window, white, (430, 505, 100, 50))
text = font26.render(mins + ':' + secs, True, black, white)
textRect = text.get_rect(center=(480, 520))
self.window.blit(text, textRect)
pygame.display.update()
if keys[pygame.K_ESCAPE]:
run = False
pygame.display.quit()
pygame.quit()
def drawGrid(self, placeNums=False):
# Draw lines
for i, offset in enumerate(range(0, 500, 50)):
if i == 3 or i == 6:
thickness = 4
else:
thickness = 2
pygame.draw.line(self.window, black, (50 + offset, 50), (50 + offset, 500), thickness)
pygame.draw.line(self.window, black, (50, 50 + offset), (500, 50 + offset), thickness)
if placeNums:
# Place numbers
for x, row in enumerate(self.board):
for y, value in enumerate(row):
if value != '.':
text = font26.render(self.board[x][y], True, black, white)
textRect = text.get_rect(center=((y * 50) + 75, (x * 50) + 75))
self.window.blit(text, textRect)
def select(self, x, y):
pygame.draw.rect(self.window, red, ((y + 1) * 50 + 2, (x + 1) * 50 + 2, 47, 47), 2)
def deselect(self, x, y):
pygame.draw.rect(self.window, white, ((y + 1) * 50 + 2, (x + 1) * 50 + 2, 48, 48), 4)
self.drawGrid() # CleanUp any problems with the border
def clearCell(self, x, y, fixGrid=True):
pygame.draw.rect(self.window, white, ((y + 1) * 50 + 2, (x + 1) * 50 + 2, 48, 48))
if fixGrid:
self.drawGrid() # CleanUp any problems with the border
def pencil(self, val, x, y):
self.temp[(x, y)] = str(val)
self.clearCell(x, y)
self.select(x, y)
text = font20.render(self.temp[(x, y)], True, black, white)
textRect = text.get_rect(center=((y * 50) + 63, (x * 50) + 64))
self.window.blit(text, textRect)
def write(self, val, x, y, color=black):
self.clearCell(x, y)
self.board[x][y] = val
self.solved.add((x, y))
text = font26.render(val, True, color, white)
textRect = text.get_rect(center=((y * 50) + 75, (x * 50) + 75))
self.window.blit(text, textRect)
def newGame(self, sameBoard=False):
if sameBoard:
for x, y in self.solved:
self.board[x][y] = '.'
self.clearCell(x, y, fixGrid=False)
self.drawGrid()
else:
self.board, self.solution = self.GeneratedBoard(self.diff)
self.temp = defaultdict(str)
self.solved = set()
pygame.draw.rect(self.window, white, (50, 50, 450, 450))
self.drawGrid(placeNums=True)
self.colorButtons(self.diff)
self.time = 0
# uses backtracking to solve the board (always solves the cell with least choices first)
def solve(self):
rows, cols, blocks = defaultdict(set), defaultdict(set), defaultdict(set)
for r in range(9):
for c in range(9):
val = self.board[r][c]
if val != '.':
rows[r].add(val)
cols[c].add(val)
blocks[(r // 3, c // 3)].add(val)
def isValid(d, r, c):
return d not in rows[r] and d not in cols[c] and d not in blocks[(r // 3, c // 3)]
def get_choices(r, c):
return [d for d in '123456789' if isValid(d, r, c)]
def get_empty_cell():
empty = [(r, c, get_choices(r, c)) for r in range(9) for c in range(9) if self.board[r][c] == '.']
return min(empty, key=lambda x: len(x[2])) if empty else None
def fill(r, c, v):
self.board[r][c] = v
rows[r].add(v)
cols[c].add(v)
blocks[(r // 3, c // 3)].add(v)
self.write(v, r, c, green)
pygame.display.update()
pygame.time.wait(50)
def unfill(r, c, v):
self.board[r][c] = '.'
rows[r].remove(v)
cols[c].remove(v)
blocks[(r // 3, c // 3)].remove(v)
self.clearCell(r, c)
pygame.display.update()
pygame.time.wait(50)
def solve_board():
events = pygame.event.get() # if events are not read, pygame will crash
empty = get_empty_cell()
if not empty:
return True
else:
r, c, choices = empty
if not choices:
return False
for choice in choices:
fill(r, c, choice)
if solve_board():
return True
else:
unfill(r, c, choice)
return False
solve_board()
def GeneratedBoard(self, diff):
difficulty = {'easy': 4, 'medium': 5, 'hard': 7}
count = 81
board = [['.' for _ in range(9)] for _ in range(9)]
rows, cols, blocks = defaultdict(set), defaultdict(set), defaultdict(set)
def isValid(d, r, c):
return d not in rows[r] and d not in cols[c] and d not in blocks[(r // 3, c // 3)]
def get_choices(r, c):
return [d for d in '123456789' if isValid(d, r, c)]
def get_empty_cell():
empty = [(r, c, get_choices(r, c)) for r in range(9) for c in range(9) if board[r][c] == '.']
return min(empty, key=lambda x: len(x[2])) if empty else None
while count > 0:
empty = get_empty_cell()
if empty:
r, c, choices = empty
if choices:
v = random.choice(choices)
board[r][c] = v
rows[r].add(v)
cols[c].add(v)
blocks[(r // 3, c // 3)].add(v)
count -= 1
else:
# Board created is invalid, restart
count = 81
board = [['.' for _ in range(9)] for _ in range(9)]
rows = defaultdict(set)
cols = defaultdict(set)
blocks = defaultdict(set)
solution = deepcopy(board)
for r in range(9):
columns = random.sample(list(range(9)), difficulty[diff])
for c in columns:
board[r][c] = '.'
return board, solution
if __name__ == '__main__':
game = Game()