diff --git a/sudoku.py b/sudoku.py index a9989fb..541c9de 100644 --- a/sudoku.py +++ b/sudoku.py @@ -12,10 +12,13 @@ # 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): """ @@ -331,6 +334,7 @@ def __init__(self, parent, game): self.highlight = 0 self.puzzle_num = 0 self.file_name = "" + self.colours = [[None for i in range(9)] for j in range(9)] self.__initUI() @@ -400,6 +404,8 @@ def __initUI(self): self.canvas.bind("", self.__cursor_down) self.canvas.bind("", self.__undo) + self.canvas.bind("", self.__toggle_mode_colouring) + self.canvas.bind("", self.__erase_colouring) self.canvas.bind("", self.__toggle_highlight) self.canvas.bind("", self.__toggle_highlight) @@ -411,6 +417,10 @@ def __initUI(self): self.canvas.bind("", self.__toggle_highlight) self.canvas.bind("", self.__toggle_highlight) + def __erase_colouring(self, event): + self.colours = [[None for i in range(9)] for j in range(9)] + self.__draw_puzzle() + def __undo(self, event): self.game.undo() self.__draw_puzzle() @@ -539,6 +549,7 @@ def __draw_puzzle(self): self.canvas.delete("candidates") self.canvas.delete("highlights") self.canvas.delete("puzzleinfo") + self.canvas.delete("cellcolouring") for i in range(9): for j in range(9): x0 = MARGIN + j * SIDE + 1 @@ -547,11 +558,17 @@ def __draw_puzzle(self): 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 if self.highlight != 0 and answer == self.highlight: self.canvas.create_rectangle(x0, y0, x1, y1, tags="highlights", fill=HLANSWER, outline=HLANSWER) elif self.highlight !=0 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.colours[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 @@ -703,6 +720,8 @@ def __draw_cursor(self): color = "blue" elif self.mode is Mode.solution: color = "red" + elif self.mode is Mode.colour: + color = "green" else: color = "pink" self.canvas.create_rectangle(x0, y0, x1, y1, outline=color, tags="cursor", width=3) @@ -718,6 +737,8 @@ def __key_pressed(self, event): 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.colours[self.row][self.col] = COLOURS[int(event.char)] self.__draw_puzzle() self.__draw_cursor() if self.game.check_win(): @@ -736,6 +757,14 @@ def __toggle_mode_candidate(self): self.mode = Mode.solution else: self.mode = Mode.candidate + self.__draw_cursor() + + def __toggle_mode_colouring(self, event): + if self.mode is Mode.colour: + self.mode = Mode.solution + else: + self.mode = Mode.colour + self.__draw_cursor() def __draw_victory(self): # create an oval