mirror of
https://codeberg.org/cblanken/boggler.git
synced 2026-07-25 19:19:20 -04:00
Add JSON output option
This commit is contained in:
+29
-35
@@ -44,47 +44,41 @@ def main():
|
||||
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)
|
||||
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.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()
|
||||
if args.sort:
|
||||
data.sort()
|
||||
|
||||
for line in data:
|
||||
print(line)
|
||||
for line in data:
|
||||
print(line)
|
||||
|
||||
case "json":
|
||||
data = { str(k):v.word_paths for k,v in boggle_tree.items()}
|
||||
print(json.dumps(data, indent=2, sort_keys=True))
|
||||
case _:
|
||||
print(f"Invalid format (-f) option provided: \"{args.format}\"")
|
||||
sys.exit()
|
||||
|
||||
case "json":
|
||||
pass
|
||||
case _:
|
||||
print(f"Invalid format (-f) option provided: \"{args.format}\"")
|
||||
sys.exit()
|
||||
else:
|
||||
print("\nBOARD")
|
||||
print(boggle_board)
|
||||
|
||||
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)
|
||||
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]}")
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
'''Boggler Utils'''
|
||||
from __future__ import annotations
|
||||
#from pathlib import Path
|
||||
from os import path
|
||||
from multiprocessing import Pool
|
||||
import logging as log
|
||||
@@ -12,7 +13,7 @@ class BoardCell:
|
||||
adjacent_cells: list[BoardCell] = None) -> BoardCell:
|
||||
self.__row: int = row
|
||||
self.__col: int = col
|
||||
self.__pos: (int, int) = (self.__row, self.__col)
|
||||
self.__pos: tuple(int, int) = (self.__row, self.__col)
|
||||
self.__letters: str = letters
|
||||
self.__adjacent_cells: list[BoardCell] = adjacent_cells
|
||||
|
||||
@@ -27,7 +28,7 @@ class BoardCell:
|
||||
return self.__col
|
||||
|
||||
@property
|
||||
def pos(self) -> (int, int):
|
||||
def pos(self) -> tuple[int, int]:
|
||||
'''Getter for pos property'''
|
||||
return self.__pos
|
||||
|
||||
@@ -168,7 +169,7 @@ class WordNode:
|
||||
return self.__parent
|
||||
|
||||
@property
|
||||
def board_pos(self) -> (int, int):
|
||||
def board_pos(self) -> tuple[int, int]:
|
||||
'''Getter for board_pos property'''
|
||||
return self.__board_pos
|
||||
|
||||
@@ -253,7 +254,10 @@ class WordTree:
|
||||
self.__word_paths = value
|
||||
|
||||
def __str__(self):
|
||||
return ", ".join(self.wordlist)
|
||||
return ", ".join([str(x) for x in self.word_paths])
|
||||
|
||||
def __repr__(self):
|
||||
return self.__str__()
|
||||
|
||||
def insert_node(self, letters: str, parent: WordNode, is_word: bool = False,
|
||||
children: dict[str, WordNode] = None, board_pos = None):
|
||||
@@ -408,6 +412,15 @@ def read_boggle_file(file):
|
||||
with open(file, 'r', encoding='utf-8') as file:
|
||||
return [x.rstrip().split(',') for x in file.readlines()]
|
||||
|
||||
def write_wordlist(filename: Path, filetype: str = "txt", sort: bool = False):
|
||||
match filetype:
|
||||
case "txt":
|
||||
pass
|
||||
case "json":
|
||||
pass
|
||||
case _:
|
||||
pass
|
||||
|
||||
def find_paths_by_word(board_letters, dictionary_path, max_len):
|
||||
'''Return list of paths by word'''
|
||||
boggle_board = BoggleBoard(board_letters, max_len)
|
||||
|
||||
Reference in New Issue
Block a user