def main(): parser = ArgumentParser(description="Solve a given boggle board") parser.add_argument("--board", dest="board", help="a list of 16 characters making up \ the board - if not provided, this is generated randomly") args = parser.parse_args() if (args.board): board = Boggle(args.board) else: board = random_board() trie = build_english_language_trie() solutions = board.solve(trie) num_words = len(solutions) points = count_points(solutions) sorted_words = sorted(solutions.keys()) # render report to HTML env = Environment(loader=PackageLoader('boggle', 'templates')) template = env.get_template('report.html') # setup solutions for render: formatted_solutions = {} for word in solutions: formatted_pos = ['%d-%d' % pos for pos in solutions[word]] formatted_solutions[word] = formatted_pos output = template.render(grid=board.grid, solutions=formatted_solutions, num_words=num_words, points=points, sorted_words=sorted_words, size=Boggle.size) print output
except RuntimeError as e: print('Error:', e, file=sys.stderr) print('Exiting.', file=sys.stderr) sys.exit(1) print('Login successful.\n') # Continuously solve puzzles until too many consecutive errors are # encountered. consecutive_errors = 0 n_puzzles = 0 while consecutive_errors < MAX_CONSECUTIVE_ERRORS: try: boggle_elements = wp.start_boggle() b = Boggle(boggle_elements) words = b.solve() score, max_score = wp.solve(words) print('Puzzle number: {:5s}, score: {:>4s}, max score: {:>4s}'. format(wp.pzlnbr, score, max_score)) time.sleep(1) # To avoid DOSing the server. consecutive_errors = 0 n_puzzles += 1 except Exception as e: print('Error:', e, file=sys.stderr) consecutive_errors += 1 print('Too many consecutive errors were encountered. Exiting.', file=sys.stderr)