-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsudoku.py
847 lines (713 loc) · 29.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
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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
import random
from collections import deque
from copy import deepcopy
from tkinter import Tk, Canvas, Frame, Button, Menu, filedialog, simpledialog, messagebox, BOTH, TOP, BOTTOM, LEFT, RIGHT
from textwrap import wrap
from enum import Enum
MARGIN = 20 # Pixels around the board
SIDE = 50 # Height of each board cell
WIDTH = HEIGHT = MARGIN * 2 + SIDE * 9 # Width and height of the whole board
# Highlight colours
HLANSWER = "light goldenrod"
HLCAND = "light blue"
COLOURS = [None, "pale green", "sienna1", "khaki1", "cornflower blue", "mediumpurple1", "peachpuff2", "tomato", "sandy brown", "hot pink"]
class Mode(Enum):
solution = 1
candidate = 2
colour = 3
colour_candidate = 4
class SudokuError(Exception):
"""
An application specific error.
"""
pass
class SudokuBoard(object):
"""
Sudoku Board Representation
"""
def __init__(self, puzzle_string):
self.__create_board(puzzle_string)
def update(self, puzzle_string):
self.__create_board(puzzle_string)
def get(self):
return self.board[:]
def set_board(self, board):
self.board = board
def __create_board(self, puzzle_string):
rows = wrap(puzzle_string, 9)
rows = [row.replace('.', '0') for row in rows]
self.board = [ [int(ch) for ch in row] for row in rows ]
def generate(self, difficulty):
file_name = difficulty + ".seed"
with open(file_name) as file:
puzzles = [line.strip() for line in file]
self.__create_board(random.choice(puzzles))
# Rotate puzzle between 0 to 3 times
for i in range(random.randint(0,3)):
self.rotate90()
# Either don't flip or do flips
i = random.randint(0,3)
if i == 0:
self.flip_hor()
elif i == 1:
self.flip_vert()
elif i == 2:
self.flip_hor()
self.flip_vert()
self.translate()
def rotate90(self):
new_board = [[0 for y in range(9)] for x in range(9)]
for i in range(9):
for j in range(9):
new_board[j][8-i] = self.board[i][j]
self.board = new_board
def flip_hor(self):
new_board = [[0 for y in range(9)] for x in range(9)]
for i in range(9):
for j in range(9):
new_board[8-i][j] = self.board[i][j]
self.board = new_board
def flip_vert(self):
new_board = [[0 for y in range(9)] for x in range(9)]
for i in range(9):
for j in range(9):
new_board[i][8-j] = self.board[i][j]
self.board = new_board
def translate(self):
numbers = [1,2,3,4,5,6,7,8,9]
random.shuffle(numbers)
new_board = [[0 for y in range(9)] for x in range(9)]
for i in range(9):
for j in range(9):
if self.board[i][j] != 0:
new_board[i][j] = numbers[self.board[i][j] - 1]
else:
new_board[i][j] = 0
self.board = new_board
class SudokuGame(object):
"""
A Sudoku game, in charge of storing the state of the board and checking
whether the puzzle is completed.
"""
def __init__(self):
self.board = SudokuBoard("0" * 81)
self.puzzle = self.board.get()
self.start_puzzle = self.board.get()
self.candidates = [[[False for z in range(10)] for x in range(9)] for y in range(9)]
self.undostack = deque()
self.null_board()
self.current_to_origin()
self.colours = [[0 for i in range(9)] for j in range(9)]
self.candidate_colours = [[[0 for x in range(10)] for y in range(9)] for z in range(9)]
def start(self):
self.game_over = False
self.candidates = [[[False for z in range(10)] for x in range(9)] for y in range(9)]
self.puzzle = []
self.colours = [[0 for i in range(9)] for j in range(9)]
self.candidate_colours = [[[0 for x in range(10)] for y in range(9)] for z in range(9)]
for i in range(9):
self.puzzle.append([])
for j in range(9):
self.puzzle[i].append(self.start_puzzle[i][j])
self.board.set_board(self.puzzle)
self.__save_undo_state()
def __save_undo_state(self):
self.undostack.append((deepcopy(self.puzzle), deepcopy(self.start_puzzle), deepcopy(self.candidates),deepcopy(self.colours), deepcopy(self.candidate_colours)))
def undo(self):
self.puzzle, self.start_puzzle, self.candidates, self.colours, self.candidate_colours = self.undostack.pop()
def get_cell(self, row, col):
return self.puzzle[row][col]
def set_cell(self, row, col, val):
self.puzzle[row][col] = val
self.update_candidates(row, col, undo=False)
self.__save_undo_state()
def calculate_all_candidates(self):
for row in range(9):
for col in range(9):
self.calculate_candidates(row, col, undo=False)
self.__save_undo_state()
def calculate_candidates(self, row, col, undo=True):
if self.puzzle[row][col] == 0:
candidates = list(set(range(10)).difference(self.__set_buddies(row, col)))
for candidate in candidates:
self.add_candidate(row, col, candidate, undo=False)
if undo:
self.__save_undo_state()
def get_candidates(self, row, col):
candidates = []
for i in range(1,10):
if self.candidates[row][col][i]:
candidates.append(i)
return candidates
def get_cell_colour(self, row, col):
return COLOURS[self.colours[row][col]]
def get_candidate_colour(self, row, col, candidate):
return COLOURS[self.candidate_colours[row][col][candidate]]
def set_candidate_colour(self, row, col, candidate, colour_number, undo=True):
self.candidate_colours[row][col][candidate] = colour_number
if undo:
self.__save_undo_state()
def set_cell_colour(self, row, col, colour_number, undo=True):
self.colours[row][col] = colour_number
if undo:
self.__save_undo_state()
def toggle_candidate(self, row, col, val, undo=True):
self.candidates[row][col][val] = not self.candidates[row][col][val]
if undo:
self.__save_undo_state()
def reset_colours(self):
self.colours = [[0 for i in range(9)] for j in range(9)]
self.candidate_colours = [[[0 for x in range(10)] for y in range(9)] for z in range(9)]
def remove_candidate(self, row, col, val, undo=True):
self.candidates[row][col][val] = False
if undo:
self.__save_undo_state()
def add_candidate(self, row, col, val, undo=True):
self.candidates[row][col][val] = True
if undo:
self.__save_undo_state()
def get_origin(self, row, col):
return self.start_puzzle[row][col]
def update_candidates(self, row, col, undo=True):
answer = self.puzzle[row][col]
buddies = self.__find_buddies(row, col)
for r, c in buddies:
self.remove_candidate(r, c, answer, undo=False)
if undo:
self.__save_undo_state()
def __find_buddies(self, row, col):
buddies = []
# Row buddies
buddies.extend([(row, c) for c in range(9)])
# Column buddies
buddies.extend([(r, col) for r in range(9)])
# Box buddies
box_row = row // 3
box_col = col // 3
buddies.extend([
(r, c)
for r in range(box_row * 3, (box_row + 1) * 3)
for c in range(box_col * 3, (box_col + 1) * 3)
])
return buddies
def current_to_origin(self):
self.game_over = False
self.start_puzzle = []
for i in range(9):
self.start_puzzle.append([])
for j in range(9):
self.start_puzzle[i].append(self.puzzle[i][j])
self.__save_undo_state()
def null_board(self):
for i in range(9):
for j in range(9):
self.start_puzzle[i][j] = 0
self.start()
def load_puzzle(self, file_name, line_number):
with open(file_name) as file:
puzzles = [line.strip() for line in file]
self.from_string(puzzles[line_number])
self.__save_undo_state()
def generate(self, difficulty):
self.board.generate(difficulty)
self.start_puzzle = self.board.get()
self.start()
def rotate90(self):
self.board.rotate90()
self.start_puzzle = self.board.get()
self.start()
def flip_hor(self):
self.board.flip_hor()
self.start_puzzle = self.board.get()
self.start()
def flip_vert(self):
self.board.flip_vert()
self.start_puzzle = self.board.get()
self.start()
def translate(self):
self.board.translate()
self.start_puzzle = self.board.get()
self.start()
def load_random_puzzle(self, file_name):
with open(file_name) as file:
puzzles = [line.strip() for line in file]
self.from_string(random.choice(puzzles))
self.__save_undo_state()
def from_string(self, puzzle_string):
self.board.update(puzzle_string)
self.start_puzzle = self.board.get()
self.start()
self.__save_undo_state()
def check_win(self):
for row in range(9):
if not self.__check_row(row):
return False
for column in range(9):
if not self.__check_column(column):
return False
for row in range(3):
for column in range(3):
if not self.__check_square(row, column):
return False
self.game_over = True
return True
def __check_group(self, block):
return set(block) == set(range(1,10))
def __check_row(self, row):
return self.__check_group(self.puzzle[row])
def __set_row(self, row):
return set(self.puzzle[row])
def __check_column(self, col):
return self.__check_group(
[self.puzzle[row][col] for row in range(9)]
)
def __set_column(self, col):
return set([self.puzzle[row][col] for row in range(9)])
def __check_square(self, row, col):
return self.__check_group(
[
self.puzzle[r][c]
for r in range(row * 3, (row + 1) * 3)
for c in range(col * 3, (col + 1) * 3)
]
)
def __set_square(self, box_row, box_col):
return set(
[
self.puzzle[r][c]
for r in range(box_row * 3, (box_row + 1) * 3)
for c in range(box_col * 3, (box_col + 1) * 3)
]
)
def __set_buddies(self, row, col):
return self.__set_row(row).union(self.__set_column(col), self.__set_square(row // 3, col // 3))
class SudokuUI(Frame):
"""
The Tkinter UI, responsible for drawing the board and accepting user input.
"""
def __init__(self, parent, game):
self.game = game
self.parent = parent
Frame.__init__(self, parent)
self.row, self.col = 0, 0
self.mode = Mode.solution
self.highlight = 0
self.puzzle_num = 0
self.file_name = ""
self.__initUI()
def __initUI(self):
self.parent.title("Simple Sudoku")
self.pack(fill=BOTH, expand=1)
# Menubar
menubar = Menu(self.parent)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Open", command=self.__from_file)
filemenu.add_command(label="Import from clipboard", command=self.__from_clip)
menubar.add_cascade(label="File", menu=filemenu)
puzzlemenu = Menu(menubar, tearoff=0)
puzzlemenu.add_command(label="Calculate candidates", command=self.__calculate_candidates)
puzzlemenu.add_command(label="Reset", command=self.__clear_answers)
puzzlemenu.add_command(label="Clear", command=self.__null_board)
puzzlemenu.add_command(label="Set Origin", command=self.__to_origin)
generatemenu = Menu(puzzlemenu, tearoff=0)
generatemenu.add_command(label="Easy", command=self.__generate_easy)
generatemenu.add_command(label="Medium", command=self.__generate_medium)
generatemenu.add_command(label="Hard", command=self.__generate_hard)
generatemenu.add_command(label="Unfair", command=self.__generate_unfair)
generatemenu.add_command(label="Extreme", command=self.__generate_extreme)
puzzlemenu.add_cascade(label="Generate", menu=generatemenu)
menubar.add_cascade(label="Puzzle", menu=puzzlemenu)
collectionmenu = Menu(menubar, tearoff=0)
collectionmenu.add_command(label="Next Puzzle", command=self.__next_puzzle)
collectionmenu.add_command(label="Previous Puzzle", command=self.__previous_puzzle)
collectionmenu.add_command(label="Go to specific Puzzle...", command=self.__goto_puzzle)
collectionmenu.add_command(label="Random Puzzle", command=self.__random_from_file)
menubar.add_cascade(label="Collection", menu=collectionmenu)
debugmenu = Menu(menubar, tearoff=0)
debugmenu.add_command(label="rotate90", command=self.__rotate90)
debugmenu.add_command(label="flip horizontal", command=self.__flip_hor)
debugmenu.add_command(label="flip vertical", command=self.__flip_vert)
debugmenu.add_command(label="translate", command=self.__translate)
menubar.add_cascade(label="Debug", menu=debugmenu)
self.parent.config(menu=menubar)
self.canvas = Canvas(self, width=WIDTH, height=HEIGHT)
self.canvas.pack(fill=BOTH, expand=True)
self.__draw_grid()
self.__draw_puzzle()
self.__draw_cursor()
self.canvas.focus_set()
self.canvas.bind("<Left>", self.__cursor_left)
self.canvas.bind("<Right>", self.__cursor_right)
self.canvas.bind("<Up>", self.__cursor_up)
self.canvas.bind("<Down>", self.__cursor_down)
self.canvas.bind("<a>", self.__cursor_left)
self.canvas.bind("<d>", self.__cursor_right)
self.canvas.bind("<w>", self.__cursor_up)
self.canvas.bind("<s>", self.__cursor_down)
self.canvas.bind("<u>", self.__undo)
self.canvas.bind("<q>", self.__toggle_mode_colouring)
self.canvas.bind("<e>", self.__erase_colouring)
self.canvas.bind("<f>", self.__toggle_mode_colour_candidate)
self.canvas.bind("<c>", self.__calculate_candidates)
self.canvas.bind("<F1>", self.__toggle_highlight)
self.canvas.bind("<F2>", self.__toggle_highlight)
self.canvas.bind("<F3>", self.__toggle_highlight)
self.canvas.bind("<F4>", self.__toggle_highlight)
self.canvas.bind("<F5>", self.__toggle_highlight)
self.canvas.bind("<F6>", self.__toggle_highlight)
self.canvas.bind("<F7>", self.__toggle_highlight)
self.canvas.bind("<F8>", self.__toggle_highlight)
self.canvas.bind("<F9>", self.__toggle_highlight)
self.canvas.bind("<Button-1>", self.__cell_clicked)
self.canvas.bind("<Key>", self.__key_pressed)
def __erase_colouring(self, event):
self.game.reset_colours()
self.__draw_puzzle()
def __undo(self, event):
self.game.undo()
self.__draw_puzzle()
def __generate_easy(self):
self.game.generate("Easy")
self.file_name = "Easy.seed"
self.puzzle_num = 0
self.__draw_puzzle()
def __generate_medium(self):
self.game.generate("Medium")
self.file_name = "Medium.seed"
self.puzzle_num = 0
self.__draw_puzzle()
def __generate_hard(self):
self.game.generate("Hard")
self.file_name = "Hard.seed"
self.puzzle_num = 0
self.__draw_puzzle()
def __generate_unfair(self):
self.game.generate("Unfair")
self.file_name = "Unfair.seed"
self.puzzle_num = 0
self.__draw_puzzle()
def __generate_extreme(self):
self.game.generate("Extreme")
self.file_name = "Extreme.seed"
self.puzzle_num = 0
self.__draw_puzzle()
def __rotate90(self):
self.game.rotate90()
self.__draw_puzzle()
def __flip_hor(self):
self.game.flip_hor()
self.__draw_puzzle()
def __flip_vert(self):
self.game.flip_vert()
self.__draw_puzzle()
def __translate(self):
self.game.translate()
self.__draw_puzzle()
def __from_file(self):
self.file_name = filedialog.askopenfilename(title="Open puzzle file")
self.puzzle_num = 0
self.game.load_puzzle(self.file_name, self.puzzle_num)
self.__draw_puzzle()
def __goto_puzzle(self):
in_num = simpledialog.askinteger("Go to puzzle", "Go to which puzzle number")
if in_num is not None:
self.puzzle_num = in_num - 1
self.game.load_puzzle(self.file_name, self.puzzle_num)
self.__draw_puzzle()
def __previous_puzzle(self):
if self.puzzle_num != 0:
self.puzzle_num -= 1
self.game.load_puzzle(self.file_name, self.puzzle_num)
self.__draw_puzzle()
def __next_puzzle(self):
self.puzzle_num += 1
self.game.load_puzzle(self.file_name, self.puzzle_num)
self.__draw_puzzle()
def __random_from_file(self):
self.game.load_random_puzzle(self.file_name)
self.__draw_puzzle()
def __calculate_candidates(self, event=None):
self.game.calculate_all_candidates()
self.__draw_puzzle()
def __toggle_highlight(self, event):
number = int(event.keysym[1])
if self.highlight == number:
self.highlight = 0
else:
self.highlight = number
self.__draw_puzzle()
def __null_board(self):
self.game.null_board()
self.__draw_puzzle()
def __to_origin(self):
self.game.current_to_origin()
self.__draw_puzzle()
def __from_clip(self):
puzzle_string = self.clipboard_get()
self.game.from_string(puzzle_string)
self.__draw_puzzle()
def __draw_grid(self):
"""
Draws grid divided with blue lines into 3x3 squares
"""
for i in range(10):
color = "gray22" if i % 3 == 0 else "gray70"
x0 = MARGIN + i * SIDE
y0 = MARGIN
x1 = MARGIN + i * SIDE
y1 = HEIGHT - MARGIN
self.canvas.create_line(x0, y0, x1, y1, fill=color, width=1)
x0 = MARGIN
y0 = MARGIN + i * SIDE
x1 = WIDTH - MARGIN
y1 = MARGIN + i * SIDE
self.canvas.create_line(x0, y0, x1, y1, fill=color, width=1)
def __draw_puzzle(self):
self.canvas.delete("numbers")
self.canvas.delete("candidates")
self.canvas.delete("highlights")
self.canvas.delete("puzzleinfo")
self.canvas.delete("cellcolouring")
self.canvas.delete("candidatecolour")
for i in range(9):
for j in range(9):
x0 = MARGIN + j * SIDE + 1
y0 = MARGIN + i * SIDE + 1
x1 = MARGIN + (j + 1) * SIDE - 1
y1 = MARGIN + (i + 1) * SIDE - 1
answer = self.game.get_cell(i,j)
candidates = self.game.get_candidates(i,j)
# First draw the highlight or else the candidates won't be visible
do_highlight = True
if self.highlight == 0 or self.mode is Mode.colour_candidate:
do_highlight = False
if do_highlight and answer == self.highlight:
self.canvas.create_rectangle(x0, y0, x1, y1, tags="highlights", fill=HLANSWER, outline=HLANSWER)
elif do_highlight and self.highlight in candidates and answer == 0:
self.canvas.create_rectangle(x0, y0, x1, y1, tags="highlights", fill=HLCAND, outline=HLCAND)
# Then draw cell colouring
colour = self.game.get_cell_colour(i,j)
if not colour is None:
self.canvas.create_rectangle(x0, y0, x1, y1, tags="cellcolouring", fill=colour , outline=colour)
if answer != 0:
# Draw big character
x = MARGIN + j * SIDE + SIDE / 2
y = MARGIN + i * SIDE + SIDE / 2
original = self.game.get_origin(i, j)
color = "black" if answer == original else "olive drab"
self.canvas.create_text(x,y, text=answer, tags="numbers", fill=color, font=("Arial",24))
else:
# Draw candidate colouring
for candidate in candidates:
self.__draw_candidate_colour(i, j, candidate)
# Draw candidates
for candidate in candidates:
self.__draw_candidate(i, j, candidate)
# Write puzzle info in the middle bottom of the puzzle
pix = WIDTH / 2
piy = HEIGHT - MARGIN / 2
puzzle_info = ""
if self.file_name != "":
collection_name = self.file_name.split("/")[-1].split(".")[-2]
if self.puzzle_num == 0:
puzzle_info = collection_name
else:
puzzle_info = "{}: {}".format(collection_name, self.puzzle_num + 1)
self.canvas.create_text(pix, piy, text=puzzle_info, tags="puzzleinfo", fill="gray", font=("Arial", 12))
def __get_candidate_pos(self, row, col, candidate):
diff = 15
cx = MARGIN + col * SIDE + SIDE / 2
cy = MARGIN + row * SIDE + SIDE / 2
if candidate == 1:
x = cx - diff
y = cy - diff
elif candidate == 2:
x = cx
y = cy - diff
elif candidate == 3:
x = cx + diff
y = cy - diff
elif candidate == 4:
x = cx - diff
y = cy
elif candidate == 5:
x = cx
y = cy
elif candidate == 6:
x = cx + diff
y = cy
elif candidate == 7:
x = cx - diff
y = cy + diff
elif candidate == 8:
x = cx
y = cy + diff
elif candidate == 9:
x = cx + diff
y = cy + diff
return x, y
def __draw_candidate(self, row, col, candidate):
x,y = self.__get_candidate_pos(row, col, candidate)
self.canvas.create_text(x,y, text=candidate, tags="candidates", fill="gray", font=("Arial", 10))
def __draw_candidate_colour(self, row, col, candidate):
diff = 7
colour = self.game.get_candidate_colour(row, col, candidate)
if colour is None:
return
x,y = self.__get_candidate_pos(row, col, candidate)
x0 = x - diff
x1 = x + diff
y0 = y - diff
y1 = y + diff
self.canvas.create_oval(x0, y0, x1, y1, tags="candidatecolour", fill=colour, outline=colour)
def __clear_answers(self):
self.game.start()
self.canvas.delete("victory")
self.__draw_puzzle()
def __cell_clicked(self, event):
if self.game.game_over:
return
x, y = event.x, event.y
if(MARGIN < x < WIDTH - MARGIN and MARGIN < y < HEIGHT - MARGIN):
self.canvas.focus_set()
# get row and col numbers from x,y coordinates
row, col = (y - MARGIN) // SIDE, (x - MARGIN) // SIDE
# if cell was selected already - deselect it
if (row, col) == (self.row, self.col):
self.row, self.col = -1, -1
elif self.game.get_origin(row,col) == 0:
self.row, self.col = row, col
else:
self.row, self.col = -1, -1
self.__draw_cursor()
def __cursor_left(self, event):
self.canvas.delete("cursor")
if self.__deselected():
self.row = 0
self.col = 8
else:
if self.col == 0:
self.col = 8
else:
self.col -= 1
self.__draw_cursor()
def __cursor_right(self, event):
self.canvas.delete("cursor")
if self.__deselected():
self.row = 0
self.col = 0
else:
if self.col == 8:
self.col = 0
else:
self.col += 1
self.__draw_cursor()
def __cursor_up(self, event):
self.canvas.delete("cursor")
if self.__deselected():
self.row = 8
self.col = 0
else:
if self.row == 0:
self.row = 8
else:
self.row -= 1
self.__draw_cursor()
def __cursor_down(self, event):
self.canvas.delete("cursor")
if self.__deselected():
self.row = 0
self.col = 0
else:
if self.row == 8:
self.row = 0
else:
self.row += 1
self.__draw_cursor()
def __deselected(self):
if self.row == -1 and self.col == -1:
return True
else:
return False
def __draw_cursor(self):
self.canvas.delete("cursor")
if self.row >= 0 and self.col >= 0:
x0 = MARGIN + self.col * SIDE + 1
y0 = MARGIN + self.row * SIDE + 1
x1 = MARGIN + (self.col + 1) * SIDE - 1
y1 = MARGIN + (self.row + 1) * SIDE - 1
color = ""
if self.mode is Mode.candidate:
color = "blue"
elif self.mode is Mode.solution:
color = "red"
elif self.mode is Mode.colour:
color = "green"
elif self.mode is Mode.colour_candidate:
color = "yellow3"
else:
color = "pink"
self.canvas.create_rectangle(x0, y0, x1, y1, outline=color, tags="cursor", width=3)
def __key_pressed(self, event):
if self.game.game_over:
return
if self.row >= 0 and self.col >= 0 and event.char in "1234567890":
if self.mode is Mode.solution and self.game.get_origin(self.row,self.col) == 0:
self.game.set_cell(self.row, self.col, int(event.char))
elif self.game.get_origin(self.row,self.col) != 0:
self.highlight = int(event.char)
elif self.mode is Mode.candidate:
self.game.toggle_candidate(self.row, self.col, int(event.char))
elif self.mode is Mode.colour:
self.game.set_cell_colour(self.row, self.col,int(event.char))
elif self.mode is Mode.colour_candidate and self.game.get_origin(self.row,self.col) != 0:
self.highlight = int(event.char)
elif self.mode is Mode.colour_candidate:
self.game.set_candidate_colour(self.row, self.col, int(event.char), self.highlight)
self.__draw_puzzle()
self.__draw_cursor()
if self.game.check_win():
messagebox.showinfo("Completed", "Congratulations, you solved the puzzle!")
elif event.char == " ":
self.__toggle_mode_candidate()
self.__draw_cursor()
else:
self.__draw_puzzle()
self.__draw_cursor()
return
def __toggle_mode_candidate(self):
if self.mode is Mode.candidate:
self.mode = Mode.solution
else:
self.mode = Mode.candidate
self.__draw_cursor()
self.__draw_puzzle()
def __toggle_mode_colouring(self, event):
if self.mode is Mode.colour:
self.mode = Mode.solution
else:
self.mode = Mode.colour
self.__draw_cursor()
self.__draw_puzzle()
def __toggle_mode_colour_candidate(self, event):
if self.mode is Mode.colour_candidate:
self.mode = Mode.solution
else:
self.mode = Mode.colour_candidate
self.__draw_cursor()
self.__draw_puzzle()
def __draw_victory(self):
# create an oval
x0 = y0 = MARGIN + SIDE * 2
x1 = y1 = MARGIN + SIDE * 7
self.canvas.create_oval(x0, y0, x1, y1, tags="victory", fill="dark orange", outline="orange")
# create text
x = y = MARGIN + 4 * SIDE + SIDE / 2
self.canvas.create_text(x, y, text="You win!", tags="victory", fill="white", font=("Arial", 32))
if __name__ == '__main__':
game = SudokuGame()
game.start()
root = Tk()
SudokuUI(root,game)
root.geometry("{}x{}".format(WIDTH, HEIGHT))
root.mainloop()