diff --git a/bogglesolver/solve_boggle.py b/bogglesolver/solve_boggle.py index b5cdb25..174eb11 100644 --- a/bogglesolver/solve_boggle.py +++ b/bogglesolver/solve_boggle.py @@ -39,6 +39,36 @@ def set_board(self, columns, rows, boggle_list=None): else: self.boggle.generate_boggle_board() + def find_word(self, word, ignore_indexes=None, adjacency_funct=get_standard_boggle_adjacent): + """ + Find the indices for a specified word in the current board. + + :param word: the word to find. + :param adjacency_funct: the function to use to determine adjacency. + :returns: a list of board indices containing the word. + """ + if ignore_indexes is None: + ignore_indexes = [] + indices = [i for i, x in enumerate(self.boggle.boggle_array) if x == word[0]] + for i in indices: + word_indices = self.recursive_find_word(i, word, 1, ignore_indexes.append(i), adjacency_funct) + word_indices.append(i) + if word_indices and len(word_indices) == len(word): + return list(reversed(word_indices)) + return [] + + def recursive_find_word(self, a_index, word, word_index, indexes_searched, adjacency_funct=get_standard_boggle_adjacent): + if len(word) <= word_index: + return [] + if indexes_searched is None: + indexes_searched = [] + for index in adjacency_funct(a_index, self.boggle.num_columns, self.boggle.num_rows, indexes_searched): + if self.boggle.boggle_array[index] == word[word_index]: + indexes = self.recursive_find_word(index, word, word_index + 1, indexes_searched.append(index), adjacency_funct=adjacency_funct) + indexes.append(index) + return indexes + return [] + def solve(self, edict, ignore_indexes=None, adjacency_funct=get_standard_boggle_adjacent): """ Solve the boggle board, or get all words for scrabble. diff --git a/bogglesolver/test/test_unit.py b/bogglesolver/test/test_unit.py index 003cf74..845baac 100644 --- a/bogglesolver/test/test_unit.py +++ b/bogglesolver/test/test_unit.py @@ -182,6 +182,22 @@ def test_set_board_can_generate_board(self): solve_game.set_board(4, 4) assert solve_game.boggle.is_full() is True + def test_can_find_word_indices(self): + solve_game = SolveBoggle(True) + solve_game.set_board(4, 4, "thoipqefqqqqrrrr") + assert solve_game.boggle.is_full() + expected_indices = [0, 1, 6, 2] + actual_indices = solve_game.find_word("theo") + assert actual_indices == expected_indices + + def test_find_word_returns_empty_list_if_word_is_not_in_board(self): + solve_game = SolveBoggle(True) + solve_game.set_board(4, 4) + assert solve_game.boggle.is_full() + expected_indices = [] + actual_indices = solve_game.find_word("zzzzzzzzzz") + assert expected_indices == actual_indices + class TestAdjacency(unittest.TestCase):