十六进制16×16数独求解器:无末尾句点输入文件读取求助
Complete Input File Reader for 16x16 Hex Sudoku Solver in Prolog
Got it, let’s build out that input file reader for your 16x16 hex sudoku solver. The code below handles your specific input format (16-character lines, . for unknowns, hex digits 0-9/a-f), includes validation steps, and gives you a structured sudoku grid to work with for solving.
Full Working Code
main :- % Open your input file (replace 'input.txt' with your actual file path if needed) open('input.txt', read, Stream), % Read all lines from the file into a list read_lines(Stream, RawLines), % Clean up the stream close(Stream), % Convert raw lines into a structured sudoku grid process_lines(RawLines, SudokuGrid), % Optional: Print the grid to verify it's read correctly print_grid(SudokuGrid). % Recursively read all lines from the input stream read_lines(Stream, []) :- at_end_of_stream(Stream). read_lines(Stream, [TrimmedLine|Rest]) :- \+ at_end_of_stream(Stream), read_line_to_string(Stream, OriginalLine), % Strip any accidental leading/trailing whitespace from the line string_strip(OriginalLine, both, TrimmedLine), read_lines(Stream, Rest). % Process each line into a sudoku row, with validation process_lines([], []). process_lines([Line|Lines], [Row|Grid]) :- % Ensure each line is exactly 16 characters (critical for 16x16 sudoku) string_length(Line, 16), % Convert the string line into a list of individual characters string_chars(Line, CharList), % Convert each character to a sudoku cell (variable for unknowns, integer for knowns) process_chars(CharList, Row), process_lines(Lines, Grid). % Convert individual characters to valid sudoku cells process_chars([], []). % Handle unknown cells: '.' becomes an unbound Prolog variable process_chars(['.'|Chars], [Unknown|Cells]) :- var(Unknown), process_chars(Chars, Cells). % Handle known hex digits: convert to integer values (0-15) process_chars([Char|Chars], [Value|Cells]) :- Char \= '.', % Normalize to lowercase to handle both 'A-F' and 'a-f' string_lower(Char, LowerChar), % Validate the character is a valid hex digit member(LowerChar, ['0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f']), % Convert the character to its integer equivalent atom_number(LowerChar, Value), % Double-check the value is within 0-15 (hex range) Value >= 0, Value =< 15, process_chars(Chars, Cells). % Helper to print the grid for debugging/verification print_grid([]). print_grid([Row|Rows]) :- writeln(Row), print_grid(Rows).
Key Features & Fixes
- File Handling: Properly opens, reads, and closes the input stream to avoid resource leaks.
- Validation: Checks that every line is exactly 16 characters long (so you don’t get malformed rows breaking your solver later).
- Hex Support: Handles both lowercase and uppercase hex digits (e.g., 'C' and 'c' both become 12), and validates that all non-
.characters are valid hex digits. - Unknowns: Converts
.to unbound Prolog variables, which is perfect for representing empty cells in a sudoku solver (since Prolog’s unification will handle filling them in later).
Quick Notes
- If your input file uses a different name or is in a different directory, update the
open/3call with the correct path (e.g.,open('../sudoku_inputs/hex_puzzle.txt', read, Stream)). - If that 'n' in your sample input was a typo (hex doesn’t include 'n'), the validation step will catch it and throw an error—you can adjust the
member/2list if you need to support special characters, but I’d recommend sticking to standard hex digits for a sudoku solver. - The
print_grid/1predicate is optional but super helpful for verifying that the input was read correctly before moving on to the solving logic.
内容的提问来源于stack exchange,提问作者Jacob
相关产品推荐
相关产品推荐

