Word removal functionality completed
authorHarishankar <v.harishankar@gmail.com>
Mon, 6 Dec 2010 08:29:05 +0000 (13:59 +0530)
committerHarishankar <v.harishankar@gmail.com>
Mon, 6 Dec 2010 08:29:05 +0000 (13:59 +0530)
Implemented the functionality for removal of across and down words

crosswordpuzzle.py
crosswordpuzzlecreator.py

index 3c0ab4e..a02f469 100644 (file)
@@ -520,3 +520,39 @@ class CrosswordPuzzle:
                                self.data[row][col].reset ()
 
                self.frozen_grid = False
+
+       # remove an across word at position
+       def remove_word_across (self, row, col):
+               # if grid is frozen don't allow removal of word
+               if self.frozen_grid is True:
+                       raise FrozenGridException
+
+               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:
+                       if self.data[brow][c].occupied_across is True:
+                               self.data[brow][c].clear_across_data ()
+                       else:
+                               break
+                       c += 1
+
+       # remove a down word at position
+       def remove_word_down (self, row, col):
+               # if grid is frozen don't allow removal of word
+               if self.frozen_grid is True:
+                       raise FrozenGridException
+
+               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:
+                       if self.data[r][bcol].occupied_down is True:
+                               self.data[r][bcol].clear_down_data ()
+                       else:
+                               break
+                       r += 1
+
+
+
index 1e62315..a0676c7 100644 (file)
@@ -161,6 +161,48 @@ class CrosswordPuzzleCreator:
                except crosswordpuzzle.NoWordException:
                        sys.stderr.write ("No down word found at that position\n")
 
+       # remove a down word
+       def on_remove_down (self):
+               self.print_puzzle ()
+
+               srow = raw_input (self.BRICKRED + "At row: " + self.ENDCOL)
+               scol = raw_input (self.BRICKRED + "At col: " + self.ENDCOL)
+               try:
+                       row = int (srow)
+                       col = int (scol)
+               except ValueError:
+                       sys.stderr.write ("Invalid row or column\n")
+                       return
+
+               try:
+                       self.puzzle.remove_word_down (row, col)
+                       sys.stdout.write (self.BLUE + "Down word removed\n" + self.ENDCOL)
+               except crosswordpuzzle.FrozenGridException:
+                       sys.stderr.write ("Word cannot be removed from a frozen puzzle\n")
+               except crosswordpuzzle.NoWordException:
+                       sys.stderr.write ("No down word found at that position\n")
+
+       # remove an across word
+       def on_remove_across (self):
+               self.print_puzzle ()
+
+               srow = raw_input (self.BRICKRED + "At row: " + self.ENDCOL)
+               scol = raw_input (self.BRICKRED + "At col: " + self.ENDCOL)
+               try:
+                       row = int (srow)
+                       col = int (scol)
+               except ValueError:
+                       sys.stderr.write ("Invalid row or column\n")
+                       return
+
+               try:
+                       self.puzzle.remove_word_across (row, col)
+                       sys.stdout.write (self.BLUE + "Across word removed\n" + self.ENDCOL)
+               except crosswordpuzzle.FrozenGridException:
+                       sys.stderr.write ("Word cannot be removed from a frozen puzzle\n")
+               except crosswordpuzzle.NoWordException:
+                       sys.stderr.write ("No across word found at that position\n")
+
        # add a word to the puzzle
        def on_add_word (self, across=True):
                # first display the grid
@@ -262,6 +304,10 @@ class CrosswordPuzzleCreator:
                                        self.on_add_word ()
                                elif ch == "3":
                                        self.on_add_word (False)
+                               elif ch == "4":
+                                       self.on_remove_across ()
+                               elif ch == "5":
+                                       self.on_remove_down ()
                                elif ch == "6":
                                        self.puzzle.freeze_grid ()
                                elif ch == "7":