Added check for non-alphabetic words
[getaclue.git] / crosswordpuzzlecreator.py
index 1e62315..baeccc5 100644 (file)
@@ -3,6 +3,7 @@
 # Licensed under the GNU GPL v3
 
 # Cross puzzle creator class
+
 import sys
 import cPickle
 import readline
@@ -147,7 +148,8 @@ class CrosswordPuzzleCreator:
                        sys.stdout.write (self.BLUE + "Across word at position: " + aword + "\n" + self.ENDCOL)
                        clue = raw_input (self.BRICKRED + "Clue for across word: " + self.ENDCOL)
                        self.puzzle.data[arow][acol].clue_across = clue
-                       sys.stdout.write (self.BLUE + "Set the clue: \n" + self.puzzle.data[arow][acol].clue_across)
+                       sys.stdout.write (self.BLUE + "Set the clue: \n" +
+                                                               self.puzzle.data[arow][acol].clue_across + "\n")
                except crosswordpuzzle.NoWordException:
                        sys.stderr.write ("No across word found at that position\n")
 
@@ -157,10 +159,53 @@ class CrosswordPuzzleCreator:
                        sys.stdout.write (self.BLUE + "Down word at position: " + dword + "\n" + self.ENDCOL)
                        clue = raw_input (self.BRICKRED + "Clue for down word: " + self.ENDCOL)
                        self.puzzle.data[drow][dcol].clue_down = clue
-                       sys.stdout.write (self.BLUE + "Set the clue: \n" + self.puzzle.data[drow][dcol].clue_down)
+                       sys.stdout.write (self.BLUE + "Set the clue: \n" +
+                                                               self.puzzle.data[drow][dcol].clue_down + "\n")
+               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
@@ -190,6 +235,8 @@ class CrosswordPuzzleCreator:
                        sys.stderr.write ("Word intersects badly with another word!\n")
                except crosswordpuzzle.FrozenGridException:
                        sys.stderr.write ("Word cannot be added to a frozen puzzle.\n")
+               except crosswordpuzzle.WordCharsException:
+                       sys.stderr.write ("Non-word characters in word. Cannot add.\n")
 
        # Export to image/HTML
        def on_export_image (self, solution=True):
@@ -262,6 +309,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":