X-Git-Url: https://harishankar.org/repos/?p=getaclue.git;a=blobdiff_plain;f=crosswordpuzzle.py;h=e4a7f58e51f779938738e36261b22111fc8e70c4;hp=d02890b0ff1100cb360bbed54431afa1f2a3e6fd;hb=0ab1dc52c24addeb82930f4d7013eaf5c178149e;hpb=f07b2f0668053c6bf2c786376f3602509e2efe0a diff --git a/crosswordpuzzle.py b/crosswordpuzzle.py index d02890b..e4a7f58 100644 --- a/crosswordpuzzle.py +++ b/crosswordpuzzle.py @@ -109,6 +109,11 @@ class NoWordException (Exception): def __init__ (self, row, col): self.pos = (row, col) +# exception when a word number is not found in the grid +class NoNumberException (Exception): + def __init__ (self, num): + self.num = num + # exception when no words are present in the grid class NoWordsException (Exception): def __init__ (self): @@ -299,6 +304,53 @@ class CrosswordPuzzle: return clues + # getting an across word at a number (note that the grid should be + # frozen for calling this otherwise a FrozenGridException will be raised) + def get_word_across_at_num (self, num): + # assert that the grid is frozen + self.assert_frozen_grid () + + # traverse the grid + for row in range (self.rows): + for col in range (self.cols): + if self.data[row][col].numbered == num: + word = self.get_word_across (row, col) + return word + + # if number is not found + raise NoNumberException (num) + + # getting a down word at a number (note that the grid should be frozen + # for calling this otherwise a FrozenGridException will be raised) + def get_word_down_at_num (self, num): + # assert that the grid is frozen + self.assert_frozen_grid () + + # traverse the grid + for row in range (self.rows): + for col in range (self.cols): + if self.data[row][col].numbered == num: + word = self.get_word_down (row, col) + return word + + # if number is not found + raise NoNumberException (num) + + # getting the position of a number on the grid (note that the grid should + # be frozen for calling this otherwise a FrozenGridException will be raised) + def get_position_of_num (self, num): + # assert that the grid is frozen + self.assert_frozen_grid () + + # traverse the grid + for row in range (self.rows): + for col in range (self.cols): + if self.data[row][col].numbered == num: + return (row, col) + + # if number is not found + raise NoNumberException (num) + # getting a down word at a position def get_word_down (self, row, col): # if index is out of bounds