mirror of
https://codeberg.org/cblanken/boggler.git
synced 2026-07-25 19:19:20 -04:00
- Output valid words for the provided board in a normal text format, one word per line
- Select between just word output and word+path output for easier post-processing
- Also includes sort and de-duplication options
91 lines
3.3 KiB
Python
Executable File
91 lines
3.3 KiB
Python
Executable File
"""Boggler Demo"""
|
|
from pprint import pprint
|
|
import argparse
|
|
import json
|
|
import csv
|
|
from itertools import chain
|
|
import sys
|
|
from pathlib import Path
|
|
from .boggler_utils import BoggleBoard, build_full_boggle_tree, read_boggle_file
|
|
|
|
parser = argparse.ArgumentParser(
|
|
prog="boggler",
|
|
description="Boggle board game solver"
|
|
)
|
|
|
|
parser.add_argument("board", type=Path, help="Path to board CSV file")
|
|
parser.add_argument("wordlists", type=Path,
|
|
help="Path to directory of wordlist files. The directory must contain \
|
|
text files of the form words_X.txt where \"X\" is a character of \
|
|
the alphabet")
|
|
parser.add_argument("max_word_length", nargs="?", type=int, default=16,
|
|
help="Maximum length of words searched for on provided board")
|
|
parser.add_argument("-f", "--format", type=str,
|
|
help="Specify alternative output format including [txt, json]")
|
|
parser.add_argument("-p", "--include-path", action="store_true", default=False,
|
|
help="Include full paths for each word in output")
|
|
parser.add_argument("-s", "--sort", action="store_true", default=False,
|
|
help="Sort output alphabetically. By default the results are sorted by the starting \
|
|
block position on the board from top-to-bottom, left-to-right as given in the \
|
|
board file.")
|
|
parser.add_argument("-d", "--dedup", action="store_true", default=False,
|
|
help="Remove duplicates from word-only output. Note that de-duplication does not preserve \
|
|
the original order of the output, so it is recommended to also use the sort option when \
|
|
de-duplicating.")
|
|
|
|
args = parser.parse_args()
|
|
|
|
def main():
|
|
"""Command line tool for sovling Boggle boards"""
|
|
board = read_boggle_file(args.board)
|
|
try:
|
|
boggle_board = BoggleBoard(board, args.max_word_length)
|
|
except ValueError:
|
|
print("Invalid MAX_WORD_LENGTH. Please try again with a valid integer.")
|
|
sys.exit()
|
|
|
|
try:
|
|
boggle_tree = build_full_boggle_tree(boggle_board, args.wordlists)
|
|
|
|
if args.format:
|
|
match args.format.lower():
|
|
case "txt":
|
|
word_paths = [start_block.word_paths for start_block in boggle_tree.values()]
|
|
data = list(chain(*word_paths))
|
|
if args.include_path:
|
|
data = [f"{line[0]} {line[1]}" for line in data]
|
|
else:
|
|
data = [x[0] for x in data]
|
|
if args.dedup:
|
|
data = list(set(data))
|
|
|
|
if args.sort:
|
|
data.sort()
|
|
|
|
for line in data:
|
|
print(line)
|
|
|
|
|
|
case "json":
|
|
pass
|
|
case _:
|
|
print(f"Invalid format (-f) option provided: \"{args.format}\"")
|
|
sys.exit()
|
|
|
|
else:
|
|
print("\nBOARD")
|
|
print(boggle_board)
|
|
|
|
for start_pos, tree in boggle_tree.items():
|
|
print(f"\nStarting @ {start_pos}...")
|
|
for word in tree.word_paths:
|
|
print(f"{word[0]: <{boggle_board.max_word_len}}: {word[1]}")
|
|
|
|
except ValueError:
|
|
print("The [MAX_WORD_LENGTH] argument must be an integer.")
|
|
print("Please try again.")
|
|
sys.exit(1)
|
|
|
|
if __name__ == '__main__':
|
|
main()
|