-
Notifications
You must be signed in to change notification settings - Fork 0
/
play_sudoku.py
511 lines (430 loc) · 18.4 KB
/
play_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
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
"""
CSC148, Winter 2021
Assignment 2: Automatic Puzzle Solver
==============================
This code is provided solely for the personal and private use of
students taking the CSC148 course at the University of Toronto.
Copying for purposes other than this use is expressly prohibited.
All forms of distribution of this code, whether as given or with
any changes, are expressly prohibited.
Authors: Diane Horton, Jonathan Calver, Sophia Huynh,
Maryam Majedi, and Jaisie Sin.
All of the files in this directory are:
Copyright (c) 2021 Diane Horton, Jonathan Calver, Sophia Huynh,
Maryam Majedi, and Jaisie Sin.
=== Module Description ===
This module contains a basic GUI to allow you to play randomly generated
instances of your SudokuPuzzle once you have completed the necessary parts
of the assignment. When run, instructions on how to use it are displayed
in the console.
Feel free to modify this module.
"""
from __future__ import annotations
from typing import List, Tuple
from random import randint, shuffle
# imports for the GUI
import pygame
import pygame_gui
from pygame.event import Event
from pygame_gui.core.interfaces import IUIManagerInterface
from pygame_gui.elements import UIButton, UILabel
# imports from our code
from puzzle import Puzzle
from solver import DfsSolver
from sudoku_puzzle import SudokuPuzzle, EMPTY_CELL
# You can configure the settings here
N = 4 # puzzle size - suggested is either 4 or 9 (untested on 16)
# Pick how many numbers you want in the grid
# Don't pick this too small or it might be slow to generate a puzzle for you!
# NUM_STARTING = N ** 2 // 2 # this is a fairly easy puzzle
NUM_STARTING = N ** 2 // 3 # slightly harder, but still easy
# NUM_STARTING = N ** 2 // 4 # slightly harder, may generate somewhat slowly.
# Some constants defining the size of the GUI, may need to adjust
# depending on the size of screen.
WIDTH = 800
HEIGHT = 800
UI_WIDTH = WIDTH
UI_HEIGHT = HEIGHT
UI_ITEM_HEIGHT = HEIGHT // (N + 2) # +2 to account for buttons at the bottom.
UI_ITEM_WIDTH = WIDTH // N
# An example of how we can extend our SudokuPuzzle class to give it more
# features.
class RandomizedSudokuPuzzle(SudokuPuzzle):
"""
A sudoku puzzle where we randomize the extensions - allowing us
to randomly generate puzzles.
"""
# This is why we were using type(self)(...) in the SudokuPuzzle
# extensions method - otherwise this wouldn't do what we intended.
def extensions(self) -> List[RandomizedSudokuPuzzle]:
"""
Return a list of extensions of this sudoku puzzle.
The order of the extensions is randomized.
"""
exts = SudokuPuzzle.extensions(self)
shuffle(exts)
return exts
# some other methods we'll use to help us in our GUI
def copy_grid(self) -> List[List[str]]:
"""
Return a copy of this sudoku's grid
"""
return [[self._grid[i][j] for j in range(self._n)]
for i in range(self._n)]
def get_possible(self, r: int, c: int) -> List[str]:
"""
Get the list of possible symbols that can go in position <r>, <c>.
"""
allowed_symbols = (self._symbol_set
- (self._row_set(r)
| self._column_set(c)
| self._subsquare_set(r, c)))
return sorted(list(allowed_symbols))
# This is the part that makes use of our
# DfsSolver.solve method and SudokuPuzzle.has_unique_solution method
# Note: This approach is rather inefficient, but demonstrates a
# straightforward, intuitive way of generating a valid sudoku puzzle.
def make_sudoku(n: int = N,
num_starting: int =
NUM_STARTING) -> Tuple[RandomizedSudokuPuzzle,
Puzzle]:
"""
Generate a valid sudoku of grid size <n> x <n>.
This function attempts to leave <num_starting> squares with numbers in them.
"""
# generate a random filled sudoku grid... using our solver!
grid = [[' ' for i in range(n)] for j in range(n)]
symbols = {str(i) for i in range(1, n + 1)}
s = RandomizedSudokuPuzzle(n, grid, symbols)
# Note: Since extensions always works from top left to bottom right,
# only the final solution is really of any use to us - if extensions
# instead randomly chose a square to fill in, then we could consider
# looking back through the solution path for a potential puzzle.
puzzle_solution = DfsSolver().solve(s)[-1]
# We'll randomly remove some values from the solved puzzle,
# ensuring not to violate uniqueness of the solution.
num_to_remove = n ** 2 - num_starting
# can fiddle with somewhat to try to decrease the time to generate puzzles
batch_size = max(1, num_to_remove // 10)
give_up_limit = num_to_remove // batch_size + batch_size
grid = puzzle_solution.copy_grid()
solution_grid = puzzle_solution.copy_grid()
puzzle = puzzle_solution
while num_to_remove > 0 and give_up_limit > 0:
num = max(1, min(num_to_remove, batch_size))
# get num random (r, c)'s to try to remove
pairs = []
while len(pairs) < num:
r = randint(0, n - 1)
c = randint(0, n - 1)
if (r, c) not in pairs and grid[r][c] != EMPTY_CELL:
pairs.append((r, c))
grid[r][c] = EMPTY_CELL
# now ensure the puzzle still has a unique solution
puzzle = RandomizedSudokuPuzzle(n, grid, symbols)
# if the puzzle is still unique, we can remove the numbers.
if puzzle.has_unique_solution():
num_to_remove -= num
else: # removing the numbers broke the uniqueness, so put them back.
for r, c in pairs:
grid[r][c] = solution_grid[r][c]
batch_size -= 1
# just to ensure we always return a puzzle eventually!
give_up_limit -= 1
return puzzle, puzzle_solution
class SudokuPuzzleGUI:
"""
Simple GUI for a Sudoku puzzle
=== Private Attributes ===
_puzzle: the sudoku puzzle
_manager: manager for the pygame gui
_window_surface: pygame surface for displaying the game
_is_running: whether or not the game is running
_buttons: list of labels containing the grid values
_hint_button: button for getting a checking correct values
_new_button: button for getting a new puzzle
_hint_toggle: button to toggle tool tip hints on and off
_solved_label: label to display when puzzle is solved
_hints_on: flag indicating if hints are enabled
_solution_grid: grid of the solved puzzle
_grid: grid of the current puzzle state
"""
_puzzle: RandomizedSudokuPuzzle
_manager: IUIManagerInterface
_window_surface: pygame.Surface
_is_running: bool
_buttons: List[List[UIButton]]
_hint_button: UIButton
_new_button: UIButton
_hint_toggle: UIButton
_solved_label: UILabel
_hints_on: bool
_solution_grid: List[List[str]]
_grid: List[List[str]]
def __init__(self) -> None:
"""
"""
pygame.init()
self._window_surface = pygame.display.set_mode((WIDTH, HEIGHT))
self._manager = pygame_gui.UIManager((UI_WIDTH, UI_HEIGHT))
self._manager.get_theme().load_theme('themes/button.json')
self._manager.get_theme().load_theme('themes/label.json')
self._setup_puzzle()
self._setup_ui() # note, this helper creates some of the attributes
self._is_running = False
def _check_if_solved(self) -> None:
"""
Check if the user action caused the puzzle to be solved and
update the GUI accordingly.
"""
if self._puzzle.is_solved():
self._solved_label.visible = 1
else:
self._solved_label.visible = 0
def _get_hint(self) -> None:
"""
Check which of the filled in squares are correct.
If a square is correct, disable the button.
"""
for i in range(N):
for j in range(N):
if self._grid[i][j] == self._solution_grid[i][j]:
self._buttons[i][j].disable()
def _update_all_tool_tips(self) -> None:
"""
Update all tool tips on the grid.
"""
# reset the tool tips appropriately
for i in range(N):
for j in range(N):
if not self._hints_on:
self._buttons[i][j].tool_tip_text = None
elif self._buttons[i][j].is_enabled:
self._update_tool_tip(i, j)
def _update_tool_tip(self, r: int, c: int) -> None:
"""
Update the tool tip to contain the set of possible numbers
that can go in square <r>, <c> in the sudoku puzzle.
"""
tmp, self._grid[r][c] = self._grid[r][c], EMPTY_CELL
ops = self._puzzle.get_possible(r, c)
self._grid[r][c] = tmp
self._buttons[r][c].tool_tip_text = str(ops)
def _update_tool_tips(self, r: int, c: int) -> None:
"""
update the tool tip text for each button in this
row, column, and subsquare...
"""
for ci in range(N):
if ci != c:
self._update_tool_tip(r, ci)
for ri in range(N):
if ri != r:
self._update_tool_tip(ri, c)
sqn = round(N ** 0.5)
# do subsquare too
ro = r // sqn * sqn
co = c // sqn * sqn
for ri in range(sqn):
for ci in range(sqn):
self._update_tool_tip(ro + ri, co + ci)
def _process_grid_press(self, event: Event) -> None:
"""
Update the grid and puzzle based on the button pressed
"""
button = event.ui_element
c = N
r = 0
while c == N and r < N:
if button in self._buttons[r]:
c = self._buttons[r].index(button)
else:
r += 1
if c != N and self._buttons[r][c].is_enabled:
cur = button.text
self._grid[r][c] = EMPTY_CELL
ops = self._puzzle.get_possible(r, c)
self._grid[r][c] = cur
# go to the next choice
if ops and cur not in ops:
cur = ops[0]
elif ops:
d = ops.index(cur) + 1
if d >= len(ops):
cur = EMPTY_CELL
else:
cur = ops[d]
else:
cur = EMPTY_CELL
self._grid[r][c] = cur
button.set_text(cur)
# if hints are on, update the tool tips accordingly
if self._hints_on:
self._update_tool_tips(r, c)
def _toggle_hints(self) -> None:
"""
Toggle the tool tip hints.
"""
if self._hints_on:
self._hint_toggle.set_text("HINTS OFF")
else:
self._hint_toggle.set_text("HINTS ON")
self._hints_on = not self._hints_on
self._update_all_tool_tips()
def _process_event(self, event: Event) -> None:
"""
Processes a pygame event - which will be one of:
- the user quit
- new button pressed
- hint ("check") button pressed
- hint toggle button pressed
- grid square button pressed
"""
if event.type == pygame.QUIT:
self._is_running = False
if event.type == pygame.USEREVENT:
if event.user_type == pygame_gui.UI_BUTTON_PRESSED:
if event.ui_element == self._new_button:
# create a new puzzle
self._setup_puzzle()
# redo the GUI...
self._manager.clear_and_reset()
self._setup_ui()
elif event.ui_element == self._hint_button:
self._get_hint()
elif event.ui_element == self._hint_toggle:
self._toggle_hints()
else: # one of the grid squares
self._process_grid_press(event)
self._check_if_solved()
def _draw_grid(self) -> None:
"""
Draw the lines dividing the subsquares.
"""
for i in range(1, int(N ** 0.5)):
pygame.draw.line(self._window_surface, (25, 25, 25),
(0, UI_ITEM_HEIGHT * i * N ** 0.5),
(UI_ITEM_WIDTH * N, UI_ITEM_HEIGHT * i * N ** 0.5),
4)
pygame.draw.line(self._window_surface, (25, 25, 25),
(UI_ITEM_WIDTH * i * N ** 0.5, 0),
(UI_ITEM_WIDTH * i * N ** 0.5, UI_ITEM_HEIGHT * N),
4)
def play_game(self) -> None:
"""
Main loop for playing a sudoku puzzle in the GUI.
"""
pygame.display.set_caption('CSC148 Sudoku 2021')
clock = pygame.time.Clock()
self._is_running = True
while self._is_running:
time_delta = clock.tick(10)
for event in pygame.event.get():
self._process_event(event)
self._manager.process_events(event)
# update the game's display
self._manager.update(time_delta)
background = (255, 255, 255)
self._window_surface.fill(background)
self._draw_grid()
self._manager.draw_ui(self._window_surface)
pygame.display.update()
def _setup_puzzle(self) -> None:
"""
Generate a random expression tree puzzle and create the image for it.
This helper is called inside the __init__ and creates several of the
private instance attributes. It is called again any time the puzzle
to be solved is changed (the new button is pressed).
"""
raw_puzzle, solution = make_sudoku()
self._grid = raw_puzzle.copy_grid()
self._solution_grid = solution.copy_grid()
# Note: we'll take advantage of aliasing here. Since our
# SudokuPuzzle class doesn't make its own copy of the grid we pass in,
# we can directly update self._grid to change the state of the puzzle!
self._puzzle = RandomizedSudokuPuzzle(N, self._grid,
{str(i) for i in range(1, N + 1)})
def _setup_ui(self) -> None:
"""
Set up the UI components for the game.
This helper is called inside the __init__ and creates several of the
private instance attributes. It is called again any time the puzzle
to be solved is changed.
"""
self._buttons = []
for i in range(N):
row = []
for j in range(N):
rect = pygame.Rect(
(UI_ITEM_WIDTH * j + 2, UI_ITEM_HEIGHT * i + 2),
(UI_ITEM_WIDTH - 2, UI_ITEM_HEIGHT - 2))
tip = str(self._puzzle.get_possible(i, j))
if self._grid[i][j] == self._solution_grid[i][j]:
tip = None
label = UIButton(relative_rect=rect,
text=self._grid[i][j],
tool_tip_text=tip,
manager=self._manager)
if self._grid[i][j] != EMPTY_CELL:
label.disable()
row.append(label)
self._buttons.append(row)
rect = pygame.Rect((0, UI_ITEM_HEIGHT * N),
((UI_ITEM_WIDTH * N) // 2, UI_ITEM_HEIGHT))
self._hint_button = UIButton(relative_rect=rect,
text='CHECK',
manager=self._manager)
rect = pygame.Rect(((UI_ITEM_WIDTH * N) // 2, UI_ITEM_HEIGHT * N),
((UI_ITEM_WIDTH * N) // 2, UI_ITEM_HEIGHT))
self._hint_toggle = UIButton(relative_rect=rect,
text='HINTS ON',
manager=self._manager)
self._hints_on = True
rect = pygame.Rect((0, UI_ITEM_HEIGHT * (N + 1)),
(UI_ITEM_WIDTH * N, UI_ITEM_HEIGHT))
self._new_button = UIButton(relative_rect=rect,
text='NEW',
manager=self._manager)
# solved label
rect = pygame.Rect(
(UI_ITEM_WIDTH * (N / 2) - UI_ITEM_WIDTH,
UI_ITEM_HEIGHT * (N / 2) - UI_ITEM_HEIGHT / 2),
(UI_ITEM_WIDTH * 2, UI_ITEM_HEIGHT))
self._solved_label = UILabel(rect, "SOLVED!", manager=self._manager,
visible=0)
def show_instructions() -> None:
"""
Print instructions for how to use the GUI to the console.
"""
# ASCII art (Ogre font) generated using:
# https://www.colorschemer.com/ascii-art-generator/
print(r"""
================================================================================
___ __ ___ _ _ _ ___ __ _ _
/ __\/ _\ / __\/ | || | ( _ ) / _\_ _ __| | ___ | | ___ _
/ / \ \ / / | | || |_ / _ \ \ \| | | |/ _` |/ _ \| |/ / | | |
/ /___ _\ \/ /___ | |__ _| (_) | _\ \ |_| | (_| | (_) | <| |_| |
\____/ \__/\____/ |_| |_| \___/ \__/\__,_|\__,_|\___/|_|\_\\__,_|
================================================================================
HOW TO CHANGE PUZZLE SIZE AND HOW MANY STARTING NUMBERS ARE IN THE GRID:
In play_sudoku.py you can change the two values below and rerun the file.
- N is the puzzle size - it should be either 4 or 9
- NUM_STARTING is roughly how many numbers will be in the grid to start
- you can see the make_sudoku function to see how the puzzle
gets generated using the code you wrote for DfsSolver.solve and
SudokuPuzzle.has_unique_solution!
================================================================================
HOW TO PLAY:
NEW - generate a new puzzle
CHECK - any VALUES you have entered that are CORRECT will become FIXED
HINTS ON/OFF - when ON, a tool tip menu shows what VALUES can go in a
square when you hover over it.
CLICKING A SQUARE - changes the VALUE in the square - cycling through the
possible VALUES that could go in that square each time
you click on it.
================================================================================
""")
if __name__ == '__main__':
show_instructions()
gui = SudokuPuzzleGUI()
gui.play_game()