#!/usr/bin/env python # BoggleTrainer.py - The trainer class that searches for valid words. # This class uses the BoggleBoard class and enchant to scan a given Boggle # board layout for valid words. # # Copyright (c) 2009, Alan Saqui # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are met: # * Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # * Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # * Neither the name of the nor the # names of its contributors may be used to endorse or promote products # derived from this software without specific prior written permission. # # THIS SOFTWARE IS PROVIDED BY ALAN SAQUI ''AS IS'' AND ANY # EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE # DISCLAIMED. IN NO EVENT SHALL ALAN SAQUI BE LIABLE FOR ANY # DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. import enchant import BoggleBoard class BoggleBashTrainer: def __init__(self, max_word_len): self.max_word_len = max_word_len self.board = BoggleBoard.BoggleBoard(4, 4, max_word_len) self.dict = enchant.Dict('en_US') def run(self): while True: layout = raw_input("Board: ") if len(layout) != 16: print "Invalid board layout." continue # Change layout into an array of 1 letter upper-case strings layout = [ x for x in layout.upper() ] # Change all Q's to QU's for (i, letter) in enumerate(layout): if letter == 'Q': layout[i] = 'QU' for length in range(3, self.max_word_len + 1): words = self.find_words(layout, length) print "\n---------- %d letter words ----------" % (length) self.print_words(words) print "\n" def find_words(self, layout, len): paths = self.board.paths[len] # Transform paths into words. words = [ ''.join([ layout[i] for i in path ]) for path in paths ] words = set(words) # strip out duplicate words. # Strip out anything that doesn't pass the spell checker words = [ word for word in words if self.dict.check(word) ] return words def print_words(self, words): for (i, word) in enumerate(words): print "%s " % (word), if (i + 1) % 7 == 0 : print if __name__ == "__main__": trainer = BoggleBashTrainer(8) trainer.run()