From 1f9de52c4798a22293aaf2c32611090a099d0ce4 Mon Sep 17 00:00:00 2001 From: Harishankar Date: Tue, 7 Dec 2010 20:40:49 +0530 Subject: [PATCH] Added the "reveal word" functionality Added the functionality to reveal across and/or downword at the currently selected cell. --- crosswordpuzzle.py | 32 +++++++++++++++++-- player_mainwindow.py | 76 +++++++++++++++++++++++++++++++++----------- playerwindow.glade | 1 + 3 files changed, 88 insertions(+), 21 deletions(-) diff --git a/crosswordpuzzle.py b/crosswordpuzzle.py index e4a7f58..319a4fc 100644 --- a/crosswordpuzzle.py +++ b/crosswordpuzzle.py @@ -582,13 +582,13 @@ class CrosswordPuzzle: # remove an across word at position def remove_word_across (self, row, col): # if grid is frozen don't allow removal of word - assert_unfrozen_grid () + self.assert_unfrozen_grid () word, brow, bcol, l = self.get_word_across (row, col) # traverse from the beginning to end of the word and erase it c = bcol - while True: + while c < self.cols: if self.data[brow][c].occupied_across is True: self.data[brow][c].clear_across_data () else: @@ -603,12 +603,38 @@ class CrosswordPuzzle: word, brow, bcol, l = self.get_word_down (row, col) # traverse from the beginn to end of the word and erase it r = brow - while True: + while r < self.rows: if self.data[r][bcol].occupied_down is True: self.data[r][bcol].clear_down_data () else: break r += 1 + # reveal/unreveal a word at position + def reveal_word_across (self, row, col, revealed=True): + # set the revealed flag for the word at the position + word= self.get_word_across (row, col) + + c = word[2] + while c < self.cols: + if self.data[word[1]][c].occupied_across is True: + self.data[word[1]][c].revealed = revealed + else: + break + c += 1 + + # reveal/unreveal a word at position + def reveal_word_down (self, row, col, revealed=True): + # set the revealed flag for the word at the position + word = self.get_word_down (row, col) + + r = word[1] + while r < self.rows: + if self.data[r][word[2]].occupied_down is True: + self.data[r][word[2]].revealed = revealed + else: + break + r += 1 + diff --git a/player_mainwindow.py b/player_mainwindow.py index 3d528cc..89459ae 100644 --- a/player_mainwindow.py +++ b/player_mainwindow.py @@ -33,6 +33,33 @@ class MainWindow: self.gtk_main_quit () + # callback for menu item reveal word activated event + def on_revealword_activate (self, menuitem): + if self.puzzle: + # reveal across/down word if any the position + if self.puzzle.data[self.selected_row][self.selected_col].occupied_across is True: + dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL, + gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, + "Are you sure you wish to reveal across word at current cell?") + # confirm that the user wants to reveal + if dlg.run () == gtk.RESPONSE_YES: + self.puzzle.reveal_word_across (self.selected_row, self.selected_col) + # redraw the grid to reveal the word + puzgrid = self.ui.get_object ("puzzlegrid") + puzgrid.queue_draw () + dlg.destroy () + if self.puzzle.data[self.selected_row][self.selected_col].occupied_down is True: + dlg = gtk.MessageDialog (self.window, gtk.DIALOG_MODAL, + gtk.MESSAGE_QUESTION, gtk.BUTTONS_YES_NO, + "Are you sure wish to reveal down word at current cell?") + if dlg.run () == gtk.RESPONSE_YES: + self.puzzle.reveal_word_down (self.selected_row, self.selected_col) + # redraw the grid to reveal the word + puzgrid = self.ui.get_object ("puzzlegrid") + puzgrid.queue_draw () + dlg.destroy () + + # function to set the selected row/col based on the number clicked # on the clues list and also set the typing mode def set_selection_of_num (self, num, across = True): @@ -112,23 +139,25 @@ class MainWindow: # selection across or down as the case may be def set_guess (self, guess_char): if self.puzzle: - self.puzzle.data[self.selected_row][self.selected_col].guess = guess_char.upper () - # across mode typing - if self.typing_mode == self.ACROSS: - # move by one character across but only if there is no block - # in between - old_col = self.selected_col - self.move_selection_across (1) - if abs (self.selected_col - old_col) > 1: - self.selected_col = old_col - # down mode typing - else: - # move by one character down but only if there is no block - # in between - old_row = self.selected_row - self.move_selection_updown (1) - if abs (self.selected_row - old_row) > 1: - self.selected_row = old_row + # set a guess only if not revealed + if self.puzzle.data[self.selected_row][self.selected_col].revealed is False: + self.puzzle.data[self.selected_row][self.selected_col].guess = guess_char.upper () + # across mode typing + if self.typing_mode == self.ACROSS: + # move by one character across but only if there is no block + # in between + old_col = self.selected_col + self.move_selection_across (1) + if abs (self.selected_col - old_col) > 1: + self.selected_col = old_col + # down mode typing + else: + # move by one character down but only if there is no block + # in between + old_row = self.selected_row + self.move_selection_updown (1) + if abs (self.selected_row - old_row) > 1: + self.selected_row = old_row # delete the guessed char in the previous row/col depending on the input mode # If input mode is ACROSS then delete guessed char at previous column else @@ -214,7 +243,7 @@ class MainWindow: # if it is backspace key then delete character at previous row/col # depending on the input mode. If across editing mode, then delete # at previous column else at previous row - elif key == "BackSpace": + elif key == "backspace": self.delete_prev_guess () drawarea.queue_draw () @@ -290,6 +319,17 @@ class MainWindow: ctx.move_to (col*30+10, row*30+20) ctx.show_text (self.puzzle.data[row][col].guess) + # if there is a revealed solution character at the location + if (self.puzzle.data[row][col].revealed is True and + (self.puzzle.data[row][col].occupied_across is True + or self.puzzle.data[row][col].occupied_down is True)): + ctx.set_source_rgb (0, 0, 0.8) + ctx.select_font_face ("Serif", cairo.FONT_SLANT_NORMAL, + cairo.FONT_WEIGHT_BOLD) + ctx.set_font_size (16) + ctx.move_to (col*30+10, row*30+20) + ctx.show_text (self.puzzle.data[row][col].char) + return False # load clues to the list diff --git a/playerwindow.glade b/playerwindow.glade index 73c4281..f8e9afe 100644 --- a/playerwindow.glade +++ b/playerwindow.glade @@ -87,6 +87,7 @@ True Reveal _word... True + -- 2.20.1