Added the "reveal word" functionality
authorHarishankar <v.harishankar@gmail.com>
Tue, 7 Dec 2010 15:10:49 +0000 (20:40 +0530)
committerHarishankar <v.harishankar@gmail.com>
Tue, 7 Dec 2010 15:10:49 +0000 (20:40 +0530)
Added the functionality to reveal across and/or downword at
the currently selected cell.

crosswordpuzzle.py
player_mainwindow.py
playerwindow.glade

index e4a7f58..319a4fc 100644 (file)
@@ -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
        # 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
 
                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:
                        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
                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
 
                        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
+
 
 
 
 
index 3d528cc..89459ae 100644 (file)
@@ -33,6 +33,33 @@ class MainWindow:
 
                self.gtk_main_quit ()
 
 
                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):
        # 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:
        # 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
 
        # 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
                        # 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 ()
 
                                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)
 
                                                        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
                        return False
 
        # load clues to the list
index 73c4281..f8e9afe 100644 (file)
@@ -87,6 +87,7 @@
                         <property name="visible">True</property>
                         <property name="label" translatable="yes">Reveal _word...</property>
                         <property name="use_underline">True</property>
                         <property name="visible">True</property>
                         <property name="label" translatable="yes">Reveal _word...</property>
                         <property name="use_underline">True</property>
+                        <signal name="activate" handler="on_revealword_activate"/>
                       </object>
                     </child>
                     <child>
                       </object>
                     </child>
                     <child>