From ba6131685060a4abee86a6453c3538c5afe3d9ca Mon Sep 17 00:00:00 2001 From: Harishankar Date: Sun, 12 Dec 2010 20:07:29 +0530 Subject: [PATCH] Added check for non-alphabetic words Added exception and handler for non-alphabetic words in the creator. This prevents creating words in the grid with numbers or non-alphabetic symbols. --- crosswordpuzzle.py | 13 +++++++++++++ crosswordpuzzlecreator.py | 2 ++ 2 files changed, 15 insertions(+) diff --git a/crosswordpuzzle.py b/crosswordpuzzle.py index 7ee206f..6283920 100644 --- a/crosswordpuzzle.py +++ b/crosswordpuzzle.py @@ -93,6 +93,11 @@ class TooLongWordException (Exception): self.word = word self.length = length +# exception for words containing non-alpha characters +class WordCharsException (Exception): + def __init__ (self, word): + self.word = word + # exception for intersecting words class IntersectWordException (Exception): def __init__ (self, word, length): @@ -431,6 +436,10 @@ class CrosswordPuzzle: # if the grid is frozen then abort self.assert_unfrozen_grid () + # if the word has non-alphabetic characters + if not word.isalpha (): + raise WordCharsException (word) + # if the word length greater than totalrows - startrow if len(word) > self.rows - row: raise TooLongWordException (word, len(word)) @@ -485,6 +494,10 @@ class CrosswordPuzzle: # if the grid is frozen then abort self.assert_unfrozen_grid () + # if the word has non-alphabetic characters + if not word.isalpha (): + raise WordCharsException (word) + # is the word length greater than totalcols - startcol? if len(word) > self.cols - col: raise TooLongWordException (word, len(word)) diff --git a/crosswordpuzzlecreator.py b/crosswordpuzzlecreator.py index 334895d..baeccc5 100644 --- a/crosswordpuzzlecreator.py +++ b/crosswordpuzzlecreator.py @@ -235,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): -- 2.20.1